OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 --expose-gc --verify-heap | |
6 | |
7 load("test/mjsunit/wasm/wasm-constants.js"); | |
8 load("test/mjsunit/wasm/wasm-module-builder.js"); | |
9 | |
10 const verifyHeap = gc; | |
11 let globalCounter = 10000000; | |
12 | |
13 function testProperties(obj) { | |
14 for (let i = 0; i < 3; i++) { | |
15 obj.x = 1001; | |
16 assertEquals(1001, obj.x); | |
17 | |
18 obj.y = "old"; | |
19 assertEquals("old", obj.y); | |
20 | |
21 delete obj.y; | |
22 assertEquals("undefined", typeof obj.y); | |
23 | |
24 let uid = globalCounter++; | |
25 let fresh = "f_" + uid; | |
26 | |
27 obj.z = fresh; | |
28 assertEquals(fresh, obj.z); | |
29 | |
30 obj[fresh] = uid; | |
31 assertEquals(uid, obj[fresh]); | |
32 | |
33 verifyHeap(); | |
34 | |
35 assertEquals(1001, obj.x); | |
36 assertEquals(fresh, obj.z); | |
37 assertEquals(uid, obj[fresh]); | |
38 } | |
39 | |
40 Object.defineProperty(obj, 'name', {value: "crazy"}); // special for JSFuncti ons. | |
Igor Sheludko
2017/07/18 17:32:39
It would be nice to respect the 80 chars limit.
titzer
2017/07/18 18:07:40
Done.
| |
41 Object.defineProperty(obj, 'length', {value: 999}); // special for JSFunction s. | |
42 } | |
43 | |
44 function minus18(x) { return x - 18; } | |
45 function id(x) { return x; } | |
46 | |
47 function printName(when, f) { | |
48 print(" " + when + ": name=" + f.name + ", length=" + f.length); | |
49 } | |
50 | |
51 (function ExportedFunctionTest() { | |
52 print("ExportedFunctionTest"); | |
53 | |
54 print(" instance 1, exporting"); | |
55 var builder = new WasmModuleBuilder(); | |
56 builder.addFunction("exp", kSig_i_i) | |
57 .addBody([ | |
58 kExprGetLocal, 0, | |
59 kExprCallFunction, 0]) | |
60 .exportAs("exp"); | |
61 let module1 = builder.toModule(); | |
62 let instance1 = new WebAssembly.Instance(module1); | |
63 let g = instance1.exports.exp; | |
64 | |
65 testProperties(g); | |
66 | |
67 // The WASM-internal fields of {g} are only inspected when {g} is | |
68 // used as an import into another instance. | |
69 print(" instance 2, importing"); | |
70 var builder = new WasmModuleBuilder(); | |
71 builder.addImport("imp", "func", kSig_i_i); | |
72 let module2 = builder.toModule(); | |
73 let instance2 = new WebAssembly.Instance(module2, {imp: {func: g}}); | |
74 | |
75 testProperties(g); | |
76 })(); | |
77 | |
78 (function ImportReexportChain() { | |
79 print("ImportReexportChain"); | |
80 | |
81 var f = id; | |
82 | |
83 for (let i = 0; i < 5; i++) { | |
84 let builder = new WasmModuleBuilder(); | |
85 builder.addImport("imp", "func", kSig_i_i); | |
86 builder.addExport("exp", 0); | |
87 let module = builder.toModule(); | |
88 let instance = new WebAssembly.Instance(module, {imp: {func: f}}); | |
89 let g = instance.exports.exp; | |
90 assertInstanceof(g, Function); | |
91 printName("before", g); | |
92 testProperties(g); | |
93 printName(" after", g); | |
94 | |
95 // The WASM-internal fields of {g} are only inspected when {g} is | |
96 // used as an import into another instance. Use {g} as the import | |
97 // the next time through the loop. | |
98 f = g; | |
99 } | |
100 })(); | |
101 | |
102 | |
103 (function ModuleTest() { | |
104 for (f of [x => (x + 19 + globalCounter), minus18]) { | |
105 print("ModuleTest"); | |
106 | |
107 let builder = new WasmModuleBuilder(); | |
108 builder.addImport("m", "f", kSig_i_i); | |
109 builder.addFunction("main", kSig_i_i) | |
110 .addBody([ | |
111 kExprGetLocal, 0, | |
112 kExprCallFunction, 0]) | |
113 .exportAs("main"); | |
114 builder.addMemory(1, 1, false) | |
115 .exportMemoryAs("memory") | |
116 | |
117 let module = builder.toModule(); | |
118 testProperties(module); | |
119 | |
120 for (let i = 0; i < 3; i++) { | |
121 print(" instance " + i); | |
122 let instance = new WebAssembly.Instance(module, {m: {f: f}}); | |
123 testProperties(instance); | |
124 | |
125 print(" memory " + i); | |
126 let m = instance.exports.memory; | |
127 assertInstanceof(m, WebAssembly.Memory); | |
128 testProperties(m); | |
129 | |
130 print(" function " + i); | |
131 let g = instance.exports.main; | |
132 assertInstanceof(g, Function); | |
133 printName("before", g); | |
134 testProperties(g); | |
135 printName(" after", g); | |
136 assertInstanceof(g, Function); | |
137 testProperties(g); | |
138 for (let j = 10; j < 15; j++) { | |
139 assertEquals(f(j), g(j)); | |
140 } | |
141 verifyHeap(); | |
142 // The WASM-internal fields of {g} are only inspected when {g} is | |
143 // used as an import into another instance. Use {g} as the import | |
144 // the next time through the loop. | |
145 f = g; | |
146 } | |
147 } | |
148 | |
149 })(); | |
150 | |
151 (function ConstructedTest() { | |
152 print("ConstructedTest"); | |
153 | |
154 var memory = undefined, table = undefined; | |
155 for (let i = 0; i < 4; i++) { | |
156 print(" iteration " + i); | |
157 | |
158 let m = new WebAssembly.Memory({initial: 1}); | |
159 let t = new WebAssembly.Table({element: "anyfunc", initial: 1}); | |
160 m.old = memory; | |
161 t.old = table; | |
162 | |
163 memory = m; | |
164 table = t; | |
165 testProperties(memory); | |
166 testProperties(table); | |
167 } | |
168 })(); | |
OLD | NEW |