1// META: global=window,dedicatedworker,jsshell 2// META: script=/wasm/jsapi/assertions.js 3// META: script=/wasm/jsapi/memory/assertions.js 4 5test(() => { 6 assert_function_name(WebAssembly.Memory, "Memory", "WebAssembly.Memory"); 7}, "name"); 8 9test(() => { 10 assert_function_length(WebAssembly.Memory, 1, "WebAssembly.Memory"); 11}, "length"); 12 13test(() => { 14 assert_throws_js(TypeError, () => new WebAssembly.Memory()); 15}, "No arguments"); 16 17test(() => { 18 const argument = { "initial": 0 }; 19 assert_throws_js(TypeError, () => WebAssembly.Memory(argument)); 20}, "Calling"); 21 22test(() => { 23 const invalidArguments = [ 24 undefined, 25 null, 26 false, 27 true, 28 "", 29 "test", 30 Symbol(), 31 1, 32 NaN, 33 {}, 34 ]; 35 for (const invalidArgument of invalidArguments) { 36 assert_throws_js(TypeError, 37 () => new WebAssembly.Memory(invalidArgument), 38 `new Memory(${format_value(invalidArgument)})`); 39 } 40}, "Invalid descriptor argument"); 41 42test(() => { 43 assert_throws_js(TypeError, () => new WebAssembly.Memory({ "initial": undefined })); 44}, "Undefined initial value in descriptor"); 45 46const outOfRangeValues = [ 47 NaN, 48 Infinity, 49 -Infinity, 50 -1, 51 0x100000000, 52 0x1000000000, 53]; 54 55for (const value of outOfRangeValues) { 56 test(() => { 57 assert_throws_js(TypeError, () => new WebAssembly.Memory({ "initial": value })); 58 }, `Out-of-range initial value in descriptor: ${format_value(value)}`); 59 60 test(() => { 61 assert_throws_js(TypeError, () => new WebAssembly.Memory({ "initial": 0, "maximum": value })); 62 }, `Out-of-range maximum value in descriptor: ${format_value(value)}`); 63} 64 65test(() => { 66 assert_throws_js(RangeError, () => new WebAssembly.Memory({ "initial": 10, "maximum": 9 })); 67}, "Initial value exceeds maximum"); 68 69test(() => { 70 const proxy = new Proxy({}, { 71 has(o, x) { 72 assert_unreached(`Should not call [[HasProperty]] with ${x}`); 73 }, 74 get(o, x) { 75 // Due to the requirement not to supply both minimum and initial, we need to ignore one of them. 76 switch (x) { 77 case "shared": 78 return false; 79 case "initial": 80 case "maximum": 81 return 0; 82 default: 83 return undefined; 84 } 85 }, 86 }); 87 new WebAssembly.Memory(proxy); 88}, "Proxy descriptor"); 89 90test(() => { 91 const order = []; 92 93 new WebAssembly.Memory({ 94 get maximum() { 95 order.push("maximum"); 96 return { 97 valueOf() { 98 order.push("maximum valueOf"); 99 return 1; 100 }, 101 }; 102 }, 103 104 get initial() { 105 order.push("initial"); 106 return { 107 valueOf() { 108 order.push("initial valueOf"); 109 return 1; 110 }, 111 }; 112 }, 113 }); 114 115 assert_array_equals(order, [ 116 "initial", 117 "initial valueOf", 118 "maximum", 119 "maximum valueOf", 120 ]); 121}, "Order of evaluation for descriptor"); 122 123test(() => { 124 const argument = { "initial": 0 }; 125 const memory = new WebAssembly.Memory(argument); 126 assert_Memory(memory, { "size": 0 }); 127}, "Zero initial"); 128 129test(() => { 130 const argument = { "initial": 4 }; 131 const memory = new WebAssembly.Memory(argument); 132 assert_Memory(memory, { "size": 4 }); 133}, "Non-zero initial"); 134 135test(() => { 136 const argument = { "initial": 0 }; 137 const memory = new WebAssembly.Memory(argument, {}); 138 assert_Memory(memory, { "size": 0 }); 139}, "Stray argument"); 140