diff --git a/src/types/argument/array.js b/src/types/argument/array.js index 5a9de11..1051114 100644 --- a/src/types/argument/array.js +++ b/src/types/argument/array.js @@ -66,14 +66,10 @@ function getArrayElement(pointer, tag, i) { } } -export function unboxArray(type, value, length) { - const pointer = cast_u64_ptr(new ExtendedDataView(value).getBigUint64()); +export function unboxArray(type, array, length = -1) { + if (!array) return null; - if (!pointer) return null; - - const array = cast_u64_ptr( - new ExtendedDataView(deref_buf(pointer, 8)).getBigUint64(), - ); + const pointer = cast_u64_ptr(array); const paramType = g.type_info.get_param_type(type, 0); const paramTag = g.type_info.get_tag(paramType); @@ -81,18 +77,19 @@ export function unboxArray(type, value, length) { let buffer; - if (!array) { + // manually get the length of the array + if (length === -1) { + let i = 0; + while (getArrayElement(pointer, paramTag, i) !== 0) i++; + + length = i * getTypeSize(paramTag); + } + + if (length <= 0) { // empty array, just return an empty TypedArray instead of returning null buffer = null; } else { - if (length === -1) { - let i = 0; - while (getArrayElement(array, paramTag, i) !== 0) i++; - - length = i * getTypeSize(paramTag); - } - - buffer = deref_buf(array, length); + buffer = deref_buf(pointer, length); } switch (paramTag) { diff --git a/src/types/callable.js b/src/types/callable.js index 2f0206a..e6a9d10 100644 --- a/src/types/callable.js +++ b/src/types/callable.js @@ -39,7 +39,7 @@ export function parseCallableArgs(info) { const argDetails = []; for (let i = 0; i < nArgs; i++) { const argInfo = g.callable_info.get_arg(info, i); - const arg = { ...createArg(argInfo), index: i }; + const arg = createArg(argInfo); argDetails.push(arg); g.base_info.unref(argInfo); } @@ -74,35 +74,28 @@ export function parseCallableArgs(info) { }; const parseOutArgs = (outArgs) => { - return outArgsDetail - .map((arg, index) => { - // keep the index for the outArgs - return { arg, index }; - }) - .filter(({ arg }) => { - // lengthArgs are not returned - return !(outArgsDetail.some((d) => d.arrLength === arg.index)); - }) - .map(({ arg, index }) => { - let length = -1; - - // get the value of the length argument - if (arg.arrLength !== -1) { - const lengthArg = outArgsDetail.findIndex(({ index }) => - arg.arrLength === index - ); - const lengthPointer = outArgs[lengthArg]; - length = new ExtendedDataView( - deref_buf(cast_u64_ptr(lengthPointer), 8), - ).getBigUint64(); - } - - return unboxArgument( - arg.type, - new BigUint64Array([outArgs[index]]).buffer, - length, - ); - }); + const values = []; + + for (const [index, arg] of outArgsDetail.entries()) { + if (outArgsDetail.some((d) => d.arrLength === index)) continue; + + // extract the length of this argument (if it's an array) + const lengthArg = outArgsDetail.findIndex(({ index }) => + arg.arrLength === index + ); + + let length = -1; + + if (lengthArg !== -1) { + const lengthPointer = cast_u64_ptr(outArgs[lengthArg]); + length = new ExtendedDataView(deref_buf(lengthPointer, 8)) + .getBigUint64(); + } + + values.push(unboxArgument(arg.type, outArgs[index], length)); + } + + return values; }; return [parseInArgs, initOutArgs, parseOutArgs];