Hi everyone,
I am JIT compiling programs in a high level language to a WebAssembly
module. I want to run them in embedded V8. I succeeded in embedding V8,
creating an isolate, and with some JS glue code instantiate the WebAssembly
module and invoke an exported function. Now I am facing the problem that
the WebAssembly code must access memory of the host. E.g. after the host
allocates an integer array, I want to map the underlying memory of the
array into the linear memory of the WebAssembly module s.t. the module can
directly access and modify it. The broader idea is that the host manages
data structures and does the memory management and the WASM modules can use
these data structures.
Here is an excerpt of what I have done so far:
/* Create WASM module. */
auto v8_wasm_module = v8::WasmModuleObject::DeserializeOrCompile(
/* isolate= */ isolate_,
/* serialized_module= */ { nullptr, 0 },
/* wire_bytes= */ v8::MemorySpan<const
uint8_t>(binary_addr, binary_size)
).ToLocalChecked();
/* Get the WebAssembly instance class prototype. */
auto web_assembly_class = context->Global()->Get(context,
V8STR("WebAssembly")).ToLocalChecked().As<v8::Object>();
/* Create a WebAssembly memory object. */
auto wasm_memory_class = web_assembly_class->Get(context,
V8STR("Memory")).ToLocalChecked().As<v8::Object>();
auto memory_params_object = v8::Object::New(isolate_);
memory_params_object->Set(context, V8STR("initial"),
v8::Int32::New(isolate_, 1));
memory_params_object->Set(context, V8STR("maximum"),
v8::Int32::New(isolate_, 256));
v8::Local<v8::Value> memory_args[] = { memory_params_object };
auto wasm_memory = wasm_memory_class->CallAsConstructor(context, 1,
memory_args).ToLocalChecked().As<v8::Object>();
/* Allocate and initialize host memory. */
auto host_memory = new int32_t[1024];
for (int32_t i = 0; i != 1024; ++i)
host_memory[i] = i;
auto store = v8::ArrayBuffer::NewBackingStore(host_memory, 1024 *
sizeof(int32_t), [](void* data, size_t, void*) { delete[]
reinterpret_cast<int32_t*>(data); }, nullptr);
auto buffer = v8::ArrayBuffer::New(isolate_, std::move(store));
/* Replace the WebAssembly.Memory's underlying ArrayBuffer. FIXME: Not
working as intended! */
wasm_memory->Set(context, V8STR("buffer"), buffer);
/* Create the import object for instantiating the WebAssembly module. */
auto host_object = v8::Object::New(isolate_);
host_object->Set(context, V8STR("mem"), wasm_memory);
auto import_object = v8::Object::New(isolate_);
import_object->Set(context, V8STR("env"), host_object);
/* Create the import object for instantiating the WebAssembly module. */
auto host_object = v8::Object::New(isolate_);
host_object->Set(context, V8STR("mem"), wasm_memory);
auto import_object = v8::Object::New(isolate_);
import_object->Set(context, V8STR("env"), host_object);
/* Get the exports of the created WebAssembly instance. */
auto exports = instance->Get(context,
V8STR("exports")).ToLocalChecked().As<v8::Object>();
/* Get exported function `run` from the exports. */
auto run = exports->Get(context,
V8STR("run")).ToLocalChecked().As<v8::Function>();
/* Invoke the exported function `run` of the module. */
v8::Local<v8::Value> args[] = { };
auto result = run->Call(context, context->Global(), 0,
args).ToLocalChecked().As<v8::Object>();
The FIXME shows what I intend to do: Create a WebAssembly.Memory instance
and then replace its underlying ArrayBuffer by an instance that wraps
memory in the host (using NewBackingStore()). This WebAssembly.Memory
object is then imported into the module when it is instantiated.
Is there any way of mapping memory of the host into a WebAssembly lienar
memory? Copying data is not an option.
Thanks in advance.
Kind regards,
Immanuel
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/v8-dev/f2ddf715-f8a6-47a8-b872-f77312b0e4c5%40googlegroups.com.