Commit 7f58be6b authored by ahaas's avatar ahaas Committed by Commit bot

[wasm] Store the function_index directly in the js-to-wasm wrapper.

If a WebAssembly function is exported, its js-to-wasm wrapper has a
field which contains a reference to the WebAssembly function.
Originally this reference was an index into the export table, which
then contains an index into the function table, which then contains
the metadata of the WebAssembly function.

With this CL we use the index into the function table directly as
the reference to the WebAssembly function.

TEST=mjsunit/wasm/test-import-export-wrapper
R=rossberg@chromium.org, mtrofin@chromium.org
CC=titzer@chromium.org

Review-Url: https://codereview.chromium.org/2472103002
Cr-Commit-Position: refs/heads/master@{#40729}
parent dd27284c
......@@ -1627,7 +1627,7 @@ class WasmInstanceBuilder {
desc.set_writable(false);
// Process each export in the export table.
int func_index = 0;
int export_index = 0;
for (auto exp : module_->export_table) {
Handle<String> name =
ExtractStringFromModuleBytes(isolate_, compiled_module_,
......@@ -1637,20 +1637,20 @@ class WasmInstanceBuilder {
case kExternalFunction: {
// Wrap and export the code as a JSFunction.
WasmFunction& function = module_->functions[exp.index];
int export_index =
static_cast<int>(module_->functions.size() + func_index);
int func_index =
static_cast<int>(module_->functions.size() + export_index);
Handle<JSFunction> js_function = js_wrappers_[exp.index];
if (js_function.is_null()) {
// Wrap the exported code as a JSFunction.
Handle<Code> export_code =
code_table->GetValueChecked<Code>(isolate_, export_index);
js_function =
WrapExportCodeAsJSFunction(isolate_, export_code, name,
function.sig, func_index, instance);
code_table->GetValueChecked<Code>(isolate_, func_index);
js_function = WrapExportCodeAsJSFunction(
isolate_, export_code, name, function.sig, function.func_index,
instance);
js_wrappers_[exp.index] = js_function;
}
desc.set_value(js_function);
func_index++;
export_index++;
break;
}
case kExternalTable: {
......
......@@ -66,6 +66,66 @@ var expect_no_elison = 1;
assertEquals(%CheckWasmWrapperElision(the_export, expect_elison), true);
})();
// Function calls stack: first_export -> first_func -> first_import ->
// second_export -> second_import
// In this case, first_import and second_export have same signature,
// So that wrappers will be removed. If the wrappers do are not removed, then
// the test crashes because of the int64 parameter, which is not allowed in the
// wrappers.
(function TestWasmWrapperElisionInt64() {
var imported = function (a) {
return a;
};
var second_module = new WasmModuleBuilder();
var sig_index1 = second_module.addType(kSig_i_i);
var sig_index_ll = second_module.addType(kSig_l_l);
second_module
.addImportWithModule("import_module_2", "import_name_2", sig_index1);
second_module
.addFunction("second_export", sig_index_ll)
.addBody([
kExprGetLocal, 0,
kExprI32ConvertI64,
kExprCallFunction, 0,
kExprI64SConvertI32,
kExprReturn
])
.exportFunc();
var first_module = new WasmModuleBuilder();
var sig_index = first_module.addType(kSig_i_v);
var sig_index_ll = first_module.addType(kSig_l_l);
first_module
.addImportWithModule("import_module_1", "import_name_1", sig_index_ll);
first_module
.addFunction("first_export", sig_index)
.addBody([
kExprI64Const, 2,
kExprCallFunction, 2,
kExprI32ConvertI64,
kExprReturn
])
.exportFunc();
first_module
.addFunction("first_func", sig_index_ll)
.addBody([
kExprI64Const, 1,
kExprGetLocal, 0,
kExprI64Add,
kExprCallFunction, 0,
kExprReturn
]);
var f = second_module
.instantiate({import_module_2: {import_name_2: imported}})
.exports.second_export;
var the_export = first_module
.instantiate({import_module_1: {import_name_1: f}})
.exports.first_export;
assertEquals(the_export(), 3);
})();
// function calls stack: first_export -> first_func -> first_import ->
// second_export -> second_import
// In this case, second_export has less params than first_import,
......
......@@ -94,6 +94,7 @@ let kMemoryZero = 0;
// Useful signatures
let kSig_i_i = makeSig([kAstI32], [kAstI32]);
let kSig_l_l = makeSig([kAstI64], [kAstI64]);
let kSig_i_l = makeSig([kAstI64], [kAstI32]);
let kSig_i_ii = makeSig([kAstI32, kAstI32], [kAstI32]);
let kSig_i_iii = makeSig([kAstI32, kAstI32, kAstI32], [kAstI32]);
......
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