grow-memory.js (1640B)
1 function linearModule(min, max, ops, current_memory, grow_memory) { 2 var opsText = ops.map(function (op) { 3 if (op[0] == "CM") { 4 res = `(if (result i32) (i32.ne (${current_memory}) (i32.const ${op[1]})) 5 (then (i32.load offset=10 (i32.const 4294967295))) 6 (else (i32.const 0)))` 7 } else if (op[0] == "GM") { 8 res = `(if (result i32) (i32.ne (${grow_memory} (i32.const ${op[1]})) (i32.const ${op[2]})) 9 (then (i32.load offset=10 (i32.const 4294967295))) 10 (else (i32.const 0)))` 11 } else if (op[0] == "L") { 12 var type = op[1]; 13 var ext = op[2]; 14 var off = op[3]; 15 var loc = op[4] 16 var align = 0; 17 res = `(${type}.load${ext} offset=${off} (i32.const ${op[4]}))`; 18 } else if (op[0] == "S") { 19 var type = op[1]; 20 var ext = op[2]; 21 var off = op[3]; 22 var loc = op[4] 23 var align = 0; 24 res = `(${type}.store${ext} offset=${off} (i32.const ${op[4]}) (i32.const 42))`; 25 } 26 return res; 27 }).join("\n") 28 29 let text = 30 `(module 31 (memory ${min} ${max}) 32 ` + (min != 0 ? `(data (i32.const 0) "\\00\\01\\02\\03\\04\\05\\06\\07\\08\\09\\0a\\0b\\0c\\0d\\0e\\0f") 33 (data (i32.const 16) "\\f0\\f1\\f2\\f3\\f4\\f5\\f6\\f7\\f8\\f9\\fa\\fb\\fc\\fd\\fe\\ff")` 34 : "") + 35 ` 36 (func (result i32) 37 (drop ` + opsText + `) 38 (${current_memory}) 39 ) (export "run" (func 0)))`; 40 41 return text; 42 } 43 44 // Just grow some memory 45 wasmFullPass(linearModule(3,5, [["CM", 3]], "memory.size", "memory.grow"), 3); // New opcode names