2015-09-01 09:25:19 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef V8_CONTEXTS_INL_H_
|
|
|
|
#define V8_CONTEXTS_INL_H_
|
|
|
|
|
|
|
|
#include "src/contexts.h"
|
2017-04-10 10:55:24 +00:00
|
|
|
#include "src/heap/heap.h"
|
2015-09-01 09:25:19 +00:00
|
|
|
#include "src/objects-inl.h"
|
2017-04-04 08:14:14 +00:00
|
|
|
#include "src/objects/dictionary.h"
|
2017-05-17 11:31:18 +00:00
|
|
|
#include "src/objects/map-inl.h"
|
2017-01-20 13:01:55 +00:00
|
|
|
#include "src/objects/regexp-match-info.h"
|
2017-05-29 13:24:32 +00:00
|
|
|
#include "src/objects/shared-function-info-inl.h"
|
2015-09-01 09:25:19 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
ScriptContextTable* ScriptContextTable::cast(Object* context) {
|
|
|
|
DCHECK(context->IsScriptContextTable());
|
|
|
|
return reinterpret_cast<ScriptContextTable*>(context);
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:58:27 +00:00
|
|
|
int ScriptContextTable::used() const { return Smi::ToInt(get(kUsedSlot)); }
|
2015-09-01 09:25:19 +00:00
|
|
|
|
|
|
|
void ScriptContextTable::set_used(int used) {
|
|
|
|
set(kUsedSlot, Smi::FromInt(used));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
Handle<Context> ScriptContextTable::GetContext(Handle<ScriptContextTable> table,
|
|
|
|
int i) {
|
|
|
|
DCHECK(i < table->used());
|
2016-01-29 18:57:26 +00:00
|
|
|
return Handle<Context>::cast(
|
|
|
|
FixedArray::get(*table, i + kFirstContextSlot, table->GetIsolate()));
|
2015-09-01 09:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
Context* Context::cast(Object* context) {
|
|
|
|
DCHECK(context->IsContext());
|
|
|
|
return reinterpret_cast<Context*>(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JSFunction* Context::closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
|
|
|
|
void Context::set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
|
|
|
|
|
|
|
|
|
|
|
|
Context* Context::previous() {
|
|
|
|
Object* result = get(PREVIOUS_INDEX);
|
|
|
|
DCHECK(IsBootstrappingOrValidParentContext(result, this));
|
|
|
|
return reinterpret_cast<Context*>(result);
|
|
|
|
}
|
|
|
|
void Context::set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
|
|
|
|
|
2016-05-12 08:50:18 +00:00
|
|
|
Object* Context::next_context_link() { return get(Context::NEXT_CONTEXT_LINK); }
|
2015-09-01 09:25:19 +00:00
|
|
|
|
2016-06-06 12:58:10 +00:00
|
|
|
bool Context::has_extension() { return !extension()->IsTheHole(GetIsolate()); }
|
2015-11-30 13:23:04 +00:00
|
|
|
HeapObject* Context::extension() {
|
|
|
|
return HeapObject::cast(get(EXTENSION_INDEX));
|
|
|
|
}
|
|
|
|
void Context::set_extension(HeapObject* object) {
|
|
|
|
set(EXTENSION_INDEX, object);
|
|
|
|
}
|
2015-09-01 09:25:19 +00:00
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
Context* Context::native_context() const {
|
2015-11-27 16:59:28 +00:00
|
|
|
Object* result = get(NATIVE_CONTEXT_INDEX);
|
|
|
|
DCHECK(IsBootstrappingOrNativeContext(this->GetIsolate(), result));
|
|
|
|
return reinterpret_cast<Context*>(result);
|
2015-09-01 09:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-27 16:59:28 +00:00
|
|
|
void Context::set_native_context(Context* context) {
|
|
|
|
set(NATIVE_CONTEXT_INDEX, context);
|
2015-09-01 09:25:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsNativeContext() const {
|
2015-09-01 09:25:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->native_context_map();
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsFunctionContext() const {
|
2015-09-01 09:25:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->function_context_map();
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsCatchContext() const {
|
2015-09-01 09:25:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->catch_context_map();
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsWithContext() const {
|
2015-09-01 09:25:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->with_context_map();
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsDebugEvaluateContext() const {
|
2016-03-31 05:58:42 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->debug_evaluate_context_map();
|
|
|
|
}
|
2015-09-01 09:25:19 +00:00
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsBlockContext() const {
|
2015-09-01 09:25:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->block_context_map();
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsModuleContext() const {
|
2015-09-01 09:25:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->module_context_map();
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsEvalContext() const {
|
2016-12-20 16:23:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->eval_context_map();
|
|
|
|
}
|
2015-09-01 09:25:19 +00:00
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::IsScriptContext() const {
|
2015-09-01 09:25:19 +00:00
|
|
|
Map* map = this->map();
|
|
|
|
return map == map->GetHeap()->script_context_map();
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::HasSameSecurityTokenAs(Context* that) const {
|
2015-11-27 16:59:28 +00:00
|
|
|
return this->native_context()->security_token() ==
|
|
|
|
that->native_context()->security_token();
|
2015-09-01 09:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
|
|
|
|
void Context::set_##name(type* value) { \
|
|
|
|
DCHECK(IsNativeContext()); \
|
|
|
|
set(index, value); \
|
|
|
|
} \
|
2017-07-19 13:39:05 +00:00
|
|
|
bool Context::is_##name(type* value) const { \
|
2015-09-01 09:25:19 +00:00
|
|
|
DCHECK(IsNativeContext()); \
|
|
|
|
return type::cast(get(index)) == value; \
|
|
|
|
} \
|
2017-07-19 13:39:05 +00:00
|
|
|
type* Context::name() const { \
|
2015-09-01 09:25:19 +00:00
|
|
|
DCHECK(IsNativeContext()); \
|
|
|
|
return type::cast(get(index)); \
|
|
|
|
}
|
|
|
|
NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
|
|
|
|
#undef NATIVE_CONTEXT_FIELD_ACCESSORS
|
|
|
|
|
2017-07-13 09:33:22 +00:00
|
|
|
#define CHECK_FOLLOWS2(v1, v2) STATIC_ASSERT((v1 + 1) == (v2))
|
|
|
|
#define CHECK_FOLLOWS4(v1, v2, v3, v4) \
|
|
|
|
CHECK_FOLLOWS2(v1, v2); \
|
|
|
|
CHECK_FOLLOWS2(v2, v3); \
|
|
|
|
CHECK_FOLLOWS2(v3, v4)
|
|
|
|
|
|
|
|
int Context::FunctionMapIndex(LanguageMode language_mode, FunctionKind kind,
|
2017-07-24 14:12:51 +00:00
|
|
|
bool has_shared_name, bool needs_home_object) {
|
2017-07-13 09:33:22 +00:00
|
|
|
if (IsClassConstructor(kind)) {
|
|
|
|
// Like the strict function map, but with no 'name' accessor. 'name'
|
|
|
|
// needs to be the last property and it is added during instantiation,
|
|
|
|
// in case a static property with the same name exists"
|
|
|
|
return CLASS_FUNCTION_MAP_INDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
int base = 0;
|
|
|
|
if (IsGeneratorFunction(kind)) {
|
|
|
|
CHECK_FOLLOWS4(GENERATOR_FUNCTION_MAP_INDEX,
|
|
|
|
GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX,
|
|
|
|
GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX,
|
|
|
|
GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
|
|
|
|
CHECK_FOLLOWS4(
|
|
|
|
ASYNC_GENERATOR_FUNCTION_MAP_INDEX,
|
|
|
|
ASYNC_GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX,
|
|
|
|
ASYNC_GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX,
|
|
|
|
ASYNC_GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
|
|
|
|
|
|
|
|
base = IsAsyncFunction(kind) ? ASYNC_GENERATOR_FUNCTION_MAP_INDEX
|
|
|
|
: GENERATOR_FUNCTION_MAP_INDEX;
|
|
|
|
|
|
|
|
} else if (IsAsyncFunction(kind)) {
|
|
|
|
CHECK_FOLLOWS4(ASYNC_FUNCTION_MAP_INDEX, ASYNC_FUNCTION_WITH_NAME_MAP_INDEX,
|
|
|
|
ASYNC_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX,
|
|
|
|
ASYNC_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
|
|
|
|
|
|
|
|
base = ASYNC_FUNCTION_MAP_INDEX;
|
|
|
|
|
|
|
|
} else if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
|
|
|
|
IsAccessorFunction(kind)) {
|
|
|
|
DCHECK_IMPLIES(IsArrowFunction(kind), !needs_home_object);
|
|
|
|
CHECK_FOLLOWS4(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
|
|
|
|
METHOD_WITH_NAME_MAP_INDEX,
|
|
|
|
METHOD_WITH_HOME_OBJECT_MAP_INDEX,
|
|
|
|
METHOD_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
|
|
|
|
|
|
|
|
base = STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DCHECK(!needs_home_object);
|
|
|
|
CHECK_FOLLOWS2(SLOPPY_FUNCTION_MAP_INDEX,
|
|
|
|
SLOPPY_FUNCTION_WITH_NAME_MAP_INDEX);
|
|
|
|
CHECK_FOLLOWS2(STRICT_FUNCTION_MAP_INDEX,
|
|
|
|
STRICT_FUNCTION_WITH_NAME_MAP_INDEX);
|
|
|
|
|
|
|
|
base = is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
|
|
|
|
: SLOPPY_FUNCTION_MAP_INDEX;
|
|
|
|
}
|
|
|
|
int offset = static_cast<int>(!has_shared_name) |
|
|
|
|
(static_cast<int>(needs_home_object) << 1);
|
|
|
|
DCHECK_EQ(0, offset & ~3);
|
|
|
|
|
|
|
|
return base + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CHECK_FOLLOWS2
|
|
|
|
#undef CHECK_FOLLOWS4
|
2015-09-01 09:25:19 +00:00
|
|
|
|
2017-07-19 13:39:05 +00:00
|
|
|
Map* Context::GetInitialJSArrayMap(ElementsKind kind) const {
|
|
|
|
DCHECK(IsNativeContext());
|
|
|
|
if (!IsFastElementsKind(kind)) return nullptr;
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
Object* const initial_js_array_map = get(Context::ArrayMapIndex(kind));
|
|
|
|
DCHECK(!initial_js_array_map->IsUndefined(GetIsolate()));
|
|
|
|
return Map::cast(initial_js_array_map);
|
|
|
|
}
|
|
|
|
|
2015-09-01 09:25:19 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_CONTEXTS_INL_H_
|