OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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/objects.h" | 5 #include "src/objects.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <iomanip> | 8 #include <iomanip> |
9 #include <memory> | 9 #include <memory> |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 19917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
19928 if (must_resolve) { | 19928 if (must_resolve) { |
19929 THROW_NEW_ERROR(isolate, | 19929 THROW_NEW_ERROR(isolate, |
19930 NewSyntaxError(MessageTemplate::kUnresolvableExport, name), | 19930 NewSyntaxError(MessageTemplate::kUnresolvableExport, name), |
19931 Cell); | 19931 Cell); |
19932 } | 19932 } |
19933 return MaybeHandle<Cell>(); | 19933 return MaybeHandle<Cell>(); |
19934 } | 19934 } |
19935 | 19935 |
19936 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context, | 19936 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context, |
19937 v8::Module::ResolveCallback callback) { | 19937 v8::Module::ResolveCallback callback) { |
19938 // Already instantiated. | 19938 if (module->instantiated()) return true; |
19939 if (module->code()->IsJSFunction()) return true; | |
19940 | 19939 |
19941 Isolate* isolate = module->GetIsolate(); | 19940 Isolate* isolate = module->GetIsolate(); |
19942 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()), | 19941 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()), |
19943 isolate); | 19942 isolate); |
19944 Handle<JSFunction> function = | 19943 Handle<JSFunction> function = |
19945 isolate->factory()->NewFunctionFromSharedFunctionInfo( | 19944 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
19946 shared, | 19945 shared, |
19947 handle(Utils::OpenHandle(*context)->native_context(), isolate)); | 19946 handle(Utils::OpenHandle(*context)->native_context(), isolate)); |
19948 module->set_code(*function); | 19947 module->set_code(*function); |
| 19948 DCHECK(module->instantiated()); |
19949 | 19949 |
19950 Handle<ModuleInfo> module_info(shared->scope_info()->ModuleDescriptorInfo(), | 19950 Handle<ModuleInfo> module_info(shared->scope_info()->ModuleDescriptorInfo(), |
19951 isolate); | 19951 isolate); |
19952 | 19952 |
19953 // Set up local exports. | 19953 // Set up local exports. |
19954 Handle<FixedArray> regular_exports(module_info->regular_exports(), isolate); | 19954 Handle<FixedArray> regular_exports(module_info->regular_exports(), isolate); |
19955 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { | 19955 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { |
19956 Handle<FixedArray> export_names( | 19956 Handle<FixedArray> export_names( |
19957 FixedArray::cast(regular_exports->get(i + 1)), isolate); | 19957 FixedArray::cast(regular_exports->get(i + 1)), isolate); |
19958 CreateExport(module, export_names); | 19958 CreateExport(module, export_names); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
20020 if (ResolveExport(module, Handle<String>::cast(name), true, &resolve_set) | 20020 if (ResolveExport(module, Handle<String>::cast(name), true, &resolve_set) |
20021 .is_null()) { | 20021 .is_null()) { |
20022 return false; | 20022 return false; |
20023 } | 20023 } |
20024 } | 20024 } |
20025 | 20025 |
20026 return true; | 20026 return true; |
20027 } | 20027 } |
20028 | 20028 |
20029 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) { | 20029 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) { |
20030 DCHECK(module->code()->IsJSFunction()); // Instantiated. | 20030 DCHECK(module->instantiated()); |
20031 | |
20032 Isolate* isolate = module->GetIsolate(); | |
20033 | 20031 |
20034 // Each module can only be evaluated once. | 20032 // Each module can only be evaluated once. |
| 20033 Isolate* isolate = module->GetIsolate(); |
20035 if (module->evaluated()) return isolate->factory()->undefined_value(); | 20034 if (module->evaluated()) return isolate->factory()->undefined_value(); |
20036 module->set_evaluated(true); | 20035 Handle<JSFunction> function(JSFunction::cast(module->code()), isolate); |
| 20036 module->set_evaluated(); |
20037 | 20037 |
20038 // Initialization. | 20038 // Initialization. |
20039 Handle<JSFunction> function(JSFunction::cast(module->code()), isolate); | |
20040 DCHECK_EQ(MODULE_SCOPE, function->shared()->scope_info()->scope_type()); | 20039 DCHECK_EQ(MODULE_SCOPE, function->shared()->scope_info()->scope_type()); |
20041 Handle<Object> receiver = isolate->factory()->undefined_value(); | 20040 Handle<Object> receiver = isolate->factory()->undefined_value(); |
20042 Handle<Object> argv[] = {module}; | 20041 Handle<Object> argv[] = {module}; |
20043 Handle<Object> generator; | 20042 Handle<Object> generator; |
20044 ASSIGN_RETURN_ON_EXCEPTION( | 20043 ASSIGN_RETURN_ON_EXCEPTION( |
20045 isolate, generator, | 20044 isolate, generator, |
20046 Execution::Call(isolate, function, receiver, arraysize(argv), argv), | 20045 Execution::Call(isolate, function, receiver, arraysize(argv), argv), |
20047 Object); | 20046 Object); |
20048 | 20047 |
20049 // Recursion. | 20048 // Recursion. |
20050 Handle<FixedArray> requested_modules(module->requested_modules(), isolate); | 20049 Handle<FixedArray> requested_modules(module->requested_modules(), isolate); |
20051 for (int i = 0, length = requested_modules->length(); i < length; ++i) { | 20050 for (int i = 0, length = requested_modules->length(); i < length; ++i) { |
20052 Handle<Module> import(Module::cast(requested_modules->get(i)), isolate); | 20051 Handle<Module> import(Module::cast(requested_modules->get(i)), isolate); |
20053 RETURN_ON_EXCEPTION(isolate, Evaluate(import), Object); | 20052 RETURN_ON_EXCEPTION(isolate, Evaluate(import), Object); |
20054 } | 20053 } |
20055 | 20054 |
20056 // Evaluation of module body. | 20055 // Evaluation of module body. |
20057 Handle<JSFunction> resume( | 20056 Handle<JSFunction> resume( |
20058 isolate->native_context()->generator_next_internal(), isolate); | 20057 isolate->native_context()->generator_next_internal(), isolate); |
20059 return Execution::Call(isolate, resume, generator, 0, nullptr); | 20058 return Execution::Call(isolate, resume, generator, 0, nullptr); |
20060 } | 20059 } |
20061 | 20060 |
20062 namespace { | 20061 namespace { |
20063 | 20062 |
20064 void FetchStarExports(Handle<Module> module, Zone* zone, | 20063 void FetchStarExports(Handle<Module> module, Zone* zone, |
20065 UnorderedModuleSet* visited) { | 20064 UnorderedModuleSet* visited) { |
20066 DCHECK(module->code()->IsJSFunction()); // Instantiated. | 20065 DCHECK(module->instantiated()); |
20067 | 20066 |
20068 bool cycle = !visited->insert(module).second; | 20067 bool cycle = !visited->insert(module).second; |
20069 if (cycle) return; | 20068 if (cycle) return; |
20070 | 20069 |
20071 Isolate* isolate = module->GetIsolate(); | 20070 Isolate* isolate = module->GetIsolate(); |
20072 Handle<ObjectHashTable> exports(module->exports(), isolate); | 20071 Handle<ObjectHashTable> exports(module->exports(), isolate); |
20073 UnorderedStringMap more_exports(zone); | 20072 UnorderedStringMap more_exports(zone); |
20074 | 20073 |
20075 // TODO(neis): Only allocate more_exports if there are star exports. | 20074 // TODO(neis): Only allocate more_exports if there are star exports. |
20076 // Maybe split special_exports into indirect_exports and star_exports. | 20075 // Maybe split special_exports into indirect_exports and star_exports. |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
20187 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr)) | 20186 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr)) |
20188 .Check(); | 20187 .Check(); |
20189 } | 20188 } |
20190 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked(); | 20189 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked(); |
20191 | 20190 |
20192 return ns; | 20191 return ns; |
20193 } | 20192 } |
20194 | 20193 |
20195 } // namespace internal | 20194 } // namespace internal |
20196 } // namespace v8 | 20195 } // namespace v8 |
OLD | NEW |