nodejs/deps/v8/test/mjsunit/wasm/gc-js-interop-helpers.js
Michaël Zasso 918fe04351
deps: update V8 to 13.6.233.8
PR-URL: https://github.com/nodejs/node/pull/58070
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2025-05-02 15:06:53 +02:00

73 lines
2.4 KiB
JavaScript

// 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.
// Helpers to test interoperability of Wasm objects in JavaScript.
// The following flags are required:
// Flags: --turbofan --no-always-turbofan --allow-natives-syntax
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
function CreateWasmObjects() {
let builder = new WasmModuleBuilder();
let struct_type = builder.addStruct([makeField(kWasmI32, true)]);
let array_type = builder.addArray(kWasmI32, true);
builder.addFunction('MakeStruct', makeSig([], [kWasmExternRef]))
.exportFunc()
.addBody([
kExprI32Const, 42, // --
kGCPrefix, kExprStructNew, struct_type, // --
kGCPrefix, kExprExternConvertAny // --
]);
builder.addFunction('MakeArray', makeSig([], [kWasmExternRef]))
.exportFunc()
.addBody([
kExprI32Const, 2, // length
kGCPrefix, kExprArrayNewDefault, array_type, // --
kGCPrefix, kExprExternConvertAny // --
]);
let instance = builder.instantiate();
return {
struct: instance.exports.MakeStruct(),
array: instance.exports.MakeArray(),
};
}
function testThrowsRepeated(fn, ErrorType) {
// Make sure there's no left over optimized code from other
// instances of the function.
%DeoptimizeFunction(fn);
const maxRuns = 3;
for (let run = 0; run < maxRuns; ++run) {
%PrepareFunctionForOptimization(fn);
for (let i = 0; i < 5; i++) assertThrows(fn, ErrorType);
%OptimizeFunctionOnNextCall(fn);
assertThrows(fn, ErrorType);
if (isOptimized(fn)) return;
}
assertOptimized(fn);
}
function repeated(fn) {
// Make sure there's no left over optimized code from other
// instances of the function.
%DeoptimizeFunction(fn);
const maxRuns = 3;
for (let run = 0; run < maxRuns; ++run) {
%PrepareFunctionForOptimization(fn);
for (let i = 0; i < 5; i++) fn();
%OptimizeFunctionOnNextCall(fn);
fn();
if (isOptimized(fn)) return;
}
assertOptimized(fn);
}
// Prevent optimization, so that the test functions can not be inlined which
// can cause issues in combination with `assertOptimized` and deopts in test
// code.
%NeverOptimizeFunction(testThrowsRepeated);
%NeverOptimizeFunction(repeated);