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 19808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
19819 zone_allocator< | 19819 zone_allocator< |
19820 std::pair<const Handle<Module>, UnorderedStringSet*>>(zone)), | 19820 std::pair<const Handle<Module>, UnorderedStringSet*>>(zone)), |
19821 zone_(zone) {} | 19821 zone_(zone) {} |
19822 | 19822 |
19823 Zone* zone() const { return zone_; } | 19823 Zone* zone() const { return zone_; } |
19824 | 19824 |
19825 private: | 19825 private: |
19826 Zone* zone_; | 19826 Zone* zone_; |
19827 }; | 19827 }; |
19828 | 19828 |
| 19829 namespace { |
| 19830 |
| 19831 int ExportIndex(int cell_index) { |
| 19832 DCHECK_EQ(ModuleDescriptor::GetCellIndexKind(cell_index), |
| 19833 ModuleDescriptor::kExport); |
| 19834 return cell_index - 1; |
| 19835 } |
| 19836 |
| 19837 int ImportIndex(int cell_index) { |
| 19838 DCHECK_EQ(ModuleDescriptor::GetCellIndexKind(cell_index), |
| 19839 ModuleDescriptor::kImport); |
| 19840 return -cell_index - 1; |
| 19841 } |
| 19842 |
| 19843 } // anonymous namespace |
| 19844 |
19829 void Module::CreateIndirectExport(Handle<Module> module, Handle<String> name, | 19845 void Module::CreateIndirectExport(Handle<Module> module, Handle<String> name, |
19830 Handle<ModuleInfoEntry> entry) { | 19846 Handle<ModuleInfoEntry> entry) { |
19831 Isolate* isolate = module->GetIsolate(); | 19847 Isolate* isolate = module->GetIsolate(); |
19832 Handle<ObjectHashTable> exports(module->exports(), isolate); | 19848 Handle<ObjectHashTable> exports(module->exports(), isolate); |
19833 DCHECK(exports->Lookup(name)->IsTheHole(isolate)); | 19849 DCHECK(exports->Lookup(name)->IsTheHole(isolate)); |
19834 exports = ObjectHashTable::Put(exports, name, entry); | 19850 exports = ObjectHashTable::Put(exports, name, entry); |
19835 module->set_exports(*exports); | 19851 module->set_exports(*exports); |
19836 } | 19852 } |
19837 | 19853 |
19838 void Module::CreateExport(Handle<Module> module, Handle<FixedArray> names) { | 19854 void Module::CreateExport(Handle<Module> module, int cell_index, |
| 19855 Handle<FixedArray> names) { |
19839 DCHECK_LT(0, names->length()); | 19856 DCHECK_LT(0, names->length()); |
19840 Isolate* isolate = module->GetIsolate(); | 19857 Isolate* isolate = module->GetIsolate(); |
| 19858 |
19841 Handle<Cell> cell = | 19859 Handle<Cell> cell = |
19842 isolate->factory()->NewCell(isolate->factory()->undefined_value()); | 19860 isolate->factory()->NewCell(isolate->factory()->undefined_value()); |
| 19861 module->regular_exports()->set(ExportIndex(cell_index), *cell); |
| 19862 |
19843 Handle<ObjectHashTable> exports(module->exports(), isolate); | 19863 Handle<ObjectHashTable> exports(module->exports(), isolate); |
19844 for (int i = 0, n = names->length(); i < n; ++i) { | 19864 for (int i = 0, n = names->length(); i < n; ++i) { |
19845 Handle<String> name(String::cast(names->get(i)), isolate); | 19865 Handle<String> name(String::cast(names->get(i)), isolate); |
19846 DCHECK(exports->Lookup(name)->IsTheHole(isolate)); | 19866 DCHECK(exports->Lookup(name)->IsTheHole(isolate)); |
19847 exports = ObjectHashTable::Put(exports, name, cell); | 19867 exports = ObjectHashTable::Put(exports, name, cell); |
19848 } | 19868 } |
19849 module->set_exports(*exports); | 19869 module->set_exports(*exports); |
19850 } | 19870 } |
19851 | 19871 |
19852 void Module::StoreExport(Handle<Module> module, Handle<String> name, | 19872 Handle<Object> Module::LoadVariable(Handle<Module> module, int cell_index) { |
19853 Handle<Object> value) { | |
19854 Handle<Cell> cell(Cell::cast(module->exports()->Lookup(name))); | |
19855 cell->set_value(*value); | |
19856 } | |
19857 | |
19858 Handle<Object> Module::LoadExport(Handle<Module> module, Handle<String> name) { | |
19859 Isolate* isolate = module->GetIsolate(); | 19873 Isolate* isolate = module->GetIsolate(); |
19860 Handle<Object> object(module->exports()->Lookup(name), isolate); | 19874 Handle<Object> object; |
| 19875 switch (ModuleDescriptor::GetCellIndexKind(cell_index)) { |
| 19876 case ModuleDescriptor::kImport: |
| 19877 object = handle(module->regular_imports()->get(ImportIndex(cell_index)), |
| 19878 isolate); |
| 19879 break; |
| 19880 case ModuleDescriptor::kExport: |
| 19881 object = handle(module->regular_exports()->get(ExportIndex(cell_index)), |
| 19882 isolate); |
| 19883 break; |
| 19884 case ModuleDescriptor::kInvalid: |
| 19885 UNREACHABLE(); |
| 19886 break; |
| 19887 } |
19861 return handle(Handle<Cell>::cast(object)->value(), isolate); | 19888 return handle(Handle<Cell>::cast(object)->value(), isolate); |
19862 } | 19889 } |
19863 | 19890 |
19864 Handle<Object> Module::LoadImport(Handle<Module> module, Handle<String> name, | 19891 void Module::StoreVariable(Handle<Module> module, int cell_index, |
19865 int module_request) { | 19892 Handle<Object> value) { |
19866 Isolate* isolate = module->GetIsolate(); | 19893 Isolate* isolate = module->GetIsolate(); |
19867 Handle<Module> requested_module( | 19894 DCHECK_EQ(ModuleDescriptor::GetCellIndexKind(cell_index), |
19868 Module::cast(module->requested_modules()->get(module_request)), isolate); | 19895 ModuleDescriptor::kExport); |
19869 return Module::LoadExport(requested_module, name); | 19896 Handle<Object> object(module->regular_exports()->get(ExportIndex(cell_index)), |
| 19897 isolate); |
| 19898 Handle<Cell>::cast(object)->set_value(*value); |
19870 } | 19899 } |
19871 | 19900 |
19872 MaybeHandle<Cell> Module::ResolveImport(Handle<Module> module, | 19901 MaybeHandle<Cell> Module::ResolveImport(Handle<Module> module, |
19873 Handle<String> name, int module_request, | 19902 Handle<String> name, int module_request, |
19874 MessageLocation loc, bool must_resolve, | 19903 MessageLocation loc, bool must_resolve, |
19875 Module::ResolveSet* resolve_set) { | 19904 Module::ResolveSet* resolve_set) { |
19876 Isolate* isolate = module->GetIsolate(); | 19905 Isolate* isolate = module->GetIsolate(); |
19877 Handle<Module> requested_module( | 19906 Handle<Module> requested_module( |
19878 Module::cast(module->requested_modules()->get(module_request)), isolate); | 19907 Module::cast(module->requested_modules()->get(module_request)), isolate); |
19879 return Module::ResolveExport(requested_module, name, loc, must_resolve, | 19908 return Module::ResolveExport(requested_module, name, loc, must_resolve, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
20014 isolate->factory()->NewFunctionFromSharedFunctionInfo( | 20043 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
20015 shared, | 20044 shared, |
20016 handle(Utils::OpenHandle(*context)->native_context(), isolate)); | 20045 handle(Utils::OpenHandle(*context)->native_context(), isolate)); |
20017 module->set_code(*function); | 20046 module->set_code(*function); |
20018 DCHECK(module->instantiated()); | 20047 DCHECK(module->instantiated()); |
20019 | 20048 |
20020 Handle<ModuleInfo> module_info(shared->scope_info()->ModuleDescriptorInfo(), | 20049 Handle<ModuleInfo> module_info(shared->scope_info()->ModuleDescriptorInfo(), |
20021 isolate); | 20050 isolate); |
20022 | 20051 |
20023 // Set up local exports. | 20052 // Set up local exports. |
| 20053 // TODO(neis): Create regular_exports array here instead of in factory method? |
20024 for (int i = 0, n = module_info->RegularExportCount(); i < n; ++i) { | 20054 for (int i = 0, n = module_info->RegularExportCount(); i < n; ++i) { |
| 20055 int cell_index = module_info->RegularExportCellIndex(i); |
20025 Handle<FixedArray> export_names(module_info->RegularExportExportNames(i), | 20056 Handle<FixedArray> export_names(module_info->RegularExportExportNames(i), |
20026 isolate); | 20057 isolate); |
20027 CreateExport(module, export_names); | 20058 CreateExport(module, cell_index, export_names); |
20028 } | 20059 } |
20029 | 20060 |
20030 // Partially set up indirect exports. | 20061 // Partially set up indirect exports. |
20031 // For each indirect export, we create the appropriate slot in the export | 20062 // For each indirect export, we create the appropriate slot in the export |
20032 // table and store its ModuleInfoEntry there. When we later find the correct | 20063 // table and store its ModuleInfoEntry there. When we later find the correct |
20033 // Cell in the module that actually provides the value, we replace the | 20064 // Cell in the module that actually provides the value, we replace the |
20034 // ModuleInfoEntry by that Cell (see ResolveExport). | 20065 // ModuleInfoEntry by that Cell (see ResolveExport). |
20035 Handle<FixedArray> special_exports(module_info->special_exports(), isolate); | 20066 Handle<FixedArray> special_exports(module_info->special_exports(), isolate); |
20036 for (int i = 0, n = special_exports->length(); i < n; ++i) { | 20067 for (int i = 0, n = special_exports->length(); i < n; ++i) { |
20037 Handle<ModuleInfoEntry> entry( | 20068 Handle<ModuleInfoEntry> entry( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
20069 Handle<FixedArray> regular_imports(module_info->regular_imports(), isolate); | 20100 Handle<FixedArray> regular_imports(module_info->regular_imports(), isolate); |
20070 for (int i = 0, n = regular_imports->length(); i < n; ++i) { | 20101 for (int i = 0, n = regular_imports->length(); i < n; ++i) { |
20071 Handle<ModuleInfoEntry> entry( | 20102 Handle<ModuleInfoEntry> entry( |
20072 ModuleInfoEntry::cast(regular_imports->get(i)), isolate); | 20103 ModuleInfoEntry::cast(regular_imports->get(i)), isolate); |
20073 Handle<String> name(String::cast(entry->import_name()), isolate); | 20104 Handle<String> name(String::cast(entry->import_name()), isolate); |
20074 Handle<Script> script( | 20105 Handle<Script> script( |
20075 Script::cast(JSFunction::cast(module->code())->shared()->script()), | 20106 Script::cast(JSFunction::cast(module->code())->shared()->script()), |
20076 isolate); | 20107 isolate); |
20077 MessageLocation loc(script, entry->beg_pos(), entry->end_pos()); | 20108 MessageLocation loc(script, entry->beg_pos(), entry->end_pos()); |
20078 ResolveSet resolve_set(&zone); | 20109 ResolveSet resolve_set(&zone); |
20079 if (ResolveImport(module, name, entry->module_request(), loc, true, | 20110 Handle<Cell> cell; |
20080 &resolve_set) | 20111 if (!ResolveImport(module, name, entry->module_request(), loc, true, |
20081 .is_null()) { | 20112 &resolve_set) |
| 20113 .ToHandle(&cell)) { |
20082 return false; | 20114 return false; |
20083 } | 20115 } |
| 20116 module->regular_imports()->set(ImportIndex(entry->cell_index()), *cell); |
20084 } | 20117 } |
20085 | 20118 |
20086 // Resolve indirect exports. | 20119 // Resolve indirect exports. |
20087 for (int i = 0, n = special_exports->length(); i < n; ++i) { | 20120 for (int i = 0, n = special_exports->length(); i < n; ++i) { |
20088 Handle<ModuleInfoEntry> entry( | 20121 Handle<ModuleInfoEntry> entry( |
20089 ModuleInfoEntry::cast(special_exports->get(i)), isolate); | 20122 ModuleInfoEntry::cast(special_exports->get(i)), isolate); |
20090 Handle<Object> name(entry->export_name(), isolate); | 20123 Handle<Object> name(entry->export_name(), isolate); |
20091 if (name->IsUndefined(isolate)) continue; // Star export. | 20124 if (name->IsUndefined(isolate)) continue; // Star export. |
20092 Handle<Script> script( | 20125 Handle<Script> script( |
20093 Script::cast(JSFunction::cast(module->code())->shared()->script()), | 20126 Script::cast(JSFunction::cast(module->code())->shared()->script()), |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
20276 // Check if the accessor uses a cached property. | 20309 // Check if the accessor uses a cached property. |
20277 if (!fti->cached_property_name()->IsTheHole(isolate)) { | 20310 if (!fti->cached_property_name()->IsTheHole(isolate)) { |
20278 return handle(Name::cast(fti->cached_property_name())); | 20311 return handle(Name::cast(fti->cached_property_name())); |
20279 } | 20312 } |
20280 } | 20313 } |
20281 return MaybeHandle<Name>(); | 20314 return MaybeHandle<Name>(); |
20282 } | 20315 } |
20283 | 20316 |
20284 } // namespace internal | 20317 } // namespace internal |
20285 } // namespace v8 | 20318 } // namespace v8 |
OLD | NEW |