How to use emit_u32v method in wpt

Best JavaScript code snippet using wpt

wasm_module_builder.js

Source:wasm_module_builder.js Github

copy

Full Screen

...19 this.push((val >> 8) & 0xff);20 this.push((val >> 16) & 0xff);21 this.push((val >> 24) & 0xff);22 }23 emit_u32v(val) {24 while (true) {25 let v = val & 0xff;26 val = val >>> 7;27 if (val == 0) {28 this.push(v);29 break;30 }31 this.push(v | 0x80);32 }33 }34 emit_bytes(data) {35 for (let i = 0; i < data.length; i++) {36 this.push(data[i] & 0xff);37 }38 }39 emit_string(string) {40 // When testing illegal names, we pass a byte array directly.41 if (string instanceof Array) {42 this.emit_u32v(string.length);43 this.emit_bytes(string);44 return;45 }46 // This is the hacky way to convert a JavaScript string to a UTF8 encoded47 // string only containing single-byte characters.48 let string_utf8 = unescape(encodeURIComponent(string));49 this.emit_u32v(string_utf8.length);50 for (let i = 0; i < string_utf8.length; i++) {51 this.emit_u8(string_utf8.charCodeAt(i));52 }53 }54 emit_header() {55 this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3,56 kWasmV0, kWasmV1, kWasmV2, kWasmV3);57 }58 emit_section(section_code, content_generator) {59 // Emit section name.60 this.emit_u8(section_code);61 // Emit the section to a temporary buffer: its full length isn't know yet.62 let section = new Binary;63 content_generator(section);64 // Emit section length.65 this.emit_u32v(section.length);66 // Copy the temporary buffer.67 for (var i = 0; i < section.length; i++) this.push(section[i]);68 }69}70class FunctionBodyBuilder extends Binary {71 get_local(index) {72 this.emit_u8(kExprGetLocal);73 this.emit_u32v(index);74 return this;75 }76 i32_const(k) {77 this.emit_u8(kExprI32Const);78 this.emit_u32v(k);79 return this;80 }81 i32_load(args) {82 let align = args != undefined && args.align != undefined ? args.align : 1;83 let offset =84 args != undefined && args.offset != undefined ? args.offset : 0;85 this.emit_u8(kExprI32LoadMem);86 this.emit_u32v(align);87 this.emit_u32v(offset);88 return this;89 }90 i32_store(args) {91 let align = args != undefined && args.align != undefined ? args.align : 1;92 let offset =93 args != undefined && args.offset != undefined ? args.offset : 0;94 this.emit_u8(kExprI32StoreMem);95 this.emit_u32v(align);96 this.emit_u32v(offset);97 return this;98 }99 grow_memory() {100 this.emit_u8(kExprGrowMemory);101 this.emit_u8(0);102 return this;103 }104 end() {105 this.emit_u8(kExprEnd);106 }107}108class WasmFunctionBuilder {109 constructor(module, name, type_index) {110 this.module = module;111 this.name = name;112 this.type_index = type_index;113 this.body = new FunctionBodyBuilder;114 }115 numLocalNames() {116 if (this.local_names === undefined) return 0;117 let num_local_names = 0;118 for (let loc_name of this.local_names) {119 if (loc_name !== undefined) ++num_local_names;120 }121 return num_local_names;122 }123 exportAs(name) {124 this.module.addExport(name, this.index);125 return this;126 }127 exportFunc() {128 this.exportAs(this.name);129 return this;130 }131 addBody(body) {132 for (let b of body) {133 if (typeof b !== 'number' || (b & (~0xFF)) !== 0 )134 throw new Error('invalid body (entries must be 8 bit values): ' + body);135 }136 this.body = body.slice();137 // Automatically add the end for the function block to the body.138 this.body.push(kExprEnd);139 return this;140 }141 addBodyWithEnd(body) {142 this.body = body;143 return this;144 }145 addLocals(locals, names) {146 this.locals = locals;147 this.local_names = names;148 return this;149 }150 end() {151 return this.module;152 }153}154class WasmGlobalBuilder {155 constructor(module, type, mutable) {156 this.module = module;157 this.type = type;158 this.mutable = mutable;159 this.init = 0;160 }161 exportAs(name) {162 this.module.exports.push({name: name, kind: kExternalGlobal,163 index: this.index});164 return this;165 }166}167class WasmModuleBuilder {168 constructor() {169 this.types = [];170 this.imports = [];171 this.exports = [];172 this.globals = [];173 this.exceptions = [];174 this.functions = [];175 this.function_table = [];176 this.function_table_length = 0;177 this.function_table_inits = [];178 this.segments = [];179 this.explicit = [];180 this.num_imported_funcs = 0;181 this.num_imported_globals = 0;182 return this;183 }184 addStart(start_index) {185 this.start_index = start_index;186 return this;187 }188 addMemory(min, max, exp) {189 this.memory = {min: min, max: max, exp: exp};190 return this;191 }192 addExplicitSection(bytes) {193 this.explicit.push(bytes);194 return this;195 }196 stringToBytes(name) {197 var result = new Binary();198 result.emit_u32v(name.length);199 for (var i = 0; i < name.length; i++) {200 result.emit_u8(name.charCodeAt(i));201 }202 return result;203 }204 addCustomSection(name, bytes) {205 name = this.stringToBytes(name);206 var length = new Binary();207 length.emit_u32v(name.length + bytes.length);208 this.explicit.push([0, ...length, ...name, ...bytes]);209 }210 addType(type) {211 this.types.push(type);212 var pl = type.params.length; // should have params213 var rl = type.results.length; // should have results214 return this.types.length - 1;215 }216 addGlobal(local_type, mutable) {217 let glob = new WasmGlobalBuilder(this, local_type, mutable);218 glob.index = this.globals.length + this.num_imported_globals;219 this.globals.push(glob);220 return glob;221 }222 addException(type) {223 if (type.results.length != 0)224 throw new Error('Invalid exception signature: ' + type);225 this.exceptions.push(type);226 return this.exceptions.length - 1;227 }228 addFunction(name, type) {229 let type_index = (typeof type) == "number" ? type : this.addType(type);230 let func = new WasmFunctionBuilder(this, name, type_index);231 func.index = this.functions.length + this.num_imported_funcs;232 this.functions.push(func);233 return func;234 }235 addImport(module = "", name, type) {236 let type_index = (typeof type) == "number" ? type : this.addType(type);237 this.imports.push({module: module, name: name, kind: kExternalFunction,238 type: type_index});239 return this.num_imported_funcs++;240 }241 addImportedGlobal(module = "", name, type) {242 let o = {module: module, name: name, kind: kExternalGlobal, type: type,243 mutable: false}244 this.imports.push(o);245 return this.num_imported_globals++;246 }247 addImportedMemory(module = "", name, initial = 0, maximum) {248 let o = {module: module, name: name, kind: kExternalMemory,249 initial: initial, maximum: maximum};250 this.imports.push(o);251 return this;252 }253 addImportedTable(module = "", name, initial, maximum) {254 let o = {module: module, name: name, kind: kExternalTable, initial: initial,255 maximum: maximum};256 this.imports.push(o);257 }258 addExport(name, index) {259 this.exports.push({name: name, kind: kExternalFunction, index: index});260 return this;261 }262 addExportOfKind(name, kind, index) {263 this.exports.push({name: name, kind: kind, index: index});264 return this;265 }266 addDataSegment(addr, data, is_global = false) {267 this.segments.push({addr: addr, data: data, is_global: is_global});268 return this.segments.length - 1;269 }270 exportMemoryAs(name) {271 this.exports.push({name: name, kind: kExternalMemory, index: 0});272 }273 addFunctionTableInit(base, is_global, array, is_import = false) {274 this.function_table_inits.push({base: base, is_global: is_global,275 array: array});276 if (!is_global) {277 var length = base + array.length;278 if (length > this.function_table_length && !is_import) {279 this.function_table_length = length;280 }281 }282 return this;283 }284 appendToTable(array) {285 for (let n of array) {286 if (typeof n != 'number')287 throw new Error('invalid table (entries have to be numbers): ' + array);288 }289 return this.addFunctionTableInit(this.function_table.length, false, array);290 }291 setFunctionTableLength(length) {292 this.function_table_length = length;293 return this;294 }295 setName(name) {296 this.name = name;297 return this;298 }299 toArray(debug = false) {300 let binary = new Binary;301 let wasm = this;302 // Add header303 binary.emit_header();304 // Add type section305 if (wasm.types.length > 0) {306 if (debug) print("emitting types @ " + binary.length);307 binary.emit_section(kTypeSectionCode, section => {308 section.emit_u32v(wasm.types.length);309 for (let type of wasm.types) {310 section.emit_u8(kWasmFunctionTypeForm);311 section.emit_u32v(type.params.length);312 for (let param of type.params) {313 section.emit_u8(param);314 }315 section.emit_u32v(type.results.length);316 for (let result of type.results) {317 section.emit_u8(result);318 }319 }320 });321 }322 // Add imports section323 if (wasm.imports.length > 0) {324 if (debug) print("emitting imports @ " + binary.length);325 binary.emit_section(kImportSectionCode, section => {326 section.emit_u32v(wasm.imports.length);327 for (let imp of wasm.imports) {328 section.emit_string(imp.module);329 section.emit_string(imp.name || '');330 section.emit_u8(imp.kind);331 if (imp.kind == kExternalFunction) {332 section.emit_u32v(imp.type);333 } else if (imp.kind == kExternalGlobal) {334 section.emit_u32v(imp.type);335 section.emit_u8(imp.mutable);336 } else if (imp.kind == kExternalMemory) {337 var has_max = (typeof imp.maximum) != "undefined";338 section.emit_u8(has_max ? 1 : 0); // flags339 section.emit_u32v(imp.initial); // initial340 if (has_max) section.emit_u32v(imp.maximum); // maximum341 } else if (imp.kind == kExternalTable) {342 section.emit_u8(kWasmAnyFunctionTypeForm);343 var has_max = (typeof imp.maximum) != "undefined";344 section.emit_u8(has_max ? 1 : 0); // flags345 section.emit_u32v(imp.initial); // initial346 if (has_max) section.emit_u32v(imp.maximum); // maximum347 } else {348 throw new Error("unknown/unsupported import kind " + imp.kind);349 }350 }351 });352 }353 // Add functions declarations354 if (wasm.functions.length > 0) {355 if (debug) print("emitting function decls @ " + binary.length);356 binary.emit_section(kFunctionSectionCode, section => {357 section.emit_u32v(wasm.functions.length);358 for (let func of wasm.functions) {359 section.emit_u32v(func.type_index);360 }361 });362 }363 // Add function_table.364 if (wasm.function_table_length > 0) {365 if (debug) print("emitting table @ " + binary.length);366 binary.emit_section(kTableSectionCode, section => {367 section.emit_u8(1); // one table entry368 section.emit_u8(kWasmAnyFunctionTypeForm);369 section.emit_u8(1);370 section.emit_u32v(wasm.function_table_length);371 section.emit_u32v(wasm.function_table_length);372 });373 }374 // Add memory section375 if (wasm.memory !== undefined) {376 if (debug) print("emitting memory @ " + binary.length);377 binary.emit_section(kMemorySectionCode, section => {378 section.emit_u8(1); // one memory entry379 const has_max = wasm.memory.max !== undefined;380 section.emit_u32v(has_max ? kResizableMaximumFlag : 0);381 section.emit_u32v(wasm.memory.min);382 if (has_max) section.emit_u32v(wasm.memory.max);383 });384 }385 // Add global section.386 if (wasm.globals.length > 0) {387 if (debug) print ("emitting globals @ " + binary.length);388 binary.emit_section(kGlobalSectionCode, section => {389 section.emit_u32v(wasm.globals.length);390 for (let global of wasm.globals) {391 section.emit_u8(global.type);392 section.emit_u8(global.mutable);393 if ((typeof global.init_index) == "undefined") {394 // Emit a constant initializer.395 switch (global.type) {396 case kWasmI32:397 section.emit_u8(kExprI32Const);398 section.emit_u32v(global.init);399 break;400 case kWasmI64:401 section.emit_u8(kExprI64Const);402 section.emit_u32v(global.init);403 break;404 case kWasmF32:405 section.emit_u8(kExprF32Const);406 f32_view[0] = global.init;407 section.emit_u8(byte_view[0]);408 section.emit_u8(byte_view[1]);409 section.emit_u8(byte_view[2]);410 section.emit_u8(byte_view[3]);411 break;412 case kWasmF64:413 section.emit_u8(kExprF64Const);414 f64_view[0] = global.init;415 section.emit_u8(byte_view[0]);416 section.emit_u8(byte_view[1]);417 section.emit_u8(byte_view[2]);418 section.emit_u8(byte_view[3]);419 section.emit_u8(byte_view[4]);420 section.emit_u8(byte_view[5]);421 section.emit_u8(byte_view[6]);422 section.emit_u8(byte_view[7]);423 break;424 }425 } else {426 // Emit a global-index initializer.427 section.emit_u8(kExprGetGlobal);428 section.emit_u32v(global.init_index);429 }430 section.emit_u8(kExprEnd); // end of init expression431 }432 });433 }434 // Add export table.435 var mem_export = (wasm.memory !== undefined && wasm.memory.exp);436 var exports_count = wasm.exports.length + (mem_export ? 1 : 0);437 if (exports_count > 0) {438 if (debug) print("emitting exports @ " + binary.length);439 binary.emit_section(kExportSectionCode, section => {440 section.emit_u32v(exports_count);441 for (let exp of wasm.exports) {442 section.emit_string(exp.name);443 section.emit_u8(exp.kind);444 section.emit_u32v(exp.index);445 }446 if (mem_export) {447 section.emit_string("memory");448 section.emit_u8(kExternalMemory);449 section.emit_u8(0);450 }451 });452 }453 // Add start function section.454 if (wasm.start_index !== undefined) {455 if (debug) print("emitting start function @ " + binary.length);456 binary.emit_section(kStartSectionCode, section => {457 section.emit_u32v(wasm.start_index);458 });459 }460 // Add table elements.461 if (wasm.function_table_inits.length > 0) {462 if (debug) print("emitting table @ " + binary.length);463 binary.emit_section(kElementSectionCode, section => {464 var inits = wasm.function_table_inits;465 section.emit_u32v(inits.length);466 for (let init of inits) {467 section.emit_u8(0); // table index468 if (init.is_global) {469 section.emit_u8(kExprGetGlobal);470 } else {471 section.emit_u8(kExprI32Const);472 }473 section.emit_u32v(init.base);474 section.emit_u8(kExprEnd);475 section.emit_u32v(init.array.length);476 for (let index of init.array) {477 section.emit_u32v(index);478 }479 }480 });481 }482 // Add exceptions.483 if (wasm.exceptions.length > 0) {484 if (debug) print("emitting exceptions @ " + binary.length);485 binary.emit_section(kExceptionSectionCode, section => {486 section.emit_u32v(wasm.exceptions.length);487 for (let type of wasm.exceptions) {488 section.emit_u32v(type.params.length);489 for (let param of type.params) {490 section.enit_u8(param);491 }492 }493 });494 }495 // Add function bodies.496 if (wasm.functions.length > 0) {497 // emit function bodies498 if (debug) print("emitting code @ " + binary.length);499 binary.emit_section(kCodeSectionCode, section => {500 section.emit_u32v(wasm.functions.length);501 for (let func of wasm.functions) {502 // Function body length will be patched later.503 let local_decls = [];504 let l = func.locals;505 if (l !== undefined) {506 let local_decls_count = 0;507 if (l.i32_count > 0) {508 local_decls.push({count: l.i32_count, type: kWasmI32});509 }510 if (l.i64_count > 0) {511 local_decls.push({count: l.i64_count, type: kWasmI64});512 }513 if (l.f32_count > 0) {514 local_decls.push({count: l.f32_count, type: kWasmF32});515 }516 if (l.f64_count > 0) {517 local_decls.push({count: l.f64_count, type: kWasmF64});518 }519 }520 let header = new Binary;521 header.emit_u32v(local_decls.length);522 for (let decl of local_decls) {523 header.emit_u32v(decl.count);524 header.emit_u8(decl.type);525 }526 section.emit_u32v(header.length + func.body.length);527 section.emit_bytes(header);528 section.emit_bytes(func.body);529 }530 });531 }532 // Add data segments.533 if (wasm.segments.length > 0) {534 if (debug) print("emitting data segments @ " + binary.length);535 binary.emit_section(kDataSectionCode, section => {536 section.emit_u32v(wasm.segments.length);537 for (let seg of wasm.segments) {538 section.emit_u8(0); // linear memory index 0539 if (seg.is_global) {540 // initializer is a global variable541 section.emit_u8(kExprGetGlobal);542 section.emit_u32v(seg.addr);543 } else {544 // initializer is a constant545 section.emit_u8(kExprI32Const);546 section.emit_u32v(seg.addr);547 }548 section.emit_u8(kExprEnd);549 section.emit_u32v(seg.data.length);550 section.emit_bytes(seg.data);551 }552 });553 }554 // Add any explicitly added sections555 for (let exp of wasm.explicit) {556 if (debug) print("emitting explicit @ " + binary.length);557 binary.emit_bytes(exp);558 }559 // Add names.560 let num_function_names = 0;561 let num_functions_with_local_names = 0;562 for (let func of wasm.functions) {563 if (func.name !== undefined) ++num_function_names;564 if (func.numLocalNames() > 0) ++num_functions_with_local_names;565 }566 if (num_function_names > 0 || num_functions_with_local_names > 0 ||567 wasm.name !== undefined) {568 if (debug) print('emitting names @ ' + binary.length);569 binary.emit_section(kUnknownSectionCode, section => {570 section.emit_string('name');571 // Emit module name.572 if (wasm.name !== undefined) {573 section.emit_section(kModuleNameCode, name_section => {574 name_section.emit_string(wasm.name);575 });576 }577 // Emit function names.578 if (num_function_names > 0) {579 section.emit_section(kFunctionNamesCode, name_section => {580 name_section.emit_u32v(num_function_names);581 for (let func of wasm.functions) {582 if (func.name === undefined) continue;583 name_section.emit_u32v(func.index);584 name_section.emit_string(func.name);585 }586 });587 }588 // Emit local names.589 if (num_functions_with_local_names > 0) {590 section.emit_section(kLocalNamesCode, name_section => {591 name_section.emit_u32v(num_functions_with_local_names);592 for (let func of wasm.functions) {593 if (func.numLocalNames() == 0) continue;594 name_section.emit_u32v(func.index);595 name_section.emit_u32v(func.numLocalNames());596 for (let i = 0; i < func.local_names.length; ++i) {597 if (func.local_names[i] === undefined) continue;598 name_section.emit_u32v(i);599 name_section.emit_string(func.local_names[i]);600 }601 }602 });603 }604 });605 }606 return binary;607 }608 toBuffer(debug = false) {609 let bytes = this.toArray(debug);610 let buffer = new ArrayBuffer(bytes.length);611 let view = new Uint8Array(buffer);612 for (let i = 0; i < bytes.length; i++) {...

Full Screen

Full Screen

wasm-module-builder.js

Source:wasm-module-builder.js Github

copy

Full Screen

...19 this.push((val >> 8) & 0xff);20 this.push((val >> 16) & 0xff);21 this.push((val >> 24) & 0xff);22 }23 emit_u32v(val) {24 while (true) {25 let v = val & 0xff;26 val = val >>> 7;27 if (val == 0) {28 this.push(v);29 break;30 }31 this.push(v | 0x80);32 }33 }34 emit_bytes(data) {35 for (let i = 0; i < data.length; i++) {36 this.push(data[i] & 0xff);37 }38 }39 emit_string(string) {40 // When testing illegal names, we pass a byte array directly.41 if (string instanceof Array) {42 this.emit_u32v(string.length);43 this.emit_bytes(string);44 return;45 }46 // This is the hacky way to convert a JavaScript string to a UTF8 encoded47 // string only containing single-byte characters.48 let string_utf8 = unescape(encodeURIComponent(string));49 this.emit_u32v(string_utf8.length);50 for (let i = 0; i < string_utf8.length; i++) {51 this.emit_u8(string_utf8.charCodeAt(i));52 }53 }54 emit_header() {55 this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3,56 kWasmV0, kWasmV1, kWasmV2, kWasmV3);57 }58 emit_section(section_code, content_generator) {59 // Emit section name.60 this.emit_u8(section_code);61 // Emit the section to a temporary buffer: its full length isn't know yet.62 let section = new Binary;63 content_generator(section);64 // Emit section length.65 this.emit_u32v(section.length);66 // Copy the temporary buffer.67 for (const sectionByte of section) {68 this.push(sectionByte);69 }70 }71}72class WasmFunctionBuilder {73 constructor(module, name, type_index) {74 this.module = module;75 this.name = name;76 this.type_index = type_index;77 this.body = [];78 }79 exportAs(name) {80 this.module.addExport(name, this.index);81 return this;82 }83 exportFunc() {84 this.exportAs(this.name);85 return this;86 }87 addBody(body) {88 for (let b of body) {89 if (typeof b != 'number')90 throw new Error('invalid body (entries have to be numbers): ' + body);91 }92 this.body = body.slice();93 // Automatically add the end for the function block to the body.94 this.body.push(kExprEnd);95 return this;96 }97 addLocals(locals) {98 this.locals = locals;99 return this;100 }101 end() {102 this.body.push(kExprEnd);103 return this.module;104 }105}106class WasmGlobalBuilder {107 constructor(module, type, mutable) {108 this.module = module;109 this.type = type;110 this.mutable = mutable;111 this.init = 0;112 }113 exportAs(name) {114 this.module.exports.push({name: name, kind: kExternalGlobal,115 index: this.index});116 return this;117 }118}119class WasmModuleBuilder {120 constructor() {121 this.types = [];122 this.imports = [];123 this.exports = [];124 this.globals = [];125 this.functions = [];126 this.function_table = [];127 this.function_table_length = 0;128 this.function_table_inits = [];129 this.segments = [];130 this.explicit = [];131 this.num_imported_funcs = 0;132 this.num_imported_globals = 0;133 return this;134 }135 addStart(start_index) {136 this.start_index = start_index;137 return this;138 }139 addMemory(min, max, exp) {140 this.memory = {min: min, max: max, exp: exp};141 return this;142 }143 addExplicitSection(bytes) {144 this.explicit.push(bytes);145 return this;146 }147 stringToBytes(name) {148 var result = new Binary();149 result.emit_u32v(name.length);150 for (var i = 0; i < name.length; i++) {151 result.emit_u8(name.charCodeAt(i));152 }153 return result;154 }155 addCustomSection(name, bytes) {156 name = this.stringToBytes(name);157 var length = new Binary();158 length.emit_u32v(name.length + bytes.length);159 this.explicit.push([0, ...length, ...name, ...bytes]);160 }161 addType(type) {162 // TODO: canonicalize types?163 this.types.push(type);164 return this.types.length - 1;165 }166 addGlobal(local_type, mutable) {167 let glob = new WasmGlobalBuilder(this, local_type, mutable);168 glob.index = this.globals.length + this.num_imported_globals;169 this.globals.push(glob);170 return glob;171 }172 addFunction(name, type) {173 let type_index = (typeof type) == "number" ? type : this.addType(type);174 let func = new WasmFunctionBuilder(this, name, type_index);175 func.index = this.functions.length + this.num_imported_funcs;176 this.functions.push(func);177 return func;178 }179 addImport(module = "", name, type) {180 let type_index = (typeof type) == "number" ? type : this.addType(type);181 this.imports.push({module: module, name: name, kind: kExternalFunction,182 type: type_index});183 return this.num_imported_funcs++;184 }185 addImportedGlobal(module = "", name, type) {186 let o = {module: module, name: name, kind: kExternalGlobal, type: type,187 mutable: false}188 this.imports.push(o);189 return this.num_imported_globals++;190 }191 addImportedMemory(module = "", name, initial = 0, maximum) {192 let o = {module: module, name: name, kind: kExternalMemory,193 initial: initial, maximum: maximum};194 this.imports.push(o);195 return this;196 }197 addImportedTable(module = "", name, initial, maximum) {198 let o = {module: module, name: name, kind: kExternalTable, initial: initial,199 maximum: maximum};200 this.imports.push(o);201 }202 addExport(name, index) {203 this.exports.push({name: name, kind: kExternalFunction, index: index});204 return this;205 }206 addExportOfKind(name, kind, index) {207 this.exports.push({name: name, kind: kind, index: index});208 return this;209 }210 addDataSegment(addr, data, is_global = false) {211 this.segments.push({addr: addr, data: data, is_global: is_global});212 return this.segments.length - 1;213 }214 exportMemoryAs(name) {215 this.exports.push({name: name, kind: kExternalMemory, index: 0});216 }217 addFunctionTableInit(base, is_global, array, is_import = false) {218 this.function_table_inits.push({base: base, is_global: is_global,219 array: array});220 if (!is_global) {221 var length = base + array.length;222 if (length > this.function_table_length && !is_import) {223 this.function_table_length = length;224 }225 }226 return this;227 }228 appendToTable(array) {229 for (let n of array) {230 if (typeof n != 'number')231 throw new Error('invalid table (entries have to be numbers): ' + array);232 }233 return this.addFunctionTableInit(this.function_table.length, false, array);234 }235 setFunctionTableLength(length) {236 this.function_table_length = length;237 return this;238 }239 toArray(debug = false) {240 let binary = new Binary;241 let wasm = this;242 // Add header243 binary.emit_header();244 // Add type section245 if (wasm.types.length > 0) {246 if (debug) print("emitting types @ " + binary.length);247 binary.emit_section(kTypeSectionCode, section => {248 section.emit_u32v(wasm.types.length);249 for (let type of wasm.types) {250 section.emit_u8(kWasmFunctionTypeForm);251 section.emit_u32v(type.params.length);252 for (let param of type.params) {253 section.emit_u8(param);254 }255 section.emit_u32v(type.results.length);256 for (let result of type.results) {257 section.emit_u8(result);258 }259 }260 });261 }262 // Add imports section263 if (wasm.imports.length > 0) {264 if (debug) print("emitting imports @ " + binary.length);265 binary.emit_section(kImportSectionCode, section => {266 section.emit_u32v(wasm.imports.length);267 for (let imp of wasm.imports) {268 section.emit_string(imp.module);269 section.emit_string(imp.name || '');270 section.emit_u8(imp.kind);271 if (imp.kind == kExternalFunction) {272 section.emit_u32v(imp.type);273 } else if (imp.kind == kExternalGlobal) {274 section.emit_u32v(imp.type);275 section.emit_u8(imp.mutable);276 } else if (imp.kind == kExternalMemory) {277 var has_max = (typeof imp.maximum) != "undefined";278 section.emit_u8(has_max ? 1 : 0); // flags279 section.emit_u32v(imp.initial); // initial280 if (has_max) section.emit_u32v(imp.maximum); // maximum281 } else if (imp.kind == kExternalTable) {282 section.emit_u8(kWasmAnyFunctionTypeForm);283 var has_max = (typeof imp.maximum) != "undefined";284 section.emit_u8(has_max ? 1 : 0); // flags285 section.emit_u32v(imp.initial); // initial286 if (has_max) section.emit_u32v(imp.maximum); // maximum287 } else {288 throw new Error("unknown/unsupported import kind " + imp.kind);289 }290 }291 });292 }293 // Add functions declarations294 let num_function_names = 0;295 let names = false;296 if (wasm.functions.length > 0) {297 if (debug) print("emitting function decls @ " + binary.length);298 binary.emit_section(kFunctionSectionCode, section => {299 section.emit_u32v(wasm.functions.length);300 for (let func of wasm.functions) {301 if (func.name !== undefined) {302 ++num_function_names;303 }304 section.emit_u32v(func.type_index);305 }306 });307 }308 // Add function_table.309 if (wasm.function_table_length > 0) {310 if (debug) print("emitting table @ " + binary.length);311 binary.emit_section(kTableSectionCode, section => {312 section.emit_u8(1); // one table entry313 section.emit_u8(kWasmAnyFunctionTypeForm);314 section.emit_u8(1);315 section.emit_u32v(wasm.function_table_length);316 section.emit_u32v(wasm.function_table_length);317 });318 }319 // Add memory section320 if (wasm.memory !== undefined) {321 if (debug) print("emitting memory @ " + binary.length);322 binary.emit_section(kMemorySectionCode, section => {323 section.emit_u8(1); // one memory entry324 const has_max = wasm.memory.max !== undefined;325 section.emit_u32v(has_max ? kResizableMaximumFlag : 0);326 section.emit_u32v(wasm.memory.min);327 if (has_max) section.emit_u32v(wasm.memory.max);328 });329 }330 // Add global section.331 if (wasm.globals.length > 0) {332 if (debug) print ("emitting globals @ " + binary.length);333 binary.emit_section(kGlobalSectionCode, section => {334 section.emit_u32v(wasm.globals.length);335 for (let global of wasm.globals) {336 section.emit_u8(global.type);337 section.emit_u8(global.mutable);338 if ((typeof global.init_index) == "undefined") {339 // Emit a constant initializer.340 switch (global.type) {341 case kWasmI32:342 section.emit_u8(kExprI32Const);343 section.emit_u32v(global.init);344 break;345 case kWasmI64:346 section.emit_u8(kExprI64Const);347 section.emit_u32v(global.init);348 break;349 case kWasmF32:350 section.emit_u8(kExprF32Const);351 f32_view[0] = global.init;352 section.emit_u8(byte_view[0]);353 section.emit_u8(byte_view[1]);354 section.emit_u8(byte_view[2]);355 section.emit_u8(byte_view[3]);356 break;357 case kWasmF64:358 section.emit_u8(kExprF64Const);359 f64_view[0] = global.init;360 section.emit_u8(byte_view[0]);361 section.emit_u8(byte_view[1]);362 section.emit_u8(byte_view[2]);363 section.emit_u8(byte_view[3]);364 section.emit_u8(byte_view[4]);365 section.emit_u8(byte_view[5]);366 section.emit_u8(byte_view[6]);367 section.emit_u8(byte_view[7]);368 break;369 }370 } else {371 // Emit a global-index initializer.372 section.emit_u8(kExprGetGlobal);373 section.emit_u32v(global.init_index);374 }375 section.emit_u8(kExprEnd); // end of init expression376 }377 });378 }379 // Add export table.380 var mem_export = (wasm.memory !== undefined && wasm.memory.exp);381 var exports_count = wasm.exports.length + (mem_export ? 1 : 0);382 if (exports_count > 0) {383 if (debug) print("emitting exports @ " + binary.length);384 binary.emit_section(kExportSectionCode, section => {385 section.emit_u32v(exports_count);386 for (let exp of wasm.exports) {387 section.emit_string(exp.name);388 section.emit_u8(exp.kind);389 section.emit_u32v(exp.index);390 }391 if (mem_export) {392 section.emit_string("memory");393 section.emit_u8(kExternalMemory);394 section.emit_u8(0);395 }396 });397 }398 // Add start function section.399 if (wasm.start_index !== undefined) {400 if (debug) print("emitting start function @ " + binary.length);401 binary.emit_section(kStartSectionCode, section => {402 section.emit_u32v(wasm.start_index);403 });404 }405 // Add table elements.406 if (wasm.function_table_inits.length > 0) {407 if (debug) print("emitting table @ " + binary.length);408 binary.emit_section(kElementSectionCode, section => {409 var inits = wasm.function_table_inits;410 section.emit_u32v(inits.length);411 for (let init of inits) {412 section.emit_u8(0); // table index413 if (init.is_global) {414 section.emit_u8(kExprGetGlobal);415 } else {416 section.emit_u8(kExprI32Const);417 }418 section.emit_u32v(init.base);419 section.emit_u8(kExprEnd);420 section.emit_u32v(init.array.length);421 for (let index of init.array) {422 section.emit_u32v(index);423 }424 }425 });426 }427 // Add function bodies.428 if (wasm.functions.length > 0) {429 // emit function bodies430 if (debug) print("emitting code @ " + binary.length);431 binary.emit_section(kCodeSectionCode, section => {432 section.emit_u32v(wasm.functions.length);433 for (let func of wasm.functions) {434 // Function body length will be patched later.435 let local_decls = [];436 let l = func.locals;437 if (l !== undefined) {438 let local_decls_count = 0;439 if (l.i32_count > 0) {440 local_decls.push({count: l.i32_count, type: kWasmI32});441 }442 if (l.i64_count > 0) {443 local_decls.push({count: l.i64_count, type: kWasmI64});444 }445 if (l.f32_count > 0) {446 local_decls.push({count: l.f32_count, type: kWasmF32});447 }448 if (l.f64_count > 0) {449 local_decls.push({count: l.f64_count, type: kWasmF64});450 }451 }452 let header = new Binary;453 header.emit_u32v(local_decls.length);454 for (let decl of local_decls) {455 header.emit_u32v(decl.count);456 header.emit_u8(decl.type);457 }458 section.emit_u32v(header.length + func.body.length);459 section.emit_bytes(header);460 section.emit_bytes(func.body);461 }462 });463 }464 // Add data segments.465 if (wasm.segments.length > 0) {466 if (debug) print("emitting data segments @ " + binary.length);467 binary.emit_section(kDataSectionCode, section => {468 section.emit_u32v(wasm.segments.length);469 for (let seg of wasm.segments) {470 section.emit_u8(0); // linear memory index 0471 if (seg.is_global) {472 // initializer is a global variable473 section.emit_u8(kExprGetGlobal);474 section.emit_u32v(seg.addr);475 } else {476 // initializer is a constant477 section.emit_u8(kExprI32Const);478 section.emit_u32v(seg.addr);479 }480 section.emit_u8(kExprEnd);481 section.emit_u32v(seg.data.length);482 section.emit_bytes(seg.data);483 }484 });485 }486 // Add any explicitly added sections487 for (let exp of wasm.explicit) {488 if (debug) print("emitting explicit @ " + binary.length);489 binary.emit_bytes(exp);490 }491 // Add function names.492 if (num_function_names > 0) {493 if (debug) print('emitting names @ ' + binary.length);494 binary.emit_section(kUnknownSectionCode, section => {495 section.emit_string('name');496 section.emit_section(kFunctionNamesCode, name_section => {497 name_section.emit_u32v(num_function_names);498 for (let func of wasm.functions) {499 if (func.name === undefined) continue;500 name_section.emit_u32v(func.index);501 name_section.emit_string(func.name);502 }503 });504 });505 }506 return binary;507 }508 toBuffer(debug = false) {509 let bytes = this.toArray(debug);510 let buffer = new ArrayBuffer(bytes.length);511 let view = new Uint8Array(buffer);512 for (let i = 0; i < bytes.length; i++) {513 let val = bytes[i];514 if ((typeof val) == "string") val = val.charCodeAt(0);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2wpt.emit_u32v(1);3wpt.emit_u32v(2);4wpt.emit_u32v(3);5wpt.emit_u32v(4);6wpt.emit_u32v(5);7wpt.emit_u32v(6);8wpt.emit_u32v(7);9wpt.emit_u32v(8);10wpt.emit_u32v(9);11wpt.emit_u32v(10);12wpt.emit_u32v(11);13wpt.emit_u32v(12);14wpt.emit_u32v(13);15wpt.emit_u32v(14);16wpt.emit_u32v(15);17wpt.emit_u32v(16);18wpt.emit_u32v(17);19wpt.emit_u32v(18);20wpt.emit_u32v(19);21wpt.emit_u32v(20);22wpt.emit_u32v(21);23wpt.emit_u32v(22);24wpt.emit_u32v(23);25wpt.emit_u32v(24);26wpt.emit_u32v(25);27wpt.emit_u32v(26);28wpt.emit_u32v(27);29wpt.emit_u32v(28);30wpt.emit_u32v(29);31wpt.emit_u32v(30);32wpt.emit_u32v(31);33wpt.emit_u32v(32);34wpt.emit_u32v(33);35wpt.emit_u32v(34);36wpt.emit_u32v(35);37wpt.emit_u32v(36);38wpt.emit_u32v(37);39wpt.emit_u32v(38);40wpt.emit_u32v(39);41wpt.emit_u32v(40);42wpt.emit_u32v(41);43wpt.emit_u32v(42);44wpt.emit_u32v(43);45wpt.emit_u32v(44);46wpt.emit_u32v(45);47wpt.emit_u32v(46);48wpt.emit_u32v(47);49wpt.emit_u32v(48);50wpt.emit_u32v(49);51wpt.emit_u32v(50);52wpt.emit_u32v(51);53wpt.emit_u32v(52);54wpt.emit_u32v(53);55wpt.emit_u32v(54);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var w = new wpt();3w.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);4var wpt = require('wpt');5var w = new wpt();6w.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);7var wpt = require('wpt');8var w = new wpt();9w.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);10var wpt = require('wpt');11var w = new wpt();12w.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);13var wpt = require('wpt');14var w = new wpt();15w.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);16var wpt = require('wpt');17var w = new wpt();18w.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var wpt = new wpt.Wpt();3wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);4wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);5wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);6wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);7wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);8wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);9wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);10wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);11wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);12wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);13wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);14var wpt = require('wpt');15var wpt = new wpt.Wpt();16wpt.emit_u32v(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);17wpt.emit_u32v(

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wiring-pi-test');2wpt.setup('wpi');3var pin = 1;4wpt.pinMode(pin, wpt.OUTPUT);5wpt.emit_u32v(pin, 0x00000000, 0x00000000, 0x00000000, 0x00000000);6var wpi = require('wiring-pi');7var u32v = require('u32v');8var u32vModule = require('u32v/build/Release/u32v');9var emit_u32v = function(pin, data0, data1, data2, data3) {10 var data = u32vModule.u32v(data0, data1, data2, data3);11 wpi.wiringPiI2CWriteReg32(pin, 0, data);12};13module.exports = {14};15"dependencies": {16}17var wpi = require('wiring-pi');18var u32v = require('u32v');19var u32vModule = require('u32v/build/Release/u32v');20var emit_u32v = function(pin, data0, data1, data2, data3) {21 var data = u32vModule.u32v(data0, data1, data2, data3);22 wpi.wiringPiI2CWriteReg32(pin, 0, data);23};24module.exports = {25};26var wpt = require('wiring-pi-test');27wpt.setup('wpi');28var pin = 1;29wpt.pinMode(pin, wpt.OUTPUT);30wpt.emit_u32v(pin

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wiring-pi').wiringPiI2CSetupInterface(0x23, 0x01);2wiringPiI2CWriteReg8(wpt, 0x80, 0x03);3wiringPiI2CWriteReg8(wpt, 0x81, 0x02);4wiringPiI2CWriteReg8(wpt, 0x82, 0x01);5wiringPiI2CWriteReg8(wpt, 0x8A, 0xFF);6wiringPiI2CWriteReg8(wpt, 0x8B, 0xFF);7wiringPiI2CWriteReg8(wpt, 0x8C, 0xFF);8wiringPiI2CWriteReg8(wpt, 0x00, 0x00);9wiringPiI2CWriteReg8(wpt, 0x01, 0x00);10wiringPiI2CWriteReg8(wpt, 0x02, 0x00);11var wpt = require('wiring-pi').wiringPiI2CSetupInterface(0x23, 0x01);12wiringPiI2CWriteReg8(wpt, 0x80, 0x03);13wiringPiI2CWriteReg8(wpt, 0x81, 0x02);14wiringPiI2CWriteReg8(wpt, 0x82, 0x01);15wiringPiI2CWriteReg8(wpt, 0x8A, 0xFF);16wiringPiI2CWriteReg8(wpt, 0x8B, 0xFF);17wiringPiI2CWriteReg8(wpt, 0x8C, 0xFF);18wiringPiI2CWriteReg8(wpt, 0x00, 0x00);19wiringPiI2CWriteReg8(wpt, 0x01, 0x00);20wiringPiI2CWriteReg8(wpt, 0x02, 0x00);21var wpt = require('

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var wptu32 = new wpt.U32v();3wptu32.emit_u32v(1234);4wptu32.emit_u32v(1234567890);5wptu32.emit_u32v(0);6wptu32.emit_u32v(4294967295);7wptu32.emit_u32v(2147483648);8wptu32.emit_u32v(2147483647);9wptu32.emit_u32v(2147483649);10wptu32.emit_u32v(4294967296);11wptu32.emit_u32v(4294967297);12wptu32.emit_u32v(9223372036854775807);13wptu32.emit_u32v(9223372036854775808);14wptu32.emit_u32v(18446744073709551615);15wptu32.emit_u32v(18446744073709551616);16wptu32.emit_u32v(18446744073709551617);17wptu32.emit_u32v(18446744073709551618);18wptu32.emit_u32v(18446744073709551619);19wptu32.emit_u32v(18446744073709551620);20wptu32.emit_u32v(18446744073709551621);21wptu32.emit_u32v(18446744073709551622);22wptu32.emit_u32v(18446744073709551623);23wptu32.emit_u32v(18446744073709551624);24wptu32.emit_u32v(18446744073709551625);25wptu32.emit_u32v(18446744073709551626);26wptu32.emit_u32v(18446744073709551627);27wptu32.emit_u32v(18446744073709551628);28wptu32.emit_u32v(18446744073709551629);29wptu32.emit_u32v(18446744073709551630);30wptu32.emit_u32v(18446744073709551631);31wptu32.emit_u32v(18446744073709551632);32wptu32.emit_u32v(184467440737095

Full Screen

Using AI Code Generation

copy

Full Screen

1emit_u32v(0x00000001, 4);2emit_u32v(0x00000002, 2);3emit_u32v(0x00000003, 1);4emit_u32v(0x00000004, 2);5emit_u32v(0x00000005, 4);6emit_u32v(0x00000006, 1);7emit_u32v(0x00000007, 4);8emit_u32v(0x00000008, 2);9emit_u32v(0x00000009, 1);10emit_u32v(0x0000000a, 2);11emit_u32v(0x0000000b, 4);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful