2022-05-12 19:30:38 +00:00
|
|
|
// Copyright 2022 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.
|
|
|
|
|
2022-09-26 14:51:56 +00:00
|
|
|
// Flags: --experimental-wasm-gc --allow-natives-syntax
|
2022-05-12 19:30:38 +00:00
|
|
|
|
|
|
|
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
let i16Array = builder.addArray(kWasmI16, true);
|
|
|
|
|
2022-08-18 08:15:11 +00:00
|
|
|
builder.addFunction('getHelloArray', makeSig([], [kWasmArrayRef]))
|
2022-05-12 19:30:38 +00:00
|
|
|
.addBody([
|
|
|
|
...wasmI32Const(72), ...wasmI32Const(69), ...wasmI32Const(76),
|
2022-06-27 10:34:39 +00:00
|
|
|
...wasmI32Const(76), ...wasmI32Const(79),
|
2022-08-25 14:16:56 +00:00
|
|
|
kGCPrefix, kExprArrayNewFixed, i16Array, 5
|
2022-05-12 19:30:38 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
2022-08-18 08:15:11 +00:00
|
|
|
builder.addFunction('getChar', makeSig([kWasmArrayRef, kWasmI32], [kWasmI32]))
|
2022-05-12 19:30:38 +00:00
|
|
|
.addBody([
|
2022-10-14 08:46:34 +00:00
|
|
|
kExprLocalGet, 0, kGCPrefix, kExprRefAsArray, kGCPrefix,
|
2022-08-31 07:53:19 +00:00
|
|
|
kExprRefCast, i16Array, kExprLocalGet, 1, kGCPrefix, kExprArrayGetS,
|
2022-05-12 19:30:38 +00:00
|
|
|
i16Array
|
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
const getHelloArray = instance.exports.getHelloArray;
|
|
|
|
const getChar = instance.exports.getChar;
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
WebAssembly.experimentalConvertArrayToString(getHelloArray(), 0, 5),
|
|
|
|
'HELLO');
|
|
|
|
assertEquals(
|
|
|
|
WebAssembly.experimentalConvertArrayToString(getHelloArray(), 1, 4),
|
|
|
|
'ELLO');
|
|
|
|
assertEquals(
|
|
|
|
WebAssembly.experimentalConvertArrayToString(getHelloArray(), 0, 3), 'HEL');
|
|
|
|
|
|
|
|
const string = 'foobar'
|
|
|
|
const array =
|
|
|
|
WebAssembly.experimentalConvertStringToArray('foobar', getHelloArray());
|
|
|
|
for (let i = 0; i < string.length; ++i) {
|
|
|
|
assertEquals(getChar(array, i), string.charCodeAt(i));
|
|
|
|
}
|
2022-09-26 14:51:56 +00:00
|
|
|
|
|
|
|
// Test calling built-ins with different amount of (invalid) arguments.
|
|
|
|
function arrayToString() {
|
|
|
|
WebAssembly.experimentalConvertArrayToString(...arguments);
|
|
|
|
}
|
|
|
|
function stringToArray() {
|
|
|
|
WebAssembly.experimentalConvertStringToArray(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = [];
|
|
|
|
for (let i = 1; i <= 5; ++i) {
|
|
|
|
assertThrows(() => arrayToString(...args));
|
|
|
|
assertThrows(() => stringToArray(...args));
|
|
|
|
%PrepareFunctionForOptimization(arrayToString);
|
|
|
|
%PrepareFunctionForOptimization(stringToArray);
|
|
|
|
%OptimizeFunctionOnNextCall(arrayToString);
|
|
|
|
%OptimizeFunctionOnNextCall(stringToArray);
|
|
|
|
assertThrows(() => arrayToString(...args));
|
|
|
|
assertThrows(() => stringToArray(...args));
|
|
|
|
args.push(i);
|
|
|
|
}
|