v8/test/mjsunit/regress/regress-8947.js
Ben L. Titzer 15925e5cc6 [wasm] Fix import of reexported API function
When a function is exported from a WebAssembly module, it is implicitly
wrapped in a WasmExportedFunction. For functions that were imports into
this module, the exported function appears like other Wasm function,
e.g. can be used in tables. When that exported function was re-imported
to another module, the logic to compute the import kind mistakenly
assumed the exported function was indeed originally a Wasm function
and tried to call it directly, instead of treating it like an imported
JS function.

R=ahaas@chromium.org
BUG=v8:8947

Change-Id: Ib8fac81fbe0f49c50cfbfb2e69d9bb60aef91fcc
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1503632
Commit-Queue: Ben Titzer <titzer@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60031}
2019-03-05 14:34:57 +00:00

50 lines
1.4 KiB
JavaScript

// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-module-builder.js");
(function testCallReexportedJSFunc() {
print(arguments.callee.name);
function dothrow() {
throw "exception";
}
var builder = new WasmModuleBuilder();
const imp_index = builder.addImport("w", "m", kSig_i_v);
builder.addExport("exp", imp_index);
var exp = builder.instantiate({w: {m: dothrow}}).exports.exp;
builder.addImport("w", "m", kSig_i_v);
builder.addFunction("main", kSig_i_v)
.addBody([
kExprCallFunction, 0, // --
]) // --
.exportFunc();
var main = builder.instantiate({w: {m: exp}}).exports.main;
assertThrowsEquals(main, "exception");
})();
(function testCallReexportedAPIFunc() {
print(arguments.callee.name);
var builder = new WasmModuleBuilder();
const imp_index = builder.addImport("w", "m", kSig_i_v);
builder.addExport("exp", imp_index);
var exp = builder.instantiate({w: {m: WebAssembly.Module}}).exports.exp;
builder.addImport("w", "m", kSig_i_v);
builder.addFunction("main", kSig_i_v)
.addBody([
kExprCallFunction, 0, // --
]) // --
.exportFunc();
var main = builder.instantiate({w: {m: exp}}).exports.main;
assertThrows(main, TypeError);
})();