OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --expose-wasm | 5 // Flags: --expose-wasm |
6 | 6 |
7 // Basic tests. | 7 // Basic tests. |
8 | 8 |
9 var outOfUint32RangeValue = 1e12; | 9 var outOfUint32RangeValue = 1e12; |
10 | 10 |
| 11 function assertMemoryIsValid(memory) { |
| 12 assertSame(WebAssembly.Memory.prototype, memory.__proto__); |
| 13 assertSame(WebAssembly.Memory, memory.constructor); |
| 14 assertTrue(memory instanceof Object); |
| 15 assertTrue(memory instanceof WebAssembly.Memory); |
| 16 } |
| 17 |
11 (function TestConstructor() { | 18 (function TestConstructor() { |
12 assertTrue(WebAssembly.Memory instanceof Function); | 19 assertTrue(WebAssembly.Memory instanceof Function); |
13 assertSame(WebAssembly.Memory, WebAssembly.Memory.prototype.constructor); | 20 assertSame(WebAssembly.Memory, WebAssembly.Memory.prototype.constructor); |
14 assertTrue(WebAssembly.Memory.prototype.grow instanceof Function); | 21 assertTrue(WebAssembly.Memory.prototype.grow instanceof Function); |
15 let desc = Object.getOwnPropertyDescriptor(WebAssembly.Memory.prototype, 'buff
er'); | 22 let desc = Object.getOwnPropertyDescriptor(WebAssembly.Memory.prototype, 'buff
er'); |
16 assertTrue(desc.get instanceof Function); | 23 assertTrue(desc.get instanceof Function); |
17 assertSame(undefined, desc.set); | 24 assertSame(undefined, desc.set); |
18 | 25 |
19 assertThrows(() => new WebAssembly.Memory(), TypeError); | 26 assertThrows(() => new WebAssembly.Memory(), TypeError); |
20 assertThrows(() => new WebAssembly.Memory(1), TypeError); | 27 assertThrows(() => new WebAssembly.Memory(1), TypeError); |
21 assertThrows(() => new WebAssembly.Memory(""), TypeError); | 28 assertThrows(() => new WebAssembly.Memory(""), TypeError); |
22 | 29 |
23 assertThrows(() => new WebAssembly.Memory({initial: -1}), RangeError); | 30 assertThrows(() => new WebAssembly.Memory({initial: -1}), RangeError); |
24 assertThrows(() => new WebAssembly.Memory({initial: outOfUint32RangeValue}), R
angeError); | 31 assertThrows(() => new WebAssembly.Memory({initial: outOfUint32RangeValue}), R
angeError); |
25 | 32 |
26 assertThrows(() => new WebAssembly.Memory({initial: 10, maximum: -1}), RangeEr
ror); | 33 assertThrows(() => new WebAssembly.Memory({initial: 10, maximum: -1}), RangeEr
ror); |
27 assertThrows(() => new WebAssembly.Memory({initial: 10, maximum: outOfUint32Ra
ngeValue}), RangeError); | 34 assertThrows(() => new WebAssembly.Memory({initial: 10, maximum: outOfUint32Ra
ngeValue}), RangeError); |
28 assertThrows(() => new WebAssembly.Memory({initial: 10, maximum: 9}), RangeErr
or); | 35 assertThrows(() => new WebAssembly.Memory({initial: 10, maximum: 9}), RangeErr
or); |
29 | 36 |
30 let memory = new WebAssembly.Memory({initial: 1}); | 37 let memory = new WebAssembly.Memory({initial: 1}); |
31 assertSame(WebAssembly.Memory.prototype, memory.__proto__); | 38 assertMemoryIsValid(memory); |
32 assertSame(WebAssembly.Memory, memory.constructor); | |
33 assertTrue(memory instanceof Object); | |
34 assertTrue(memory instanceof WebAssembly.Memory); | |
35 })(); | 39 })(); |
36 | 40 |
37 (function TestConstructorWithMaximum() { | 41 (function TestConstructorWithMaximum() { |
38 let memory = new WebAssembly.Memory({initial: 1, maximum: 10}); | 42 let memory = new WebAssembly.Memory({initial: 1, maximum: 10}); |
39 assertSame(WebAssembly.Memory.prototype, memory.__proto__); | 43 assertMemoryIsValid(memory); |
40 assertSame(WebAssembly.Memory, memory.constructor); | 44 })(); |
41 assertTrue(memory instanceof Object); | 45 |
42 assertTrue(memory instanceof WebAssembly.Memory); | 46 (function TestInitialIsUndefined() { |
| 47 // New memory with initial = undefined, which means initial = 0. |
| 48 let memory = new WebAssembly.Memory({initial: undefined}); |
| 49 assertMemoryIsValid(memory); |
| 50 })(); |
| 51 |
| 52 (function TestMaximumIsUndefined() { |
| 53 // New memory with maximum = undefined, which means maximum = 0. |
| 54 let memory = new WebAssembly.Memory({initial: 0, maximum: undefined}); |
| 55 assertMemoryIsValid(memory); |
| 56 })(); |
| 57 |
| 58 (function TestMaximumIsReadOnce() { |
| 59 var a = true; |
| 60 var desc = {initial: 10}; |
| 61 Object.defineProperty(desc, 'maximum', {get: function() { |
| 62 if (a) { |
| 63 a = false; |
| 64 return 16; |
| 65 } |
| 66 else { |
| 67 // Change the return value on the second call so it throws. |
| 68 return -1; |
| 69 } |
| 70 }}); |
| 71 let memory = new WebAssembly.Memory(desc); |
| 72 assertMemoryIsValid(memory); |
| 73 })(); |
| 74 |
| 75 (function TestMaximumDoesHasProperty() { |
| 76 var hasPropertyWasCalled = false; |
| 77 var desc = {initial: 10}; |
| 78 var proxy = new Proxy({maximum: 16}, { |
| 79 has: function(target, name) { hasPropertyWasCalled = true; } |
| 80 }); |
| 81 Object.setPrototypeOf(desc, proxy); |
| 82 let memory = new WebAssembly.Memory(desc); |
| 83 assertMemoryIsValid(memory); |
| 84 assertTrue(hasPropertyWasCalled); |
43 })(); | 85 })(); |
44 | 86 |
45 (function TestBuffer() { | 87 (function TestBuffer() { |
46 let memory = new WebAssembly.Memory({initial: 1}); | 88 let memory = new WebAssembly.Memory({initial: 1}); |
47 assertTrue(memory.buffer instanceof Object); | 89 assertTrue(memory.buffer instanceof Object); |
48 assertTrue(memory.buffer instanceof ArrayBuffer); | 90 assertTrue(memory.buffer instanceof ArrayBuffer); |
49 assertThrows(() => {'use strict'; memory.buffer = memory.buffer}, TypeError) | 91 assertThrows(() => {'use strict'; memory.buffer = memory.buffer}, TypeError) |
50 assertThrows(() => ({__proto__: memory}).buffer, TypeError) | 92 assertThrows(() => ({__proto__: memory}).buffer, TypeError) |
51 })(); | 93 })(); |
OLD | NEW |