Best JavaScript code snippet using wpt
Deserializer.ts
Source:Deserializer.ts
1/// <reference path="../../node_modules/assemblyscript/std/assembly/rt/index.d.ts" />2import { ASON_INSTRUCTION_TYPE, __arraySegment, __circularSegment, __dataSegment, __endSegment, __nullSegment, __popSegment, __referenceSegment, __valueSegment } from "./util";3import { ASON_ERROR } from "./Error";4class Box<T> { constructor(public value: T) {}}5@global6export class Deserializer {7 stackIndex: i32 = -1;8 stack: StaticArray<usize> = new StaticArray<usize>(1000);9 binaryIndex: i32 = 0;10 binary: StaticArray<u8> = [];11 seen: Map<u32, usize> = new Map<u32, usize>();12 deserialize<T>(binary: StaticArray<u8>): T {13 this.binary = binary;14 this.binaryIndex = 0;15 let box = new Box<T>(16 isInteger<T>() || isFloat<T>()17 ? <T>018 : changetype<T>(0)19 );20 unchecked(this.stack[0] = changetype<usize>(box));21 this.stackIndex = 0;22 let token = this.peek();23 // assuming object/array24 switch(token) {25 case ASON_INSTRUCTION_TYPE.ARRAY: {26 if (!this.tryConsumeArray(token)) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);27 break;28 }29 case ASON_INSTRUCTION_TYPE.REFERENCE: {30 if (!this.tryConsumeReference(token)) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);31 break;32 }33 default: throw new Error(ASON_ERROR.E_TYPE_MISMATCH);34 }35 this.consumeEnd();36 return box.value;37 }38 private peek(): ASON_INSTRUCTION_TYPE {39 return load<ASON_INSTRUCTION_TYPE>(changetype<usize>(this.binary) + <usize>this.binaryIndex);40 }41 /**42 * Pushes the stack index +1, assembles a buffer, then creates an array, then returns it.43 *44 * @param {ASON_INSTRUCTION_TYPE} token - Peeked token, so it doesn't need to `peek()`45 */46 private tryConsumeArray(token: ASON_INSTRUCTION_TYPE): bool {47 // will only consume an array48 if (token != ASON_INSTRUCTION_TYPE.ARRAY) return false;49 let stackIndex = this.stackIndex;50 let arraySegment = this.arraySegment;51 let arrayOffset = arraySegment.offset;52 let arrayAlign = arraySegment.align;53 let arrayLength = arraySegment.length;54 let arrayIsStaticArray = arraySegment.isStaticArray;55 let arrayClassId = arraySegment.classId;56 let arrayEntryId = arraySegment.entryId;57 trace("entry id", 1, arrayEntryId);58 let arrayValueIsNullable = arraySegment.isValueNullable;59 // advance the parser, no turning back now60 this.binaryIndex += offsetof<__arraySegment>();61 token = this.peek();62 let parentPtr = this.stack[stackIndex];63 let arrayByteLength = <usize>arrayLength << arrayAlign;64 let result: usize = 0;65 switch (token) {66 case ASON_INSTRUCTION_TYPE.DATA: {67 let dataSegment = this.dataSegment;68 let array: usize;69 let dataStart = changetype<usize>(dataSegment) + offsetof<__dataSegment>();70 if (arrayIsStaticArray) {71 array = __new(arrayByteLength, arrayClassId);72 memory.copy(array, dataStart, arrayByteLength);73 } else {74 // fast path, new array75 array = __newArray(arrayLength, arrayAlign, arrayClassId, dataStart);76 }77 // always link. every item on the stack is garunteed to be managed78 __link(parentPtr, array, false);79 trace("setting seen", 2, arrayEntryId, array);80 this.seen.set(arrayEntryId, array);81 result = array;82 // store it on the parent (or at the result pointer)83 store<usize>(parentPtr + arrayOffset, array);84 this.binaryIndex += <i32>(offsetof<__dataSegment>() + arrayByteLength); // advance the buffer85 break;86 }87 case ASON_INSTRUCTION_TYPE.CIRCULAR:88 case ASON_INSTRUCTION_TYPE.REFERENCE:89 case ASON_INSTRUCTION_TYPE.NULL:90 case ASON_INSTRUCTION_TYPE.ARRAY: {91 let result: usize = 0;92 // allocate the right kind of "buffer"93 let buffer = arrayIsStaticArray94 ? __new(arrayByteLength, arrayClassId)95 : __new(arrayByteLength, idof<ArrayBuffer>());96 __pin(buffer);97 // push the stack98 let workingIndex = ++this.stackIndex;99 // We will be writing to the "buffer"100 this.stack[workingIndex] = buffer; // data is written to this buffer101 // If this is a static array the "buffer" is the result102 if (arrayIsStaticArray) {103 result = buffer;104 } else {105 // we need to allocate another reference for the array itself106 result = __new(offsetof<Array<usize>>(), arrayClassId);107 // immediately pin and link the array to the buffer108 __pin(result);109 __link(result, buffer, false);110 // now we can unpin the buffer111 __unpin(buffer);112 // TODO: Assemble the array properties here113 // private buffer: ArrayBuffer;114 store<usize>(result, buffer, offsetof<Array<usize>>("buffer"));115 //private dataStart: usize;116 store<usize>(result, buffer, offsetof<Array<usize>>("dataStart"));117 // private byteLength: i32;118 store<i32>(result, <i32>arrayByteLength, offsetof<Array<usize>>("byteLength"));119 // private length_: i32;120 store<i32>(result, <i32>arrayLength, offsetof<Array<usize>>());121 }122 // we could consume circular references123 this.seen.set(arrayEntryId, result);124 while (125 this.tryConsumeCircular(token)126 || this.tryConsumeReference(token)127 || this.tryConsumeArray(token)128 || (arrayValueIsNullable && this.tryConsumeNull(token))129 ) {130 token = this.peek();131 }132 this.stackIndex--;133 break;134 }135 default: throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);136 }137 store<usize>(parentPtr + arrayOffset, result);138 __link(parentPtr, result, false);139 this.consumePop();140 return true;141 }142 private tryConsumeReference(token: ASON_INSTRUCTION_TYPE): bool {143 if (token != ASON_INSTRUCTION_TYPE.REFERENCE) return false;144 let referenceSegment = this.referenceSegment;145 let referenceByteLength = referenceSegment.byteLength;146 let referenceClassId = referenceSegment.classId;147 let referenceEntryId = referenceSegment.entryId;148 let referenceOffset = referenceSegment.offset;149 let stackIndex = this.stackIndex;150 let parentPointer = this.stack[stackIndex];151 let ref = __pin(__new(referenceByteLength, referenceClassId));152 store<usize>(parentPointer + referenceOffset, ref);153 __link(parentPointer, ref, false);154 if (this.seen.has(referenceEntryId)) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);155 trace("ref is", 2, <f64>referenceEntryId, <f64>ref);156 this.seen.set(referenceEntryId, ref);157 let currentStackIndex = stackIndex + 1;158 this.stackIndex = currentStackIndex;159 this.stack[currentStackIndex] = ref;160 this.binaryIndex += offsetof<__referenceSegment>();161 token = this.peek();162 while (163 this.tryConsumeArray(token)164 || this.tryConsumeCircular(token)165 || this.tryConsumeNull(token)166 || this.tryConsumeReference(token)167 || this.tryConsumeValue(token)168 ) {169 token = this.peek();170 }171 this.stackIndex = stackIndex;172 this.consumePop();173 return true;174 }175 private tryConsumeValue(token: ASON_INSTRUCTION_TYPE): bool {176 if (token != ASON_INSTRUCTION_TYPE.VALUE) return false;177 // consume the valueSegment178 let nextIndex = this.binaryIndex + offsetof<__valueSegment>();179 if (nextIndex >= this.binary.length) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);180 let valueSegment = this.valueSegment;181 // used as top level validation182 // let valueIsFloat = valueSegment.isFloat;183 let valueOffset = valueSegment.offset;184 let valueSize = valueSegment.size;185 let valuePointer = changetype<usize>(valueSegment) + offsetof<__valueSegment>("value");186 let parentPointer = this.stack[this.stackIndex];187 switch (valueSize) {188 case 1: {189 store<u8>(parentPointer + valueOffset, load<u8>(valuePointer));190 break;191 }192 case 2: {193 store<u16>(parentPointer + valueOffset, load<u16>(valuePointer));194 break;195 }196 case 4: {197 store<u32>(parentPointer + valueOffset, load<u32>(valuePointer));198 break;199 }200 case 8: {201 store<u64>(parentPointer + valueOffset, load<u64>(valuePointer));202 break;203 }204 }205 // cannot obtain the value exactly206 // let value = valueSegment.value;207 this.binaryIndex = nextIndex;208 return true;209 }210 private consumePop(): void {211 if (this.peek() != ASON_INSTRUCTION_TYPE.POP) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);212 let nextIndex = this.binaryIndex + offsetof<__popSegment>();213 if (nextIndex >= this.binary.length) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);214 this.binaryIndex = nextIndex;215 }216 private tryConsumeNull(token: ASON_INSTRUCTION_TYPE): bool {217 if (token != ASON_INSTRUCTION_TYPE.NULL) return false;218 let nullSegment = this.nullSegment;219 let parent = this.stack[this.stackIndex];220 store<usize>(parent + nullSegment.offset, 0);221 let nextIndex = this.binaryIndex + offsetof<__nullSegment>();222 if (nextIndex >= this.binary.length) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);223 this.binaryIndex = nextIndex;224 return true;225 }226 private tryConsumeCircular(token: ASON_INSTRUCTION_TYPE): bool {227 if (token != ASON_INSTRUCTION_TYPE.CIRCULAR) return false;228 trace("found circular");229 let circularSegment = this.circularSegment;230 let circularSegmentID = circularSegment.id;231 let circularSegmentOffset = circularSegment.offset;232 let parent = this.stack[this.stackIndex];233 if (!this.seen.has(circularSegmentID)) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);234 let child = this.seen.get(circularSegmentID);235 trace("circularInfo", 4,236 <f64>circularSegmentID,237 <f64>circularSegmentOffset,238 <f64>parent,239 <f64>child,240 );241 store<usize>(parent + circularSegmentOffset, child);242 __link(parent, child, true);243 this.binaryIndex += offsetof<__circularSegment>();244 return true;245 }246 private consumeEnd(): void {247 if (this.peek() != ASON_INSTRUCTION_TYPE.END248 || (this.binaryIndex + offsetof<__endSegment>()) != this.binary.length249 ) throw new Error(ASON_ERROR.E_INVALID_DATA_FORMAT);250 this.binaryIndex = this.binary.length; // all consumed251 }252 private get valueSegment(): __valueSegment {253 return changetype<__valueSegment>(changetype<usize>(this.binary) + <usize>this.binaryIndex);254 }255 private get arraySegment(): __arraySegment {256 return changetype<__arraySegment>(changetype<usize>(this.binary) + <usize>this.binaryIndex);257 }258 private get nullSegment(): __nullSegment {259 return changetype<__nullSegment>(changetype<usize>(this.binary) + <usize>this.binaryIndex);260 }261 private get circularSegment(): __circularSegment {262 return changetype<__circularSegment>(changetype<usize>(this.binary) + <usize>this.binaryIndex);263 }264 private get referenceSegment(): __referenceSegment {265 return changetype<__referenceSegment>(changetype<usize>(this.binary) + <usize>this.binaryIndex);266 }267 private get dataSegment(): __dataSegment {268 return changetype<__dataSegment>(changetype<usize>(this.binary) + <usize>this.binaryIndex);269 }...
wav.ts
Source:wav.ts
1// Copyright (c) 2017 Tracktunes Inc2// see: http://soundfile.sapp.org/doc/WaveFormat/3export function makeWavBlobHeaderView(4 nSamples: number,5 sampleRate: number6): DataView {7 'use strict';8 const arrayByteLength: number = nSamples * 2,9 headerView: DataView = new DataView(new ArrayBuffer(44)),10 writeAscii:11 (dataView: DataView, offset: number, text: string) => void =12 (dataView: DataView, offset: number, text: string) => {13 const len: number = text.length;14 for (let i: number = 0; i < len; i++) {15 dataView.setUint8(offset + i, text.charCodeAt(i));16 }17 };18 //19 // NB: this is single-channel (mono)20 //21 // 0-4: ChunkId22 writeAscii(headerView, 0, 'RIFF');23 // 4-8: ChunkSize24 headerView.setUint32(4, 36 + arrayByteLength, true);25 // 8-12: Format26 writeAscii(headerView, 8, 'WAVE');27 // 12-16: Subchunk1ID28 writeAscii(headerView, 12, 'fmt ');29 // 16-20: Subchunk1Size30 headerView.setUint32(16, 16, true);31 // 20-22: AudioFormat32 headerView.setUint16(20, 1, true);33 // 22-24: NumChannels34 headerView.setUint16(22, 1, true);35 // 24-28: SampleRate36 headerView.setUint32(24, sampleRate, true);37 // 28-32: ByteRate38 headerView.setUint32(28, sampleRate * 2, true);39 // 32-34: BlockAlign40 headerView.setUint16(32, 2, true);41 // 34-36: BitsPerSample42 headerView.setUint16(34, 16, true);43 // 36-40: Subchunk2ID44 writeAscii(headerView, 36, 'data');45 // 40-44: Subchunk2Size46 headerView.setUint32(40, arrayByteLength, true);47 return headerView;...
util.spec.ts
Source:util.spec.ts
...15 randomNodeId = generateRandomNodeIdAtDistance(nodeId, 25)16 t.ok(log2Distance(nodeId, randomNodeId) === 25, 'calculated random node id at distance 25')17 const arrayOfUint8Arrays = [Uint8Array.from([1, 2, 3]), Uint8Array.from([1, 2])]18 const arrayOfBuffers = [Buffer.from([1, 2, 3]), Buffer.from([1, 2, 3, 4, 5])]19 t.equal(arrayByteLength(arrayOfUint8Arrays), 5, 'computed correct length of nested Uint8Arrays')20 t.equal(arrayByteLength(arrayOfBuffers), 8, 'computed correct length of nested Buffers')21 t.equal(await dirSize('./test/util/testDir'), 0.00002765655517578125)22 t.end()...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var arrayByteLength = wptoolkit.arrayByteLength;3var array = [1, 2, 3, 4, 5];4var arrayByteLength = arrayByteLength(array);5console.log(arrayByteLength);6var wptoolkit = require('wptoolkit');7var arrayByteLength = wptoolkit.arrayByteLength;8var array = [1, 2, 3, 4, 5];9var arrayByteLength = arrayByteLength(array);10console.log(arrayByteLength);11var wptoolkit = require('wptoolkit');12var arrayByteLength = wptoolkit.arrayByteLength;13var array = [1, 2, 3, 4, 5];14var arrayByteLength = arrayByteLength(array);15console.log(arrayByteLength);16var wptoolkit = require('wptoolkit');17var arrayByteLength = wptoolkit.arrayByteLength;18var array = [1, 2, 3, 4, 5];19var arrayByteLength = arrayByteLength(array);20console.log(arrayByteLength);21var wptoolkit = require('wptoolkit');22var arrayByteLength = wptoolkit.arrayByteLength;23var array = [1, 2, 3, 4, 5];24var arrayByteLength = arrayByteLength(array);25console.log(arrayByteLength);26var wptoolkit = require('wptoolkit');27var arrayByteLength = wptoolkit.arrayByteLength;28var array = [1, 2, 3, 4, 5];29var arrayByteLength = arrayByteLength(array);30console.log(arrayByteLength);
Using AI Code Generation
1var wptb = require('wptb');2var testArray = [1,2,3,4,5];3var testArray2 = [1,2,3,4,5,6,7,8,9,10];4var testArray3 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];5console.log(wptb.arrayByteLength(testArray));6console.log(wptb.arrayByteLength(testArray2));7console.log(wptb.arrayByteLength(testArray3));8var wptb = require('wptb');9var testArray = [1,2,3,4,5];10var testArray2 = [1,2,3,4,5,6,7,8,9,10];11var testArray3 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];12console.log(wptb.arrayByteLength(testArray));13console.log(wptb.arrayByteLength(testArray2));14console.log(wptb.arrayByteLength(testArray3));
Using AI Code Generation
1var decoder = new TextDecoder();2var buffer = new ArrayBuffer(100);3var view = new Uint8Array(buffer);4view[0] = 0x41;5view[1] = 0x42;6view[2] = 0x43;7decoder.decode(view);8decoder.decode(view, {stream: true});9decoder.decode();10decoder.decode(view, {stream: true});11(function(global) {12 'use strict';13 var TextDecoder = function(encoding, options) {14 if (!this || this === global) {15 return new TextDecoder(encoding, options);16 }17 var label = encoding ? String(encoding) : 'utf-8';18 var fatal = options ? Boolean(options.fatal) : false;19 var ignoreBOM = options ? Boolean(options.ignoreBOM) : false;20 var decoder = new TextDecoder(label, {fatal: fatal});21 this.decode = function(input, options) {22 if (input === undefined) {23 return decoder.decode();24 }25 if (options && options.stream) {26 return decoder.decode(input, {stream: true});27 }28 return decoder.decode(input);29 };30 };31 global.TextDecoder = TextDecoder;32})(this);33(function(global) {34 'use strict';35 var TextEncoder = function() {36 if (!this || this === global) {37 return new TextEncoder();38 }39 this.encode = function(input) {40 return new Uint8Array(0);41 };42 };43 global.TextEncoder = TextEncoder;44})(this);45(function(global) {46 'use strict';47 var TextDecoder = function(encoding, options) {48 if (!this || this === global) {49 return new TextDecoder(encoding, options);50 }51 var label = encoding ? String(encoding) : 'utf-8';52 var fatal = options ? Boolean(options.fatal) : false;53 var ignoreBOM = options ? Boolean(options.ignoreBOM) : false;54 var decoder = new TextDecoder(label, {fatal: fatal});55 this.decode = function(input, options) {56 if (input === undefined) {57 return decoder.decode();58 }59 if (options && options.stream) {60 return decoder.decode(input, {stream: true});61 }62 return decoder.decode(input);
Using AI Code Generation
1let encoder = new TextEncoder('utf-8');2let encoded = encoder.encode('Hello World');3let length = encoded.byteLength;4let decoder = new TextDecoder('utf-8');5let decoded = decoder.decode(encoded);6let length = decoded.byteLength;
Using AI Code Generation
1var buffer = new ArrayBuffer(8);2var view = new Int32Array(buffer);3var arrayByteLength = view.byteLength;4console.log(arrayByteLength);5var buffer = new ArrayBuffer(8);6var view = new Int32Array(buffer);7var arrayByteLength = view.byteLength;8console.log(arrayByteLength);9var buffer = new ArrayBuffer(8);10var view = new Int32Array(buffer);11var arrayByteLength = view.byteLength;12console.log(arrayByteLength);13var buffer = new ArrayBuffer(8);14var view = new Int32Array(buffer);15var arrayByteLength = view.byteLength;16console.log(arrayByteLength);17var buffer = new ArrayBuffer(8);18var view = new Int32Array(buffer);19var arrayByteLength = view.byteLength;20console.log(arrayByteLength);21var buffer = new ArrayBuffer(8);22var view = new Int32Array(buffer);23var arrayByteLength = view.byteLength;24console.log(arrayByteLength);25var buffer = new ArrayBuffer(8);26var view = new Int32Array(buffer);27var arrayByteLength = view.byteLength;28console.log(arrayByteLength);29var buffer = new ArrayBuffer(8);30var view = new Int32Array(buffer);31var arrayByteLength = view.byteLength;32console.log(arrayByteLength);33var buffer = new ArrayBuffer(8);34var view = new Int32Array(buffer);35var arrayByteLength = view.byteLength;36console.log(arrayByteLength);37var buffer = new ArrayBuffer(8);38var view = new Int32Array(buffer);39var arrayByteLength = view.byteLength;40console.log(arrayByteLength);41var buffer = new ArrayBuffer(8);42var view = new Int32Array(buffer);43var arrayByteLength = view.byteLength;44console.log(arrayByteLength);45var buffer = new ArrayBuffer(8);46var view = new Int32Array(buffer);
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!!