[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Array Arguments Better #21

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft

Conversation

vixalien
Copy link
Collaborator

Hello again. This PR is currently a draft because while it does not depend on #17, the code there needs to be merged before this one because by doing so, the code in this PR will be vastly simplified.

This PR does 3 main things:

1. Unbox C Arrays into TypedArrays (Uint8Array, Uint16Array etc..)

This is similar to GJS behaviour and differs from the current behaviour where each element would be unboxed into an argument. This doesn't work for stuff like GLib.file_get_contents, which returns an array, but the array is of random data. This PR would, for example, produce a Uint8Array instead of an array of numbers.

2. Utilise the lengthArg to unboxArrays

Functions that return an array usually have a length parameter. For the GLib.file_get_contents case, that parameter is called length. The bindings now automatically infer the array's length from the related parameter.

image

3. Omit the length parameter from being returned.

In the case of the above function, since GLib.file_get_contents returns an array, the length parameter is no longer required as we can infer that from Uint8Array.length and adds to the noise. This behaviour is what GJS does, too.

TODO in a separate PR: Handle different arrays based on the GIArrayType. Currently, deno_gi only handles C arrays, but there are other types of arrays namely: GArray, GPtrArray and GByteArray. Adding support for them should be pretty straightforward. Maybe an issue needs to be filed to keep track of this.

@@ -24,15 +24,19 @@ export function initArgument(type) {
g.base_info.unref(info);
return result;
}
default: {
return cast_ptr_u64(cast_buf_ptr(new Uint8Array(1)));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I believe all arguments should be initialised to an empty pointer (GIArgInfo)

This comment was marked as duplicate.

@@ -24,15 +24,19 @@ export function initArgument(type) {
g.base_info.unref(info);
return result;
}
default: {
return cast_ptr_u64(cast_buf_ptr(new Uint8Array(1)));

This comment was marked as duplicate.

src/types/callable.js Outdated Show resolved Hide resolved
@vixalien
Copy link
Collaborator Author
vixalien commented Jan 15, 2024

TODO: handle return values' length DONE

Here's some test code for this:

import { require } from "../mod.ts";

const GLib = require("GLib", "2.0");

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const bfr = encoder.encode("Hello, World!");

const bytes = GLib.Bytes.new(bfr, bfr.length);

// console.log("size", bytes.getSize());

const data = bytes.getData();
console.log("data", data);

Although currently, it seems GLib.Bytes.getData() doesn't set the size argument. this needs to be investigated.

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
ensures that the return argument's length can be inferred
For example, `GLib.Bytes.getData`. This needs to be investigated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant