Debian Codegraph @

wasm/browser/src/utils/structure-buffer-to-object.js

raw
1 import { sizeOfPrimitive } from "./native-sizes.js";
2 import { uint2String } from "./text-encoders.js";
3 import { trimNull } from "./trim-null.js";
4
5 export const structBufferToObject = (jsStruct, buffer) => {
6 const [result] = jsStruct.reduce(
7 ([parameters, offset], [parameterName, primitive, ...rest]) => {
8 const currentSize =
9 primitive === "char" ? sizeOfPrimitive[primitive] * rest[0] : sizeOfPrimitive[primitive];
10 const currentValue =
11 primitive === "char"
12 ? trimNull(uint2String(buffer.subarray(offset, currentSize))) || ""
13 : buffer[offset];
14 parameters[parameterName] = currentValue;
15 return [parameters, offset + currentSize];
16 },
17 [{}, 0],
18 );
19 return result;
20 };