| 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 1874 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1885 | 1885 |
| 1886 | 1886 |
| 1887 Local<UnboundScript> Script::GetUnboundScript() { | 1887 Local<UnboundScript> Script::GetUnboundScript() { |
| 1888 i::Handle<i::Object> obj = Utils::OpenHandle(this); | 1888 i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 1889 return ToApiHandle<UnboundScript>( | 1889 return ToApiHandle<UnboundScript>( |
| 1890 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared())); | 1890 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared())); |
| 1891 } | 1891 } |
| 1892 | 1892 |
| 1893 int Module::GetModuleRequestsLength() const { | 1893 int Module::GetModuleRequestsLength() const { |
| 1894 i::Handle<i::Module> self = Utils::OpenHandle(this); | 1894 i::Handle<i::Module> self = Utils::OpenHandle(this); |
| 1895 i::Isolate* isolate = self->GetIsolate(); | 1895 return self->info()->module_requests()->length(); |
| 1896 i::Handle<i::SharedFunctionInfo> shared; | |
| 1897 if (self->code()->IsSharedFunctionInfo()) { | |
| 1898 shared = i::handle(i::SharedFunctionInfo::cast(self->code()), isolate); | |
| 1899 } else { | |
| 1900 shared = i::handle(i::JSFunction::cast(self->code())->shared(), isolate); | |
| 1901 } | |
| 1902 return shared->scope_info() | |
| 1903 ->ModuleDescriptorInfo() | |
| 1904 ->module_requests() | |
| 1905 ->length(); | |
| 1906 } | 1896 } |
| 1907 | 1897 |
| 1908 Local<String> Module::GetModuleRequest(int i) const { | 1898 Local<String> Module::GetModuleRequest(int i) const { |
| 1909 CHECK_GE(i, 0); | 1899 CHECK_GE(i, 0); |
| 1910 i::Handle<i::Module> self = Utils::OpenHandle(this); | 1900 i::Handle<i::Module> self = Utils::OpenHandle(this); |
| 1911 i::Isolate* isolate = self->GetIsolate(); | 1901 i::Isolate* isolate = self->GetIsolate(); |
| 1912 i::Handle<i::SharedFunctionInfo> shared; | 1902 i::Handle<i::FixedArray> module_requests(self->info()->module_requests(), |
| 1913 if (self->code()->IsSharedFunctionInfo()) { | 1903 isolate); |
| 1914 shared = i::handle(i::SharedFunctionInfo::cast(self->code()), isolate); | |
| 1915 } else { | |
| 1916 shared = i::handle(i::JSFunction::cast(self->code())->shared(), isolate); | |
| 1917 } | |
| 1918 i::Handle<i::FixedArray> module_requests( | |
| 1919 shared->scope_info()->ModuleDescriptorInfo()->module_requests(), isolate); | |
| 1920 CHECK_LT(i, module_requests->length()); | 1904 CHECK_LT(i, module_requests->length()); |
| 1921 return ToApiHandle<String>(i::handle(module_requests->get(i), isolate)); | 1905 return ToApiHandle<String>(i::handle(module_requests->get(i), isolate)); |
| 1922 } | 1906 } |
| 1923 | 1907 |
| 1924 void Module::SetEmbedderData(Local<Value> data) { | 1908 void Module::SetEmbedderData(Local<Value> data) { |
| 1925 Utils::OpenHandle(this)->set_embedder_data(*Utils::OpenHandle(*data)); | 1909 Utils::OpenHandle(this)->set_embedder_data(*Utils::OpenHandle(*data)); |
| 1926 } | 1910 } |
| 1927 | 1911 |
| 1928 Local<Value> Module::GetEmbedderData() const { | 1912 Local<Value> Module::GetEmbedderData() const { |
| 1929 auto self = Utils::OpenHandle(this); | 1913 auto self = Utils::OpenHandle(this); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1943 if (module->code()->IsJSFunction()) return true; | 1927 if (module->code()->IsJSFunction()) return true; |
| 1944 | 1928 |
| 1945 i::Handle<i::SharedFunctionInfo> shared( | 1929 i::Handle<i::SharedFunctionInfo> shared( |
| 1946 i::SharedFunctionInfo::cast(module->code()), isolate); | 1930 i::SharedFunctionInfo::cast(module->code()), isolate); |
| 1947 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); | 1931 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); |
| 1948 i::Handle<i::JSFunction> function = | 1932 i::Handle<i::JSFunction> function = |
| 1949 isolate->factory()->NewFunctionFromSharedFunctionInfo( | 1933 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 1950 shared, handle(context->native_context(), isolate)); | 1934 shared, handle(context->native_context(), isolate)); |
| 1951 module->set_code(*function); | 1935 module->set_code(*function); |
| 1952 | 1936 |
| 1953 for (int i = 0, length = v8_module->GetModuleRequestsLength(); i < length; | 1937 i::Handle<i::FixedArray> regular_exports = i::handle( |
| 1954 ++i) { | 1938 shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), isolate); |
| 1955 Local<Module> import; | 1939 i::Handle<i::FixedArray> regular_imports = i::handle( |
| 1956 // TODO(adamk): Revisit these failure cases once d8 knows how to | 1940 shared->scope_info()->ModuleDescriptorInfo()->regular_imports(), isolate); |
| 1957 // persist a module_map across multiple top-level module loads, as | 1941 i::Handle<i::FixedArray> special_exports = i::handle( |
| 1958 // the current module is left in a "half-instantiated" state. | 1942 shared->scope_info()->ModuleDescriptorInfo()->special_exports(), isolate); |
| 1959 if (!callback(v8_context, v8_module->GetModuleRequest(i), v8_module, | |
| 1960 callback_data) | |
| 1961 .ToLocal(&import)) { | |
| 1962 // TODO(adamk): Give this a better error message. But this is a | |
| 1963 // misuse of the API anyway. | |
| 1964 isolate->ThrowIllegalOperation(); | |
| 1965 return false; | |
| 1966 } | |
| 1967 if (!InstantiateModule(import, v8_context, callback, callback_data)) { | |
| 1968 return false; | |
| 1969 } | |
| 1970 module->requested_modules()->set(i, *Utils::OpenHandle(*import)); | |
| 1971 } | |
| 1972 | 1943 |
| 1973 // Set up local exports. | 1944 // Set up local exports. |
| 1974 i::Handle<i::FixedArray> regular_exports = i::handle( | |
| 1975 shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), isolate); | |
| 1976 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { | 1945 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { |
| 1977 i::Handle<i::FixedArray> export_names( | 1946 i::Handle<i::FixedArray> export_names( |
| 1978 i::FixedArray::cast(regular_exports->get(i + 1)), isolate); | 1947 i::FixedArray::cast(regular_exports->get(i + 1)), isolate); |
| 1979 i::Module::CreateExport(module, export_names); | 1948 i::Module::CreateExport(module, export_names); |
| 1980 } | 1949 } |
| 1981 | 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 if (!requested_module->Instantiate(v8_context, callback, callback_data)) { |
| 1980 return false; |
| 1981 } |
| 1982 module->requested_modules()->set(i, *Utils::OpenHandle(*requested_module)); |
| 1983 } |
| 1984 |
| 1985 // Resolve imports. |
| 1986 for (int i = 0, n = regular_imports->length(); i < n; ++i) { |
| 1987 i::Handle<i::ModuleInfoEntry> entry( |
| 1988 i::ModuleInfoEntry::cast(regular_imports->get(i)), isolate); |
| 1989 i::Handle<i::String> name(i::String::cast(entry->import_name()), isolate); |
| 1990 int module_request = i::Smi::cast(entry->module_request())->value(); |
| 1991 if (i::Module::ResolveImport(module, name, module_request).is_null()) { |
| 1992 return false; |
| 1993 } |
| 1994 } |
| 1995 |
| 1996 // Resolve indirect exports. |
| 1997 for (int i = 0, n = special_exports->length(); i < n; ++i) { |
| 1998 i::Handle<i::ModuleInfoEntry> entry( |
| 1999 i::ModuleInfoEntry::cast(special_exports->get(i)), isolate); |
| 2000 i::Handle<i::String> name(i::String::cast(entry->export_name()), isolate); |
| 2001 if (i::Module::ResolveExport(module, name).is_null()) { |
| 2002 return false; |
| 2003 } |
| 2004 } |
| 2005 |
| 1982 return true; | 2006 return true; |
| 1983 } | 2007 } |
| 1984 | 2008 |
| 1985 bool Module::Instantiate(Local<Context> context, | 2009 bool Module::Instantiate(Local<Context> context, |
| 1986 Module::ResolveCallback callback, | 2010 Module::ResolveCallback callback, |
| 1987 Local<Value> callback_data) { | 2011 Local<Value> callback_data) { |
| 1988 PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate); | 2012 PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate); |
| 1989 has_pending_exception = | 2013 has_pending_exception = |
| 1990 !InstantiateModule(Utils::ToLocal(Utils::OpenHandle(this)), context, | 2014 !InstantiateModule(Utils::ToLocal(Utils::OpenHandle(this)), context, |
| 1991 callback, callback_data); | 2015 callback, callback_data); |
| (...skipping 7461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9453 Address callback_address = | 9477 Address callback_address = |
| 9454 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 9478 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
| 9455 VMState<EXTERNAL> state(isolate); | 9479 VMState<EXTERNAL> state(isolate); |
| 9456 ExternalCallbackScope call_scope(isolate, callback_address); | 9480 ExternalCallbackScope call_scope(isolate, callback_address); |
| 9457 callback(info); | 9481 callback(info); |
| 9458 } | 9482 } |
| 9459 | 9483 |
| 9460 | 9484 |
| 9461 } // namespace internal | 9485 } // namespace internal |
| 9462 } // namespace v8 | 9486 } // namespace v8 |
| OLD | NEW |