[wasm][anyref] Add support for js-to-wasm and wasm-to-js wrappers

R=titzer@chromium.org

Bug: v8:7581
Change-Id: Ib100a45ad51ec6b6a0400cfe97fe918089c04ead
Reviewed-on: https://chromium-review.googlesource.com/998095
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: Ben Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52468}
This commit is contained in:
Andreas Haas 2018-04-06 15:18:49 +02:00 committed by Commit Bot
parent 563c352466
commit 1800e9bb36
3 changed files with 80 additions and 0 deletions

View File

@ -2840,6 +2840,8 @@ Node* WasmGraphBuilder::ToJS(Node* node, wasm::ValueType type) {
return BuildChangeFloat64ToTagged(node);
case wasm::kWasmF64:
return BuildChangeFloat64ToTagged(node);
case wasm::kWasmAnyRef:
return node;
case wasm::kWasmStmt:
return jsgraph()->UndefinedConstant();
default:
@ -2922,6 +2924,11 @@ Node* WasmGraphBuilder::FromJS(Node* node, Node* js_context,
wasm::ValueType type) {
DCHECK_NE(wasm::kWasmStmt, type);
// The parameter is of type AnyRef, we take it as is.
if (type == wasm::kWasmAnyRef) {
return node;
}
// Do a JavaScript ToNumber.
Node* num = BuildJavaScriptToNumber(node, js_context);

View File

@ -0,0 +1,70 @@
// Copyright 2018 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 --experimental-wasm-anyref --expose-gc
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
(function testAnyRefIdentityFunction() {
print(arguments.callee.name);
const builder = new WasmModuleBuilder();
builder.addFunction('main', kSig_r_r)
.addBody([kExprGetLocal, 0])
.exportFunc();
const instance = builder.instantiate();
let obj = {'hello' : 'world'};
assertEquals(obj, instance.exports.main(obj));
assertEquals(1234, instance.exports.main(1234));
assertEquals(123.4, instance.exports.main(123.4));
assertEquals(undefined, instance.exports.main(undefined));
assertEquals(null, instance.exports.main(null));
assertEquals(print, instance.exports.main(print));
})();
(function testPassAnyRefToImportedFunction() {
print(arguments.callee.name);
const builder = new WasmModuleBuilder();
const sig_index = builder.addType(kSig_v_r);
const imp_index = builder.addImport("q", "func", sig_index);
builder.addFunction('main', sig_index)
.addBody([kExprGetLocal, 0,
kExprCallFunction, imp_index])
.exportFunc();
function checkFunction(value) {
assertEquals('world', value.hello);
}
const instance = builder.instantiate({q: {func: checkFunction}});
instance.exports.main({hello: 'world'});
})();
(function testPassAnyRefWithGC() {
print(arguments.callee.name);
const builder = new WasmModuleBuilder();
const ref_sig = builder.addType(kSig_v_r);
const void_sig = builder.addType(kSig_v_v);
const imp_index = builder.addImport("q", "func", ref_sig);
const gc_index = builder.addImport("q", "gc", void_sig);
// First call the gc, then check if the object still exists.
builder.addFunction('main', ref_sig)
.addBody([
kExprCallFunction, gc_index, // call gc
kExprGetLocal, 0, kExprCallFunction, imp_index // call import
])
.exportFunc();
function checkFunction(value) {
assertEquals('world', value.hello);
}
const instance = builder.instantiate({q: {func: checkFunction, gc: gc}});
instance.exports.main({hello: 'world'});
})();

View File

@ -93,6 +93,7 @@ let kWasmI64 = 0x7e;
let kWasmF32 = 0x7d;
let kWasmF64 = 0x7c;
let kWasmS128 = 0x7b;
let kWasmAnyRef = 0x6f;
let kExternalFunction = 0;
let kExternalTable = 1;
@ -133,6 +134,8 @@ let kSig_iii_ii = makeSig([kWasmI32, kWasmI32], [kWasmI32, kWasmI32, kWasmI32]);
let kSig_v_f = makeSig([kWasmF32], []);
let kSig_f_f = makeSig([kWasmF32], [kWasmF32]);
let kSig_d_d = makeSig([kWasmF64], [kWasmF64]);
let kSig_r_r = makeSig([kWasmAnyRef], [kWasmAnyRef]);
let kSig_v_r = makeSig([kWasmAnyRef], []);
function makeSig(params, results) {
return {params: params, results: results};