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

Side by Side Diff: src/wasm/wasm-module.cc

Issue 2629523007: [wasm] JS-API: enable WebAssembly.instantiate tests; fix LinkError (Closed)
Patch Set: Fixes Created 3 years, 11 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
« no previous file with comments | « src/wasm/wasm-module.h ('k') | test/cctest/wasm/test-run-wasm-module.cc » ('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 <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 1103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1114 UnwrapImportWrapper(function)); 1114 UnwrapImportWrapper(function));
1115 } 1115 }
1116 } 1116 }
1117 1117
1118 // A helper class to simplify instantiating a module from a compiled module. 1118 // A helper class to simplify instantiating a module from a compiled module.
1119 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule}, 1119 // It closes over the {Isolate}, the {ErrorThrower}, the {WasmCompiledModule},
1120 // etc. 1120 // etc.
1121 class WasmInstanceBuilder { 1121 class WasmInstanceBuilder {
1122 public: 1122 public:
1123 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower, 1123 WasmInstanceBuilder(Isolate* isolate, ErrorThrower* thrower,
1124 Handle<JSObject> module_object, Handle<JSReceiver> ffi, 1124 Handle<WasmModuleObject> module_object,
1125 Handle<JSArrayBuffer> memory) 1125 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory)
1126 : isolate_(isolate), 1126 : isolate_(isolate),
1127 thrower_(thrower), 1127 thrower_(thrower),
1128 module_object_(module_object), 1128 module_object_(module_object),
1129 ffi_(ffi), 1129 ffi_(ffi),
1130 memory_(memory) {} 1130 memory_(memory) {}
1131 1131
1132 // Build an instance, in all of its glory. 1132 // Build an instance, in all of its glory.
1133 MaybeHandle<WasmInstanceObject> Build() { 1133 MaybeHandle<WasmInstanceObject> Build() {
1134 MaybeHandle<WasmInstanceObject> nothing; 1134 MaybeHandle<WasmInstanceObject> nothing;
1135 HistogramTimerScope wasm_instantiate_module_time_scope( 1135 HistogramTimerScope wasm_instantiate_module_time_scope(
(...skipping 11 matching lines...) Expand all
1147 { 1147 {
1148 // Root the owner, if any, before doing any allocations, which 1148 // Root the owner, if any, before doing any allocations, which
1149 // may trigger GC. 1149 // may trigger GC.
1150 // Both owner and original template need to be in sync. Even 1150 // Both owner and original template need to be in sync. Even
1151 // after we lose the original template handle, the code 1151 // after we lose the original template handle, the code
1152 // objects we copied from it have data relative to the 1152 // objects we copied from it have data relative to the
1153 // instance - such as globals addresses. 1153 // instance - such as globals addresses.
1154 Handle<WasmCompiledModule> original; 1154 Handle<WasmCompiledModule> original;
1155 { 1155 {
1156 DisallowHeapAllocation no_gc; 1156 DisallowHeapAllocation no_gc;
1157 original = handle( 1157 original = handle(module_object_->compiled_module());
1158 WasmCompiledModule::cast(module_object_->GetInternalField(0)));
1159 if (original->has_weak_owning_instance()) { 1158 if (original->has_weak_owning_instance()) {
1160 owner = handle(WasmInstanceObject::cast( 1159 owner = handle(WasmInstanceObject::cast(
1161 original->weak_owning_instance()->value())); 1160 original->weak_owning_instance()->value()));
1162 } 1161 }
1163 } 1162 }
1164 DCHECK(!original.is_null()); 1163 DCHECK(!original.is_null());
1165 // Always make a new copy of the code_table, since the old_code_table 1164 // Always make a new copy of the code_table, since the old_code_table
1166 // may still have placeholders for imports. 1165 // may still have placeholders for imports.
1167 old_code_table = original->code_table(); 1166 old_code_table = original->code_table();
1168 code_table = factory->CopyFixedArray(old_code_table); 1167 code_table = factory->CopyFixedArray(old_code_table);
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 // It's unfortunate that the new instance is already linked in the 1434 // It's unfortunate that the new instance is already linked in the
1436 // chain. However, we need to set up everything before executing the 1435 // chain. However, we need to set up everything before executing the
1437 // start function, such that stack trace information can be generated 1436 // start function, such that stack trace information can be generated
1438 // correctly already in the start function. 1437 // correctly already in the start function.
1439 return nothing; 1438 return nothing;
1440 } 1439 }
1441 } 1440 }
1442 1441
1443 DCHECK(!isolate_->has_pending_exception()); 1442 DCHECK(!isolate_->has_pending_exception());
1444 TRACE("Finishing instance %d\n", compiled_module_->instance_id()); 1443 TRACE("Finishing instance %d\n", compiled_module_->instance_id());
1445 TRACE_CHAIN(WasmCompiledModule::cast(module_object_->GetInternalField(0))); 1444 TRACE_CHAIN(module_object_->compiled_module());
1446 return instance; 1445 return instance;
1447 } 1446 }
1448 1447
1449 private: 1448 private:
1450 // Represents the initialized state of a table. 1449 // Represents the initialized state of a table.
1451 struct TableInstance { 1450 struct TableInstance {
1452 Handle<WasmTableObject> table_object; // WebAssembly.Table instance 1451 Handle<WasmTableObject> table_object; // WebAssembly.Table instance
1453 Handle<FixedArray> js_wrappers; // JSFunctions exported 1452 Handle<FixedArray> js_wrappers; // JSFunctions exported
1454 Handle<FixedArray> function_table; // internal code array 1453 Handle<FixedArray> function_table; // internal code array
1455 Handle<FixedArray> signature_table; // internal sig array 1454 Handle<FixedArray> signature_table; // internal sig array
1456 }; 1455 };
1457 1456
1458 Isolate* isolate_; 1457 Isolate* isolate_;
1459 WasmModule* module_; 1458 WasmModule* module_;
1460 ErrorThrower* thrower_; 1459 ErrorThrower* thrower_;
1461 Handle<JSObject> module_object_; 1460 Handle<WasmModuleObject> module_object_;
1462 Handle<JSReceiver> ffi_; 1461 Handle<JSReceiver> ffi_;
1463 Handle<JSArrayBuffer> memory_; 1462 Handle<JSArrayBuffer> memory_;
1464 Handle<JSArrayBuffer> globals_; 1463 Handle<JSArrayBuffer> globals_;
1465 Handle<WasmCompiledModule> compiled_module_; 1464 Handle<WasmCompiledModule> compiled_module_;
1466 std::vector<TableInstance> table_instances_; 1465 std::vector<TableInstance> table_instances_;
1467 std::vector<Handle<JSFunction>> js_wrappers_; 1466 std::vector<Handle<JSFunction>> js_wrappers_;
1468 1467
1469 // Helper routines to print out errors with imports. 1468 // Helper routines to print out errors with imports.
1470 void ReportLinkError(const char* error, uint32_t index, 1469 void ReportLinkError(const char* error, uint32_t index,
1471 Handle<String> module_name, Handle<String> import_name) { 1470 Handle<String> module_name, Handle<String> import_name) {
1472 thrower_->LinkError( 1471 thrower_->LinkError(
1473 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index, 1472 "Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", index,
1474 module_name->length(), module_name->ToCString().get(), 1473 module_name->length(), module_name->ToCString().get(),
1475 import_name->length(), import_name->ToCString().get(), error); 1474 import_name->length(), import_name->ToCString().get(), error);
1476 } 1475 }
1477 1476
1478 MaybeHandle<Object> ReportTypeError(const char* error, uint32_t index, 1477 MaybeHandle<Object> ReportLinkError(const char* error, uint32_t index,
1479 Handle<String> module_name) { 1478 Handle<String> module_name) {
1480 thrower_->TypeError("Import #%d module=\"%.*s\" error: %s", index, 1479 thrower_->LinkError("Import #%d module=\"%.*s\" error: %s", index,
1481 module_name->length(), module_name->ToCString().get(), 1480 module_name->length(), module_name->ToCString().get(),
1482 error); 1481 error);
1483 return MaybeHandle<Object>(); 1482 return MaybeHandle<Object>();
1484 } 1483 }
1485 1484
1486 // Look up an import value in the {ffi_} object. 1485 // Look up an import value in the {ffi_} object.
1487 MaybeHandle<Object> LookupImport(uint32_t index, Handle<String> module_name, 1486 MaybeHandle<Object> LookupImport(uint32_t index, Handle<String> module_name,
1488 Handle<String> import_name) { 1487 Handle<String> import_name) {
1489 if (ffi_.is_null()) { 1488 // We pre-validated in the js-api layer that the ffi object is present, and
1490 return ReportTypeError("FFI is not an object", index, module_name); 1489 // a JSObject, if the module has imports.
rossberg 2017/01/13 08:59:12 Actually, I just realised that it is incorrect tha
1491 } 1490 DCHECK(!ffi_.is_null());
1492 1491
1493 // Look up the module first. 1492 // Look up the module first.
1494 MaybeHandle<Object> result = 1493 MaybeHandle<Object> result =
1495 Object::GetPropertyOrElement(ffi_, module_name); 1494 Object::GetPropertyOrElement(ffi_, module_name);
1496 if (result.is_null()) { 1495 if (result.is_null()) {
1497 return ReportTypeError("module not found", index, module_name); 1496 return ReportLinkError("module not found", index, module_name);
1498 } 1497 }
1499 1498
1500 Handle<Object> module = result.ToHandleChecked(); 1499 Handle<Object> module = result.ToHandleChecked();
1501 1500
1502 // Look up the value in the module. 1501 // Look up the value in the module.
1503 if (!module->IsJSReceiver()) { 1502 if (!module->IsJSReceiver()) {
1504 return ReportTypeError("module is not an object or function", index, 1503 return ReportLinkError("module is not an object or function", index,
1505 module_name); 1504 module_name);
1506 } 1505 }
1507 1506
1508 result = Object::GetPropertyOrElement(module, import_name); 1507 result = Object::GetPropertyOrElement(module, import_name);
1509 if (result.is_null()) { 1508 if (result.is_null()) {
1510 ReportLinkError("import not found", index, module_name, import_name); 1509 ReportLinkError("import not found", index, module_name, import_name);
1511 return MaybeHandle<JSFunction>(); 1510 return MaybeHandle<JSFunction>();
1512 } 1511 }
1513 1512
1514 return result; 1513 return result;
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
2075 } 2074 }
2076 } 2075 }
2077 compiled_module_->set_function_tables(new_function_tables); 2076 compiled_module_->set_function_tables(new_function_tables);
2078 compiled_module_->set_signature_tables(new_signature_tables); 2077 compiled_module_->set_signature_tables(new_signature_tables);
2079 } 2078 }
2080 }; 2079 };
2081 2080
2082 // Instantiates a WASM module, creating a WebAssembly.Instance from a 2081 // Instantiates a WASM module, creating a WebAssembly.Instance from a
2083 // WebAssembly.Module. 2082 // WebAssembly.Module.
2084 MaybeHandle<WasmInstanceObject> WasmModule::Instantiate( 2083 MaybeHandle<WasmInstanceObject> WasmModule::Instantiate(
2085 Isolate* isolate, ErrorThrower* thrower, Handle<JSObject> wasm_module, 2084 Isolate* isolate, ErrorThrower* thrower,
2086 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory) { 2085 Handle<WasmModuleObject> wasm_module, Handle<JSReceiver> ffi,
2086 Handle<JSArrayBuffer> memory) {
2087 WasmInstanceBuilder builder(isolate, thrower, wasm_module, ffi, memory); 2087 WasmInstanceBuilder builder(isolate, thrower, wasm_module, ffi, memory);
2088 return builder.Build(); 2088 return builder.Build();
2089 } 2089 }
2090 2090
2091 bool wasm::IsWasmInstance(Object* object) { 2091 bool wasm::IsWasmInstance(Object* object) {
2092 return WasmInstanceObject::IsWasmInstanceObject(object); 2092 return WasmInstanceObject::IsWasmInstanceObject(object);
2093 } 2093 }
2094 2094
2095 Handle<Script> wasm::GetScript(Handle<JSObject> instance) { 2095 Handle<Script> wasm::GetScript(Handle<JSObject> instance) {
2096 WasmCompiledModule* compiled_module = 2096 WasmCompiledModule* compiled_module =
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
2487 2487
2488 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(), 2488 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(),
2489 NONE); 2489 NONE);
2490 JSObject::AddProperty(entry, kind_string, export_kind, NONE); 2490 JSObject::AddProperty(entry, kind_string, export_kind, NONE);
2491 2491
2492 storage->set(index, *entry); 2492 storage->set(index, *entry);
2493 } 2493 }
2494 2494
2495 return array_object; 2495 return array_object;
2496 } 2496 }
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | test/cctest/wasm/test-run-wasm-module.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698