Commit ce7ad569 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Move UpdateDispatchTables to wasm-objects.h

Make this a member method of WasmTableObject, because it is pretty
coupled with that object anyways, and is always being called together
with WasmTableObject::Set or WasmTableObject::AddDispatchTable.
It also simplifies another refactoring: https://crrev.com/c/866733

R=titzer@chromium.org

Change-Id: I53392fb9cf21f2e45c2a144d180e9b3614657094
Reviewed-on: https://chromium-review.googlesource.com/866933Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50605}
parent e447ee22
......@@ -3513,13 +3513,6 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table,
for (int index = 0; index < function_table_count; ++index) {
TableInstance& table_instance = table_instances_[index];
Handle<FixedArray> all_dispatch_tables;
if (!table_instance.table_object.is_null()) {
// Get the existing dispatch table(s) with the WebAssembly.Table object.
all_dispatch_tables =
handle(table_instance.table_object->dispatch_tables());
}
// Count the number of table exports for each function (needed for lazy
// compilation).
std::unordered_map<uint32_t, uint32_t> num_table_exports;
......@@ -3572,7 +3565,7 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table,
table_instance.function_table->set(table_index, *wasm_code.GetCode());
value_to_update_with = wasm_code.GetCode();
}
if (!all_dispatch_tables.is_null()) {
if (!table_instance.table_object.is_null()) {
if (js_wrappers_[func_index].is_null()) {
// No JSFunction entry yet exists for this function. Create one.
// TODO(titzer): We compile JS->wasm wrappers for functions are
......@@ -3623,8 +3616,9 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table,
WasmCode::kWasmToWasmWrapper);
}
}
UpdateDispatchTables(isolate_, all_dispatch_tables, table_index,
function, value_to_update_with);
WasmTableObject::UpdateDispatchTables(
isolate_, table_instance.table_object, table_index, function->sig,
value_to_update_with);
}
}
}
......@@ -3642,7 +3636,7 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table,
// initialized.
if (!table_instance.table_object.is_null()) {
// Add the new dispatch table to the WebAssembly.Table object.
all_dispatch_tables = WasmTableObject::AddDispatchTable(
WasmTableObject::AddDispatchTable(
isolate_, table_instance.table_object, instance, index,
table_instance.function_table, table_instance.signature_table);
}
......
......@@ -188,30 +188,6 @@ Handle<Object> GetOrCreateIndirectCallWrapper(
return code;
}
void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables,
int index, const WasmFunction* function,
Handle<Object> code_or_foreign) {
DCHECK_EQ(0, dispatch_tables->length() % 4);
for (int i = 0; i < dispatch_tables->length(); i += 4) {
Handle<FixedArray> function_table(
FixedArray::cast(dispatch_tables->get(i + 2)), isolate);
Handle<FixedArray> signature_table(
FixedArray::cast(dispatch_tables->get(i + 3)), isolate);
if (function) {
Handle<WasmInstanceObject> instance(
WasmInstanceObject::cast(dispatch_tables->get(i)), isolate);
// Note that {SignatureMap::Find} may return {-1} if the signature is
// not found; it will simply never match any check.
auto sig_index = instance->module()->signature_map.Find(function->sig);
signature_table->set(index, Smi::FromInt(sig_index));
function_table->set(index, *code_or_foreign);
} else {
signature_table->set(index, Smi::FromInt(-1));
function_table->set(index, Smi::kZero);
}
}
}
bool IsWasmCodegenAllowed(Isolate* isolate, Handle<Context> context) {
// TODO(wasm): Once wasm has its own CSP policy, we should introduce a
// separate callback that includes information about the module about to be
......
......@@ -273,10 +273,6 @@ Handle<FixedArray> DecodeLocalNames(Isolate*, Handle<WasmSharedModuleData>);
// TODO(titzer): move this to WasmExportedFunction.
WasmFunction* GetWasmFunctionForExport(Isolate* isolate, Handle<Object> target);
void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables,
int index, const WasmFunction* function,
Handle<Object> code_or_foreign);
Handle<Object> GetOrCreateIndirectCallWrapper(
Isolate* isolate, Handle<WasmInstanceObject> owning_instance,
WasmCodeWrapper wasm_code, uint32_t index, FunctionSig* sig);
......
......@@ -212,14 +212,16 @@ Handle<WasmTableObject> WasmTableObject::New(Isolate* isolate, uint32_t initial,
return Handle<WasmTableObject>::cast(table_obj);
}
Handle<FixedArray> WasmTableObject::AddDispatchTable(
Isolate* isolate, Handle<WasmTableObject> table_obj,
Handle<WasmInstanceObject> instance, int table_index,
Handle<FixedArray> function_table, Handle<FixedArray> signature_table) {
void WasmTableObject::AddDispatchTable(Isolate* isolate,
Handle<WasmTableObject> table_obj,
Handle<WasmInstanceObject> instance,
int table_index,
Handle<FixedArray> function_table,
Handle<FixedArray> signature_table) {
Handle<FixedArray> dispatch_tables(table_obj->dispatch_tables());
DCHECK_EQ(0, dispatch_tables->length() % 4);
if (instance.is_null()) return dispatch_tables;
if (instance.is_null()) return;
// TODO(titzer): use weak cells here to avoid leaking instances.
// Grow the dispatch table and add a new entry at the end.
......@@ -233,8 +235,6 @@ Handle<FixedArray> WasmTableObject::AddDispatchTable(
new_dispatch_tables->set(dispatch_tables->length() + 3, *signature_table);
table_obj->set_dispatch_tables(*new_dispatch_tables);
return new_dispatch_tables;
}
void WasmTableObject::Grow(Isolate* isolate, uint32_t count) {
......@@ -322,16 +322,17 @@ void WasmTableObject::Set(Isolate* isolate, Handle<WasmTableObject> table,
Handle<FixedArray> dispatch_tables(table->dispatch_tables(), isolate);
WasmFunction* wasm_function = nullptr;
wasm::FunctionSig* sig = nullptr;
Handle<Object> code = Handle<Object>::null();
Handle<Object> value = isolate->factory()->null_value();
if (!function.is_null()) {
auto exported_function = Handle<WasmExportedFunction>::cast(function);
wasm_function = wasm::GetWasmFunctionForExport(isolate, function);
auto* wasm_function = wasm::GetWasmFunctionForExport(isolate, function);
// The verification that {function} is an export was done
// by the caller.
DCHECK_NOT_NULL(wasm_function);
DCHECK(wasm_function != nullptr && wasm_function->sig != nullptr);
sig = wasm_function->sig;
value = function;
// TODO(titzer): Make JSToWasm wrappers just call the WASM to WASM wrapper,
// and then we can just reuse the WASM to WASM wrapper.
......@@ -343,12 +344,40 @@ void WasmTableObject::Set(Isolate* isolate, Handle<WasmTableObject> table,
native_module);
code = wasm::GetOrCreateIndirectCallWrapper(
isolate, handle(exported_function->instance()), wasm_code,
exported_function->function_index(), wasm_function->sig);
exported_function->function_index(), sig);
}
UpdateDispatchTables(isolate, dispatch_tables, index, wasm_function, code);
UpdateDispatchTables(isolate, table, index, sig, code);
array->set(index, *value);
}
void WasmTableObject::UpdateDispatchTables(Isolate* isolate,
Handle<WasmTableObject> table,
int index, wasm::FunctionSig* sig,
Handle<Object> code_or_foreign) {
Handle<FixedArray> dispatch_tables(table->dispatch_tables(), isolate);
DCHECK_EQ(0, dispatch_tables->length() % 4);
for (int i = 0; i < dispatch_tables->length(); i += 4) {
Handle<FixedArray> function_table(
FixedArray::cast(dispatch_tables->get(i + 2)), isolate);
Handle<FixedArray> signature_table(
FixedArray::cast(dispatch_tables->get(i + 3)), isolate);
if (sig) {
DCHECK(code_or_foreign->IsCode() || code_or_foreign->IsForeign());
Handle<WasmInstanceObject> instance(
WasmInstanceObject::cast(dispatch_tables->get(i)), isolate);
// Note that {SignatureMap::Find} may return {-1} if the signature is
// not found; it will simply never match any check.
auto sig_index = instance->module()->signature_map.Find(sig);
signature_table->set(index, Smi::FromInt(sig_index));
function_table->set(index, *code_or_foreign);
} else {
DCHECK(code_or_foreign.is_null());
signature_table->set(index, Smi::FromInt(-1));
function_table->set(index, Smi::kZero);
}
}
}
namespace {
Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate,
......
......@@ -129,13 +129,19 @@ class WasmTableObject : public JSObject {
static Handle<WasmTableObject> New(Isolate* isolate, uint32_t initial,
int64_t maximum,
Handle<FixedArray>* js_functions);
static Handle<FixedArray> AddDispatchTable(
Isolate* isolate, Handle<WasmTableObject> table,
Handle<WasmInstanceObject> instance, int table_index,
Handle<FixedArray> function_table, Handle<FixedArray> signature_table);
static void AddDispatchTable(Isolate* isolate, Handle<WasmTableObject> table,
Handle<WasmInstanceObject> instance,
int table_index,
Handle<FixedArray> function_table,
Handle<FixedArray> signature_table);
static void Set(Isolate* isolate, Handle<WasmTableObject> table,
int32_t index, Handle<JSFunction> function);
static void UpdateDispatchTables(Isolate* isolate,
Handle<WasmTableObject> table, int index,
wasm::FunctionSig* sig,
Handle<Object> code_or_foreign);
};
// Representation of a WebAssembly.Memory JavaScript-level object.
......
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