2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2015-12-17 10:54:08 +00:00
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
load('test/mjsunit/wasm/wasm-constants.js');
|
|
|
|
load('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
|
|
|
function CreateDefaultBuilder() {
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
const sig_index = kSig_i_dd;
|
|
|
|
builder.addImport('mod', 'fun', sig_index);
|
|
|
|
builder.addFunction('main', sig_index)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprCallFunction, 0, // --
|
|
|
|
]) // --
|
|
|
|
.exportFunc();
|
|
|
|
return builder;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
function checkSuccessfulInstantiation(builder, ffi, handler) {
|
|
|
|
// Test synchronous instantiation.
|
|
|
|
const instance = builder.instantiate(ffi);
|
|
|
|
if (handler) handler(instance);
|
2017-10-16 08:49:45 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
// Test asynchronous instantiation.
|
|
|
|
assertPromiseResult(builder.asyncInstantiate(ffi), handler);
|
|
|
|
}
|
2017-10-16 08:49:45 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
function checkFailingInstantiation(builder, ffi, error, message) {
|
|
|
|
// Test synchronous instantiation.
|
|
|
|
assertThrows(_ => builder.instantiate(ffi), error, message);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
// Test asynchronous instantiation.
|
|
|
|
assertPromiseResult(builder.asyncInstantiate(ffi), assertUnreachable, e => {
|
|
|
|
assertInstanceof(e, error);
|
|
|
|
assertEquals(message, e.message);
|
|
|
|
});
|
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
(function testValidFFI() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let ffi = {'mod': {fun: print}};
|
|
|
|
checkSuccessfulInstantiation(CreateDefaultBuilder(), ffi, undefined);
|
|
|
|
})();
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
(function testInvalidFFIs() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
checkFailingInstantiation(
|
|
|
|
CreateDefaultBuilder(), 17, TypeError,
|
|
|
|
'WebAssembly Instantiation: Argument 1 must be an object');
|
|
|
|
checkFailingInstantiation(
|
|
|
|
CreateDefaultBuilder(), {}, TypeError,
|
|
|
|
'WebAssembly Instantiation: Import #0 module="mod" error: module is not an object or function');
|
|
|
|
checkFailingInstantiation(
|
|
|
|
CreateDefaultBuilder(), {mod: {}}, WebAssembly.LinkError,
|
|
|
|
'WebAssembly Instantiation: Import #0 module="mod" function="fun" error: function import requires a callable');
|
|
|
|
checkFailingInstantiation(
|
|
|
|
CreateDefaultBuilder(), {mod: {fun: {}}}, WebAssembly.LinkError,
|
|
|
|
'WebAssembly Instantiation: Import #0 module="mod" function="fun" error: function import requires a callable');
|
|
|
|
checkFailingInstantiation(
|
|
|
|
CreateDefaultBuilder(), {mod: {fun: 0}}, WebAssembly.LinkError,
|
|
|
|
'WebAssembly Instantiation: Import #0 module="mod" function="fun" error: function import requires a callable');
|
|
|
|
})();
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
(function testImportWithInvalidSignature() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
// "fun" should have signature "i_dd"
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
let sig_index = kSig_i_dd;
|
|
|
|
builder.addFunction('exp', kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal,
|
|
|
|
0,
|
|
|
|
]) // --
|
|
|
|
.exportFunc();
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
let exported = builder.instantiate().exports.exp;
|
|
|
|
checkFailingInstantiation(
|
|
|
|
CreateDefaultBuilder(), {mod: {fun: exported}}, WebAssembly.LinkError,
|
|
|
|
'WebAssembly Instantiation: Import #0 module="mod" function="fun" error: imported function does not match the expected type');
|
|
|
|
})();
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
(function regression870646() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const ffi = {mod: {fun: function() {}}};
|
|
|
|
Object.defineProperty(ffi, 'mod', {
|
|
|
|
get: function() {
|
|
|
|
throw new Error('my_exception');
|
|
|
|
}
|
|
|
|
});
|
2017-10-16 08:49:45 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
checkFailingInstantiation(CreateDefaultBuilder(), ffi, Error, 'my_exception');
|
|
|
|
})();
|
2017-10-16 08:49:45 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
// "fun" matches signature "i_dd"
|
|
|
|
(function testImportWithValidSignature() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addFunction('exp', kSig_i_dd)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const,
|
|
|
|
33,
|
|
|
|
]) // --
|
|
|
|
.exportFunc();
|
2017-10-16 08:49:45 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
let exported = builder.instantiate().exports.exp;
|
2016-08-18 14:32:52 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
checkSuccessfulInstantiation(
|
|
|
|
CreateDefaultBuilder(), {mod: {fun: exported}},
|
|
|
|
instance => assertEquals(33, instance.exports.main()));
|
2017-10-16 08:49:45 +00:00
|
|
|
})();
|
2016-08-18 14:32:52 +00:00
|
|
|
|
|
|
|
(function I64InSignatureThrows() {
|
2018-08-07 10:00:21 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2016-08-18 14:32:52 +00:00
|
|
|
|
|
|
|
builder.addMemory(1, 1, true);
|
2018-08-07 10:00:21 +00:00
|
|
|
builder.addFunction('function_with_invalid_signature', kSig_l_ll)
|
2016-08-18 14:32:52 +00:00
|
|
|
.addBody([ // --
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprI64Sub]) // --
|
|
|
|
.exportFunc()
|
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
checkSuccessfulInstantiation(
|
|
|
|
builder, undefined,
|
|
|
|
instance => assertThrows(function() {
|
|
|
|
instance.exports.function_with_invalid_signature(33, 88);
|
|
|
|
}, TypeError, 'wasm function signature contains illegal type'));
|
2016-08-18 14:32:52 +00:00
|
|
|
})();
|
2016-08-30 12:45:46 +00:00
|
|
|
|
2016-09-21 15:32:22 +00:00
|
|
|
(function I64ParamsInSignatureThrows() {
|
2018-08-07 10:00:21 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2016-09-21 15:32:22 +00:00
|
|
|
|
|
|
|
builder.addMemory(1, 1, true);
|
2018-08-07 10:00:21 +00:00
|
|
|
builder.addFunction('function_with_invalid_signature', kSig_i_l)
|
|
|
|
.addBody([kExprGetLocal, 0, kExprI32ConvertI64])
|
|
|
|
.exportFunc();
|
2016-09-21 15:32:22 +00:00
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
checkSuccessfulInstantiation(
|
|
|
|
builder, undefined,
|
|
|
|
instance => assertThrows(
|
|
|
|
_ => instance.exports.function_with_invalid_signature(12), TypeError,
|
|
|
|
'wasm function signature contains illegal type'));
|
2016-09-21 15:32:22 +00:00
|
|
|
|
|
|
|
})();
|
2016-08-30 12:45:46 +00:00
|
|
|
|
|
|
|
(function I64JSImportThrows() {
|
2018-08-07 10:00:21 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let sig_index = builder.addType(kSig_i_i);
|
|
|
|
let sig_i64_index = builder.addType(kSig_i_l);
|
|
|
|
let index = builder.addImport('', 'func', sig_i64_index);
|
|
|
|
builder.addFunction('main', sig_index)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, kExprI64SConvertI32, kExprCallFunction, index // --
|
|
|
|
]) // --
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
checkSuccessfulInstantiation(
|
|
|
|
builder, {'': {func: _ => {}}},
|
|
|
|
instance => assertThrows(
|
|
|
|
instance.exports.main, TypeError,
|
|
|
|
'wasm function signature contains illegal type'));
|
|
|
|
|
2016-08-30 12:45:46 +00:00
|
|
|
})();
|
2016-12-07 13:54:02 +00:00
|
|
|
|
2017-03-15 10:25:16 +00:00
|
|
|
(function ImportI64ParamWithF64ReturnThrows() {
|
2018-08-07 10:00:21 +00:00
|
|
|
print(arguments.callee.name);
|
2017-03-15 10:25:16 +00:00
|
|
|
// This tests that we generate correct code by using the correct return
|
|
|
|
// register. See bug 6096.
|
2018-08-07 10:00:21 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2017-03-15 10:25:16 +00:00
|
|
|
builder.addImport('', 'f', makeSig([kWasmI64], [kWasmF64]));
|
|
|
|
builder.addFunction('main', kSig_v_v)
|
|
|
|
.addBody([kExprI64Const, 0, kExprCallFunction, 0, kExprDrop])
|
|
|
|
.exportFunc();
|
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
checkSuccessfulInstantiation(
|
|
|
|
builder, {'': {f: i => i}},
|
|
|
|
instance => assertThrows(
|
|
|
|
instance.exports.main, TypeError,
|
|
|
|
'wasm function signature contains illegal type'));
|
|
|
|
|
2017-03-15 10:25:16 +00:00
|
|
|
})();
|
|
|
|
|
2017-03-15 15:13:22 +00:00
|
|
|
(function ImportI64Return() {
|
2018-08-07 10:00:21 +00:00
|
|
|
print(arguments.callee.name);
|
2017-03-15 15:13:22 +00:00
|
|
|
// This tests that we generate correct code by using the correct return
|
|
|
|
// register(s). See bug 6104.
|
2018-08-07 10:00:21 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2017-03-15 15:13:22 +00:00
|
|
|
builder.addImport('', 'f', makeSig([], [kWasmI64]));
|
|
|
|
builder.addFunction('main', kSig_v_v)
|
|
|
|
.addBody([kExprCallFunction, 0, kExprDrop])
|
|
|
|
.exportFunc();
|
|
|
|
|
2018-08-07 10:00:21 +00:00
|
|
|
checkSuccessfulInstantiation(
|
|
|
|
builder, {'': {f: _ => 1}},
|
|
|
|
instance => assertThrows(
|
|
|
|
instance.exports.main, TypeError,
|
|
|
|
'wasm function signature contains illegal type'));
|
|
|
|
|
2017-03-15 15:13:22 +00:00
|
|
|
})();
|
|
|
|
|
2016-12-07 13:54:02 +00:00
|
|
|
(function ImportSymbolToNumberThrows() {
|
2018-08-07 10:00:21 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let index = builder.addImport('', 'f', kSig_i_v);
|
|
|
|
builder.addFunction('main', kSig_i_v)
|
2016-12-07 13:54:02 +00:00
|
|
|
.addBody([kExprCallFunction, 0])
|
|
|
|
.exportFunc();
|
2018-08-07 10:00:21 +00:00
|
|
|
|
|
|
|
checkSuccessfulInstantiation(
|
|
|
|
builder, {'': {f: _ => Symbol()}},
|
|
|
|
instance => assertThrows(
|
|
|
|
instance.exports.main, TypeError,
|
|
|
|
'Cannot convert a Symbol value to a number'));
|
2016-12-07 13:54:02 +00:00
|
|
|
})();
|