1// Copyright 2016 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Flags: --expose-wasm 6 7load('test/mjsunit/wasm/wasm-constants.js'); 8load('test/mjsunit/wasm/wasm-module-builder.js'); 9 10var debug = false; 11 12(function BasicTest() { 13 var module = new WasmModuleBuilder(); 14 module.addMemory(1, 2, false); 15 module.addFunction("foo", kSig_i) 16 .addBody([kExprI8Const, 11]) 17 .exportAs("blarg"); 18 19 var buffer = module.toBuffer(debug); 20 var instance = Wasm.instantiateModule(buffer); 21 assertEquals(11, instance.exports.blarg()); 22})(); 23 24(function ImportTest() { 25 var module = new WasmModuleBuilder(); 26 var index = module.addImport("print", makeSig_v_x(kAstI32)); 27 module.addFunction("foo", kSig_v_v) 28 .addBody([kExprI8Const, 13, kExprCallImport, kArity1, index]) 29 .exportAs("main"); 30 31 var buffer = module.toBuffer(debug); 32 var instance = Wasm.instantiateModule(buffer, {print: print}); 33 print("should print 13! "); 34 instance.exports.main(); 35})(); 36 37(function LocalsTest() { 38 var module = new WasmModuleBuilder(); 39 module.addFunction(undefined, kSig_i_i) 40 .addLocals({i32_count: 1}) 41 .addBody([kExprGetLocal, 0, kExprSetLocal, 1]) 42 .exportAs("main"); 43 44 var buffer = module.toBuffer(debug); 45 var instance = Wasm.instantiateModule(buffer); 46 assertEquals(19, instance.exports.main(19)); 47 assertEquals(27777, instance.exports.main(27777)); 48})(); 49 50(function LocalsTest2() { 51 // TODO(titzer): i64 only works on 64-bit platforms. 52 var types = [ 53 {locals: {i32_count: 1}, type: kAstI32}, 54// {locals: {i64_count: 1}, type: kAstI64}, 55 {locals: {f32_count: 1}, type: kAstF32}, 56 {locals: {f64_count: 1}, type: kAstF64}, 57 ]; 58 59 for (p of types) { 60 var module = new WasmModuleBuilder(); 61 module.addFunction(undefined, makeSig_r_x(p.type, p.type)) 62 .addLocals(p.locals) 63 .addBody([kExprGetLocal, 0, kExprSetLocal, 1]) 64 .exportAs("main"); 65 66 var buffer = module.toBuffer(debug); 67 var instance = Wasm.instantiateModule(buffer); 68 assertEquals(19, instance.exports.main(19)); 69 assertEquals(27777, instance.exports.main(27777)); 70 } 71})(); 72 73(function CallTest() { 74 var module = new WasmModuleBuilder(); 75 module.addFunction("add", kSig_i_ii) 76 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); 77 module.addFunction("main", kSig_i_ii) 78 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, kArity2, 0]) 79 .exportAs("main"); 80 81 var instance = module.instantiate(); 82 assertEquals(44, instance.exports.main(11, 33)); 83 assertEquals(7777, instance.exports.main(2222, 5555)); 84})(); 85 86(function IndirectCallTest() { 87 var module = new WasmModuleBuilder(); 88 module.addFunction("add", kSig_i_ii) 89 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); 90 module.addFunction("main", kSig_i_iii) 91 .addBody([kExprGetLocal, 92 0, kExprGetLocal, 1, kExprGetLocal, 2, kExprCallIndirect, kArity2, 0]) 93 .exportAs("main"); 94 module.appendToTable([0]); 95 96 var instance = module.instantiate(); 97 assertEquals(44, instance.exports.main(0, 11, 33)); 98 assertEquals(7777, instance.exports.main(0, 2222, 5555)); 99 assertThrows(function() { instance.exports.main(1, 1, 1); }); 100})(); 101 102(function DataSegmentTest() { 103 var module = new WasmModuleBuilder(); 104 module.addMemory(1, 1, false); 105 module.addFunction("load", kSig_i_i) 106 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) 107 .exportAs("load"); 108 module.addDataSegment(0, [9, 9, 9, 9], true); 109 110 var buffer = module.toBuffer(debug); 111 var instance = Wasm.instantiateModule(buffer); 112 assertEquals(151587081, instance.exports.load(0)); 113})(); 114 115 116(function BasicTestWithUint8Array() { 117 var module = new WasmModuleBuilder(); 118 module.addMemory(1, 2, false); 119 module.addFunction("foo", kSig_i) 120 .addBody([kExprI8Const, 17]) 121 .exportAs("blarg"); 122 123 var buffer = module.toBuffer(debug); 124 var array = new Uint8Array(buffer); 125 var instance = Wasm.instantiateModule(array); 126 assertEquals(17, instance.exports.blarg()); 127 128 var kPad = 5; 129 var buffer2 = new ArrayBuffer(kPad + buffer.byteLength + kPad); 130 var whole = new Uint8Array(buffer2); 131 for (var i = 0; i < whole.byteLength; i++) { 132 whole[i] = 0xff; 133 } 134 var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength); 135 for (var i = 0; i < array2.byteLength; i++) { 136 array2[i] = array[i]; 137 } 138 var instance = Wasm.instantiateModule(array2); 139 assertEquals(17, instance.exports.blarg()); 140})(); 141 142(function ImportTestTwoLevel() { 143 var module = new WasmModuleBuilder(); 144 var index = module.addImportWithModule("mod", "print", makeSig_v_x(kAstI32)); 145 module.addFunction("foo", kSig_v_v) 146 .addBody([kExprI8Const, 19, kExprCallImport, kArity1, index]) 147 .exportAs("main"); 148 149 var buffer = module.toBuffer(debug); 150 var instance = Wasm.instantiateModule(buffer, {mod: {print: print}}); 151 print("should print 19! "); 152 instance.exports.main(); 153})(); 154