| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/api.h" | 5 #include "src/api.h" |
| 6 | 6 |
| 7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
| 8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
| 9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
| 10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
| (...skipping 1897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1908 void Module::SetEmbedderData(Local<Value> data) { | 1908 void Module::SetEmbedderData(Local<Value> data) { |
| 1909 Utils::OpenHandle(this)->set_embedder_data(*Utils::OpenHandle(*data)); | 1909 Utils::OpenHandle(this)->set_embedder_data(*Utils::OpenHandle(*data)); |
| 1910 } | 1910 } |
| 1911 | 1911 |
| 1912 Local<Value> Module::GetEmbedderData() const { | 1912 Local<Value> Module::GetEmbedderData() const { |
| 1913 auto self = Utils::OpenHandle(this); | 1913 auto self = Utils::OpenHandle(this); |
| 1914 return ToApiHandle<Value>( | 1914 return ToApiHandle<Value>( |
| 1915 i::handle(self->embedder_data(), self->GetIsolate())); | 1915 i::handle(self->embedder_data(), self->GetIsolate())); |
| 1916 } | 1916 } |
| 1917 | 1917 |
| 1918 MUST_USE_RESULT | |
| 1919 static bool InstantiateModule(Local<Module> v8_module, | |
| 1920 Local<Context> v8_context, | |
| 1921 Module::ResolveCallback callback, | |
| 1922 Local<Value> callback_data) { | |
| 1923 i::Handle<i::Module> module = Utils::OpenHandle(*v8_module); | |
| 1924 i::Isolate* isolate = module->GetIsolate(); | |
| 1925 | |
| 1926 // Already instantiated. | |
| 1927 if (module->code()->IsJSFunction()) return true; | |
| 1928 | |
| 1929 i::Handle<i::SharedFunctionInfo> shared( | |
| 1930 i::SharedFunctionInfo::cast(module->code()), isolate); | |
| 1931 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); | |
| 1932 i::Handle<i::JSFunction> function = | |
| 1933 isolate->factory()->NewFunctionFromSharedFunctionInfo( | |
| 1934 shared, handle(context->native_context(), isolate)); | |
| 1935 module->set_code(*function); | |
| 1936 | |
| 1937 i::Handle<i::FixedArray> regular_exports = i::handle( | |
| 1938 shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), isolate); | |
| 1939 i::Handle<i::FixedArray> regular_imports = i::handle( | |
| 1940 shared->scope_info()->ModuleDescriptorInfo()->regular_imports(), isolate); | |
| 1941 i::Handle<i::FixedArray> special_exports = i::handle( | |
| 1942 shared->scope_info()->ModuleDescriptorInfo()->special_exports(), isolate); | |
| 1943 | |
| 1944 // Set up local exports. | |
| 1945 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { | |
| 1946 i::Handle<i::FixedArray> export_names( | |
| 1947 i::FixedArray::cast(regular_exports->get(i + 1)), isolate); | |
| 1948 i::Module::CreateExport(module, export_names); | |
| 1949 } | |
| 1950 | |
| 1951 // Partially set up indirect exports. | |
| 1952 // For each indirect export, we create the appropriate slot in the export | |
| 1953 // table and store its ModuleInfoEntry there. When we later find the correct | |
| 1954 // Cell in the module that actually provides the value, we replace the | |
| 1955 // ModuleInfoEntry by that Cell (see ResolveExport). | |
| 1956 for (int i = 0, n = special_exports->length(); i < n; ++i) { | |
| 1957 i::Handle<i::ModuleInfoEntry> entry( | |
| 1958 i::ModuleInfoEntry::cast(special_exports->get(i)), isolate); | |
| 1959 i::Handle<i::Object> export_name(entry->export_name(), isolate); | |
| 1960 if (export_name->IsUndefined(isolate)) continue; // Star export. | |
| 1961 i::Module::CreateIndirectExport( | |
| 1962 module, i::Handle<i::String>::cast(export_name), entry); | |
| 1963 } | |
| 1964 | |
| 1965 for (int i = 0, length = v8_module->GetModuleRequestsLength(); i < length; | |
| 1966 ++i) { | |
| 1967 Local<Module> requested_module; | |
| 1968 // TODO(adamk): Revisit these failure cases once d8 knows how to | |
| 1969 // persist a module_map across multiple top-level module loads, as | |
| 1970 // the current module is left in a "half-instantiated" state. | |
| 1971 if (!callback(v8_context, v8_module->GetModuleRequest(i), v8_module, | |
| 1972 callback_data) | |
| 1973 .ToLocal(&requested_module)) { | |
| 1974 // TODO(adamk): Give this a better error message. But this is a | |
| 1975 // misuse of the API anyway. | |
| 1976 isolate->ThrowIllegalOperation(); | |
| 1977 return false; | |
| 1978 } | |
| 1979 module->requested_modules()->set(i, *Utils::OpenHandle(*requested_module)); | |
| 1980 if (!InstantiateModule(requested_module, v8_context, callback, | |
| 1981 callback_data)) { | |
| 1982 return false; | |
| 1983 } | |
| 1984 } | |
| 1985 | |
| 1986 // Resolve imports. | |
| 1987 for (int i = 0, n = regular_imports->length(); i < n; ++i) { | |
| 1988 i::Handle<i::ModuleInfoEntry> entry( | |
| 1989 i::ModuleInfoEntry::cast(regular_imports->get(i)), isolate); | |
| 1990 i::Handle<i::String> name(i::String::cast(entry->import_name()), isolate); | |
| 1991 int module_request = i::Smi::cast(entry->module_request())->value(); | |
| 1992 if (i::Module::ResolveImport(module, name, module_request, true) | |
| 1993 .is_null()) { | |
| 1994 return false; | |
| 1995 } | |
| 1996 } | |
| 1997 | |
| 1998 // Resolve indirect exports. | |
| 1999 for (int i = 0, n = special_exports->length(); i < n; ++i) { | |
| 2000 i::Handle<i::ModuleInfoEntry> entry( | |
| 2001 i::ModuleInfoEntry::cast(special_exports->get(i)), isolate); | |
| 2002 i::Handle<i::Object> name(entry->export_name(), isolate); | |
| 2003 if (name->IsUndefined(isolate)) continue; // Star export. | |
| 2004 if (i::Module::ResolveExport(module, i::Handle<i::String>::cast(name), true) | |
| 2005 .is_null()) { | |
| 2006 return false; | |
| 2007 } | |
| 2008 } | |
| 2009 | |
| 2010 return true; | |
| 2011 } | |
| 2012 | |
| 2013 bool Module::Instantiate(Local<Context> context, | 1918 bool Module::Instantiate(Local<Context> context, |
| 2014 Module::ResolveCallback callback, | 1919 Module::ResolveCallback callback, |
| 2015 Local<Value> callback_data) { | 1920 Local<Value> callback_data) { |
| 2016 PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate); | 1921 PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate); |
| 2017 has_pending_exception = | 1922 has_pending_exception = !i::Module::Instantiate( |
| 2018 !InstantiateModule(Utils::ToLocal(Utils::OpenHandle(this)), context, | 1923 Utils::OpenHandle(this), context, callback, callback_data); |
| 2019 callback, callback_data); | |
| 2020 RETURN_ON_FAILED_EXECUTION_BOOL(); | 1924 RETURN_ON_FAILED_EXECUTION_BOOL(); |
| 2021 return true; | 1925 return true; |
| 2022 } | 1926 } |
| 2023 | 1927 |
| 2024 MaybeLocal<Value> Module::Evaluate(Local<Context> context) { | 1928 MaybeLocal<Value> Module::Evaluate(Local<Context> context) { |
| 2025 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( | 1929 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( |
| 2026 "v8", "V8.Execute", context, Module, Evaluate, MaybeLocal<Value>(), | 1930 "v8", "V8.Execute", context, Module, Evaluate, MaybeLocal<Value>(), |
| 2027 InternalEscapableScope, true); | 1931 InternalEscapableScope, true); |
| 2028 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); | 1932 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); |
| 2029 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); | 1933 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); |
| (...skipping 7456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9486 Address callback_address = | 9390 Address callback_address = |
| 9487 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 9391 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
| 9488 VMState<EXTERNAL> state(isolate); | 9392 VMState<EXTERNAL> state(isolate); |
| 9489 ExternalCallbackScope call_scope(isolate, callback_address); | 9393 ExternalCallbackScope call_scope(isolate, callback_address); |
| 9490 callback(info); | 9394 callback(info); |
| 9491 } | 9395 } |
| 9492 | 9396 |
| 9493 | 9397 |
| 9494 } // namespace internal | 9398 } // namespace internal |
| 9495 } // namespace v8 | 9399 } // namespace v8 |
| OLD | NEW |