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 Jan 6, 2024
1 parent 8adef11 commit 73c65d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/types/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export function initArgument(type) {

/** This function is given a pointer OR a value, and must hence extract it
* @param {Deno.PointerObject} type
* @param {number | bigint} pointer
* @param {ArrayBufferLike} value
* @param {number?} length
* @returns
*/
export function unboxArgument(type, pointer) {
export function unboxArgument(type, value, length) {
const dataView = new ExtendedDataView(value);
const tag = g.type_info.get_tag(type);

switch (tag) {
Expand Down Expand Up @@ -75,7 +77,7 @@ export function unboxArgument(type, pointer) {
/* non-basic types */

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

case GITypeTag.GLIST:
Expand Down
35 changes: 31 additions & 4 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 Down Expand Up @@ -38,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 Down Expand Up @@ -73,9 +74,35 @@ export function parseCallableArgs(info) {
};

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

Please sign in to comment.