Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: src/api.cc

Issue 2362083002: [modules] Do basic linking. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/ast/modules.h » ('j') | src/messages.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 (int i = 0, n = special_exports->length(); i < n; ++i) {
1953 i::Handle<i::ModuleInfoEntry> entry(
1954 i::ModuleInfoEntry::cast(special_exports->get(i)), isolate);
1955 i::Handle<i::Object> export_name(entry->export_name(), isolate);
1956 if (export_name->IsUndefined(isolate)) continue; // Star export.
1957 i::Module::CreateIndirectExport(
1958 module, i::Handle<i::String>::cast(export_name), entry);
1959 }
1960
1961 for (int i = 0, length = v8_module->GetModuleRequestsLength(); i < length;
1962 ++i) {
1963 Local<Module> requested_module;
1964 // TODO(adamk): Revisit these failure cases once d8 knows how to
1965 // persist a module_map across multiple top-level module loads, as
1966 // the current module is left in a "half-instantiated" state.
1967 if (!callback(v8_context, v8_module->GetModuleRequest(i), v8_module,
1968 callback_data)
1969 .ToLocal(&requested_module)) {
1970 // TODO(adamk): Give this a better error message. But this is a
1971 // misuse of the API anyway.
1972 isolate->ThrowIllegalOperation();
1973 return false;
1974 }
1975 if (!requested_module->Instantiate(v8_context, callback, callback_data)) {
1976 return false;
1977 }
1978 module->requested_modules()->set(i, *Utils::OpenHandle(*requested_module));
1979 }
1980
1981 // Resolve imports.
1982 for (int i = 0, n = regular_imports->length(); i < n; ++i) {
1983 i::Handle<i::ModuleInfoEntry> entry(
1984 i::ModuleInfoEntry::cast(regular_imports->get(i)), isolate);
1985 i::Handle<i::String> name(i::String::cast(entry->import_name()), isolate);
1986 int module_request = i::Smi::cast(entry->module_request())->value();
1987 if (i::Module::ResolveImport(module, name, module_request).is_null()) {
1988 return false;
1989 }
1990 }
1991
1992 // Resolve indirect exports.
1993 for (int i = 0, n = special_exports->length(); i < n; ++i) {
1994 i::Handle<i::ModuleInfoEntry> entry(
1995 i::ModuleInfoEntry::cast(special_exports->get(i)), isolate);
1996 i::Handle<i::String> name(i::String::cast(entry->export_name()), isolate);
1997 if (i::Module::ResolveExport(module, name).is_null()) {
1998 return false;
1999 }
2000 }
2001
1982 return true; 2002 return true;
1983 } 2003 }
1984 2004
1985 bool Module::Instantiate(Local<Context> context, 2005 bool Module::Instantiate(Local<Context> context,
1986 Module::ResolveCallback callback, 2006 Module::ResolveCallback callback,
1987 Local<Value> callback_data) { 2007 Local<Value> callback_data) {
1988 PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate); 2008 PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate);
1989 has_pending_exception = 2009 has_pending_exception =
1990 !InstantiateModule(Utils::ToLocal(Utils::OpenHandle(this)), context, 2010 !InstantiateModule(Utils::ToLocal(Utils::OpenHandle(this)), context,
1991 callback, callback_data); 2011 callback, callback_data);
(...skipping 7461 matching lines...) Expand 10 before | Expand all | Expand 10 after
9453 Address callback_address = 9473 Address callback_address =
9454 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 9474 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
9455 VMState<EXTERNAL> state(isolate); 9475 VMState<EXTERNAL> state(isolate);
9456 ExternalCallbackScope call_scope(isolate, callback_address); 9476 ExternalCallbackScope call_scope(isolate, callback_address);
9457 callback(info); 9477 callback(info);
9458 } 9478 }
9459 9479
9460 9480
9461 } // namespace internal 9481 } // namespace internal
9462 } // namespace v8 9482 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/ast/modules.h » ('j') | src/messages.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698