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 1866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1877 RETURN_TO_LOCAL_UNCHECKED(Run(context), Value); | 1877 RETURN_TO_LOCAL_UNCHECKED(Run(context), Value); |
1878 } | 1878 } |
1879 | 1879 |
1880 | 1880 |
1881 Local<UnboundScript> Script::GetUnboundScript() { | 1881 Local<UnboundScript> Script::GetUnboundScript() { |
1882 i::Handle<i::Object> obj = Utils::OpenHandle(this); | 1882 i::Handle<i::Object> obj = Utils::OpenHandle(this); |
1883 return ToApiHandle<UnboundScript>( | 1883 return ToApiHandle<UnboundScript>( |
1884 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared())); | 1884 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared())); |
1885 } | 1885 } |
1886 | 1886 |
1887 bool Module::Instantiate(Local<Context> v8_context) { | 1887 int Module::GetModuleRequestsLength() const { |
| 1888 i::Handle<i::Module> self = Utils::OpenHandle(this); |
| 1889 i::Isolate* isolate = self->GetIsolate(); |
| 1890 i::Handle<i::SharedFunctionInfo> shared; |
| 1891 if (self->code()->IsSharedFunctionInfo()) { |
| 1892 shared = i::handle(i::SharedFunctionInfo::cast(self->code()), isolate); |
| 1893 } else { |
| 1894 shared = i::handle(i::JSFunction::cast(self->code())->shared(), isolate); |
| 1895 } |
| 1896 return shared->scope_info() |
| 1897 ->ModuleDescriptorInfo() |
| 1898 ->module_requests() |
| 1899 ->length(); |
| 1900 } |
| 1901 |
| 1902 Local<String> Module::GetModuleRequest(int i) const { |
| 1903 CHECK_GE(i, 0); |
| 1904 i::Handle<i::Module> self = Utils::OpenHandle(this); |
| 1905 i::Isolate* isolate = self->GetIsolate(); |
| 1906 i::Handle<i::SharedFunctionInfo> shared; |
| 1907 if (self->code()->IsSharedFunctionInfo()) { |
| 1908 shared = i::handle(i::SharedFunctionInfo::cast(self->code()), isolate); |
| 1909 } else { |
| 1910 shared = i::handle(i::JSFunction::cast(self->code())->shared(), isolate); |
| 1911 } |
| 1912 i::Handle<i::FixedArray> module_requests( |
| 1913 shared->scope_info()->ModuleDescriptorInfo()->module_requests(), isolate); |
| 1914 CHECK_LT(i, module_requests->length()); |
| 1915 return ToApiHandle<String>(i::handle(module_requests->get(i), isolate)); |
| 1916 } |
| 1917 |
| 1918 bool Module::Instantiate(Local<Context> v8_context, |
| 1919 Module::ResolveCallback callback, |
| 1920 Local<Value> callback_data) { |
1888 i::Handle<i::Module> self = Utils::OpenHandle(this); | 1921 i::Handle<i::Module> self = Utils::OpenHandle(this); |
1889 i::Isolate* isolate = self->GetIsolate(); | 1922 i::Isolate* isolate = self->GetIsolate(); |
1890 | 1923 |
1891 // Already instantiated. | 1924 // Already instantiated. |
1892 if (self->code()->IsJSFunction()) return true; | 1925 if (self->code()->IsJSFunction()) return true; |
1893 | 1926 |
1894 i::Handle<i::SharedFunctionInfo> shared( | 1927 i::Handle<i::SharedFunctionInfo> shared( |
1895 i::SharedFunctionInfo::cast(self->code()), isolate); | 1928 i::SharedFunctionInfo::cast(self->code()), isolate); |
1896 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); | 1929 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); |
1897 i::Handle<i::JSFunction> function = | 1930 i::Handle<i::JSFunction> function = |
1898 isolate->factory()->NewFunctionFromSharedFunctionInfo( | 1931 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
1899 shared, handle(context->native_context(), isolate)); | 1932 shared, handle(context->native_context(), isolate)); |
1900 self->set_code(*function); | 1933 self->set_code(*function); |
1901 | 1934 |
1902 // TODO(adamk): This could fail in the future when Instantiate | 1935 for (int i = 0, length = GetModuleRequestsLength(); i < length; ++i) { |
1903 // does linking. | 1936 Local<Module> import; |
| 1937 // TODO(adamk): Revisit these failure cases once d8 knows how to |
| 1938 // persist a module_map across multiple top-level module loads, as |
| 1939 // the current module is left in a "half-instantiated" state. |
| 1940 if (!callback(v8_context, GetModuleRequest(i), Utils::ToLocal(self), |
| 1941 callback_data) |
| 1942 .ToLocal(&import)) { |
| 1943 // TODO(adamk): Throw an exception. |
| 1944 return false; |
| 1945 } |
| 1946 if (!import->Instantiate(v8_context, callback, callback_data)) { |
| 1947 return false; |
| 1948 } |
| 1949 self->requested_modules()->set(i, *Utils::OpenHandle(*import)); |
| 1950 } |
| 1951 |
| 1952 // TODO(neis): This will create multiple cells for the same local variable if |
| 1953 // exported under multiple names, which is wrong but cannot be observed at the |
| 1954 // moment. This will be fixed by doing the full-fledged linking here once we |
| 1955 // get there. |
| 1956 i::Handle<i::FixedArray> regular_exports = i::handle( |
| 1957 shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), isolate); |
| 1958 for (int i = 0, length = regular_exports->length(); i < length; ++i) { |
| 1959 i::Handle<i::ModuleInfoEntry> entry = |
| 1960 i::handle(i::ModuleInfoEntry::cast(regular_exports->get(i)), isolate); |
| 1961 DCHECK(entry->import_name()->IsUndefined(isolate)); |
| 1962 i::Handle<i::String> export_name = |
| 1963 handle(i::String::cast(entry->export_name()), isolate); |
| 1964 i::Module::CreateExport(self, export_name); |
| 1965 } |
| 1966 |
1904 return true; | 1967 return true; |
1905 } | 1968 } |
1906 | 1969 |
1907 MaybeLocal<Value> Module::Evaluate(Local<Context> context) { | 1970 MaybeLocal<Value> Module::Evaluate(Local<Context> context) { |
1908 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( | 1971 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( |
1909 "v8", "V8.Execute", context, Script, Run, MaybeLocal<Value>(), | 1972 "v8", "V8.Execute", context, Script, Run, MaybeLocal<Value>(), |
1910 InternalEscapableScope, true); | 1973 InternalEscapableScope, true); |
1911 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); | 1974 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); |
1912 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); | 1975 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); |
1913 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); | 1976 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); |
1914 | 1977 |
1915 i::Handle<i::Module> self = Utils::OpenHandle(this); | 1978 i::Handle<i::Module> self = Utils::OpenHandle(this); |
1916 // It's an API error to call Evaluate before Instantiate. | 1979 // It's an API error to call Evaluate before Instantiate. |
1917 CHECK(self->code()->IsJSFunction()); | 1980 CHECK(self->code()->IsJSFunction()); |
1918 | 1981 |
| 1982 // Each module can only be evaluated once. |
| 1983 if (self->evaluated()) return Undefined(reinterpret_cast<Isolate*>(isolate)); |
| 1984 self->set_evaluated(true); |
| 1985 |
| 1986 i::Handle<i::FixedArray> requested_modules(self->requested_modules(), |
| 1987 isolate); |
| 1988 for (int i = 0, length = requested_modules->length(); i < length; ++i) { |
| 1989 i::Handle<i::Module> import(i::Module::cast(requested_modules->get(i)), |
| 1990 isolate); |
| 1991 MaybeLocal<Value> maybe_result = Utils::ToLocal(import)->Evaluate(context); |
| 1992 if (maybe_result.IsEmpty()) return maybe_result; |
| 1993 } |
| 1994 |
1919 i::Handle<i::JSFunction> function(i::JSFunction::cast(self->code()), isolate); | 1995 i::Handle<i::JSFunction> function(i::JSFunction::cast(self->code()), isolate); |
1920 DCHECK_EQ(i::MODULE_SCOPE, function->shared()->scope_info()->scope_type()); | 1996 DCHECK_EQ(i::MODULE_SCOPE, function->shared()->scope_info()->scope_type()); |
1921 i::Handle<i::Object> receiver = isolate->factory()->undefined_value(); | 1997 i::Handle<i::Object> receiver = isolate->factory()->undefined_value(); |
1922 | 1998 |
1923 Local<Value> result; | 1999 Local<Value> result; |
1924 i::Handle<i::Object> argv[] = {self}; | 2000 i::Handle<i::Object> argv[] = {self}; |
1925 has_pending_exception = !ToLocal<Value>( | 2001 has_pending_exception = !ToLocal<Value>( |
1926 i::Execution::Call(isolate, function, receiver, arraysize(argv), argv), | 2002 i::Execution::Call(isolate, function, receiver, arraysize(argv), argv), |
1927 &result); | 2003 &result); |
1928 | 2004 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2042 MaybeLocal<Module> ScriptCompiler::CompileModule(Isolate* isolate, | 2118 MaybeLocal<Module> ScriptCompiler::CompileModule(Isolate* isolate, |
2043 Source* source) { | 2119 Source* source) { |
2044 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 2120 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
2045 | 2121 |
2046 auto maybe = CompileUnboundInternal(isolate, source, kNoCompileOptions, true); | 2122 auto maybe = CompileUnboundInternal(isolate, source, kNoCompileOptions, true); |
2047 Local<UnboundScript> unbound; | 2123 Local<UnboundScript> unbound; |
2048 if (!maybe.ToLocal(&unbound)) return MaybeLocal<Module>(); | 2124 if (!maybe.ToLocal(&unbound)) return MaybeLocal<Module>(); |
2049 | 2125 |
2050 i::Handle<i::SharedFunctionInfo> shared = Utils::OpenHandle(*unbound); | 2126 i::Handle<i::SharedFunctionInfo> shared = Utils::OpenHandle(*unbound); |
2051 i::Handle<i::Module> module = i_isolate->factory()->NewModule(shared); | 2127 i::Handle<i::Module> module = i_isolate->factory()->NewModule(shared); |
2052 | |
2053 // TODO(neis): This will create multiple cells for the same local variable if | |
2054 // exported under multiple names, which is wrong but cannot be observed at the | |
2055 // moment. This will be fixed by doing the full-fledged linking here once we | |
2056 // get there. | |
2057 i::Handle<i::FixedArray> regular_exports = | |
2058 i::handle(shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), | |
2059 i_isolate); | |
2060 for (int i = 0, length = regular_exports->length(); i < length; ++i) { | |
2061 i::Handle<i::ModuleInfoEntry> entry = | |
2062 i::handle(i::ModuleInfoEntry::cast(regular_exports->get(i)), i_isolate); | |
2063 DCHECK(entry->import_name()->IsUndefined(i_isolate)); | |
2064 i::Handle<i::String> export_name = | |
2065 handle(i::String::cast(entry->export_name()), i_isolate); | |
2066 i::Module::CreateExport(module, export_name); | |
2067 } | |
2068 | |
2069 return ToApiHandle<Module>(module); | 2128 return ToApiHandle<Module>(module); |
2070 } | 2129 } |
2071 | 2130 |
2072 | 2131 |
2073 class IsIdentifierHelper { | 2132 class IsIdentifierHelper { |
2074 public: | 2133 public: |
2075 IsIdentifierHelper() : is_identifier_(false), first_char_(true) {} | 2134 IsIdentifierHelper() : is_identifier_(false), first_char_(true) {} |
2076 | 2135 |
2077 bool Check(i::String* string) { | 2136 bool Check(i::String* string) { |
2078 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0); | 2137 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0); |
(...skipping 7289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9368 Address callback_address = | 9427 Address callback_address = |
9369 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 9428 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
9370 VMState<EXTERNAL> state(isolate); | 9429 VMState<EXTERNAL> state(isolate); |
9371 ExternalCallbackScope call_scope(isolate, callback_address); | 9430 ExternalCallbackScope call_scope(isolate, callback_address); |
9372 callback(info); | 9431 callback(info); |
9373 } | 9432 } |
9374 | 9433 |
9375 | 9434 |
9376 } // namespace internal | 9435 } // namespace internal |
9377 } // namespace v8 | 9436 } // namespace v8 |
OLD | NEW |