Skip to content

Commit

Permalink
simplify array unboxing code and use direct pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
vixalien committed Jan 15, 2024
1 parent 7ae040c commit aefedbb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 deletions.
29 changes: 13 additions & 16 deletions src/types/argument/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,30 @@ 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);
g.base_info.unref(paramType);

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) {
Expand Down
53 changes: 23 additions & 30 deletions src/types/callable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit aefedbb

Please sign in to comment.