Best JavaScript code snippet using wpt
wasm-module-builder.js
Source:wasm-module-builder.js
...854 }855 emit_u32v(val) {856 this.emit_leb_u(val, kMaxVarInt32Size);857 }858 emit_u64v(val) {859 this.emit_leb_u(val, kMaxVarInt64Size);860 }861 emit_bytes(data) {862 this.ensure_space(data.length);863 this.buffer.set(data, this.length);864 this.length += data.length;865 }866 emit_string(string) {867 // When testing illegal names, we pass a byte array directly.868 if (string instanceof Array) {869 this.emit_u32v(string.length);870 this.emit_bytes(string);871 return;872 }873 // This is the hacky way to convert a JavaScript string to a UTF8 encoded874 // string only containing single-byte characters.875 let string_utf8 = unescape(encodeURIComponent(string));876 this.emit_u32v(string_utf8.length);877 for (let i = 0; i < string_utf8.length; i++) {878 this.emit_u8(string_utf8.charCodeAt(i));879 }880 }881 emit_type(type) {882 if ((typeof type) == 'number') {883 this.emit_u8(type);884 } else {885 this.emit_u8(type.opcode);886 if ('depth' in type) this.emit_u8(type.depth);887 this.emit_u32v(type.index);888 }889 }890 emit_header() {891 this.emit_bytes([892 kWasmH0, kWasmH1, kWasmH2, kWasmH3, kWasmV0, kWasmV1, kWasmV2, kWasmV3893 ]);894 }895 emit_section(section_code, content_generator) {896 // Emit section name.897 this.emit_u8(section_code);898 // Emit the section to a temporary buffer: its full length isn't know yet.899 const section = new Binary;900 content_generator(section);901 // Emit section length.902 this.emit_u32v(section.length);903 // Copy the temporary buffer.904 // Avoid spread because {section} can be huge.905 this.emit_bytes(section.trunc_buffer());906 }907}908class WasmFunctionBuilder {909 // Encoding of local names: a string corresponds to a local name,910 // a number n corresponds to n undefined names.911 constructor(module, name, type_index, arg_names) {912 this.module = module;913 this.name = name;914 this.type_index = type_index;915 this.body = [];916 this.locals = [];917 this.local_names = arg_names;918 this.body_offset = undefined; // Not valid until module is serialized.919 }920 numLocalNames() {921 let num_local_names = 0;922 for (let loc_name of this.local_names) {923 if (typeof loc_name == 'string') ++num_local_names;924 }925 return num_local_names;926 }927 exportAs(name) {928 this.module.addExport(name, this.index);929 return this;930 }931 exportFunc() {932 this.exportAs(this.name);933 return this;934 }935 setCompilationHint(strategy, baselineTier, topTier) {936 this.module.setCompilationHint(strategy, baselineTier, topTier, this.index);937 return this;938 }939 addBody(body) {940 for (let b of body) {941 if (typeof b !== 'number' || (b & (~0xFF)) !== 0) {942 throw new Error(943 'invalid body (entries must be 8 bit numbers): ' + body);944 }945 }946 this.body = body.slice();947 // Automatically add the end for the function block to the body.948 this.body.push(kExprEnd);949 return this;950 }951 addBodyWithEnd(body) {952 this.body = body;953 return this;954 }955 getNumLocals() {956 let total_locals = 0;957 for (let l of this.locals) {958 total_locals += l.count959 }960 return total_locals;961 }962 addLocals(type, count, names) {963 this.locals.push({type: type, count: count});964 names = names || [];965 if (names.length > count) throw new Error('too many locals names given');966 this.local_names.push(...names);967 if (count > names.length) this.local_names.push(count - names.length);968 return this;969 }970 end() {971 return this.module;972 }973}974class WasmGlobalBuilder {975 constructor(module, type, mutable) {976 this.module = module;977 this.type = type;978 this.mutable = mutable;979 this.init = 0;980 }981 exportAs(name) {982 this.module.exports.push(983 {name: name, kind: kExternalGlobal, index: this.index});984 return this;985 }986}987class WasmTableBuilder {988 constructor(module, type, initial_size, max_size, init_func_index) {989 this.module = module;990 this.type = type;991 this.initial_size = initial_size;992 this.has_max = max_size != undefined;993 this.max_size = max_size;994 this.init_func_index = init_func_index;995 this.has_init = init_func_index != undefined;996 }997 exportAs(name) {998 this.module.exports.push(999 {name: name, kind: kExternalTable, index: this.index});1000 return this;1001 }1002}1003function makeField(type, mutability) {1004 assertEquals(1005 'boolean', typeof mutability, 'field mutability must be boolean');1006 return {type: type, mutability: mutability};1007}1008class WasmStruct {1009 constructor(fields) {1010 assertTrue(Array.isArray(fields), 'struct fields must be an array');1011 this.fields = fields;1012 }1013}1014class WasmArray {1015 constructor(type) {1016 this.type = type;1017 }1018}1019class WasmModuleBuilder {1020 constructor() {1021 this.types = [];1022 this.imports = [];1023 this.exports = [];1024 this.globals = [];1025 this.tables = [];1026 this.exceptions = [];1027 this.functions = [];1028 this.compilation_hints = [];1029 this.element_segments = [];1030 this.data_segments = [];1031 this.explicit = [];1032 this.num_imported_funcs = 0;1033 this.num_imported_globals = 0;1034 this.num_imported_tables = 0;1035 this.num_imported_exceptions = 0;1036 return this;1037 }1038 addStart(start_index) {1039 this.start_index = start_index;1040 return this;1041 }1042 addMemory(min, max, exported, shared) {1043 this.memory = {1044 min: min,1045 max: max,1046 exported: exported,1047 shared: shared || false,1048 is_memory64: false1049 };1050 return this;1051 }1052 addMemory64(min, max, exported) {1053 this.memory = {1054 min: min,1055 max: max,1056 exported: exported,1057 shared: false,1058 is_memory64: true1059 };1060 return this;1061 }1062 addExplicitSection(bytes) {1063 this.explicit.push(bytes);1064 return this;1065 }1066 stringToBytes(name) {1067 var result = new Binary();1068 result.emit_u32v(name.length);1069 for (var i = 0; i < name.length; i++) {1070 result.emit_u8(name.charCodeAt(i));1071 }1072 return result.trunc_buffer()1073 }1074 createCustomSection(name, bytes) {1075 name = this.stringToBytes(name);1076 var section = new Binary();1077 section.emit_u8(0);1078 section.emit_u32v(name.length + bytes.length);1079 section.emit_bytes(name);1080 section.emit_bytes(bytes);1081 return section.trunc_buffer();1082 }1083 addCustomSection(name, bytes) {1084 this.explicit.push(this.createCustomSection(name, bytes));1085 }1086 addType(type) {1087 this.types.push(type);1088 var pl = type.params.length; // should have params1089 var rl = type.results.length; // should have results1090 return this.types.length - 1;1091 }1092 addStruct(fields) {1093 this.types.push(new WasmStruct(fields));1094 return this.types.length - 1;1095 }1096 addArray(type) {1097 this.types.push(new WasmArray(type));1098 return this.types.length - 1;1099 }1100 addGlobal(type, mutable) {1101 let glob = new WasmGlobalBuilder(this, type, mutable);1102 glob.index = this.globals.length + this.num_imported_globals;1103 this.globals.push(glob);1104 return glob;1105 }1106 addTable(1107 type, initial_size, max_size = undefined, init_func_index = undefined) {1108 if (type == kWasmI32 || type == kWasmI64 || type == kWasmF32 ||1109 type == kWasmF64 || type == kWasmS128 || type == kWasmVoid) {1110 throw new Error('Tables must be of a reference type');1111 }1112 let table = new WasmTableBuilder(1113 this, type, initial_size, max_size, init_func_index);1114 table.index = this.tables.length + this.num_imported_tables;1115 this.tables.push(table);1116 return table;1117 }1118 addException(type) {1119 let type_index = (typeof type) == 'number' ? type : this.addType(type);1120 let except_index = this.exceptions.length + this.num_imported_exceptions;1121 this.exceptions.push(type_index);1122 return except_index;1123 }1124 addFunction(name, type, arg_names) {1125 arg_names = arg_names || [];1126 let type_index = (typeof type) == 'number' ? type : this.addType(type);1127 let num_args = this.types[type_index].params.length;1128 if (num_args < arg_names.length)1129 throw new Error('too many arg names provided');1130 if (num_args > arg_names.length)1131 arg_names.push(num_args - arg_names.length);1132 let func = new WasmFunctionBuilder(this, name, type_index, arg_names);1133 func.index = this.functions.length + this.num_imported_funcs;1134 this.functions.push(func);1135 return func;1136 }1137 addImport(module, name, type) {1138 if (this.functions.length != 0) {1139 throw new Error('Imported functions must be declared before local ones');1140 }1141 let type_index = (typeof type) == 'number' ? type : this.addType(type);1142 this.imports.push({1143 module: module,1144 name: name,1145 kind: kExternalFunction,1146 type_index: type_index1147 });1148 return this.num_imported_funcs++;1149 }1150 addImportedGlobal(module, name, type, mutable = false) {1151 if (this.globals.length != 0) {1152 throw new Error('Imported globals must be declared before local ones');1153 }1154 let o = {1155 module: module,1156 name: name,1157 kind: kExternalGlobal,1158 type: type,1159 mutable: mutable1160 };1161 this.imports.push(o);1162 return this.num_imported_globals++;1163 }1164 addImportedMemory(module, name, initial = 0, maximum, shared) {1165 let o = {1166 module: module,1167 name: name,1168 kind: kExternalMemory,1169 initial: initial,1170 maximum: maximum,1171 shared: shared1172 };1173 this.imports.push(o);1174 return this;1175 }1176 addImportedTable(module, name, initial, maximum, type) {1177 if (this.tables.length != 0) {1178 throw new Error('Imported tables must be declared before local ones');1179 }1180 let o = {1181 module: module,1182 name: name,1183 kind: kExternalTable,1184 initial: initial,1185 maximum: maximum,1186 type: type || kWasmFuncRef1187 };1188 this.imports.push(o);1189 return this.num_imported_tables++;1190 }1191 addImportedException(module, name, type) {1192 if (this.exceptions.length != 0) {1193 throw new Error('Imported exceptions must be declared before local ones');1194 }1195 let type_index = (typeof type) == 'number' ? type : this.addType(type);1196 let o = {1197 module: module,1198 name: name,1199 kind: kExternalException,1200 type_index: type_index1201 };1202 this.imports.push(o);1203 return this.num_imported_exceptions++;1204 }1205 addExport(name, index) {1206 this.exports.push({name: name, kind: kExternalFunction, index: index});1207 return this;1208 }1209 addExportOfKind(name, kind, index) {1210 if (index == undefined && kind != kExternalTable &&1211 kind != kExternalMemory) {1212 throw new Error(1213 'Index for exports other than tables/memories must be provided');1214 }1215 if (index !== undefined && (typeof index) != 'number') {1216 throw new Error('Index for exports must be a number')1217 }1218 this.exports.push({name: name, kind: kind, index: index});1219 return this;1220 }1221 setCompilationHint(strategy, baselineTier, topTier, index) {1222 this.compilation_hints[index] = {1223 strategy: strategy,1224 baselineTier: baselineTier,1225 topTier: topTier1226 };1227 return this;1228 }1229 addDataSegment(addr, data, is_global = false) {1230 this.data_segments.push(1231 {addr: addr, data: data, is_global: is_global, is_active: true});1232 return this.data_segments.length - 1;1233 }1234 addPassiveDataSegment(data) {1235 this.data_segments.push({data: data, is_active: false});1236 return this.data_segments.length - 1;1237 }1238 exportMemoryAs(name) {1239 this.exports.push({name: name, kind: kExternalMemory, index: 0});1240 }1241 addElementSegment(table, base, is_global, array) {1242 this.element_segments.push({1243 table: table,1244 base: base,1245 is_global: is_global,1246 array: array,1247 is_active: true,1248 is_declarative: false1249 });1250 return this;1251 }1252 addPassiveElementSegment(array) {1253 this.element_segments.push(1254 {array: array, is_active: false, is_declarative: false});1255 return this;1256 }1257 addDeclarativeElementSegment(array) {1258 this.element_segments.push(1259 {array: array, is_active: false, is_declarative: true});1260 return this;1261 }1262 appendToTable(array) {1263 for (let n of array) {1264 if (typeof n != 'number')1265 throw new Error('invalid table (entries have to be numbers): ' + array);1266 }1267 if (this.tables.length == 0) {1268 this.addTable(kWasmAnyFunc, 0);1269 }1270 // Adjust the table to the correct size.1271 let table = this.tables[0];1272 const base = table.initial_size;1273 const table_size = base + array.length;1274 table.initial_size = table_size;1275 if (table.has_max && table_size > table.max_size) {1276 table.max_size = table_size;1277 }1278 return this.addElementSegment(0, base, false, array);1279 }1280 setTableBounds(min, max = undefined) {1281 if (this.tables.length != 0) {1282 throw new Error('The table bounds of table \'0\' have already been set.');1283 }1284 this.addTable(kWasmAnyFunc, min, max);1285 return this;1286 }1287 setName(name) {1288 this.name = name;1289 return this;1290 }1291 toBuffer(debug = false) {1292 let binary = new Binary;1293 let wasm = this;1294 // Add header1295 binary.emit_header();1296 // Add type section1297 if (wasm.types.length > 0) {1298 if (debug) print('emitting types @ ' + binary.length);1299 binary.emit_section(kTypeSectionCode, section => {1300 section.emit_u32v(wasm.types.length);1301 for (let type of wasm.types) {1302 if (type instanceof WasmStruct) {1303 section.emit_u8(kWasmStructTypeForm);1304 section.emit_u32v(type.fields.length);1305 for (let field of type.fields) {1306 section.emit_type(field.type);1307 section.emit_u8(field.mutability ? 1 : 0);1308 }1309 } else if (type instanceof WasmArray) {1310 section.emit_u8(kWasmArrayTypeForm);1311 section.emit_type(type.type);1312 section.emit_u8(1); // Only mutable arrays supported currently.1313 } else {1314 section.emit_u8(kWasmFunctionTypeForm);1315 section.emit_u32v(type.params.length);1316 for (let param of type.params) {1317 section.emit_type(param);1318 }1319 section.emit_u32v(type.results.length);1320 for (let result of type.results) {1321 section.emit_type(result);1322 }1323 }1324 }1325 });1326 }1327 // Add imports section1328 if (wasm.imports.length > 0) {1329 if (debug) print('emitting imports @ ' + binary.length);1330 binary.emit_section(kImportSectionCode, section => {1331 section.emit_u32v(wasm.imports.length);1332 for (let imp of wasm.imports) {1333 section.emit_string(imp.module);1334 section.emit_string(imp.name || '');1335 section.emit_u8(imp.kind);1336 if (imp.kind == kExternalFunction) {1337 section.emit_u32v(imp.type_index);1338 } else if (imp.kind == kExternalGlobal) {1339 section.emit_type(imp.type);1340 section.emit_u8(imp.mutable);1341 } else if (imp.kind == kExternalMemory) {1342 var has_max = (typeof imp.maximum) != 'undefined';1343 var is_shared = (typeof imp.shared) != 'undefined';1344 if (is_shared) {1345 section.emit_u8(has_max ? 3 : 2); // flags1346 } else {1347 section.emit_u8(has_max ? 1 : 0); // flags1348 }1349 section.emit_u32v(imp.initial); // initial1350 if (has_max) section.emit_u32v(imp.maximum); // maximum1351 } else if (imp.kind == kExternalTable) {1352 section.emit_type(imp.type);1353 var has_max = (typeof imp.maximum) != 'undefined';1354 section.emit_u8(has_max ? 1 : 0); // flags1355 section.emit_u32v(imp.initial); // initial1356 if (has_max) section.emit_u32v(imp.maximum); // maximum1357 } else if (imp.kind == kExternalException) {1358 section.emit_u32v(kExceptionAttribute);1359 section.emit_u32v(imp.type_index);1360 } else {1361 throw new Error('unknown/unsupported import kind ' + imp.kind);1362 }1363 }1364 });1365 }1366 // Add functions declarations1367 if (wasm.functions.length > 0) {1368 if (debug) print('emitting function decls @ ' + binary.length);1369 binary.emit_section(kFunctionSectionCode, section => {1370 section.emit_u32v(wasm.functions.length);1371 for (let func of wasm.functions) {1372 section.emit_u32v(func.type_index);1373 }1374 });1375 }1376 // Add table section1377 if (wasm.tables.length > 0) {1378 if (debug) print('emitting tables @ ' + binary.length);1379 binary.emit_section(kTableSectionCode, section => {1380 section.emit_u32v(wasm.tables.length);1381 for (let table of wasm.tables) {1382 section.emit_type(table.type);1383 section.emit_u8(table.has_max);1384 section.emit_u32v(table.initial_size);1385 if (table.has_max) section.emit_u32v(table.max_size);1386 if (table.has_init) {1387 section.emit_u8(kExprRefFunc);1388 section.emit_u32v(table.init_func_index);1389 section.emit_u8(kExprEnd);1390 }1391 }1392 });1393 }1394 // Add memory section1395 if (wasm.memory !== undefined) {1396 if (debug) print('emitting memory @ ' + binary.length);1397 binary.emit_section(kMemorySectionCode, section => {1398 section.emit_u8(1); // one memory entry1399 const has_max = wasm.memory.max !== undefined;1400 if (wasm.memory.is_memory64) {1401 assertFalse(1402 wasm.memory.shared, 'sharing memory64 is not supported (yet)');1403 section.emit_u8(1404 has_max ? kLimitsMemory64WithMaximum : kLimitsMemory64NoMaximum);1405 section.emit_u64v(wasm.memory.min);1406 if (has_max) section.emit_u64v(wasm.memory.max);1407 } else {1408 section.emit_u8(1409 wasm.memory.shared ?1410 (has_max ? kLimitsSharedWithMaximum :1411 kLimitsSharedNoMaximum) :1412 (has_max ? kLimitsWithMaximum : kLimitsNoMaximum));1413 section.emit_u32v(wasm.memory.min);1414 if (has_max) section.emit_u32v(wasm.memory.max);1415 }1416 });1417 }1418 // Add event section.1419 if (wasm.exceptions.length > 0) {1420 if (debug) print('emitting events @ ' + binary.length);1421 binary.emit_section(kExceptionSectionCode, section => {1422 section.emit_u32v(wasm.exceptions.length);1423 for (let type_index of wasm.exceptions) {1424 section.emit_u32v(kExceptionAttribute);1425 section.emit_u32v(type_index);1426 }1427 });1428 }1429 // Add global section.1430 if (wasm.globals.length > 0) {1431 if (debug) print('emitting globals @ ' + binary.length);1432 binary.emit_section(kGlobalSectionCode, section => {1433 section.emit_u32v(wasm.globals.length);1434 for (let global of wasm.globals) {1435 section.emit_type(global.type);1436 section.emit_u8(global.mutable);1437 if ((typeof global.init_index) == 'undefined') {1438 // Emit a constant initializer.1439 switch (global.type) {1440 case kWasmI32:1441 section.emit_u8(kExprI32Const);1442 section.emit_u32v(global.init);1443 break;1444 case kWasmI64:1445 section.emit_u8(kExprI64Const);1446 section.emit_u64v(global.init);1447 break;1448 case kWasmF32:1449 section.emit_bytes(wasmF32Const(global.init));1450 break;1451 case kWasmF64:1452 section.emit_bytes(wasmF64Const(global.init));1453 break;1454 case kWasmS128:1455 section.emit_bytes(wasmS128Const(global.init));1456 break;1457 case kWasmExternRef:1458 section.emit_u8(kExprRefNull);1459 section.emit_u8(kWasmExternRef);1460 assertEquals(global.function_index, undefined);...
Using AI Code Generation
1var wpt = require('./build/Release/wpt');2wpt.emit_u64v(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);3var wpt = require('./build/Release/wpt');4wpt.emit_s(0, "Hello World");5var wpt = require('./build/Release/wpt');6wpt.emit_sv(0, "Hello World", 1);
Using AI Code Generation
1var wpt = require('wpt');2wpt.emit_u64v(1, 2, 3, 4, 5);3var wpt = require('wpt');4wpt.emit_u64v(1, 2, 3, 4, 5);5var wpt = require('wpt');6wpt.emit_u64v(1, 2, 3, 4, 5);7var wpt = require('wpt');8wpt.emit_u64v(1, 2, 3, 4, 5);9var wpt = require('wpt');10wpt.emit_u64v(1, 2, 3, 4, 5);11var wpt = require('wpt');12wpt.emit_u64v(1, 2, 3, 4, 5);13var wpt = require('wpt');14wpt.emit_u64v(1, 2, 3, 4, 5);15var wpt = require('wpt');16wpt.emit_u64v(1, 2, 3, 4, 5);17var wpt = require('wpt');18wpt.emit_u64v(1, 2, 3, 4, 5);19var wpt = require('wpt');20wpt.emit_u64v(1, 2, 3, 4, 5);
Using AI Code Generation
1wpt.emit_u64v(100, [1,2,3,4,5]);2wpt.emit_u64v(100, [1,2,3,4,5]);3wpt.emit_u64v(100, [1,2,3,4,5]);4wpt.emit_u64v(100, [1,2,3,4,5]);5wpt.emit_u64v(100, [1,2,3,4,5]);6wpt.emit_u64v(100, [1,2,3,4,5]);7wpt.emit_u64v(100, [1,2,3,4,5]);8wpt.emit_u64v(100, [1,2,3,4,5]);9wpt.emit_u64v(100, [1,2,3,4,5]);10wpt.emit_u64v(100, [1,2,3,4,5]);11wpt.emit_u64v(100, [1,2,3,4,5]);12wpt.emit_u64v(100, [1,2,3,4,5]);13wpt.emit_u64v(100, [1,2,3,4
Using AI Code Generation
1var wpt = require('wpt');2var a = [1, 2, 3, 4];3wpt.emit_u64v('test', a);4var wpt = require('wpt');5var a = [1, 2, 3, 4];6wpt.emit_u64v('test', a);7var wpt = require('wpt');8var a = [1, 2, 3, 4];9wpt.emit_u64v('test', a);10var wpt = require('wpt');11var a = [1, 2, 3, 4];12wpt.emit_u64v('test', a);13var wpt = require('wpt');14var a = [1, 2, 3, 4];15wpt.emit_u64v('test', a);16var wpt = require('wpt');17var a = [1, 2, 3, 4];18wpt.emit_u64v('test', a);19var wpt = require('wpt');20var a = [1, 2, 3, 4];21wpt.emit_u64v('test', a);22var wpt = require('wpt');23var a = [1, 2, 3, 4];24wpt.emit_u64v('test', a);25var wpt = require('wpt');26var a = [1, 2, 3, 4];27wpt.emit_u64v('test', a);28var wpt = require('wpt
Using AI Code Generation
1const wpt = require('wpt');2var u64v = [ 1, 2, 3, 4, 5 ];3wpt.emit_u64v('u64v', u64v);4const wpt = require('wpt');5wpt.u64v('u64v', (u64v) => {6 console.log(u64v);7});
Using AI Code Generation
1var wpt = require('wpt');2var fd = wpt.open('test.bin', 'w+');3wpt.emit_u64v(fd, 0x1234567890abcdef);4wpt.close(fd);5var wpt = require('wpt');6var fd = wpt.open('test.bin', 'w+');7wpt.emit_u64v(fd, 0x1234567890abcdef);8wpt.close(fd);9var wpt = require('wpt');10var fd = wpt.open('test.bin', 'w+');11wpt.emit_u64v(fd, 0x1234567890abcdef);12wpt.close(fd);13var wpt = require('wpt');14var fd = wpt.open('test.bin', 'w+');15wpt.emit_u64v(fd, 0x1234567890abcdef);16wpt.close(fd);
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!!