v8/test/mjsunit/wasm/extended-constants.js
Manos Koukoutos bf1565d708 [wasm] Implement the Extended Constants proposal
This proposal adds i32 and i64 addition, subtraction, and multiplication
to the list of constant expressions.
See https://github.com/WebAssembly/extended-const.

Bug: v8:12089
Change-Id: I23a27a54a15fd37ee1d553992ab3b355eb9d317c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3497665
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79362}
2022-03-04 08:49:52 +00:00

75 lines
2.5 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.
// Flags: --experimental-wasm-extended-const
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
(function ExtendedConstantsTestI32() {
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
let imported_global_0 = builder.addImportedGlobal("m", "g0", kWasmI32, false);
let imported_global_1 = builder.addImportedGlobal("m", "g1", kWasmI32, false);
let defined_global = builder.addGlobal(
kWasmI32, false,
WasmInitExpr.I32Add(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I32Mul(
WasmInitExpr.GlobalGet(imported_global_1),
WasmInitExpr.I32Sub(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I32Const(1)))));
builder.addExportOfKind("global", kExternalGlobal, defined_global.index);
let value0 = 123;
let value1 = -450;
let global_obj0 = new WebAssembly.Global({value: "i32", mutable: false},
value0);
let global_obj1 = new WebAssembly.Global({value: "i32", mutable: false},
value1);
let instance = builder.instantiate({m : {g0: global_obj0, g1: global_obj1}});
assertEquals(value0 + (value1 * (value0 - 1)), instance.exports.global.value);
})();
(function ExtendedConstantsTestI64() {
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
let imported_global_0 = builder.addImportedGlobal("m", "g0", kWasmI64, false);
let imported_global_1 = builder.addImportedGlobal("m", "g1", kWasmI64, false);
let defined_global = builder.addGlobal(
kWasmI64, false,
WasmInitExpr.I64Add(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I64Mul(
WasmInitExpr.GlobalGet(imported_global_1),
WasmInitExpr.I64Sub(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I64Const(1)))));
builder.addExportOfKind("global", kExternalGlobal, defined_global.index);
let value0 = 123n;
let value1 = -450n;
let global_obj0 = new WebAssembly.Global({value: "i64", mutable: false},
value0);
let global_obj1 = new WebAssembly.Global({value: "i64", mutable: false},
value1);
let instance = builder.instantiate({m : {g0: global_obj0, g1: global_obj1}});
assertEquals(value0 + (value1 * (value0 - 1n)),
instance.exports.global.value);
})();