Best JavaScript code snippet using wpt
wasm-module-builder.js
Source:wasm-module-builder.js
...73 this.name = name;74 this.type_index = type_index;75 this.body = [];76 }77 numLocalNames() {78 if (this.local_names === undefined) return 0;79 let num_local_names = 0;80 for (let loc_name of this.local_names) {81 if (loc_name !== undefined) ++num_local_names;82 }83 return num_local_names;84 }85 exportAs(name) {86 this.module.addExport(name, this.index);87 return this;88 }89 exportFunc() {90 this.exportAs(this.name);91 return this;92 }93 addBody(body) {94 for (let b of body) {95 if (typeof b != 'number')96 throw new Error('invalid body (entries have to be numbers): ' + body);97 }98 this.body = body.slice();99 // Automatically add the end for the function block to the body.100 this.body.push(kExprEnd);101 return this;102 }103 addBodyWithEnd(body) {104 this.body = body;105 return this;106 }107 addLocals(locals, names) {108 this.locals = locals;109 this.local_names = names;110 return this;111 }112 end() {113 return this.module;114 }115}116class WasmGlobalBuilder {117 constructor(module, type, mutable) {118 this.module = module;119 this.type = type;120 this.mutable = mutable;121 this.init = 0;122 }123 exportAs(name) {124 this.module.exports.push({name: name, kind: kExternalGlobal,125 index: this.index});126 return this;127 }128}129class WasmModuleBuilder {130 constructor() {131 this.types = [];132 this.imports = [];133 this.exports = [];134 this.globals = [];135 this.functions = [];136 this.function_table = [];137 this.function_table_length = 0;138 this.function_table_inits = [];139 this.segments = [];140 this.explicit = [];141 this.num_imported_funcs = 0;142 this.num_imported_globals = 0;143 return this;144 }145 addStart(start_index) {146 this.start_index = start_index;147 return this;148 }149 addMemory(min, max, exp) {150 this.memory = {min: min, max: max, exp: exp};151 return this;152 }153 addExplicitSection(bytes) {154 this.explicit.push(bytes);155 return this;156 }157 stringToBytes(name) {158 var result = new Binary();159 result.emit_u32v(name.length);160 for (var i = 0; i < name.length; i++) {161 result.emit_u8(name.charCodeAt(i));162 }163 return result;164 }165 addCustomSection(name, bytes) {166 name = this.stringToBytes(name);167 var length = new Binary();168 length.emit_u32v(name.length + bytes.length);169 this.explicit.push([0, ...length, ...name, ...bytes]);170 }171 addType(type) {172 // TODO: canonicalize types?173 this.types.push(type);174 return this.types.length - 1;175 }176 addGlobal(local_type, mutable) {177 let glob = new WasmGlobalBuilder(this, local_type, mutable);178 glob.index = this.globals.length + this.num_imported_globals;179 this.globals.push(glob);180 return glob;181 }182 addFunction(name, type) {183 let type_index = (typeof type) == "number" ? type : this.addType(type);184 let func = new WasmFunctionBuilder(this, name, type_index);185 func.index = this.functions.length + this.num_imported_funcs;186 this.functions.push(func);187 return func;188 }189 addImport(module = "", name, type) {190 let type_index = (typeof type) == "number" ? type : this.addType(type);191 this.imports.push({module: module, name: name, kind: kExternalFunction,192 type: type_index});193 return this.num_imported_funcs++;194 }195 addImportedGlobal(module = "", name, type) {196 let o = {module: module, name: name, kind: kExternalGlobal, type: type,197 mutable: false}198 this.imports.push(o);199 return this.num_imported_globals++;200 }201 addImportedMemory(module = "", name, initial = 0, maximum) {202 let o = {module: module, name: name, kind: kExternalMemory,203 initial: initial, maximum: maximum};204 this.imports.push(o);205 return this;206 }207 addImportedTable(module = "", name, initial, maximum) {208 let o = {module: module, name: name, kind: kExternalTable, initial: initial,209 maximum: maximum};210 this.imports.push(o);211 }212 addExport(name, index) {213 this.exports.push({name: name, kind: kExternalFunction, index: index});214 return this;215 }216 addExportOfKind(name, kind, index) {217 this.exports.push({name: name, kind: kind, index: index});218 return this;219 }220 addDataSegment(addr, data, is_global = false) {221 this.segments.push({addr: addr, data: data, is_global: is_global});222 return this.segments.length - 1;223 }224 exportMemoryAs(name) {225 this.exports.push({name: name, kind: kExternalMemory, index: 0});226 }227 addFunctionTableInit(base, is_global, array, is_import = false) {228 this.function_table_inits.push({base: base, is_global: is_global,229 array: array});230 if (!is_global) {231 var length = base + array.length;232 if (length > this.function_table_length && !is_import) {233 this.function_table_length = length;234 }235 }236 return this;237 }238 appendToTable(array) {239 for (let n of array) {240 if (typeof n != 'number')241 throw new Error('invalid table (entries have to be numbers): ' + array);242 }243 return this.addFunctionTableInit(this.function_table.length, false, array);244 }245 setFunctionTableLength(length) {246 this.function_table_length = length;247 return this;248 }249 setName(name) {250 this.name = name;251 return this;252 }253 toArray(debug = false) {254 let binary = new Binary;255 let wasm = this;256 // Add header257 binary.emit_header();258 // Add type section259 if (wasm.types.length > 0) {260 if (debug) print("emitting types @ " + binary.length);261 binary.emit_section(kTypeSectionCode, section => {262 section.emit_u32v(wasm.types.length);263 for (let type of wasm.types) {264 section.emit_u8(kWasmFunctionTypeForm);265 section.emit_u32v(type.params.length);266 for (let param of type.params) {267 section.emit_u8(param);268 }269 section.emit_u32v(type.results.length);270 for (let result of type.results) {271 section.emit_u8(result);272 }273 }274 });275 }276 // Add imports section277 if (wasm.imports.length > 0) {278 if (debug) print("emitting imports @ " + binary.length);279 binary.emit_section(kImportSectionCode, section => {280 section.emit_u32v(wasm.imports.length);281 for (let imp of wasm.imports) {282 section.emit_string(imp.module);283 section.emit_string(imp.name || '');284 section.emit_u8(imp.kind);285 if (imp.kind == kExternalFunction) {286 section.emit_u32v(imp.type);287 } else if (imp.kind == kExternalGlobal) {288 section.emit_u32v(imp.type);289 section.emit_u8(imp.mutable);290 } else if (imp.kind == kExternalMemory) {291 var has_max = (typeof imp.maximum) != "undefined";292 section.emit_u8(has_max ? 1 : 0); // flags293 section.emit_u32v(imp.initial); // initial294 if (has_max) section.emit_u32v(imp.maximum); // maximum295 } else if (imp.kind == kExternalTable) {296 section.emit_u8(kWasmAnyFunctionTypeForm);297 var has_max = (typeof imp.maximum) != "undefined";298 section.emit_u8(has_max ? 1 : 0); // flags299 section.emit_u32v(imp.initial); // initial300 if (has_max) section.emit_u32v(imp.maximum); // maximum301 } else {302 throw new Error("unknown/unsupported import kind " + imp.kind);303 }304 }305 });306 }307 // Add functions declarations308 if (wasm.functions.length > 0) {309 if (debug) print("emitting function decls @ " + binary.length);310 binary.emit_section(kFunctionSectionCode, section => {311 section.emit_u32v(wasm.functions.length);312 for (let func of wasm.functions) {313 section.emit_u32v(func.type_index);314 }315 });316 }317 // Add function_table.318 if (wasm.function_table_length > 0) {319 if (debug) print("emitting table @ " + binary.length);320 binary.emit_section(kTableSectionCode, section => {321 section.emit_u8(1); // one table entry322 section.emit_u8(kWasmAnyFunctionTypeForm);323 section.emit_u8(1);324 section.emit_u32v(wasm.function_table_length);325 section.emit_u32v(wasm.function_table_length);326 });327 }328 // Add memory section329 if (wasm.memory !== undefined) {330 if (debug) print("emitting memory @ " + binary.length);331 binary.emit_section(kMemorySectionCode, section => {332 section.emit_u8(1); // one memory entry333 section.emit_u32v(kResizableMaximumFlag);334 section.emit_u32v(wasm.memory.min);335 section.emit_u32v(wasm.memory.max);336 });337 }338 // Add global section.339 if (wasm.globals.length > 0) {340 if (debug) print ("emitting globals @ " + binary.length);341 binary.emit_section(kGlobalSectionCode, section => {342 section.emit_u32v(wasm.globals.length);343 for (let global of wasm.globals) {344 section.emit_u8(global.type);345 section.emit_u8(global.mutable);346 if ((typeof global.init_index) == "undefined") {347 // Emit a constant initializer.348 switch (global.type) {349 case kWasmI32:350 section.emit_u8(kExprI32Const);351 section.emit_u32v(global.init);352 break;353 case kWasmI64:354 section.emit_u8(kExprI64Const);355 section.emit_u32v(global.init);356 break;357 case kWasmF32:358 section.emit_u8(kExprF32Const);359 f32_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 break;365 case kWasmF64:366 section.emit_u8(kExprF64Const);367 f64_view[0] = global.init;368 section.emit_u8(byte_view[0]);369 section.emit_u8(byte_view[1]);370 section.emit_u8(byte_view[2]);371 section.emit_u8(byte_view[3]);372 section.emit_u8(byte_view[4]);373 section.emit_u8(byte_view[5]);374 section.emit_u8(byte_view[6]);375 section.emit_u8(byte_view[7]);376 break;377 }378 } else {379 // Emit a global-index initializer.380 section.emit_u8(kExprGetGlobal);381 section.emit_u32v(global.init_index);382 }383 section.emit_u8(kExprEnd); // end of init expression384 }385 });386 }387 // Add export table.388 var mem_export = (wasm.memory !== undefined && wasm.memory.exp);389 var exports_count = wasm.exports.length + (mem_export ? 1 : 0);390 if (exports_count > 0) {391 if (debug) print("emitting exports @ " + binary.length);392 binary.emit_section(kExportSectionCode, section => {393 section.emit_u32v(exports_count);394 for (let exp of wasm.exports) {395 section.emit_string(exp.name);396 section.emit_u8(exp.kind);397 section.emit_u32v(exp.index);398 }399 if (mem_export) {400 section.emit_string("memory");401 section.emit_u8(kExternalMemory);402 section.emit_u8(0);403 }404 });405 }406 // Add start function section.407 if (wasm.start_index !== undefined) {408 if (debug) print("emitting start function @ " + binary.length);409 binary.emit_section(kStartSectionCode, section => {410 section.emit_u32v(wasm.start_index);411 });412 }413 // Add table elements.414 if (wasm.function_table_inits.length > 0) {415 if (debug) print("emitting table @ " + binary.length);416 binary.emit_section(kElementSectionCode, section => {417 var inits = wasm.function_table_inits;418 section.emit_u32v(inits.length);419 for (let init of inits) {420 section.emit_u8(0); // table index421 if (init.is_global) {422 section.emit_u8(kExprGetGlobal);423 } else {424 section.emit_u8(kExprI32Const);425 }426 section.emit_u32v(init.base);427 section.emit_u8(kExprEnd);428 section.emit_u32v(init.array.length);429 for (let index of init.array) {430 section.emit_u32v(index);431 }432 }433 });434 }435 // Add function bodies.436 if (wasm.functions.length > 0) {437 // emit function bodies438 if (debug) print("emitting code @ " + binary.length);439 binary.emit_section(kCodeSectionCode, section => {440 section.emit_u32v(wasm.functions.length);441 for (let func of wasm.functions) {442 // Function body length will be patched later.443 let local_decls = [];444 let l = func.locals;445 if (l !== undefined) {446 let local_decls_count = 0;447 if (l.i32_count > 0) {448 local_decls.push({count: l.i32_count, type: kWasmI32});449 }450 if (l.i64_count > 0) {451 local_decls.push({count: l.i64_count, type: kWasmI64});452 }453 if (l.f32_count > 0) {454 local_decls.push({count: l.f32_count, type: kWasmF32});455 }456 if (l.f64_count > 0) {457 local_decls.push({count: l.f64_count, type: kWasmF64});458 }459 }460 let header = new Binary;461 header.emit_u32v(local_decls.length);462 for (let decl of local_decls) {463 header.emit_u32v(decl.count);464 header.emit_u8(decl.type);465 }466 section.emit_u32v(header.length + func.body.length);467 section.emit_bytes(header);468 section.emit_bytes(func.body);469 }470 });471 }472 // Add data segments.473 if (wasm.segments.length > 0) {474 if (debug) print("emitting data segments @ " + binary.length);475 binary.emit_section(kDataSectionCode, section => {476 section.emit_u32v(wasm.segments.length);477 for (let seg of wasm.segments) {478 section.emit_u8(0); // linear memory index 0479 if (seg.is_global) {480 // initializer is a global variable481 section.emit_u8(kExprGetGlobal);482 section.emit_u32v(seg.addr);483 } else {484 // initializer is a constant485 section.emit_u8(kExprI32Const);486 section.emit_u32v(seg.addr);487 }488 section.emit_u8(kExprEnd);489 section.emit_u32v(seg.data.length);490 section.emit_bytes(seg.data);491 }492 });493 }494 // Add any explicitly added sections495 for (let exp of wasm.explicit) {496 if (debug) print("emitting explicit @ " + binary.length);497 binary.emit_bytes(exp);498 }499 // Add names.500 let num_function_names = 0;501 let num_functions_with_local_names = 0;502 for (let func of wasm.functions) {503 if (func.name !== undefined) ++num_function_names;504 if (func.numLocalNames() > 0) ++num_functions_with_local_names;505 }506 if (num_function_names > 0 || num_functions_with_local_names > 0 ||507 wasm.name !== undefined) {508 if (debug) print('emitting names @ ' + binary.length);509 binary.emit_section(kUnknownSectionCode, section => {510 section.emit_string('name');511 // Emit module name.512 if (wasm.name !== undefined) {513 section.emit_section(kModuleNameCode, name_section => {514 name_section.emit_string(wasm.name);515 });516 }517 // Emit function names.518 if (num_function_names > 0) {519 section.emit_section(kFunctionNamesCode, name_section => {520 name_section.emit_u32v(num_function_names);521 for (let func of wasm.functions) {522 if (func.name === undefined) continue;523 name_section.emit_u32v(func.index);524 name_section.emit_string(func.name);525 }526 });527 }528 // Emit local names.529 if (num_functions_with_local_names > 0) {530 section.emit_section(kLocalNamesCode, name_section => {531 name_section.emit_u32v(num_functions_with_local_names);532 for (let func of wasm.functions) {533 if (func.numLocalNames() == 0) continue;534 name_section.emit_u32v(func.index);535 name_section.emit_u32v(func.numLocalNames());536 for (let i = 0; i < func.local_names.length; ++i) {537 if (func.local_names[i] === undefined) continue;538 name_section.emit_u32v(i);539 name_section.emit_string(func.local_names[i]);540 }541 }542 });543 }544 });545 }546 return binary;547 }548 toBuffer(debug = false) {549 let bytes = this.toArray(debug);...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.numLocalNames(function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10{11}12var wpt = require('wpt');13var wpt = new WebPageTest('www.webpagetest.org');14wpt.testInfo('130713_5R_1R4', function(err, data) {15 if (err) {16 console.log(err);17 } else {18 console.log(data);19 }20});21{22}23var wpt = require('wpt');24var wpt = new WebPageTest('www.webpagetest.org');25wpt.getLocations(function(err, data) {26 if (err) {27 console.log(err);28 } else {29 console.log(data);30 }31});32{33 {
Using AI Code Generation
1var numLocalNames = wpt.numLocalNames();2console.log("Number of local names: " + numLocalNames);3var localName = wpt.getLocalName(0);4console.log("Local Name: " + localName);5var numRemoteNames = wpt.numRemoteNames();6console.log("Number of remote names: " + numRemoteNames);7var remoteName = wpt.getRemoteName(0);8console.log("Remote Name: " + remoteName);9var numRemoteAddresses = wpt.numRemoteAddresses();10console.log("Number of remote addresses: " + numRemoteAddresses);11var remoteAddress = wpt.getRemoteAddress(0);12console.log("Remote Address: " + remoteAddress);13var numRemotePorts = wpt.numRemotePorts();14console.log("Number of remote ports: " + numRemotePorts);15var remotePort = wpt.getRemotePort(0);16console.log("Remote Port: " + remotePort);17var numLocalAddresses = wpt.numLocalAddresses();18console.log("Number of local addresses: " + numLocalAddresses);19var localAddress = wpt.getLocalAddress(0);20console.log("Local Address: " + localAddress);21var numLocalPorts = wpt.numLocalPorts();22console.log("Number of local ports: " + numLocalPorts);23var localPort = wpt.getLocalPort(0);24console.log("Local Port: " + localPort);
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Barack_Obama');3wp.get(function(err, page) {4 console.log(page.numLocalNames());5});6var wptools = require('wptools');7wptools.setLang('fr');8var wptools = require('wptools');9var wp = wptools.page('Barack_Obama').setLang('fr');10var wptools = require('wptools');11var wp = wptools.page('Barack_Obama');12wp.get(function(err, page) {13 console.log(page.infobox().setLang('fr').get('date de naissance'));14});
Using AI Code Generation
1var wptextpattern = require('wptextpattern');2var text = "This is a test.";3var pattern = new wptextpattern.TextPattern(text);4var numLocalNames = pattern.numLocalNames();5console.log("Number of local names: " + numLocalNames);6var wptextpattern = require('wptextpattern');7var text = "This is a test.";8var pattern = new wptextpattern.TextPattern(text);9var numGlobalNames = pattern.numGlobalNames();10console.log("Number of global names: " + numGlobalNames);11var wptextpattern = require('wptextpattern');12var text = "This is a test.";13var pattern = new wptextpattern.TextPattern(text);14var numInterwikiLinks = pattern.numInterwikiLinks();15console.log("Number of interwiki links: " + numInterwikiLinks);16var wptextpattern = require('wptextpattern');17var text = "This is a test.";18var pattern = new wptextpattern.TextPattern(text);19var numExternalLinks = pattern.numExternalLinks();20console.log("Number of external links: " + numExternalLinks);
Using AI Code Generation
1var wpt = require('wpt');2wpt.numLocalNames(function(err, data){3 if(err) console.log(err);4 console.log(data);5});6### numLocalNames(callback)7wpt.numLocalNames(function(err, data){8 if(err) console.log(err);9 console.log(data);10});11### numNameRecords(callback)12wpt.numNameRecords(function(err, data){13 if(err) console.log(err);14 console.log(data);15});16### numNameRecordsForPlatform(platform, callback)17wpt.numNameRecordsForPlatform(3, function(err, data){18 if(err) console.log(err);19 console.log(data);20});21### numNameRecordsForEncoding(encoding, callback)22wpt.numNameRecordsForEncoding(1, function(err, data){23 if(err) console.log(err);24 console.log(data);25});26### numNameRecordsForLanguage(language, callback)27wpt.numNameRecordsForLanguage(0x0409, function(err, data){28 if(err) console.log(err);29 console.log(data);30});31### numNameRecordsForNameId(nameId, callback)
Using AI Code Generation
1var wptp = require('wptextpattern');2var numLocalNames = wptp.numLocalNames;3var num = numLocalNames('[[en:John]]');4console.log(num);5var wptp = require('wptextpattern');6var getLocalName = wptp.getLocalName;7var name = getLocalName('[[en:John]]');8console.log(name);9var wptp = require('wptextpattern');10var getLocalNames = wptp.getLocalNames;11var names = getLocalNames('[[en:John]]');12console.log(names);13var wptp = require('wptextpattern');14var getLocalNameAt = wptp.getLocalNameAt;15var name = getLocalNameAt('[[en:John]]', 0);16console.log(name);17var wptp = require('wptextpattern');18var getLocalNamesAt = wptp.getLocalNamesAt;19var names = getLocalNamesAt('[[en:John]]', 0);20console.log(names);21var wptp = require('wptextpattern');22var getLocalNameAt = wptp.getLocalNameAt;23var name = getLocalNameAt('[[en:John]]', 1);24console.log(name);25var wptp = require('wptextpattern');26var getLocalNamesAt = wptp.getLocalNamesAt;27var names = getLocalNamesAt('[[en:John]]', 1);28console.log(names);
Using AI Code Generation
1var textPattern = new WpTextPattern();2var numLocalNames = textPattern.numLocalNames();3alert(numLocalNames);4## 4.2.2. numLocalNames()5numLocalNames()6var textPattern = new WpTextPattern();7var numLocalNames = textPattern.numLocalNames();8alert(numLocalNames);9## 4.2.3. numLocalNames()10numLocalNames()11var textPattern = new WpTextPattern();12var numLocalNames = textPattern.numLocalNames();13alert(numLocalNames);14## 4.2.4. numLocalNames()15numLocalNames()16var textPattern = new WpTextPattern();17var numLocalNames = textPattern.numLocalNames();18alert(numLocalNames);
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!