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

Side by Side Diff: src/objects.cc

Issue 2407183002: [modules] Don't unnecessarily keep function alive after evaluation. (Closed)
Patch Set: Created 4 years, 2 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
OLDNEW
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 19835 matching lines...) Expand 10 before | Expand all | Expand 10 after
19846 THROW_NEW_ERROR(isolate, 19846 THROW_NEW_ERROR(isolate,
19847 NewSyntaxError(MessageTemplate::kUnresolvableExport, name), 19847 NewSyntaxError(MessageTemplate::kUnresolvableExport, name),
19848 Cell); 19848 Cell);
19849 } 19849 }
19850 return MaybeHandle<Cell>(); 19850 return MaybeHandle<Cell>();
19851 } 19851 }
19852 19852
19853 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context, 19853 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context,
19854 v8::Module::ResolveCallback callback, 19854 v8::Module::ResolveCallback callback,
19855 v8::Local<v8::Value> callback_data) { 19855 v8::Local<v8::Value> callback_data) {
19856 // Already instantiated. 19856 if (module->instantiated()) return true;
19857 if (module->code()->IsJSFunction()) return true;
19858 19857
19859 Isolate* isolate = module->GetIsolate(); 19858 Isolate* isolate = module->GetIsolate();
19860 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()), 19859 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()),
19861 isolate); 19860 isolate);
19862 Handle<JSFunction> function = 19861 Handle<JSFunction> function =
19863 isolate->factory()->NewFunctionFromSharedFunctionInfo( 19862 isolate->factory()->NewFunctionFromSharedFunctionInfo(
19864 shared, 19863 shared,
19865 handle(Utils::OpenHandle(*context)->native_context(), isolate)); 19864 handle(Utils::OpenHandle(*context)->native_context(), isolate));
19866 module->set_code(*function); 19865 module->set_code(*function);
19866 DCHECK(module->instantiated());
19867 19867
19868 Handle<ModuleInfo> module_info(shared->scope_info()->ModuleDescriptorInfo(), 19868 Handle<ModuleInfo> module_info(shared->scope_info()->ModuleDescriptorInfo(),
19869 isolate); 19869 isolate);
19870 19870
19871 // Set up local exports. 19871 // Set up local exports.
19872 Handle<FixedArray> regular_exports(module_info->regular_exports(), isolate); 19872 Handle<FixedArray> regular_exports(module_info->regular_exports(), isolate);
19873 for (int i = 0, n = regular_exports->length(); i < n; i += 2) { 19873 for (int i = 0, n = regular_exports->length(); i < n; i += 2) {
19874 Handle<FixedArray> export_names( 19874 Handle<FixedArray> export_names(
19875 FixedArray::cast(regular_exports->get(i + 1)), isolate); 19875 FixedArray::cast(regular_exports->get(i + 1)), isolate);
19876 CreateExport(module, export_names); 19876 CreateExport(module, export_names);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
19938 if (ResolveExport(module, Handle<String>::cast(name), true, &resolve_set) 19938 if (ResolveExport(module, Handle<String>::cast(name), true, &resolve_set)
19939 .is_null()) { 19939 .is_null()) {
19940 return false; 19940 return false;
19941 } 19941 }
19942 } 19942 }
19943 19943
19944 return true; 19944 return true;
19945 } 19945 }
19946 19946
19947 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) { 19947 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) {
19948 DCHECK(module->code()->IsJSFunction()); // Instantiated. 19948 DCHECK(module->instantiated());
19949
19950 Isolate* isolate = module->GetIsolate();
19951 19949
19952 // Each module can only be evaluated once. 19950 // Each module can only be evaluated once.
19951 Isolate* isolate = module->GetIsolate();
19953 if (module->evaluated()) return isolate->factory()->undefined_value(); 19952 if (module->evaluated()) return isolate->factory()->undefined_value();
19954 module->set_evaluated(true); 19953 Handle<JSFunction> function(JSFunction::cast(module->code()), isolate);
19954 module->set_evaluated();
19955 19955
19956 // Initialization. 19956 // Initialization.
19957 Handle<JSFunction> function(JSFunction::cast(module->code()), isolate);
19958 DCHECK_EQ(MODULE_SCOPE, function->shared()->scope_info()->scope_type()); 19957 DCHECK_EQ(MODULE_SCOPE, function->shared()->scope_info()->scope_type());
19959 Handle<Object> receiver = isolate->factory()->undefined_value(); 19958 Handle<Object> receiver = isolate->factory()->undefined_value();
19960 Handle<Object> argv[] = {module}; 19959 Handle<Object> argv[] = {module};
19961 Handle<Object> generator; 19960 Handle<Object> generator;
19962 ASSIGN_RETURN_ON_EXCEPTION( 19961 ASSIGN_RETURN_ON_EXCEPTION(
19963 isolate, generator, 19962 isolate, generator,
19964 Execution::Call(isolate, function, receiver, arraysize(argv), argv), 19963 Execution::Call(isolate, function, receiver, arraysize(argv), argv),
19965 Object); 19964 Object);
19966 19965
19967 // Recursion. 19966 // Recursion.
19968 Handle<FixedArray> requested_modules(module->requested_modules(), isolate); 19967 Handle<FixedArray> requested_modules(module->requested_modules(), isolate);
19969 for (int i = 0, length = requested_modules->length(); i < length; ++i) { 19968 for (int i = 0, length = requested_modules->length(); i < length; ++i) {
19970 Handle<Module> import(Module::cast(requested_modules->get(i)), isolate); 19969 Handle<Module> import(Module::cast(requested_modules->get(i)), isolate);
19971 RETURN_ON_EXCEPTION(isolate, Evaluate(import), Object); 19970 RETURN_ON_EXCEPTION(isolate, Evaluate(import), Object);
19972 } 19971 }
19973 19972
19974 // Evaluation of module body. 19973 // Evaluation of module body.
19975 Handle<JSFunction> resume( 19974 Handle<JSFunction> resume(
19976 isolate->native_context()->generator_next_internal(), isolate); 19975 isolate->native_context()->generator_next_internal(), isolate);
19977 return Execution::Call(isolate, resume, generator, 0, nullptr); 19976 return Execution::Call(isolate, resume, generator, 0, nullptr);
19978 } 19977 }
19979 19978
19980 namespace { 19979 namespace {
19981 19980
19982 void FetchStarExports(Handle<Module> module, Zone* zone, 19981 void FetchStarExports(Handle<Module> module, Zone* zone,
19983 UnorderedModuleSet* visited) { 19982 UnorderedModuleSet* visited) {
19984 DCHECK(module->code()->IsJSFunction()); // Instantiated. 19983 DCHECK(module->instantiated());
19985 19984
19986 bool cycle = !visited->insert(module).second; 19985 bool cycle = !visited->insert(module).second;
19987 if (cycle) return; 19986 if (cycle) return;
19988 19987
19989 Isolate* isolate = module->GetIsolate(); 19988 Isolate* isolate = module->GetIsolate();
19990 Handle<ObjectHashTable> exports(module->exports(), isolate); 19989 Handle<ObjectHashTable> exports(module->exports(), isolate);
19991 UnorderedStringMap more_exports(zone); 19990 UnorderedStringMap more_exports(zone);
19992 19991
19993 // TODO(neis): Only allocate more_exports if there are star exports. 19992 // TODO(neis): Only allocate more_exports if there are star exports.
19994 // Maybe split special_exports into indirect_exports and star_exports. 19993 // Maybe split special_exports into indirect_exports and star_exports.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
20105 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr)) 20104 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr))
20106 .Check(); 20105 .Check();
20107 } 20106 }
20108 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked(); 20107 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked();
20109 20108
20110 return ns; 20109 return ns;
20111 } 20110 }
20112 20111
20113 } // namespace internal 20112 } // namespace internal
20114 } // namespace v8 20113 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698