test_memory.js 1.25 KB
Newer Older
1 2 3 4
// Copyright 2020 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.

5
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
6 7 8 9 10 11 12

var builder = new WasmModuleBuilder();

builder.addGlobal(kWasmI32).exportAs('g_n');

builder.addMemory(32, 128).exportMemoryAs('mem')

13 14
builder.addDataSegment(0, [0x2a, 0x2b, 0x2c, 0x2d])

15 16 17 18 19 20 21
var func_a_idx =
    builder.addFunction('wasm_A', kSig_v_i).addBody([kExprNop, kExprNop]).index;

// wasm_B calls wasm_A <param0> times.
builder.addFunction('wasm_B', kSig_v_i)
    .addBody([
      kExprLoop,
22
      kWasmVoid,  // while
23 24 25
      kExprLocalGet,
      0,  // -
      kExprIf,
26
      kWasmVoid,  // if <param0> != 0
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      kExprLocalGet,
      0,  // -
      kExprI32Const,
      1,            // -
      kExprI32Sub,  // -
      kExprLocalSet,
      0,                      // decrease <param0>
      ...wasmI32Const(1024),  // some longer i32 const (2 byte imm)
      kExprCallFunction,
      func_a_idx,  // -
      kExprBr,
      1,         // continue
      kExprEnd,  // -
      kExprEnd,  // break
    ])
    .exportAs('main');

const instance = builder.instantiate();
const wasm_main = instance.exports.main;

function f() {
  wasm_main(42);
}
f();