89(function SectionsWithSameCustomName() {
90 const b = (new Builder()).Unknown("foo").End();
91 assert.throws(() => b.Unknown("foo"), Error, `Cannot have to sections with the same name "foo" and ID 0`);
92})();
93
94(function EmptyTypeSection() {
95 const b = (new Builder()).Type().End();
96 const j = JSON.parse(b.json());
97 assert.eq(j.section.length, 1);
98 assert.eq(j.section[0].name, "Type");
99 assert.eq(j.section[0].data.length, 0);
100})();
101
102(function TwoTypeSections() {
103 const b = (new Builder()).Type().End();
104 assert.throws(() => b.Type(), Error, `Cannot have to sections with the same name "Type" and ID 1`);
105})();
106
107(function SimpleTypeSection() {
108 const b = (new Builder()).Type()
109 .Func([])
110 .Func([], "void")
111 .Func([], "i32")
112 .Func([], "i64")
113 .Func([], "f32")
114 .Func([], "f64")
115 .Func(["i32", "i64", "f32", "f64"])
116 .End();
117 const j = JSON.parse(b.json());
118 assert.eq(j.section[0].data.length, 7);
119 assert.eq(j.section[0].data[0].params, []);
120 assert.eq(j.section[0].data[0].ret, "void");
121 assert.eq(j.section[0].data[1].params, []);
122 assert.eq(j.section[0].data[1].ret, "void");
123 assert.eq(j.section[0].data[2].params, []);
124 assert.eq(j.section[0].data[2].ret, "i32");
125 assert.eq(j.section[0].data[3].params, []);
126 assert.eq(j.section[0].data[3].ret, "i64");
127 assert.eq(j.section[0].data[4].params, []);
128 assert.eq(j.section[0].data[4].ret, "f32");
129 assert.eq(j.section[0].data[5].params, []);
130 assert.eq(j.section[0].data[5].ret, "f64");
131 assert.eq(j.section[0].data[6].params, ["i32", "i64", "f32", "f64"]);
132 assert.eq(j.section[0].data[6].ret, "void");
133})();
134
135(function EmptyImportSection() {
136 const b = (new Builder()).Import().End();
137 const j = JSON.parse(b.json());
138 assert.eq(j.section.length, 1);
139 assert.eq(j.section[0].name, "Import");
140 assert.eq(j.section[0].data.length, 0);
141})();
142
143(function ImportBeforeTypeSections() {
144 const b = (new Builder()).Import().End();
145 assert.throws(() => b.Type(), Error, `Bad section ordering: "Import" cannot precede "Type"`);
146})();
147
148(function ImportFunctionWithoutTypeSection() {
149 const i = (new Builder()).Import();
150 assert.throws(() => i.Function("foo", "bar", 0), Error, `Can't use type 0 if a type section isn't present`);
151})();
152
153(function ImportFunctionWithInvalidType() {
154 const i = (new Builder()).Type().End().Import();
155 assert.throws(() => i.Function("foo", "bar", 0), Error, `Type 0 doesn't exist in type section`);
156})();
157
158(function ImportFunction() {
159 const b = (new Builder())
160 .Type().Func([]).End()
161 .Import()
162 .Function("foo", "bar", 0)
163 .End();
164 const j = JSON.parse(b.json());
165 assert.eq(j.section[1].data.length, 1);
166 assert.eq(j.section[1].data[0].module, "foo");
167 assert.eq(j.section[1].data[0].field, "bar");
168 assert.eq(j.section[1].data[0].type, 0);
169 assert.eq(j.section[1].data[0].kind, "Function");
170})();
171
172(function ImportFunctionsWithExistingTypes() {
173 const b = (new Builder())
174 .Type()
175 .Func([])
176 .Func([], "i32")
177 .Func(["i64", "i32"])
178 .Func(["i64", "i64"])
179 .End()
180 .Import()
181 .Function("foo", "bar", { params: [] })
182 .Function("foo", "baz", { params: [], ret: "i32" })
183 .Function("foo", "boo", { params: ["i64", "i64"] })
184 .End();
185 const j = JSON.parse(b.json());
186 assert.eq(j.section[0].data.length, 4);
187 assert.eq(j.section[1].data.length, 3);
188 assert.eq(j.section[1].data[0].type, 0);
189 assert.eq(j.section[1].data[1].type, 1);
190 assert.eq(j.section[1].data[2].type, 3);
191})();
192
193(function ImportFunctionWithNewType() {
194 const b = (new Builder())
195 .Type().End()
196 .Import()
197 .Function("foo", "bar", { params: [] })
198 .Function("foo", "baz", { params: [], ret: "i32" })
199 .Function("foo", "boo", { params: ["i64", "i64"] })
200 .End();
201 const j = JSON.parse(b.json());
202 assert.eq(j.section[0].data.length, 3);
203 assert.eq(j.section[0].data[0].ret, "void");
204 assert.eq(j.section[0].data[0].params, []);
205 assert.eq(j.section[0].data[1].ret, "i32");
206 assert.eq(j.section[0].data[1].params, []);
207 assert.eq(j.section[0].data[2].ret, "void");
208 assert.eq(j.section[0].data[2].params, ["i64", "i64"]);
209})();
210
211(function EmptyExportSection() {
212 const b = (new Builder()).Export().End();
213 const j = JSON.parse(b.json());
214 assert.eq(j.section.length, 1);
215 assert.eq(j.section[0].name, "Export");
216 assert.eq(j.section[0].data.length, 0);
217})();
218
219(function ExportFunctionWithoutTypeSection() {
220 const e = (new Builder()).Export();
221 assert.throws(() => e.Function("foo", 0, 0), Error, `Can't use type 0 if a type section isn't present`);
222})();
223
224(function ExportFunctionWithInvalidType() {
225 const e = (new Builder()).Type().End().Export();
226 assert.throws(() => e.Function("foo", 0, 0), Error, `Type 0 doesn't exist in type section`);
227})();
228
229(function ExportAnImport() {
230 const b = (new Builder())
231 .Type().End()
232 .Import().Function("foo", "bar", { params: [] }).End()
233 .Export().Function("ExportAnImport", { module: "foo", field: "bar" }).End();
234 const j = JSON.parse(b.json());
235 assert.eq(j.section[2].name, "Export");
236 assert.eq(j.section[2].data.length, 1);
237 assert.eq(j.section[2].data[0].field, "ExportAnImport");
238 assert.eq(j.section[2].data[0].type, 0);
239 assert.eq(j.section[2].data[0].index, 0);
240 assert.eq(j.section[2].data[0].kind, "Function");
241})();
242
243(function ExportMismatchedImport() {
244 const e = (new Builder())
245 .Type().End()
246 .Import().Function("foo", "bar", { params: [] }).End()
247 .Export();
248 assert.throws(() => e.Function("foo", 0, { params: ["i32"] }), Error, `Re-exporting import "bar" as "foo" has mismatching type`);
249})();
250