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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "src/assembler-inl.h" | 7 #include "src/assembler-inl.h" |
8 #include "src/base/adapters.h" | 8 #include "src/base/adapters.h" |
9 #include "src/base/atomic-utils.h" | 9 #include "src/base/atomic-utils.h" |
10 #include "src/code-stubs.h" | 10 #include "src/code-stubs.h" |
(...skipping 1124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 } | 1135 } |
1136 } | 1136 } |
1137 | 1137 |
1138 // A helper class to simplify instantiating a module from a compiled module. | 1138 // A helper class to simplify instantiating a module from a compiled module. |
1139 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, | 1139 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, |
1140 // etc. | 1140 // etc. |
1141 class WasmInstanceBuilder { | 1141 class WasmInstanceBuilder { |
1142 public: | 1142 public: |
1143 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, | 1143 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, |
1144 Handle<WasmModuleObject> module_object, | 1144 Handle<WasmModuleObject> module_object, |
1145 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory) | 1145 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory, |
| 1146 Handle<JSObject> export_to) |
1146 : isolate_(isolate), | 1147 : isolate_(isolate), |
1147 module_(module_object->compiled_module()->module()), | 1148 module_(module_object->compiled_module()->module()), |
1148 thrower_(thrower), | 1149 thrower_(thrower), |
1149 module_object_(module_object), | 1150 module_object_(module_object), |
1150 ffi_(ffi), | 1151 ffi_(ffi), |
1151 memory_(memory) {} | 1152 memory_(memory), |
| 1153 export_to_(export_to) {} |
1152 | 1154 |
1153 // Build an instance, in all of its glory. | 1155 // Build an instance, in all of its glory. |
1154 MaybeHandle<WasmInstanceObject> Build() { | 1156 MaybeHandle<WasmInstanceObject> Build() { |
1155 MaybeHandle<WasmInstanceObject> nothing; | 1157 MaybeHandle<WasmInstanceObject> nothing; |
1156 | 1158 |
1157 // Check that an imports argument was provided, if the module requires it. | 1159 // Check that an imports argument was provided, if the module requires it. |
1158 // No point in continuing otherwise. | 1160 // No point in continuing otherwise. |
1159 if (!module_->import_table.empty() && ffi_.is_null()) { | 1161 if (!module_->import_table.empty() && ffi_.is_null()) { |
1160 thrower_->TypeError( | 1162 thrower_->TypeError( |
1161 "Imports argument must be present and must be an object"); | 1163 "Imports argument must be present and must be an object"); |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1375 deopt_data->set(0, *weak_link); | 1377 deopt_data->set(0, *weak_link); |
1376 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); | 1378 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); |
1377 deopt_data->set_length(2); | 1379 deopt_data->set_length(2); |
1378 code->set_deoptimization_data(*deopt_data); | 1380 code->set_deoptimization_data(*deopt_data); |
1379 } | 1381 } |
1380 } | 1382 } |
1381 | 1383 |
1382 //-------------------------------------------------------------------------- | 1384 //-------------------------------------------------------------------------- |
1383 // Set up the exports object for the new instance. | 1385 // Set up the exports object for the new instance. |
1384 //-------------------------------------------------------------------------- | 1386 //-------------------------------------------------------------------------- |
1385 ProcessExports(code_table, instance, compiled_module_); | 1387 ProcessExports(code_table, instance, export_to_, compiled_module_); |
1386 | 1388 |
1387 //-------------------------------------------------------------------------- | 1389 //-------------------------------------------------------------------------- |
1388 // Add instance to Memory object | 1390 // Add instance to Memory object |
1389 //-------------------------------------------------------------------------- | 1391 //-------------------------------------------------------------------------- |
1390 DCHECK(wasm::IsWasmInstance(*instance)); | 1392 DCHECK(wasm::IsWasmInstance(*instance)); |
1391 if (instance->has_memory_object()) { | 1393 if (instance->has_memory_object()) { |
1392 instance->memory_object()->AddInstance(isolate_, instance); | 1394 instance->memory_object()->AddInstance(isolate_, instance); |
1393 } | 1395 } |
1394 | 1396 |
1395 //-------------------------------------------------------------------------- | 1397 //-------------------------------------------------------------------------- |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1524 Handle<FixedArray> function_table; // internal code array | 1526 Handle<FixedArray> function_table; // internal code array |
1525 Handle<FixedArray> signature_table; // internal sig array | 1527 Handle<FixedArray> signature_table; // internal sig array |
1526 }; | 1528 }; |
1527 | 1529 |
1528 Isolate* isolate_; | 1530 Isolate* isolate_; |
1529 WasmModule* const module_; | 1531 WasmModule* const module_; |
1530 ErrorThrower* thrower_; | 1532 ErrorThrower* thrower_; |
1531 Handle<WasmModuleObject> module_object_; | 1533 Handle<WasmModuleObject> module_object_; |
1532 Handle<JSReceiver> ffi_; | 1534 Handle<JSReceiver> ffi_; |
1533 Handle<JSArrayBuffer> memory_; | 1535 Handle<JSArrayBuffer> memory_; |
| 1536 Handle<JSObject> export_to_; |
1534 Handle<JSArrayBuffer> globals_; | 1537 Handle<JSArrayBuffer> globals_; |
1535 Handle<WasmCompiledModule> compiled_module_; | 1538 Handle<WasmCompiledModule> compiled_module_; |
1536 std::vector<TableInstance> table_instances_; | 1539 std::vector<TableInstance> table_instances_; |
1537 std::vector<Handle<JSFunction>> js_wrappers_; | 1540 std::vector<Handle<JSFunction>> js_wrappers_; |
1538 | 1541 |
1539 // Helper routines to print out errors with imports. | 1542 // Helper routines to print out errors with imports. |
1540 void ReportLinkError(const char* error, uint32_t index, | 1543 void ReportLinkError(const char* error, uint32_t index, |
1541 Handle<String> module_name, Handle<String> import_name) { | 1544 Handle<String> module_name, Handle<String> import_name) { |
1542 thrower_->LinkError( | 1545 thrower_->LinkError( |
1543 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, | 1546 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1904 for (auto table : module_->function_tables) { | 1907 for (auto table : module_->function_tables) { |
1905 if (table.exported) return true; | 1908 if (table.exported) return true; |
1906 } | 1909 } |
1907 return false; | 1910 return false; |
1908 } | 1911 } |
1909 | 1912 |
1910 // Process the exports, creating wrappers for functions, tables, memories, | 1913 // Process the exports, creating wrappers for functions, tables, memories, |
1911 // and globals. | 1914 // and globals. |
1912 void ProcessExports(Handle<FixedArray> code_table, | 1915 void ProcessExports(Handle<FixedArray> code_table, |
1913 Handle<WasmInstanceObject> instance, | 1916 Handle<WasmInstanceObject> instance, |
| 1917 Handle<JSObject> exports, |
1914 Handle<WasmCompiledModule> compiled_module) { | 1918 Handle<WasmCompiledModule> compiled_module) { |
1915 if (NeedsWrappers()) { | 1919 if (NeedsWrappers()) { |
1916 // Fill the table to cache the exported JSFunction wrappers. | 1920 // Fill the table to cache the exported JSFunction wrappers. |
1917 js_wrappers_.insert(js_wrappers_.begin(), module_->functions.size(), | 1921 js_wrappers_.insert(js_wrappers_.begin(), module_->functions.size(), |
1918 Handle<JSFunction>::null()); | 1922 Handle<JSFunction>::null()); |
1919 } | 1923 } |
1920 | 1924 |
1921 Handle<JSObject> exports_object = instance; | 1925 Handle<JSObject> exports_object = exports; |
1922 if (module_->origin == kWasmOrigin) { | 1926 if (module_->origin == kWasmOrigin) { |
1923 // Create the "exports" object. | 1927 // Create the "exports" object. |
1924 exports_object = isolate_->factory()->NewJSObjectWithNullProto(); | 1928 exports_object = isolate_->factory()->NewJSObjectWithNullProto(); |
1925 Handle<String> exports_name = | 1929 Handle<String> exports_name = |
1926 isolate_->factory()->InternalizeUtf8String("exports"); | 1930 isolate_->factory()->InternalizeUtf8String("exports"); |
1927 JSObject::AddProperty(instance, exports_name, exports_object, NONE); | 1931 JSObject::AddProperty(instance, exports_name, exports_object, NONE); |
1928 } | 1932 } |
1929 | 1933 |
1930 PropertyDescriptor desc; | 1934 PropertyDescriptor desc; |
1931 desc.set_writable(false); | 1935 desc.set_writable(module_->origin == kAsmJsOrigin); |
1932 desc.set_enumerable(true); | 1936 desc.set_enumerable(true); |
1933 | 1937 |
1934 // Count up export indexes. | 1938 // Count up export indexes. |
1935 int export_index = 0; | 1939 int export_index = 0; |
1936 for (auto exp : module_->export_table) { | 1940 for (auto exp : module_->export_table) { |
1937 if (exp.kind == kExternalFunction) { | 1941 if (exp.kind == kExternalFunction) { |
1938 ++export_index; | 1942 ++export_index; |
1939 } | 1943 } |
1940 } | 1944 } |
1941 | 1945 |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2216 } | 2220 } |
2217 } | 2221 } |
2218 } | 2222 } |
2219 }; | 2223 }; |
2220 | 2224 |
2221 // Instantiates a WASM module, creating a WebAssembly.Instance from a | 2225 // Instantiates a WASM module, creating a WebAssembly.Instance from a |
2222 // WebAssembly.Module. | 2226 // WebAssembly.Module. |
2223 MaybeHandle<WasmInstanceObject> WasmModule::Instantiate( | 2227 MaybeHandle<WasmInstanceObject> WasmModule::Instantiate( |
2224 Isolate* isolate, ErrorThrower* thrower, | 2228 Isolate* isolate, ErrorThrower* thrower, |
2225 Handle<WasmModuleObject> wasm_module, Handle<JSReceiver> ffi, | 2229 Handle<WasmModuleObject> wasm_module, Handle<JSReceiver> ffi, |
2226 Handle<JSArrayBuffer> memory) { | 2230 Handle<JSArrayBuffer> memory, Handle<JSObject> export_to) { |
2227 WasmInstanceBuilder builder(isolate, thrower, wasm_module, ffi, memory); | 2231 WasmInstanceBuilder builder(isolate, thrower, wasm_module, ffi, memory, |
| 2232 export_to); |
2228 return builder.Build(); | 2233 return builder.Build(); |
2229 } | 2234 } |
2230 | 2235 |
2231 bool wasm::IsWasmInstance(Object* object) { | 2236 bool wasm::IsWasmInstance(Object* object) { |
2232 return WasmInstanceObject::IsWasmInstanceObject(object); | 2237 return WasmInstanceObject::IsWasmInstanceObject(object); |
2233 } | 2238 } |
2234 | 2239 |
2235 Handle<Script> wasm::GetScript(Handle<JSObject> instance) { | 2240 Handle<Script> wasm::GetScript(Handle<JSObject> instance) { |
2236 WasmCompiledModule* compiled_module = | 2241 WasmCompiledModule* compiled_module = |
2237 WasmInstanceObject::cast(*instance)->compiled_module(); | 2242 WasmInstanceObject::cast(*instance)->compiled_module(); |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2793 Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections); | 2798 Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections); |
2794 JSArray::SetContent(array_object, storage); | 2799 JSArray::SetContent(array_object, storage); |
2795 array_object->set_length(Smi::FromInt(num_custom_sections)); | 2800 array_object->set_length(Smi::FromInt(num_custom_sections)); |
2796 | 2801 |
2797 for (int i = 0; i < num_custom_sections; i++) { | 2802 for (int i = 0; i < num_custom_sections; i++) { |
2798 storage->set(i, *matching_sections[i]); | 2803 storage->set(i, *matching_sections[i]); |
2799 } | 2804 } |
2800 | 2805 |
2801 return array_object; | 2806 return array_object; |
2802 } | 2807 } |
OLD | NEW |