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

Side by Side Diff: src/objects.cc

Issue 2451153002: [modules] Improve error messages. (Closed)
Patch Set: . 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
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 int beg_pos = entry->beg_pos();
adamk 2016/10/26 12:38:16 These can be inlined into the constructor call two
19956 int end_pos = entry->end_pos();
19957 MessageLocation new_loc(script, beg_pos, end_pos);
19958
19946 Handle<Cell> cell; 19959 Handle<Cell> cell;
19947 if (ResolveImport(module, name, module_request, false, resolve_set) 19960 if (ResolveImport(module, name, module_request, new_loc, false,
19961 resolve_set)
19948 .ToHandle(&cell)) { 19962 .ToHandle(&cell)) {
19949 if (unique_cell.is_null()) unique_cell = cell; 19963 if (unique_cell.is_null()) unique_cell = cell;
19950 if (*unique_cell != *cell) { 19964 if (*unique_cell != *cell) {
19951 THROW_NEW_ERROR( 19965 return isolate->Throw<Cell>(
19952 isolate, NewSyntaxError(MessageTemplate::kAmbiguousExport, name), 19966 isolate->factory()->NewSyntaxError(
19953 Cell); 19967 MessageTemplate::kAmbiguousExport, name),
19968 &loc);
19954 } 19969 }
19955 } else if (isolate->has_pending_exception()) { 19970 } else if (isolate->has_pending_exception()) {
19956 return MaybeHandle<Cell>(); 19971 return MaybeHandle<Cell>();
19957 } 19972 }
19958 } 19973 }
19959 19974
19960 if (!unique_cell.is_null()) { 19975 if (!unique_cell.is_null()) {
19961 // Found a unique star export for this name. 19976 // Found a unique star export for this name.
19962 Handle<ObjectHashTable> exports(module->exports(), isolate); 19977 Handle<ObjectHashTable> exports(module->exports(), isolate);
19963 DCHECK(exports->Lookup(name)->IsTheHole(isolate)); 19978 DCHECK(exports->Lookup(name)->IsTheHole(isolate));
19964 exports = ObjectHashTable::Put(exports, name, unique_cell); 19979 exports = ObjectHashTable::Put(exports, name, unique_cell);
19965 module->set_exports(*exports); 19980 module->set_exports(*exports);
19966 return unique_cell; 19981 return unique_cell;
19967 } 19982 }
19968 } 19983 }
19969 19984
19970 // Unresolvable. 19985 // Unresolvable.
19971 if (must_resolve) { 19986 if (must_resolve) {
19972 THROW_NEW_ERROR(isolate, 19987 return isolate->Throw<Cell>(isolate->factory()->NewSyntaxError(
19973 NewSyntaxError(MessageTemplate::kUnresolvableExport, name), 19988 MessageTemplate::kUnresolvableExport, name),
19974 Cell); 19989 &loc);
19975 } 19990 }
19976 return MaybeHandle<Cell>(); 19991 return MaybeHandle<Cell>();
19977 } 19992 }
19978 19993
19979 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context, 19994 bool Module::Instantiate(Handle<Module> module, v8::Local<v8::Context> context,
19980 v8::Module::ResolveCallback callback) { 19995 v8::Module::ResolveCallback callback) {
19981 if (module->instantiated()) return true; 19996 if (module->instantiated()) return true;
19982 19997
19983 Isolate* isolate = module->GetIsolate(); 19998 Isolate* isolate = module->GetIsolate();
19984 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()), 19999 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(module->code()),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
20039 20054
20040 Zone zone(isolate->allocator(), ZONE_NAME); 20055 Zone zone(isolate->allocator(), ZONE_NAME);
20041 20056
20042 // Resolve imports. 20057 // Resolve imports.
20043 Handle<FixedArray> regular_imports(module_info->regular_imports(), isolate); 20058 Handle<FixedArray> regular_imports(module_info->regular_imports(), isolate);
20044 for (int i = 0, n = regular_imports->length(); i < n; ++i) { 20059 for (int i = 0, n = regular_imports->length(); i < n; ++i) {
20045 Handle<ModuleInfoEntry> entry( 20060 Handle<ModuleInfoEntry> entry(
20046 ModuleInfoEntry::cast(regular_imports->get(i)), isolate); 20061 ModuleInfoEntry::cast(regular_imports->get(i)), isolate);
20047 Handle<String> name(String::cast(entry->import_name()), isolate); 20062 Handle<String> name(String::cast(entry->import_name()), isolate);
20048 int module_request = Smi::cast(entry->module_request())->value(); 20063 int module_request = Smi::cast(entry->module_request())->value();
20064 Handle<Script> script(
20065 Script::cast(JSFunction::cast(module->code())->shared()->script()),
20066 isolate);
20067 int beg_pos = entry->beg_pos();
20068 int end_pos = entry->end_pos();
20069 MessageLocation loc(script, beg_pos, end_pos);
20049 ResolveSet resolve_set(&zone); 20070 ResolveSet resolve_set(&zone);
20050 if (ResolveImport(module, name, module_request, true, &resolve_set) 20071 if (ResolveImport(module, name, module_request, loc, true, &resolve_set)
20051 .is_null()) { 20072 .is_null()) {
20052 return false; 20073 return false;
20053 } 20074 }
20054 } 20075 }
20055 20076
20056 // Resolve indirect exports. 20077 // Resolve indirect exports.
20057 for (int i = 0, n = special_exports->length(); i < n; ++i) { 20078 for (int i = 0, n = special_exports->length(); i < n; ++i) {
20058 Handle<ModuleInfoEntry> entry( 20079 Handle<ModuleInfoEntry> entry(
20059 ModuleInfoEntry::cast(special_exports->get(i)), isolate); 20080 ModuleInfoEntry::cast(special_exports->get(i)), isolate);
20060 Handle<Object> name(entry->export_name(), isolate); 20081 Handle<Object> name(entry->export_name(), isolate);
20061 if (name->IsUndefined(isolate)) continue; // Star export. 20082 if (name->IsUndefined(isolate)) continue; // Star export.
20083 Handle<Script> script(
20084 Script::cast(JSFunction::cast(module->code())->shared()->script()),
20085 isolate);
20086 int beg_pos = entry->beg_pos();
20087 int end_pos = entry->end_pos();
20088 MessageLocation loc(script, beg_pos, end_pos);
20062 ResolveSet resolve_set(&zone); 20089 ResolveSet resolve_set(&zone);
20063 if (ResolveExport(module, Handle<String>::cast(name), true, &resolve_set) 20090 if (ResolveExport(module, Handle<String>::cast(name), loc, true,
20091 &resolve_set)
20064 .is_null()) { 20092 .is_null()) {
20065 return false; 20093 return false;
20066 } 20094 }
20067 } 20095 }
20068 20096
20069 return true; 20097 return true;
20070 } 20098 }
20071 20099
20072 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) { 20100 MaybeHandle<Object> Module::Evaluate(Handle<Module> module) {
20073 DCHECK(module->instantiated()); 20101 DCHECK(module->instantiated());
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
20229 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr)) 20257 ns, Accessors::ModuleNamespaceEntryInfo(isolate, name, attr))
20230 .Check(); 20258 .Check();
20231 } 20259 }
20232 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked(); 20260 JSObject::PreventExtensions(ns, THROW_ON_ERROR).ToChecked();
20233 20261
20234 return ns; 20262 return ns;
20235 } 20263 }
20236 20264
20237 } // namespace internal 20265 } // namespace internal
20238 } // namespace v8 20266 } // namespace v8
OLDNEW
« src/ast/modules.cc ('K') | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698