// Copyright 2014 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. #include "src/bootstrapper.h" #include "src/accessors.h" #include "src/api-natives.h" #include "src/base/ieee754.h" #include "src/code-stubs.h" #include "src/compiler.h" #include "src/debug/debug.h" #include "src/extensions/externalize-string-extension.h" #include "src/extensions/free-buffer-extension.h" #include "src/extensions/gc-extension.h" #include "src/extensions/ignition-statistics-extension.h" #include "src/extensions/statistics-extension.h" #include "src/extensions/trigger-failure-extension.h" #include "src/ffi/ffi-compiler.h" #include "src/heap/heap.h" #include "src/isolate-inl.h" #include "src/snapshot/natives.h" #include "src/snapshot/snapshot.h" #include "src/wasm/wasm-js.h" #if V8_INTL_SUPPORT #include "src/objects/intl-objects.h" #endif // V8_INTL_SUPPORT namespace v8 { namespace internal { Bootstrapper::Bootstrapper(Isolate* isolate) : isolate_(isolate), nesting_(0), extensions_cache_(Script::TYPE_EXTENSION) {} Handle Bootstrapper::GetNativeSource(NativeType type, int index) { NativesExternalStringResource* resource = new NativesExternalStringResource(type, index); Handle source_code = isolate_->factory()->NewNativeSourceString(resource); isolate_->heap()->RegisterExternalString(*source_code); DCHECK(source_code->is_short()); return source_code; } void Bootstrapper::Initialize(bool create_heap_objects) { extensions_cache_.Initialize(isolate_, create_heap_objects); } static const char* GCFunctionName() { bool flag_given = FLAG_expose_gc_as != NULL && strlen(FLAG_expose_gc_as) != 0; return flag_given ? FLAG_expose_gc_as : "gc"; } v8::Extension* Bootstrapper::free_buffer_extension_ = NULL; v8::Extension* Bootstrapper::gc_extension_ = NULL; v8::Extension* Bootstrapper::externalize_string_extension_ = NULL; v8::Extension* Bootstrapper::statistics_extension_ = NULL; v8::Extension* Bootstrapper::trigger_failure_extension_ = NULL; v8::Extension* Bootstrapper::ignition_statistics_extension_ = NULL; void Bootstrapper::InitializeOncePerProcess() { free_buffer_extension_ = new FreeBufferExtension; v8::RegisterExtension(free_buffer_extension_); gc_extension_ = new GCExtension(GCFunctionName()); v8::RegisterExtension(gc_extension_); externalize_string_extension_ = new ExternalizeStringExtension; v8::RegisterExtension(externalize_string_extension_); statistics_extension_ = new StatisticsExtension; v8::RegisterExtension(statistics_extension_); trigger_failure_extension_ = new TriggerFailureExtension; v8::RegisterExtension(trigger_failure_extension_); ignition_statistics_extension_ = new IgnitionStatisticsExtension; v8::RegisterExtension(ignition_statistics_extension_); } void Bootstrapper::TearDownExtensions() { delete free_buffer_extension_; free_buffer_extension_ = NULL; delete gc_extension_; gc_extension_ = NULL; delete externalize_string_extension_; externalize_string_extension_ = NULL; delete statistics_extension_; statistics_extension_ = NULL; delete trigger_failure_extension_; trigger_failure_extension_ = NULL; delete ignition_statistics_extension_; ignition_statistics_extension_ = NULL; } void Bootstrapper::TearDown() { extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical } class Genesis BASE_EMBEDDED { public: Genesis(Isolate* isolate, MaybeHandle maybe_global_proxy, v8::Local global_proxy_template, size_t context_snapshot_index, v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer, GlobalContextType context_type); Genesis(Isolate* isolate, MaybeHandle maybe_global_proxy, v8::Local global_proxy_template); ~Genesis() { } Isolate* isolate() const { return isolate_; } Factory* factory() const { return isolate_->factory(); } Builtins* builtins() const { return isolate_->builtins(); } Heap* heap() const { return isolate_->heap(); } Handle result() { return result_; } Handle global_proxy() { return global_proxy_; } private: Handle native_context() { return native_context_; } // Creates some basic objects. Used for creating a context from scratch. void CreateRoots(); // Creates the empty function. Used for creating a context from scratch. Handle CreateEmptyFunction(Isolate* isolate); // Returns the %ThrowTypeError% intrinsic function. // See ES#sec-%throwtypeerror% for details. Handle GetThrowTypeErrorIntrinsic(); void CreateSloppyModeFunctionMaps(Handle empty); void CreateStrictModeFunctionMaps(Handle empty); void CreateObjectFunction(Handle empty); void CreateIteratorMaps(Handle empty); void CreateAsyncIteratorMaps(Handle empty); void CreateAsyncFunctionMaps(Handle empty); void CreateJSProxyMaps(); // Make the "arguments" and "caller" properties throw a TypeError on access. void AddRestrictedFunctionProperties(Handle empty); // Creates the global objects using the global proxy and the template passed // in through the API. We call this regardless of whether we are building a // context from scratch or using a deserialized one from the partial snapshot // but in the latter case we don't use the objects it produces directly, as // we have to use the deserialized ones that are linked together with the // rest of the context snapshot. At the end we link the global proxy and the // context to each other. Handle CreateNewGlobals( v8::Local global_proxy_template, Handle global_proxy); // Similarly, we want to use the global that has been created by the templates // passed through the API. The global from the snapshot is detached from the // other objects in the snapshot. void HookUpGlobalObject(Handle global_object); // Hooks the given global proxy into the context in the case we do not // replace the global object from the deserialized native context. void HookUpGlobalProxy(Handle global_proxy); // The native context has a ScriptContextTable that store declarative bindings // made in script scopes. Add a "this" binding to that table pointing to the // global proxy. void InstallGlobalThisBinding(); // New context initialization. Used for creating a context from scratch. void InitializeGlobal(Handle global_object, Handle empty_function, GlobalContextType context_type); void InitializeExperimentalGlobal(); // Depending on the situation, expose and/or get rid of the utils object. void ConfigureUtilsObject(GlobalContextType context_type); #define DECLARE_FEATURE_INITIALIZATION(id, descr) \ void InitializeGlobal_##id(); HARMONY_INPROGRESS(DECLARE_FEATURE_INITIALIZATION) HARMONY_STAGED(DECLARE_FEATURE_INITIALIZATION) HARMONY_SHIPPING(DECLARE_FEATURE_INITIALIZATION) #undef DECLARE_FEATURE_INITIALIZATION Handle CreateArrayBuffer(Handle name, Builtins::Name call_byteLength, BuiltinFunctionId byteLength_id, Builtins::Name call_slice); Handle InstallInternalArray(Handle target, const char* name, ElementsKind elements_kind); bool InstallNatives(GlobalContextType context_type); void InstallTypedArray(const char* name, ElementsKind elements_kind, Handle* fun); bool InstallExtraNatives(); bool InstallExperimentalExtraNatives(); bool InstallDebuggerNatives(); void InstallBuiltinFunctionIds(); void InstallExperimentalBuiltinFunctionIds(); void InitializeNormalizedMapCaches(); enum ExtensionTraversalState { UNVISITED, VISITED, INSTALLED }; class ExtensionStates { public: ExtensionStates(); ExtensionTraversalState get_state(RegisteredExtension* extension); void set_state(RegisteredExtension* extension, ExtensionTraversalState state); private: base::HashMap map_; DISALLOW_COPY_AND_ASSIGN(ExtensionStates); }; // Used both for deserialized and from-scratch contexts to add the extensions // provided. static bool InstallExtensions(Handle native_context, v8::ExtensionConfiguration* extensions); static bool InstallAutoExtensions(Isolate* isolate, ExtensionStates* extension_states); static bool InstallRequestedExtensions(Isolate* isolate, v8::ExtensionConfiguration* extensions, ExtensionStates* extension_states); static bool InstallExtension(Isolate* isolate, const char* name, ExtensionStates* extension_states); static bool InstallExtension(Isolate* isolate, v8::RegisteredExtension* current, ExtensionStates* extension_states); static bool InstallSpecialObjects(Handle native_context); bool ConfigureApiObject(Handle object, Handle object_template); bool ConfigureGlobalObjects( v8::Local global_proxy_template); // Migrates all properties from the 'from' object to the 'to' // object and overrides the prototype in 'to' with the one from // 'from'. void TransferObject(Handle from, Handle to); void TransferNamedProperties(Handle from, Handle to); void TransferIndexedProperties(Handle from, Handle to); static bool CallUtilsFunction(Isolate* isolate, const char* name); static bool CompileExtension(Isolate* isolate, v8::Extension* extension); Isolate* isolate_; Handle result_; Handle native_context_; Handle global_proxy_; // Temporary function maps needed only during bootstrapping. Handle strict_function_with_home_object_map_; Handle strict_function_with_name_and_home_object_map_; // %ThrowTypeError%. See ES#sec-%throwtypeerror% for details. Handle restricted_properties_thrower_; BootstrapperActive active_; friend class Bootstrapper; }; void Bootstrapper::Iterate(RootVisitor* v) { extensions_cache_.Iterate(v); v->Synchronize(VisitorSynchronization::kExtensions); } Handle Bootstrapper::CreateEnvironment( MaybeHandle maybe_global_proxy, v8::Local global_proxy_template, v8::ExtensionConfiguration* extensions, size_t context_snapshot_index, v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer, GlobalContextType context_type) { HandleScope scope(isolate_); Genesis genesis(isolate_, maybe_global_proxy, global_proxy_template, context_snapshot_index, embedder_fields_deserializer, context_type); Handle env = genesis.result(); if (env.is_null() || !InstallExtensions(env, extensions)) { return Handle(); } return scope.CloseAndEscape(env); } Handle Bootstrapper::NewRemoteContext( MaybeHandle maybe_global_proxy, v8::Local global_proxy_template) { HandleScope scope(isolate_); Genesis genesis(isolate_, maybe_global_proxy, global_proxy_template); Handle global_proxy = genesis.global_proxy(); if (global_proxy.is_null()) return Handle(); return scope.CloseAndEscape(global_proxy); } void Bootstrapper::DetachGlobal(Handle env) { Isolate* isolate = env->GetIsolate(); isolate->counters()->errors_thrown_per_context()->AddSample( env->GetErrorsThrown()); Heap* heap = isolate->heap(); Handle global_proxy(JSGlobalProxy::cast(env->global_proxy())); global_proxy->set_native_context(heap->null_value()); JSObject::ForceSetPrototype(global_proxy, isolate->factory()->null_value()); global_proxy->map()->SetConstructor(heap->null_value()); if (FLAG_track_detached_contexts) { env->GetIsolate()->AddDetachedContext(env); } } namespace { // Non-construct case. Handle SimpleCreateSharedFunctionInfo(Isolate* isolate, Builtins::Name call, Handle name, int len) { Handle code = isolate->builtins()->builtin_handle(call); Handle shared = isolate->factory()->NewSharedFunctionInfo(name, code, false); shared->set_lazy_deserialization_builtin_id(call); shared->set_internal_formal_parameter_count(len); shared->set_length(len); return shared; } // Construct case. Handle SimpleCreateSharedFunctionInfo( Isolate* isolate, Builtins::Name call, Handle name, Handle instance_class_name, int len) { Handle code = isolate->builtins()->builtin_handle(call); Handle shared = isolate->factory()->NewSharedFunctionInfo(name, code, false); shared->SetConstructStub(*BUILTIN_CODE(isolate, JSBuiltinsConstructStub)); shared->set_instance_class_name(*instance_class_name); if (Builtins::IsLazy(call)) shared->set_lazy_deserialization_builtin_id(call); shared->set_internal_formal_parameter_count(len); shared->set_length(len); return shared; } void InstallFunction(Handle target, Handle property_name, Handle function, Handle function_name, PropertyAttributes attributes = DONT_ENUM) { JSObject::AddProperty(target, property_name, function, attributes); if (target->IsJSGlobalObject()) { function->shared()->set_instance_class_name(*function_name); } } void InstallFunction(Handle target, Handle function, Handle name, PropertyAttributes attributes = DONT_ENUM) { Handle name_string = Name::ToFunctionName(name).ToHandleChecked(); InstallFunction(target, name, function, name_string, attributes); } Handle CreateFunction(Isolate* isolate, Handle name, InstanceType type, int instance_size, MaybeHandle maybe_prototype, Builtins::Name call) { Factory* factory = isolate->factory(); Handle call_code(isolate->builtins()->builtin(call)); Handle prototype; Handle result = maybe_prototype.ToHandle(&prototype) ? factory->NewFunction(name, call_code, prototype, type, instance_size, STRICT, IMMUTABLE) : factory->NewFunctionWithoutPrototype(name, call_code, STRICT); if (Builtins::IsLazy(call)) { result->shared()->set_lazy_deserialization_builtin_id(call); } result->shared()->set_native(true); return result; } Handle InstallFunction(Handle target, Handle name, InstanceType type, int instance_size, MaybeHandle maybe_prototype, Builtins::Name call, PropertyAttributes attributes) { Handle name_string = Name::ToFunctionName(name).ToHandleChecked(); Handle function = CreateFunction(target->GetIsolate(), name_string, type, instance_size, maybe_prototype, call); InstallFunction(target, name, function, name_string, attributes); return function; } Handle InstallFunction(Handle target, const char* name, InstanceType type, int instance_size, MaybeHandle maybe_prototype, Builtins::Name call) { Factory* const factory = target->GetIsolate()->factory(); PropertyAttributes attributes = DONT_ENUM; return InstallFunction(target, factory->InternalizeUtf8String(name), type, instance_size, maybe_prototype, call, attributes); } Handle SimpleCreateFunction(Isolate* isolate, Handle name, Builtins::Name call, int len, bool adapt) { Handle fun = CreateFunction(isolate, name, JS_OBJECT_TYPE, JSObject::kHeaderSize, MaybeHandle(), call); if (adapt) { fun->shared()->set_internal_formal_parameter_count(len); } else { fun->shared()->DontAdaptArguments(); } fun->shared()->set_length(len); return fun; } Handle SimpleInstallFunction( Handle base, Handle property_name, Handle function_name, Builtins::Name call, int len, bool adapt, PropertyAttributes attrs = DONT_ENUM, BuiltinFunctionId id = kInvalidBuiltinFunctionId) { Handle fun = SimpleCreateFunction(base->GetIsolate(), function_name, call, len, adapt); if (id != kInvalidBuiltinFunctionId) { fun->shared()->set_builtin_function_id(id); } InstallFunction(base, fun, property_name, attrs); return fun; } Handle SimpleInstallFunction( Handle base, Handle name, Builtins::Name call, int len, bool adapt, PropertyAttributes attrs = DONT_ENUM, BuiltinFunctionId id = kInvalidBuiltinFunctionId) { return SimpleInstallFunction(base, name, name, call, len, adapt, attrs, id); } Handle SimpleInstallFunction( Handle base, Handle property_name, const char* function_name, Builtins::Name call, int len, bool adapt, PropertyAttributes attrs = DONT_ENUM, BuiltinFunctionId id = kInvalidBuiltinFunctionId) { Factory* const factory = base->GetIsolate()->factory(); // Function name does not have to be internalized. return SimpleInstallFunction( base, property_name, factory->NewStringFromAsciiChecked(function_name), call, len, adapt, attrs, id); } Handle SimpleInstallFunction( Handle base, const char* name, Builtins::Name call, int len, bool adapt, PropertyAttributes attrs = DONT_ENUM, BuiltinFunctionId id = kInvalidBuiltinFunctionId) { Factory* const factory = base->GetIsolate()->factory(); // Although function name does not have to be internalized the property name // will be internalized during property addition anyway, so do it here now. return SimpleInstallFunction(base, factory->InternalizeUtf8String(name), call, len, adapt, attrs, id); } Handle SimpleInstallFunction(Handle base, const char* name, Builtins::Name call, int len, bool adapt, BuiltinFunctionId id) { return SimpleInstallFunction(base, name, call, len, adapt, DONT_ENUM, id); } void SimpleInstallGetterSetter(Handle base, Handle name, Builtins::Name call_getter, Builtins::Name call_setter, PropertyAttributes attribs) { Isolate* const isolate = base->GetIsolate(); Handle getter_name = Name::ToFunctionName(name, isolate->factory()->get_string()) .ToHandleChecked(); Handle getter = SimpleCreateFunction(isolate, getter_name, call_getter, 0, true); Handle setter_name = Name::ToFunctionName(name, isolate->factory()->set_string()) .ToHandleChecked(); Handle setter = SimpleCreateFunction(isolate, setter_name, call_setter, 1, true); JSObject::DefineAccessor(base, name, getter, setter, attribs).Check(); } Handle SimpleInstallGetter(Handle base, Handle name, Handle property_name, Builtins::Name call, bool adapt) { Isolate* const isolate = base->GetIsolate(); Handle getter_name = Name::ToFunctionName(name, isolate->factory()->get_string()) .ToHandleChecked(); Handle getter = SimpleCreateFunction(isolate, getter_name, call, 0, adapt); Handle setter = isolate->factory()->undefined_value(); JSObject::DefineAccessor(base, property_name, getter, setter, DONT_ENUM) .Check(); return getter; } Handle SimpleInstallGetter(Handle base, Handle name, Builtins::Name call, bool adapt) { return SimpleInstallGetter(base, name, name, call, adapt); } Handle SimpleInstallGetter(Handle base, Handle name, Builtins::Name call, bool adapt, BuiltinFunctionId id) { Handle fun = SimpleInstallGetter(base, name, call, adapt); fun->shared()->set_builtin_function_id(id); return fun; } void InstallConstant(Isolate* isolate, Handle holder, const char* name, Handle value) { JSObject::AddProperty( holder, isolate->factory()->NewStringFromAsciiChecked(name), value, static_cast(DONT_DELETE | DONT_ENUM | READ_ONLY)); } void InstallSpeciesGetter(Handle constructor) { Factory* factory = constructor->GetIsolate()->factory(); // TODO(adamk): We should be able to share a SharedFunctionInfo // between all these JSFunctins. SimpleInstallGetter(constructor, factory->symbol_species_string(), factory->species_symbol(), Builtins::kReturnReceiver, true); } } // namespace Handle Genesis::CreateEmptyFunction(Isolate* isolate) { Factory* factory = isolate->factory(); // Allocate the function map first and then patch the prototype later. Handle empty_function_map = factory->CreateSloppyFunctionMap( FUNCTION_WITHOUT_PROTOTYPE, MaybeHandle()); empty_function_map->set_is_prototype_map(true); DCHECK(!empty_function_map->is_dictionary_map()); // Allocate the empty function as the prototype for function according to // ES#sec-properties-of-the-function-prototype-object Handle code(BUILTIN_CODE(isolate, EmptyFunction)); Handle empty_function = factory->NewFunction(empty_function_map, factory->empty_string(), code); empty_function->shared()->set_language_mode(STRICT); // --- E m p t y --- Handle source = factory->NewStringFromStaticChars("() {}"); Handle