Commit 3a4ba583 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Refactor Table.Set

The old implementation of Table.Set in wasm-js.cc accessed information
which should be hidden from this level of abstraction, e.g. the internal
structure of a WasmTableObject. With this CL, all that is done in
wasm-js.cc is the extraction of parameters. The actual logic is happening
in wasm-module.{h|cc}. This CL will also make refactoring wasm-module.cc
easier.

R=clemensh@chromium.org, mtrofin@chromium.org

Change-Id: Ifbce6f739459dffc9f9d47e4cd8227638867f3e9
Reviewed-on: https://chromium-review.googlesource.com/525694
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45754}
parent 5005eea8
...@@ -702,6 +702,16 @@ void WebAssemblyTableSet(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -702,6 +702,16 @@ void WebAssemblyTableSet(const v8::FunctionCallbackInfo<v8::Value>& args) {
thrower.TypeError("Argument 1 must be null or a function"); thrower.TypeError("Argument 1 must be null or a function");
return; return;
} }
// {This} parameter.
auto receiver =
i::Handle<i::WasmTableObject>::cast(Utils::OpenHandle(*args.This()));
// Parameter 0.
int32_t index;
if (!args[0]->Int32Value(context).To(&index)) return;
// Parameter 1.
i::Handle<i::Object> value = Utils::OpenHandle(*args[1]); i::Handle<i::Object> value = Utils::OpenHandle(*args[1]);
if (!value->IsNull(i_isolate) && if (!value->IsNull(i_isolate) &&
(!value->IsJSFunction() || (!value->IsJSFunction() ||
...@@ -711,27 +721,10 @@ void WebAssemblyTableSet(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -711,27 +721,10 @@ void WebAssemblyTableSet(const v8::FunctionCallbackInfo<v8::Value>& args) {
return; return;
} }
auto receiver = i::wasm::TableSet(&thrower, i_isolate, receiver, index,
i::Handle<i::WasmTableObject>::cast(Utils::OpenHandle(*args.This())); value->IsNull(i_isolate)
i::Handle<i::FixedArray> array(receiver->functions(), i_isolate); ? i::Handle<i::JSFunction>::null()
int i; : i::Handle<i::JSFunction>::cast(value));
if (!args[0]->Int32Value(context).To(&i)) return;
if (i < 0 || i >= array->length()) {
thrower.RangeError("index out of bounds");
return;
}
i::Handle<i::FixedArray> dispatch_tables(receiver->dispatch_tables(),
i_isolate);
if (value->IsNull(i_isolate)) {
i::wasm::UpdateDispatchTables(i_isolate, dispatch_tables, i,
i::Handle<i::JSFunction>::null());
} else {
i::wasm::UpdateDispatchTables(i_isolate, dispatch_tables, i,
i::Handle<i::JSFunction>::cast(value));
}
i::Handle<i::FixedArray>::cast(array)->set(i, *value);
} }
// WebAssembly.Memory.grow(num) -> num // WebAssembly.Memory.grow(num) -> num
......
...@@ -1095,9 +1095,9 @@ Handle<Code> CompileImportWrapper(Isolate* isolate, int index, FunctionSig* sig, ...@@ -1095,9 +1095,9 @@ Handle<Code> CompileImportWrapper(Isolate* isolate, int index, FunctionSig* sig,
module_name, import_name, origin); module_name, import_name, origin);
} }
void UpdateDispatchTablesInternal(Isolate* isolate, void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables,
Handle<FixedArray> dispatch_tables, int index, int index, WasmFunction* function,
WasmFunction* function, Handle<Code> code) { Handle<Code> code) {
DCHECK_EQ(0, dispatch_tables->length() % 4); DCHECK_EQ(0, dispatch_tables->length() % 4);
for (int i = 0; i < dispatch_tables->length(); i += 4) { for (int i = 0; i < dispatch_tables->length(); i += 4) {
int table_index = Smi::cast(dispatch_tables->get(i + 1))->value(); int table_index = Smi::cast(dispatch_tables->get(i + 1))->value();
...@@ -1123,18 +1123,30 @@ void UpdateDispatchTablesInternal(Isolate* isolate, ...@@ -1123,18 +1123,30 @@ void UpdateDispatchTablesInternal(Isolate* isolate,
} // namespace } // namespace
void wasm::UpdateDispatchTables(Isolate* isolate, void wasm::TableSet(ErrorThrower* thrower, Isolate* isolate,
Handle<FixedArray> dispatch_tables, int index, Handle<WasmTableObject> table, int32_t index,
Handle<JSFunction> function) { Handle<JSFunction> function) {
if (function.is_null()) { Handle<FixedArray> array(table->functions(), isolate);
UpdateDispatchTablesInternal(isolate, dispatch_tables, index, nullptr,
Handle<Code>::null()); if (index < 0 || index >= array->length()) {
} else { thrower->RangeError("index out of bounds");
UpdateDispatchTablesInternal( return;
isolate, dispatch_tables, index, }
GetWasmFunctionForImportWrapper(isolate, function),
UnwrapImportWrapper(function)); Handle<FixedArray> dispatch_tables(table->dispatch_tables(), isolate);
WasmFunction* wasm_function = nullptr;
Handle<Code> code = Handle<Code>::null();
Handle<Object> value = handle(isolate->heap()->null_value());
if (!function.is_null()) {
wasm_function = GetWasmFunctionForImportWrapper(isolate, function);
code = UnwrapImportWrapper(function);
value = Handle<Object>::cast(function);
} }
UpdateDispatchTables(isolate, dispatch_tables, index, wasm_function, code);
array->set(index, *value);
} }
// A helper class to simplify instantiating a module from a compiled module. // A helper class to simplify instantiating a module from a compiled module.
...@@ -2305,8 +2317,8 @@ class InstantiationHelper { ...@@ -2305,8 +2317,8 @@ class InstantiationHelper {
table_instance.js_wrappers->set(table_index, table_instance.js_wrappers->set(table_index,
*js_wrappers_[func_index]); *js_wrappers_[func_index]);
UpdateDispatchTablesInternal(isolate_, all_dispatch_tables, UpdateDispatchTables(isolate_, all_dispatch_tables, table_index,
table_index, function, wasm_code); function, wasm_code);
} }
} }
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "src/parsing/preparse-data.h" #include "src/parsing/preparse-data.h"
#include "src/wasm/signature-map.h" #include "src/wasm/signature-map.h"
#include "src/wasm/wasm-objects.h"
#include "src/wasm/wasm-opcodes.h" #include "src/wasm/wasm-opcodes.h"
namespace v8 { namespace v8 {
...@@ -440,8 +441,9 @@ void DetachWebAssemblyMemoryBuffer(Isolate* isolate, ...@@ -440,8 +441,9 @@ void DetachWebAssemblyMemoryBuffer(Isolate* isolate,
Handle<JSArrayBuffer> buffer, Handle<JSArrayBuffer> buffer,
bool free_memory); bool free_memory);
void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables, void TableSet(ErrorThrower* thrower, Isolate* isolate,
int index, Handle<JSFunction> js_function); Handle<WasmTableObject> table, int32_t index,
Handle<JSFunction> function);
//============================================================================ //============================================================================
//== Compilation and instantiation =========================================== //== Compilation and instantiation ===========================================
......
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