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

Unified Diff: src/wasm/wasm-module.cc

Issue 2392943006: [wasm] Implement importing of WebAssembly.Memory. (Closed)
Patch Set: Created 4 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: src/wasm/wasm-module.cc
diff --git a/src/wasm/wasm-module.cc b/src/wasm/wasm-module.cc
index 9ebe84a4f294c4e8edc47cf3ee790bcb123fbb87..f1658106262967740d93536c6646f9ada8c8c238 100644
--- a/src/wasm/wasm-module.cc
+++ b/src/wasm/wasm-module.cc
@@ -53,6 +53,7 @@ enum WasmInstanceObjectFields {
kWasmCompiledModule = 0,
kWasmModuleFunctionTable,
kWasmModuleCodeTable,
+ kWasmMemObject,
Mircea Trofin 2016/10/06 18:10:44 Could this move on the compiled module object inst
kWasmMemArrayBuffer,
kWasmGlobalsArrayBuffer,
// TODO(clemensh): Remove function name array, extract names from module
@@ -1236,6 +1237,36 @@ class WasmInstanceBuilder {
JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED);
instance->SetInternalField(kWasmModuleCodeTable, *code_table);
+ instance->SetInternalField(kWasmMemObject, *factory->undefined_value());
+
+ //--------------------------------------------------------------------------
+ // Set up the globals for the new instance.
+ //--------------------------------------------------------------------------
+ MaybeHandle<JSArrayBuffer> old_globals;
+ MaybeHandle<JSArrayBuffer> globals;
+ uint32_t globals_size = compiled_module_->globals_size();
+ if (globals_size > 0) {
+ Handle<JSArrayBuffer> global_buffer =
+ NewArrayBuffer(isolate_, globals_size);
+ globals = global_buffer;
+ if (globals.is_null()) {
+ thrower_->Error("Out of memory: wasm globals");
+ return nothing;
+ }
+ Address old_address =
+ owner.is_null() ? nullptr : GetGlobalStartAddressFromCodeTemplate(
+ *factory->undefined_value(),
+ JSObject::cast(*owner));
+ RelocateGlobals(instance, old_address,
+ static_cast<Address>(global_buffer->backing_store()));
+ instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer);
+ }
+
+ //--------------------------------------------------------------------------
+ // Process the imports for the module.
+ //--------------------------------------------------------------------------
+ int num_imported_functions = ProcessImports(globals, code_table, instance);
+ if (num_imported_functions < 0) return nothing;
//--------------------------------------------------------------------------
// Set up the memory for the new instance.
@@ -1272,35 +1303,6 @@ class WasmInstanceBuilder {
}
//--------------------------------------------------------------------------
- // Set up the globals for the new instance.
- //--------------------------------------------------------------------------
- MaybeHandle<JSArrayBuffer> old_globals;
- MaybeHandle<JSArrayBuffer> globals;
- uint32_t globals_size = compiled_module_->globals_size();
- if (globals_size > 0) {
- Handle<JSArrayBuffer> global_buffer =
- NewArrayBuffer(isolate_, globals_size);
- globals = global_buffer;
- if (globals.is_null()) {
- thrower_->Error("Out of memory: wasm globals");
- return nothing;
- }
- Address old_address =
- owner.is_null() ? nullptr : GetGlobalStartAddressFromCodeTemplate(
- *factory->undefined_value(),
- JSObject::cast(*owner));
- RelocateGlobals(instance, old_address,
- static_cast<Address>(global_buffer->backing_store()));
- instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer);
- }
-
- //--------------------------------------------------------------------------
- // Process the imports for the module.
- //--------------------------------------------------------------------------
- int num_imported_functions = ProcessImports(globals, code_table);
- if (num_imported_functions < 0) return nothing;
-
- //--------------------------------------------------------------------------
// Process the initialization for the module's globals.
//--------------------------------------------------------------------------
ProcessInits(globals);
@@ -1638,7 +1640,7 @@ class WasmInstanceBuilder {
// order, loading them from the {ffi_} object. Returns the number of imported
// functions.
int ProcessImports(MaybeHandle<JSArrayBuffer> globals,
- Handle<FixedArray> code_table) {
+ Handle<FixedArray> code_table, Handle<JSObject> instance) {
int num_imported_functions = 0;
if (!compiled_module_->has_imports()) return num_imported_functions;
@@ -1680,9 +1682,17 @@ class WasmInstanceBuilder {
case kExternalTable:
// TODO(titzer): Table imports must be a WebAssembly.Table.
break;
- case kExternalMemory:
- // TODO(titzer): Memory imports must be a WebAssembly.Memory.
+ case kExternalMemory: {
+ Handle<Object> object = result.ToHandleChecked();
+ if (!WasmJs::IsWasmMemoryObject(isolate_, object)) {
+ ReportFFIError("memory import must be a WebAssembly.Memory object",
+ index, module_name, function_name);
+ return -1;
+ }
+ instance->SetInternalField(kWasmMemObject, *object);
+ memory_ = WasmJs::GetWasmMemoryArrayBuffer(isolate_, object);
break;
+ }
case kExternalGlobal: {
// Global imports are converted to numbers and written into the
// {globals} array buffer.
@@ -1799,13 +1809,21 @@ class WasmInstanceBuilder {
// TODO(titzer): should it have the same identity as an import?
break;
case kExternalMemory: {
- // TODO(titzer): should memory have the same identity as an
- // import?
- Handle<JSArrayBuffer> buffer =
- Handle<JSArrayBuffer>(JSArrayBuffer::cast(
- instance->GetInternalField(kWasmMemArrayBuffer)));
- desc.set_value(
- WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0));
+ // Export the memory as a WebAssembly.Memory object.
+ Handle<Object> memory_object(
+ instance->GetInternalField(kWasmMemObject), isolate_);
+ if (memory_object->IsUndefined(isolate_)) {
+ // If there was no imported WebAssembly.Memory object, create one.
+ Handle<JSArrayBuffer> buffer(
+ JSArrayBuffer::cast(
+ instance->GetInternalField(kWasmMemArrayBuffer)),
+ isolate_);
+ memory_object =
+ WasmJs::CreateWasmMemoryObject(isolate_, buffer, false, 0);
+ instance->SetInternalField(kWasmMemObject, *memory_object);
+ }
+
+ desc.set_value(memory_object);
break;
}
case kExternalGlobal: {

Powered by Google App Engine
This is Rietveld 408576698