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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 #define PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, class_name, \ | 128 #define PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, class_name, \ |
129 function_name, T) \ | 129 function_name, T) \ |
130 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \ | 130 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \ |
131 MaybeLocal<T>(), InternalEscapableScope, \ | 131 MaybeLocal<T>(), InternalEscapableScope, \ |
132 true) | 132 true) |
133 | 133 |
134 #define PREPARE_FOR_EXECUTION_PRIMITIVE(context, class_name, function_name, T) \ | 134 #define PREPARE_FOR_EXECUTION_PRIMITIVE(context, class_name, function_name, T) \ |
135 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \ | 135 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \ |
136 Nothing<T>(), i::HandleScope, false) | 136 Nothing<T>(), i::HandleScope, false) |
137 | 137 |
| 138 #define PREPARE_FOR_EXECUTION_BOOL(context, class_name, function_name) \ |
| 139 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \ |
| 140 false, i::HandleScope, false) |
| 141 |
138 #define EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, value) \ | 142 #define EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, value) \ |
139 do { \ | 143 do { \ |
140 if (has_pending_exception) { \ | 144 if (has_pending_exception) { \ |
141 call_depth_scope.Escape(); \ | 145 call_depth_scope.Escape(); \ |
142 return value; \ | 146 return value; \ |
143 } \ | 147 } \ |
144 } while (false) | 148 } while (false) |
145 | 149 |
146 | 150 |
147 #define RETURN_ON_FAILED_EXECUTION(T) \ | 151 #define RETURN_ON_FAILED_EXECUTION(T) \ |
148 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, MaybeLocal<T>()) | 152 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, MaybeLocal<T>()) |
149 | 153 |
150 | 154 |
151 #define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \ | 155 #define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \ |
152 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, Nothing<T>()) | 156 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, Nothing<T>()) |
153 | 157 |
| 158 #define RETURN_ON_FAILED_EXECUTION_BOOL() \ |
| 159 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, false) |
154 | 160 |
155 #define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \ | 161 #define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \ |
156 return maybe_local.FromMaybe(Local<T>()); | 162 return maybe_local.FromMaybe(Local<T>()); |
157 | 163 |
158 | 164 |
159 #define RETURN_ESCAPED(value) return handle_scope.Escape(value); | 165 #define RETURN_ESCAPED(value) return handle_scope.Escape(value); |
160 | 166 |
161 | 167 |
162 namespace { | 168 namespace { |
163 | 169 |
(...skipping 1754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1918 void Module::SetEmbedderData(Local<Value> data) { | 1924 void Module::SetEmbedderData(Local<Value> data) { |
1919 Utils::OpenHandle(this)->set_embedder_data(*Utils::OpenHandle(*data)); | 1925 Utils::OpenHandle(this)->set_embedder_data(*Utils::OpenHandle(*data)); |
1920 } | 1926 } |
1921 | 1927 |
1922 Local<Value> Module::GetEmbedderData() const { | 1928 Local<Value> Module::GetEmbedderData() const { |
1923 auto self = Utils::OpenHandle(this); | 1929 auto self = Utils::OpenHandle(this); |
1924 return ToApiHandle<Value>( | 1930 return ToApiHandle<Value>( |
1925 i::handle(self->embedder_data(), self->GetIsolate())); | 1931 i::handle(self->embedder_data(), self->GetIsolate())); |
1926 } | 1932 } |
1927 | 1933 |
1928 bool Module::Instantiate(Local<Context> v8_context, | 1934 MUST_USE_RESULT |
1929 Module::ResolveCallback callback, | 1935 static bool InstantiateModule(Local<Module> v8_module, |
1930 Local<Value> callback_data) { | 1936 Local<Context> v8_context, |
1931 i::Handle<i::Module> self = Utils::OpenHandle(this); | 1937 Module::ResolveCallback callback, |
1932 i::Isolate* isolate = self->GetIsolate(); | 1938 Local<Value> callback_data) { |
| 1939 i::Handle<i::Module> module = Utils::OpenHandle(*v8_module); |
| 1940 i::Isolate* isolate = module->GetIsolate(); |
1933 | 1941 |
1934 // Already instantiated. | 1942 // Already instantiated. |
1935 if (self->code()->IsJSFunction()) return true; | 1943 if (module->code()->IsJSFunction()) return true; |
1936 | 1944 |
1937 i::Handle<i::SharedFunctionInfo> shared( | 1945 i::Handle<i::SharedFunctionInfo> shared( |
1938 i::SharedFunctionInfo::cast(self->code()), isolate); | 1946 i::SharedFunctionInfo::cast(module->code()), isolate); |
1939 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); | 1947 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); |
1940 i::Handle<i::JSFunction> function = | 1948 i::Handle<i::JSFunction> function = |
1941 isolate->factory()->NewFunctionFromSharedFunctionInfo( | 1949 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
1942 shared, handle(context->native_context(), isolate)); | 1950 shared, handle(context->native_context(), isolate)); |
1943 self->set_code(*function); | 1951 module->set_code(*function); |
1944 | 1952 |
1945 for (int i = 0, length = GetModuleRequestsLength(); i < length; ++i) { | 1953 for (int i = 0, length = v8_module->GetModuleRequestsLength(); i < length; |
| 1954 ++i) { |
1946 Local<Module> import; | 1955 Local<Module> import; |
1947 // TODO(adamk): Revisit these failure cases once d8 knows how to | 1956 // TODO(adamk): Revisit these failure cases once d8 knows how to |
1948 // persist a module_map across multiple top-level module loads, as | 1957 // persist a module_map across multiple top-level module loads, as |
1949 // the current module is left in a "half-instantiated" state. | 1958 // the current module is left in a "half-instantiated" state. |
1950 if (!callback(v8_context, GetModuleRequest(i), Utils::ToLocal(self), | 1959 if (!callback(v8_context, v8_module->GetModuleRequest(i), v8_module, |
1951 callback_data) | 1960 callback_data) |
1952 .ToLocal(&import)) { | 1961 .ToLocal(&import)) { |
1953 // TODO(adamk): Throw an exception. | 1962 // TODO(adamk): Give this a better error message. But this is a |
| 1963 // misuse of the API anyway. |
| 1964 isolate->ThrowIllegalOperation(); |
1954 return false; | 1965 return false; |
1955 } | 1966 } |
1956 if (!import->Instantiate(v8_context, callback, callback_data)) { | 1967 if (!InstantiateModule(import, v8_context, callback, callback_data)) { |
1957 return false; | 1968 return false; |
1958 } | 1969 } |
1959 self->requested_modules()->set(i, *Utils::OpenHandle(*import)); | 1970 module->requested_modules()->set(i, *Utils::OpenHandle(*import)); |
1960 } | 1971 } |
1961 | 1972 |
1962 // Set up local exports. | 1973 // Set up local exports. |
1963 i::Handle<i::FixedArray> regular_exports = i::handle( | 1974 i::Handle<i::FixedArray> regular_exports = i::handle( |
1964 shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), isolate); | 1975 shared->scope_info()->ModuleDescriptorInfo()->regular_exports(), isolate); |
1965 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { | 1976 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { |
1966 i::Handle<i::FixedArray> export_names( | 1977 i::Handle<i::FixedArray> export_names( |
1967 i::FixedArray::cast(regular_exports->get(i + 1)), isolate); | 1978 i::FixedArray::cast(regular_exports->get(i + 1)), isolate); |
1968 i::Module::CreateExport(self, export_names); | 1979 i::Module::CreateExport(module, export_names); |
1969 } | 1980 } |
1970 | 1981 |
1971 return true; | 1982 return true; |
1972 } | 1983 } |
1973 | 1984 |
| 1985 bool Module::Instantiate(Local<Context> context, |
| 1986 Module::ResolveCallback callback, |
| 1987 Local<Value> callback_data) { |
| 1988 PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate); |
| 1989 has_pending_exception = |
| 1990 !InstantiateModule(Utils::ToLocal(Utils::OpenHandle(this)), context, |
| 1991 callback, callback_data); |
| 1992 RETURN_ON_FAILED_EXECUTION_BOOL(); |
| 1993 return true; |
| 1994 } |
| 1995 |
1974 MaybeLocal<Value> Module::Evaluate(Local<Context> context) { | 1996 MaybeLocal<Value> Module::Evaluate(Local<Context> context) { |
1975 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( | 1997 PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( |
1976 "v8", "V8.Execute", context, Script, Run, MaybeLocal<Value>(), | 1998 "v8", "V8.Execute", context, Module, Evaluate, MaybeLocal<Value>(), |
1977 InternalEscapableScope, true); | 1999 InternalEscapableScope, true); |
1978 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); | 2000 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true); |
1979 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); | 2001 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy()); |
1980 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); | 2002 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); |
1981 | 2003 |
1982 i::Handle<i::Module> self = Utils::OpenHandle(this); | 2004 i::Handle<i::Module> self = Utils::OpenHandle(this); |
1983 // It's an API error to call Evaluate before Instantiate. | 2005 // It's an API error to call Evaluate before Instantiate. |
1984 CHECK(self->code()->IsJSFunction()); | 2006 CHECK(self->code()->IsJSFunction()); |
1985 | 2007 |
1986 // Each module can only be evaluated once. | 2008 // Each module can only be evaluated once. |
(...skipping 7444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9431 Address callback_address = | 9453 Address callback_address = |
9432 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 9454 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
9433 VMState<EXTERNAL> state(isolate); | 9455 VMState<EXTERNAL> state(isolate); |
9434 ExternalCallbackScope call_scope(isolate, callback_address); | 9456 ExternalCallbackScope call_scope(isolate, callback_address); |
9435 callback(info); | 9457 callback(info); |
9436 } | 9458 } |
9437 | 9459 |
9438 | 9460 |
9439 } // namespace internal | 9461 } // namespace internal |
9440 } // namespace v8 | 9462 } // namespace v8 |
OLD | NEW |