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

Side by Side Diff: src/objects.cc

Issue 2451153002: [modules] Improve error messages. (Closed)
Patch Set: Address comments. Created 4 years, 1 month 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
« no previous file with comments | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 19837 matching lines...) Expand 10 before | Expand all | Expand 10 after
19848 Handle<Object> Module::LoadImport(Handle<Module> module, Handle<String> name, 19848 Handle<Object> Module::LoadImport(Handle<Module> module, Handle<String> name,
19849 int module_request) { 19849 int module_request) {
19850 Isolate* isolate = module->GetIsolate(); 19850 Isolate* isolate = module->GetIsolate();
19851 Handle<Module> requested_module( 19851 Handle<Module> requested_module(
19852 Module::cast(module->requested_modules()->get(module_request)), isolate); 19852 Module::cast(module->requested_modules()->get(module_request)), isolate);
19853 return Module::LoadExport(requested_module, name); 19853 return Module::LoadExport(requested_module, name);
19854 } 19854 }
19855 19855
19856 MaybeHandle<Cell> Module::ResolveImport(Handle<Module> module, 19856 MaybeHandle<Cell> Module::ResolveImport(Handle<Module> module,
19857 Handle<String> name, int module_request, 19857 Handle<String> name, int module_request,
19858 bool must_resolve, 19858 MessageLocation loc, bool must_resolve,
19859 Module::ResolveSet* resolve_set) { 19859 Module::ResolveSet* resolve_set) {
19860 Isolate* isolate = module->GetIsolate(); 19860 Isolate* isolate = module->GetIsolate();
19861 Handle<Module> requested_module( 19861 Handle<Module> requested_module(
19862 Module::cast(module->requested_modules()->get(module_request)), isolate); 19862 Module::cast(module->requested_modules()->get(module_request)), isolate);
19863 return Module::ResolveExport(requested_module, name, must_resolve, 19863 return Module::ResolveExport(requested_module, name, loc, must_resolve,
19864 resolve_set); 19864 resolve_set);
19865 } 19865 }
19866 19866
19867 MaybeHandle<Cell> Module::ResolveExport(Handle<Module> module, 19867 MaybeHandle<Cell> Module::ResolveExport(Handle<Module> module,
19868 Handle<String> name, bool must_resolve, 19868 Handle<String> name,
19869 MessageLocation loc, bool must_resolve,
19869 Module::ResolveSet* resolve_set) { 19870 Module::ResolveSet* resolve_set) {
19870 Isolate* isolate = module->GetIsolate(); 19871 Isolate* isolate = module->GetIsolate();
19871 Handle<Object> object(module->exports()->Lookup(name), isolate); 19872 Handle<Object> object(module->exports()->Lookup(name), isolate);
19872 if (object->IsCell()) { 19873 if (object->IsCell()) {
19873 // Already resolved (e.g. because it's a local export). 19874 // Already resolved (e.g. because it's a local export).
19874 return Handle<Cell>::cast(object); 19875 return Handle<Cell>::cast(object);
19875 } 19876 }
19876 19877
19877 // Check for cycle before recursing. 19878 // Check for cycle before recursing.
19878 { 19879 {
19879 // Attempt insertion with a null string set. 19880 // Attempt insertion with a null string set.
19880 auto result = resolve_set->insert({module, nullptr}); 19881 auto result = resolve_set->insert({module, nullptr});
19881 UnorderedStringSet*& name_set = result.first->second; 19882 UnorderedStringSet*& name_set = result.first->second;
19882 if (result.second) { 19883 if (result.second) {
19883 // |module| wasn't in the map previously, so allocate a new name set. 19884 // |module| wasn't in the map previously, so allocate a new name set.
19884 Zone* zone = resolve_set->zone(); 19885 Zone* zone = resolve_set->zone();
19885 name_set = 19886 name_set =
19886 new (zone->New(sizeof(UnorderedStringSet))) UnorderedStringSet(zone); 19887 new (zone->New(sizeof(UnorderedStringSet))) UnorderedStringSet(zone);
19887 } else if (name_set->count(name)) { 19888 } else if (name_set->count(name)) {
19888 // Cycle detected. 19889 // Cycle detected.
19889 if (must_resolve) { 19890 if (must_resolve) {
19890 THROW_NEW_ERROR( 19891 return isolate->Throw<Cell>(
19891 isolate, 19892 isolate->factory()->NewSyntaxError(
19892 NewSyntaxError(MessageTemplate::kCyclicModuleDependency, name), 19893 MessageTemplate::kCyclicModuleDependency, name),
19893 Cell); 19894 &loc);
19894 } 19895 }
19895 return MaybeHandle<Cell>(); 19896 return MaybeHandle<Cell>();
19896 } 19897 }
19897 name_set->insert(name); 19898 name_set->insert(name);
19898 } 19899 }
19899 19900
19900 if (object->IsModuleInfoEntry()) { 19901 if (object->IsModuleInfoEntry()) {
19901 // Not yet resolved indirect export. 19902 // Not yet resolved indirect export.
19902 Handle<ModuleInfoEntry> entry = Handle<ModuleInfoEntry>::cast(object); 19903 Handle<ModuleInfoEntry> entry = Handle<ModuleInfoEntry>::cast(object);
19903 int module_request = Smi::cast(entry->module_request())->value(); 19904 int module_request = Smi::cast(entry->module_request())->value();
19904 Handle<String> import_name(String::cast(entry->import_name()), isolate); 19905 Handle<String> import_name(String::cast(entry->import_name()), isolate);
19906 Handle<Script> script(
19907 Script::cast(JSFunction::cast(module->code())->shared()->script()),
19908 isolate);
19909 MessageLocation new_loc(script, entry->beg_pos(), entry->end_pos());
19905 19910
19906 Handle<Cell> cell; 19911 Handle<Cell> cell;
19907 if (!ResolveImport(module, import_name, module_request, true, resolve_set) 19912 if (!ResolveImport(module, import_name, module_request, new_loc, true,
19913 resolve_set)
19908 .ToHandle(&cell)) { 19914 .ToHandle(&cell)) {
19909 DCHECK(isolate->has_pending_exception()); 19915 DCHECK(isolate->has_pending_exception());
19910 return MaybeHandle<Cell>(); 19916 return MaybeHandle<Cell>();
19911 } 19917 }
19912 19918
19913 // The export table may have changed but the entry in question should be 19919 // The export table may have changed but the entry in question should be
19914 // unchanged. 19920 // unchanged.
19915 Handle<ObjectHashTable> exports(module->exports(), isolate); 19921 Handle<ObjectHashTable> exports(module->exports(), isolate);
19916 DCHECK(exports->Lookup(name)->IsModuleInfoEntry()); 19922 DCHECK(exports->Lookup(name)->IsModuleInfoEntry());
19917 19923
19918 exports = ObjectHashTable::Put(exports, name, cell); 19924 exports = ObjectHashTable::Put(exports, name, cell);
19919 module->set_exports(*exports); 19925 module->set_exports(*exports);
19920 return cell; 19926 return cell;
19921 } 19927 }
19922 19928
19923 DCHECK(object->IsTheHole(isolate)); 19929 DCHECK(object->IsTheHole(isolate));
19924 return Module::ResolveExportUsingStarExports(module, name, must_resolve, 19930 return Module::ResolveExportUsingStarExports(module, name, loc, must_resolve,
19925 resolve_set); 19931 resolve_set);
19926 } 19932 }
19927 19933
19928 MaybeHandle<Cell> Module::ResolveExportUsingStarExports( 19934 MaybeHandle<Cell> Module::ResolveExportUsingStarExports(
19929 Handle<Module> module, Handle<String> name, bool must_resolve, 19935 Handle<Module> module, Handle<String> name, MessageLocation loc,
19930 Module::ResolveSet* resolve_set) { 19936 bool must_resolve, Module::ResolveSet* resolve_set) {
19931 Isolate* isolate = module->GetIsolate(); 19937 Isolate* isolate = module->GetIsolate();
19932 if (!name->Equals(isolate->heap()->default_string())) { 19938 if (!name->Equals(isolate->heap()->default_string())) {
19933 // Go through all star exports looking for the given name. If multiple star 19939 // Go through all star exports looking for the given name. If multiple star
19934 // exports provide the name, make sure they all map it to the same cell. 19940 // exports provide the name, make sure they all map it to the same cell.
19935 Handle<Cell> unique_cell; 19941 Handle<Cell> unique_cell;
19936 Handle<FixedArray> special_exports(module->info()->special_exports(), 19942 Handle<FixedArray> special_exports(module->info()->special_exports(),
19937 isolate); 19943 isolate);
19938 for (int i = 0, n = special_exports->length(); i < n; ++i) { 19944 for (int i = 0, n = special_exports->length(); i < n; ++i) {
19939 i::Handle<i::ModuleInfoEntry> entry( 19945 i::Handle<i::ModuleInfoEntry> entry(
19940 i::ModuleInfoEntry::cast(special_exports->get(i)), isolate); 19946 i::ModuleInfoEntry::cast(special_exports->get(i)), isolate);
19941 if (!entry->export_name()->IsUndefined(isolate)) { 19947 if (!entry->export_name()->IsUndefined(isolate)) {
19942 continue; // Indirect export. 19948 continue; // Indirect export.
19943 } 19949 }
19944 int module_request = Smi::cast(entry->module_request())->value(); 19950 int module_request = Smi::cast(entry->module_request())->value();
19945 19951
19952 Handle<Script> script(
19953 Script::cast(JSFunction::cast(module->code())->shared()->script()),
19954 isolate);
19955 MessageLocation new_loc(script, entry->beg_pos(), entry->end_pos());
19956
19946 Handle<Cell> cell; 19957 Handle<Cell> cell;
19947 if (ResolveImport(module, name, module_request, false, resolve_set) 19958 if (ResolveImport(module, name, module_request, new_loc, false,
19959 resolve_set)
19948 .ToHandle(&cell)) { 19960 .ToHandle(&cell)) {
19949 if (unique_cell.is_null()) unique_cell = cell; 19961 if (unique_cell.is_null()) unique_cell = cell;
19950 if (*unique_cell != *cell) { 19962 if (*unique_cell != *cell) {
19951 THROW_NEW_ERROR( 19963 return isolate->Throw<Cell>(
19952 isolate, NewSyntaxError(MessageTemplate::kAmbiguousExport, name), 19964 isolate->factory()->NewSyntaxError(
19953 Cell); 19965 MessageTemplate::kAmbiguousExport, name),
19966 &loc);
19954 } 19967 }
19955 } else if (isolate->has_pending_exception()) { 19968 } else if (isolate->has_pending_exception()) {
19956 return MaybeHandle<Cell>(); 19969 return MaybeHandle<Cell>();
19957 } 19970 }
19958 } 19971 }
19959 19972
19960 if (!unique_cell.is_null()) { 19973 if (!unique_cell.is_null()) {
19961 // Found a unique star export for this name. 19974 // Found a unique star export for this name.
19962 Handle<ObjectHashTable> exports(module->exports(), isolate); 19975 Handle<ObjectHashTable> exports(module->exports(), isolate);
19963 DCHECK(exports->Lookup(name)->IsTheHole(isolate)); 19976 DCHECK(exports->Lookup(name)->IsTheHole(isolate));
19964 exports = ObjectHashTable::Put(exports, name, unique_cell); 19977 exports = ObjectHashTable::Put(exports, name, unique_cell);
19965 module->set_exports(*exports); 19978 module->set_exports(*exports);
19966 return unique_cell; 19979 return unique_cell;
19967 } 19980 }
19968 } 19981 }
19969 19982
19970 // Unresolvable. 19983 // Unresolvable.
19971 if (must_resolve) { 19984 if (must_resolve) {
19972 THROW_NEW_ERROR(isolate, 19985 return isolate->Throw<Cell>(isolate->factory()->NewSyntaxError(
19973 NewSyntaxError(MessageTemplate::kUnresolvableExport, name), 19986 MessageTemplate::kUnresolvableExport, name),
19974 Cell); 19987 &loc);
19975 } 19988 }
19976 return MaybeHandle<Cell>(); 19989 return MaybeHandle<Cell>();
19977 } 19990 }
19978 19991
19979 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context, 19992 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context,
19980 v8::Module::ResolveCallback callback) { 19993 v8::Module::ResolveCallback callback) {
19981 if (module->instantiated()) return true; 19994 if (module->instantiated()) return true;
19982 19995
19983 Isolate* isolate = module->GetIsolate(); 19996 Isolate* isolate = module->GetIsolate();
19984 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()), 19997 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
20039 20052
20040 Zone zone(isolate->allocator(), ZONE_NAME); 20053 Zone zone(isolate->allocator(), ZONE_NAME);
20041 20054
20042 // Resolve imports. 20055 // Resolve imports.
20043 Handle<FixedArray> regular_imports(module_info->regular_imports(), isolate); 20056 Handle<FixedArray> regular_imports(module_info->regular_imports(), isolate);
20044 for (int i = 0, n = regular_imports->length(); i < n; ++i) { 20057 for (int i = 0, n = regular_imports->length(); i < n; ++i) {
20045 Handle<ModuleInfoEntry> entry( 20058 Handle<ModuleInfoEntry> entry(
20046 ModuleInfoEntry::cast(regular_imports->get(i)), isolate); 20059 ModuleInfoEntry::cast(regular_imports->get(i)), isolate);
20047 Handle<String> name(String::cast(entry->import_name()), isolate); 20060 Handle<String> name(String::cast(entry->import_name()), isolate);
20048 int module_request = Smi::cast(entry->module_request())->value(); 20061 int module_request = Smi::cast(entry->module_request())->value();
20062 Handle<Script> script(
20063 Script::cast(JSFunction::cast(module->code())->shared()->script()),
20064 isolate);
20065 MessageLocation loc(script, entry->beg_pos(), entry->end_pos());
20049 ResolveSet resolve_set(&zone); 20066 ResolveSet resolve_set(&zone);
20050 if (ResolveImport(module, name, module_request, true, &resolve_set) 20067 if (ResolveImport(module, name, module_request, loc, true, &resolve_set)
20051 .is_null()) { 20068 .is_null()) {
20052 return false; 20069 return false;
20053 } 20070 }
20054 } 20071 }
20055 20072
20056 // Resolve indirect exports. 20073 // Resolve indirect exports.
20057 for (int i = 0, n = special_exports->length(); i < n; ++i) { 20074 for (int i = 0, n = special_exports->length(); i < n; ++i) {
20058 Handle<ModuleInfoEntry> entry( 20075 Handle<ModuleInfoEntry> entry(
20059 ModuleInfoEntry::cast(special_exports->get(i)), isolate); 20076 ModuleInfoEntry::cast(special_exports->get(i)), isolate);
20060 Handle<Object> name(entry->export_name(), isolate); 20077 Handle<Object> name(entry->export_name(), isolate);
20061 if (name->IsUndefined(isolate)) continue; // Star export. 20078 if (name->IsUndefined(isolate)) continue; // Star export.
20079 Handle<Script> script(
20080 Script::cast(JSFunction::cast(module->code())->shared()->script()),
20081 isolate);
20082 MessageLocation loc(script, entry->beg_pos(), entry->end_pos());
20062 ResolveSet resolve_set(&zone); 20083 ResolveSet resolve_set(&zone);
20063 if (ResolveExport(module, Handle<String>::cast(name), true, &resolve_set) 20084 if (ResolveExport(module, Handle<String>::cast(name), loc, true,
20085 &resolve_set)
20064 .is_null()) { 20086 .is_null()) {
20065 return false; 20087 return false;
20066 } 20088 }
20067 } 20089 }
20068 20090
20069 return true; 20091 return true;
20070 } 20092 }
20071 20093
20072 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) { 20094 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) {
20073 DCHECK(module->instantiated()); 20095 DCHECK(module->instantiated());
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
20229 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr)) 20251 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr))
20230 .Check(); 20252 .Check();
20231 } 20253 }
20232 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked(); 20254 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked();
20233 20255
20234 return ns; 20256 return ns;
20235 } 20257 }
20236 20258
20237 } // namespace internal 20259 } // namespace internal
20238 } // namespace v8 20260 } // namespace v8
OLDNEW
« no previous file with comments | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698