Commit bfeb78a7 authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Introduce second {WasmModuleObject::New} method.

This introduces a second factory method for allocating new module
objects that are based on existing native modules which are potentially
shared between multiple Isolates.

R=clemensh@chromium.org
TEST=cctest/test-wasm-shared-engine
BUG=v8:7424

Change-Id: I8c74d821542d443a8ad2352cb77b84b445e21acb
Reviewed-on: https://chromium-review.googlesource.com/1126931
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54261}
parent 9d8d074d
......@@ -174,17 +174,40 @@ enum DispatchTableElements : int {
} // namespace
// static
Handle<WasmModuleObject> WasmModuleObject::New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<const wasm::WasmModule> shared_module, wasm::ModuleEnv& env,
OwnedVector<const uint8_t> wire_bytes, Handle<Script> script,
Handle<ByteArray> asm_js_offset_table) {
DCHECK_EQ(shared_module.get(), env.module);
// Now create the {WasmModuleObject}.
Handle<JSFunction> module_cons(
isolate->native_context()->wasm_module_constructor(), isolate);
auto module_object = Handle<WasmModuleObject>::cast(
isolate->factory()->NewJSObject(module_cons));
// Create a new {NativeModule} first.
size_t native_memory_estimate =
isolate->wasm_engine()->code_manager()->EstimateNativeModuleSize(
env.module);
auto native_module = isolate->wasm_engine()->code_manager()->NewNativeModule(
isolate, native_memory_estimate,
wasm::NativeModule::kCanAllocateMoreMemory, std::move(shared_module),
env);
native_module->set_wire_bytes(std::move(wire_bytes));
native_module->SetRuntimeStubs(isolate);
// Delegate to the shared {WasmModuleObject::New} allocator.
Handle<WasmModuleObject> module_object =
New(isolate, export_wrappers, std::move(native_module), script);
if (!asm_js_offset_table.is_null()) {
module_object->set_asm_js_offset_table(*asm_js_offset_table);
}
return module_object;
}
// static
Handle<WasmModuleObject> WasmModuleObject::New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<wasm::NativeModule> native_module, Handle<Script> script) {
Handle<WasmModuleObject> module_object = Handle<WasmModuleObject>::cast(
isolate->factory()->NewJSObject(isolate->wasm_module_constructor()));
module_object->set_export_wrappers(*export_wrappers);
if (script->type() == Script::TYPE_WASM) {
script->set_wasm_module_object(*module_object);
......@@ -192,27 +215,18 @@ Handle<WasmModuleObject> WasmModuleObject::New(
module_object->set_script(*script);
module_object->set_weak_instance_list(
ReadOnlyRoots(isolate).empty_weak_array_list());
if (!asm_js_offset_table.is_null()) {
module_object->set_asm_js_offset_table(*asm_js_offset_table);
}
// Create the {NativeModule}, and let the {WasmModuleObject} reference it.
// Use the given shared {NativeModule}, but increase its reference count by
// allocating a new {Managed<T>} that the {WasmModuleObject} references.
size_t native_memory_estimate =
isolate->wasm_engine()->code_manager()->EstimateNativeModuleSize(
env.module);
native_module->module());
size_t memory_estimate =
EstimateWasmModuleSize(env.module) + native_memory_estimate;
auto native_module = isolate->wasm_engine()->code_manager()->NewNativeModule(
isolate, native_memory_estimate,
wasm::NativeModule::kCanAllocateMoreMemory, std::move(shared_module),
env);
native_module->set_wire_bytes(std::move(wire_bytes));
native_module->SetRuntimeStubs(isolate);
EstimateWasmModuleSize(native_module->module()) + native_memory_estimate;
Handle<Managed<wasm::NativeModule>> managed_native_module =
Managed<wasm::NativeModule>::FromUniquePtr(isolate, memory_estimate,
Managed<wasm::NativeModule>::FromSharedPtr(isolate, memory_estimate,
std::move(native_module));
module_object->set_managed_native_module(*managed_native_module);
return module_object;
}
......
......@@ -132,12 +132,19 @@ class WasmModuleObject : public JSObject {
WASM_MODULE_OBJECT_FIELDS)
#undef WASM_MODULE_OBJECT_FIELDS
// Creates a new {WasmModuleObject} with a new {NativeModule} underneath.
static Handle<WasmModuleObject> New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<const wasm::WasmModule> module, wasm::ModuleEnv& env,
OwnedVector<const uint8_t> wire_bytes, Handle<Script> script,
Handle<ByteArray> asm_js_offset_table);
// Creates a new {WasmModuleObject} for an existing {NativeModule} that is
// reference counted and might be shared between multiple Isolates.
static Handle<WasmModuleObject> New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<wasm::NativeModule> native_module, Handle<Script> script);
// Set a breakpoint on the given byte position inside the given module.
// This will affect all live and future instances of the module.
// The passed position might be modified to point to the next breakable
......
......@@ -83,32 +83,6 @@ class SharedEngineIsolate {
return instance.ToHandleChecked();
}
// TODO(mstarzinger): Turn this into a {WasmModuleObject::New} method.
Handle<WasmModuleObject> NewWasmModuleObject(
Handle<FixedArray> export_wrappers, Handle<Script> script,
SharedModule shared_module) {
Handle<JSFunction> module_cons(
isolate()->native_context()->wasm_module_constructor(), isolate());
auto module_object = Handle<WasmModuleObject>::cast(
isolate()->factory()->NewJSObject(module_cons));
module_object->set_export_wrappers(*export_wrappers);
script->set_wasm_module_object(*module_object);
module_object->set_script(*script);
module_object->set_weak_instance_list(
ReadOnlyRoots(isolate()).empty_weak_array_list());
size_t native_memory_estimate =
isolate()->wasm_engine()->code_manager()->EstimateNativeModuleSize(
shared_module->module());
size_t memory_estimate = EstimateWasmModuleSize(shared_module->module()) +
native_memory_estimate;
Handle<Managed<wasm::NativeModule>> managed_native_module =
Managed<wasm::NativeModule>::FromSharedPtr(isolate(), memory_estimate,
shared_module);
module_object->set_managed_native_module(*managed_native_module);
return module_object;
}
// TODO(mstarzinger): Switch over to a public API for sharing modules via the
// {v8::WasmCompiledModule::TransferrableModule} class once it is ready.
Handle<WasmInstanceObject> ImportInstance(SharedModule shared_module) {
......@@ -118,8 +92,8 @@ class SharedEngineIsolate {
static_cast<int>(shared_module->module()->num_exported_functions);
Handle<FixedArray> export_wrappers =
isolate()->factory()->NewFixedArray(export_wrappers_size, TENURED);
Handle<WasmModuleObject> module_object =
NewWasmModuleObject(export_wrappers, script, shared_module);
Handle<WasmModuleObject> module_object = WasmModuleObject::New(
isolate(), export_wrappers, shared_module, script);
// TODO(6792): Wrappers below might be cloned using {Factory::CopyCode}.
// This requires unlocking the code space here. This should eventually be
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment