Skip to content

Commit

Permalink
unbox arrays using the provided length arg
Browse files Browse the repository at this point in the history
and don't return the length argument. It's just noise since you can already get the length of an array using JS's .length and is ommitted in GJS as well. the code doesn't read too well though
  • Loading branch information
vixalien committed Dec 19, 2023
1 parent c365da3 commit a0c5154
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/types/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ export function initArgument(type) {
/**
* @param {Deno.PointerObject} type
* @param {ArrayBufferLike} value
* @param {number?} length
* @returns
*/
export function unboxArgument(type, value) {
export function unboxArgument(type, value, length) {
const dataView = new ExtendedDataView(value);
const tag = g.type_info.get_tag(type);
const pointer = dataView.getBigUint64();
Expand Down Expand Up @@ -92,7 +93,7 @@ export function unboxArgument(type, value) {
/* non-basic types */

case GITypeTag.ARRAY: {
return unboxArray(type, value, -1);
return unboxArray(type, value, length);
}

case GITypeTag.GLIST:
Expand Down
46 changes: 40 additions & 6 deletions src/types/callable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cast_u64_ptr, deref_buf } from "../base_utils/convert.ts";
import {
GIDirection,
GIFunctionInfoFlags,
Expand All @@ -20,7 +21,15 @@ export function createArg(info) {
const transfer = g.arg_info.get_ownership_transfer(info);
const callerAllocates = g.arg_info.is_caller_allocates(info);
const isReturn = g.arg_info.is_return_value(info);
return { type, arrLength, isSkip, direction, transfer, callerAllocates, isReturn };
return {
type,
arrLength,
isSkip,
direction,
transfer,
callerAllocates,
isReturn,
};
}

export function parseCallableArgs(info) {
Expand All @@ -30,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);
const arg = { ...createArg(argInfo), index: i };
argDetails.push(arg);
g.base_info.unref(argInfo);
}
Expand All @@ -39,7 +48,6 @@ export function parseCallableArgs(info) {
(arg) => !(arg.direction & GIDirection.OUT),
);


const outArgsDetail = argDetails.filter(
(arg) => arg.direction & GIDirection.OUT,
);
Expand Down Expand Up @@ -68,9 +76,35 @@ export function parseCallableArgs(info) {
};

const parseOutArgs = (outArgs) => {
return outArgsDetail.map((d, i) => {
return unboxArgument(d.type, new BigUint64Array([outArgs[i]]).buffer);
});
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,
);
});
};

return [parseInArgs, initOutArgs, parseOutArgs];
Expand Down

0 comments on commit a0c5154

Please sign in to comment.