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 1840 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1851 | 1851 |
1852 MaybeLocal<Value> Script::Run(Local<Context> context) { | 1852 MaybeLocal<Value> Script::Run(Local<Context> context) { |
1853 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( | 1853 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( |
1854 "v8", "V8.Execute", context, Script, Run, MaybeLocal<Value>(), | 1854 "v8", "V8.Execute", context, Script, Run, MaybeLocal<Value>(), |
1855 InternalEscapableScope, true); | 1855 InternalEscapableScope, true); |
1856 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); | 1856 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); |
1857 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); | 1857 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); |
1858 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); | 1858 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); |
1859 auto fun = i::Handle<i::JSFunction>::cast(Utils::OpenHandle(this)); | 1859 auto fun = i::Handle<i::JSFunction>::cast(Utils::OpenHandle(this)); |
1860 | 1860 |
1861 i::Handle<i::Object> receiver; | 1861 i::Handle<i::Object> receiver = isolate->global_proxy(); |
1862 Local<Value> result; | 1862 Local<Value> result; |
1863 | 1863 has_pending_exception = !ToLocal<Value>( |
1864 if (fun->shared()->scope_info()->scope_type() == i::MODULE_SCOPE) { | 1864 i::Execution::Call(isolate, fun, receiver, 0, nullptr), &result); |
1865 receiver = isolate->factory()->undefined_value(); | |
1866 i::Handle<i::Object> argv[] = { | |
1867 handle(isolate->native_context()->current_module())}; | |
1868 has_pending_exception = !ToLocal<Value>( | |
1869 i::Execution::Call(isolate, fun, receiver, 1, argv), &result); | |
1870 } else { | |
1871 receiver = isolate->global_proxy(); | |
1872 has_pending_exception = !ToLocal<Value>( | |
1873 i::Execution::Call(isolate, fun, receiver, 0, nullptr), &result); | |
1874 } | |
1875 | 1865 |
1876 RETURN_ON_FAILED_EXECUTION(Value); | 1866 RETURN_ON_FAILED_EXECUTION(Value); |
1877 RETURN_ESCAPED(result); | 1867 RETURN_ESCAPED(result); |
1878 } | 1868 } |
1879 | 1869 |
1880 | 1870 |
1881 Local<Value> Script::Run() { | 1871 Local<Value> Script::Run() { |
1882 auto self = Utils::OpenHandle(this, true); | 1872 auto self = Utils::OpenHandle(this, true); |
1883 // If execution is terminating, Compile(..)->Run() requires this | 1873 // If execution is terminating, Compile(..)->Run() requires this |
1884 // check. | 1874 // check. |
1885 if (self.is_null()) return Local<Value>(); | 1875 if (self.is_null()) return Local<Value>(); |
1886 auto context = ContextFromHeapObject(self); | 1876 auto context = ContextFromHeapObject(self); |
1887 RETURN_TO_LOCAL_UNCHECKED(Run(context), Value); | 1877 RETURN_TO_LOCAL_UNCHECKED(Run(context), Value); |
1888 } | 1878 } |
1889 | 1879 |
1890 | 1880 |
1891 Local<UnboundScript> Script::GetUnboundScript() { | 1881 Local<UnboundScript> Script::GetUnboundScript() { |
1892 i::Handle<i::Object> obj = Utils::OpenHandle(this); | 1882 i::Handle<i::Object> obj = Utils::OpenHandle(this); |
1893 return ToApiHandle<UnboundScript>( | 1883 return ToApiHandle<UnboundScript>( |
1894 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared())); | 1884 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared())); |
1895 } | 1885 } |
1896 | 1886 |
| 1887 bool Module::Instantiate(Local<Context> v8_context) { |
| 1888 i::Handle<i::Module> self = Utils::OpenHandle(this); |
| 1889 i::Isolate* isolate = self->GetIsolate(); |
| 1890 |
| 1891 // Already instantiated. |
| 1892 if (self->code()->IsJSFunction()) return true; |
| 1893 |
| 1894 i::Handle<i::SharedFunctionInfo> shared( |
| 1895 i::SharedFunctionInfo::cast(self->code()), isolate); |
| 1896 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); |
| 1897 i::Handle<i::JSFunction> function = |
| 1898 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 1899 shared, handle(context->native_context(), isolate)); |
| 1900 self->set_code(*function); |
| 1901 |
| 1902 // TODO(adamk): This could fail in the future when Instantiate |
| 1903 // does linking. |
| 1904 return true; |
| 1905 } |
| 1906 |
| 1907 MaybeLocal<Value> Module::Evaluate(Local<Context> context) { |
| 1908 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( |
| 1909 "v8", "V8.Execute", context, Script, Run, MaybeLocal<Value>(), |
| 1910 InternalEscapableScope, true); |
| 1911 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); |
| 1912 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); |
| 1913 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); |
| 1914 |
| 1915 i::Handle<i::Module> self = Utils::OpenHandle(this); |
| 1916 // It's an API error to call Evaluate before Instantiate. |
| 1917 CHECK(self->code()->IsJSFunction()); |
| 1918 |
| 1919 i::Handle<i::JSFunction> function(i::JSFunction::cast(self->code()), isolate); |
| 1920 DCHECK_EQ(i::MODULE_SCOPE, function->shared()->scope_info()->scope_type()); |
| 1921 i::Handle<i::Object> receiver = isolate->factory()->undefined_value(); |
| 1922 |
| 1923 Local<Value> result; |
| 1924 i::Handle<i::Object> argv[] = {self}; |
| 1925 has_pending_exception = !ToLocal<Value>( |
| 1926 i::Execution::Call(isolate, function, receiver, arraysize(argv), argv), |
| 1927 &result); |
| 1928 |
| 1929 RETURN_ON_FAILED_EXECUTION(Value); |
| 1930 RETURN_ESCAPED(result); |
| 1931 } |
1897 | 1932 |
1898 MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal( | 1933 MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal( |
1899 Isolate* v8_isolate, Source* source, CompileOptions options, | 1934 Isolate* v8_isolate, Source* source, CompileOptions options, |
1900 bool is_module) { | 1935 bool is_module) { |
1901 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); | 1936 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
1902 PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, ScriptCompiler, CompileUnbound, | 1937 PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, ScriptCompiler, CompileUnbound, |
1903 UnboundScript); | 1938 UnboundScript); |
1904 TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler"); | 1939 TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler"); |
1905 | 1940 |
1906 // Don't try to produce any kind of cache when the debugger is loaded. | 1941 // Don't try to produce any kind of cache when the debugger is loaded. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1997 | 2032 |
1998 | 2033 |
1999 Local<Script> ScriptCompiler::Compile( | 2034 Local<Script> ScriptCompiler::Compile( |
2000 Isolate* v8_isolate, | 2035 Isolate* v8_isolate, |
2001 Source* source, | 2036 Source* source, |
2002 CompileOptions options) { | 2037 CompileOptions options) { |
2003 auto context = v8_isolate->GetCurrentContext(); | 2038 auto context = v8_isolate->GetCurrentContext(); |
2004 RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, options), Script); | 2039 RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, options), Script); |
2005 } | 2040 } |
2006 | 2041 |
2007 | 2042 MaybeLocal<Module> ScriptCompiler::CompileModule(Isolate* isolate, |
2008 MaybeLocal<Script> ScriptCompiler::CompileModule(Local<Context> context, | 2043 Source* source) { |
2009 Source* source, | |
2010 CompileOptions options) { | |
2011 auto isolate = context->GetIsolate(); | |
2012 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 2044 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
2013 | 2045 |
2014 auto maybe = CompileUnboundInternal(isolate, source, options, true); | 2046 auto maybe = CompileUnboundInternal(isolate, source, kNoCompileOptions, true); |
2015 Local<UnboundScript> generic; | 2047 Local<UnboundScript> unbound; |
2016 if (!maybe.ToLocal(&generic)) return MaybeLocal<Script>(); | 2048 if (!maybe.ToLocal(&unbound)) return MaybeLocal<Module>(); |
2017 v8::Context::Scope scope(context); | |
2018 auto result = generic->BindToCurrentContext(); | |
2019 | 2049 |
2020 i::Handle<i::SharedFunctionInfo> shared = Utils::OpenHandle(*generic); | 2050 i::Handle<i::SharedFunctionInfo> shared = Utils::OpenHandle(*unbound); |
2021 i::Handle<i::FixedArray> regular_exports = | 2051 i::Handle<i::FixedArray> regular_exports = |
2022 i::handle(shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), | 2052 i::handle(shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), |
2023 i_isolate); | 2053 i_isolate); |
2024 int regular_exports_length = regular_exports->length(); | 2054 int regular_exports_length = regular_exports->length(); |
2025 | 2055 |
2026 i::Handle<i::Module> module = | 2056 i::Handle<i::Module> module = |
2027 i_isolate->factory()->NewModule(regular_exports_length); | 2057 i_isolate->factory()->NewModule(shared, regular_exports_length); |
2028 | |
2029 // TODO(neis): Storing the module into the native context is a temporary hack | |
2030 // to pass it to the Script::Run function. This will be removed once we | |
2031 // support modules in the API. | |
2032 i_isolate->native_context()->set_current_module(*module); | |
2033 | 2058 |
2034 // TODO(neis): This will create multiple cells for the same local variable if | 2059 // TODO(neis): This will create multiple cells for the same local variable if |
2035 // exported under multiple names, which is wrong but cannot be observed at the | 2060 // exported under multiple names, which is wrong but cannot be observed at the |
2036 // moment. This will be fixed by doing the full-fledged linking here once we | 2061 // moment. This will be fixed by doing the full-fledged linking here once we |
2037 // get there. | 2062 // get there. |
2038 for (int i = 0; i < regular_exports_length; ++i) { | 2063 for (int i = 0; i < regular_exports_length; ++i) { |
2039 i::Handle<i::ModuleInfoEntry> entry = | 2064 i::Handle<i::ModuleInfoEntry> entry = |
2040 i::handle(i::ModuleInfoEntry::cast(regular_exports->get(i)), i_isolate); | 2065 i::handle(i::ModuleInfoEntry::cast(regular_exports->get(i)), i_isolate); |
2041 DCHECK(entry->import_name()->IsUndefined(i_isolate)); | 2066 DCHECK(entry->import_name()->IsUndefined(i_isolate)); |
2042 i::Handle<i::String> export_name = | 2067 i::Handle<i::String> export_name = |
2043 handle(i::String::cast(entry->export_name()), i_isolate); | 2068 handle(i::String::cast(entry->export_name()), i_isolate); |
2044 i::Module::CreateExport(module, export_name); | 2069 i::Module::CreateExport(module, export_name); |
2045 } | 2070 } |
2046 | 2071 |
2047 return result; | 2072 return ToApiHandle<Module>(module); |
2048 } | 2073 } |
2049 | 2074 |
2050 | 2075 |
2051 class IsIdentifierHelper { | 2076 class IsIdentifierHelper { |
2052 public: | 2077 public: |
2053 IsIdentifierHelper() : is_identifier_(false), first_char_(true) {} | 2078 IsIdentifierHelper() : is_identifier_(false), first_char_(true) {} |
2054 | 2079 |
2055 bool Check(i::String* string) { | 2080 bool Check(i::String* string) { |
2056 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0); | 2081 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0); |
2057 if (cons_string == NULL) return is_identifier_; | 2082 if (cons_string == NULL) return is_identifier_; |
(...skipping 7289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9347 Address callback_address = | 9372 Address callback_address = |
9348 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 9373 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
9349 VMState<EXTERNAL> state(isolate); | 9374 VMState<EXTERNAL> state(isolate); |
9350 ExternalCallbackScope call_scope(isolate, callback_address); | 9375 ExternalCallbackScope call_scope(isolate, callback_address); |
9351 callback(info); | 9376 callback(info); |
9352 } | 9377 } |
9353 | 9378 |
9354 | 9379 |
9355 } // namespace internal | 9380 } // namespace internal |
9356 } // namespace v8 | 9381 } // namespace v8 |
OLD | NEW |