(debug = false)
| 1194 | } |
| 1195 | |
| 1196 | toBuffer(debug = false) { |
| 1197 | let binary = new Binary; |
| 1198 | let wasm = this; |
| 1199 | |
| 1200 | // Add header |
| 1201 | binary.emit_header(); |
| 1202 | |
| 1203 | // Add type section |
| 1204 | if (wasm.types.length > 0) { |
| 1205 | if (debug) print('emitting types @ ' + binary.length); |
| 1206 | binary.emit_section(kTypeSectionCode, section => { |
| 1207 | let length_with_groups = wasm.types.length; |
| 1208 | for (let group of wasm.rec_groups) { |
| 1209 | length_with_groups -= group.size - 1; |
| 1210 | } |
| 1211 | section.emit_u32v(length_with_groups); |
| 1212 | |
| 1213 | let rec_group_index = 0; |
| 1214 | |
| 1215 | for (let i = 0; i < wasm.types.length; i++) { |
| 1216 | if (rec_group_index < wasm.rec_groups.length && |
| 1217 | wasm.rec_groups[rec_group_index].start == i) { |
| 1218 | section.emit_u8(kWasmRecursiveTypeGroupForm); |
| 1219 | section.emit_u32v(wasm.rec_groups[rec_group_index].size); |
| 1220 | rec_group_index++; |
| 1221 | } |
| 1222 | |
| 1223 | let type = wasm.types[i]; |
| 1224 | if (type.supertype != kNoSuperType) { |
| 1225 | section.emit_u8(type.is_final ? kWasmSubtypeFinalForm |
| 1226 | : kWasmSubtypeForm); |
| 1227 | section.emit_u8(1); // supertype count |
| 1228 | section.emit_u32v(type.supertype); |
| 1229 | } else if (!type.is_final) { |
| 1230 | section.emit_u8(kWasmSubtypeForm); |
| 1231 | section.emit_u8(0); // no supertypes |
| 1232 | } |
| 1233 | if (type instanceof WasmStruct) { |
| 1234 | section.emit_u8(kWasmStructTypeForm); |
| 1235 | section.emit_u32v(type.fields.length); |
| 1236 | for (let field of type.fields) { |
| 1237 | section.emit_type(field.type); |
| 1238 | section.emit_u8(field.mutability ? 1 : 0); |
| 1239 | } |
| 1240 | } else if (type instanceof WasmArray) { |
| 1241 | section.emit_u8(kWasmArrayTypeForm); |
| 1242 | section.emit_type(type.type); |
| 1243 | section.emit_u8(type.mutability ? 1 : 0); |
| 1244 | } else { |
| 1245 | section.emit_u8(kWasmFunctionTypeForm); |
| 1246 | section.emit_u32v(type.params.length); |
| 1247 | for (let param of type.params) { |
| 1248 | section.emit_type(param); |
| 1249 | } |
| 1250 | section.emit_u32v(type.results.length); |
| 1251 | for (let result of type.results) { |
| 1252 | section.emit_type(result); |
| 1253 | } |
no test coverage detected