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

feat(ext/ffi): Add static method variants to Deno.UnsafePointerView #15146

Merged

Conversation

aapoalas
Copy link
Collaborator

Add static method variants to Deno.UnsafePointerView for usage with pointer BigInts. Since Deno.UnsafePointer is no longer a thing that exists and BigInt values are returned directly, it seems like it might be helpful to offer static variants for the more direct return types that Deno FFI does not directly support.

This PR depends on #15143

Examples:

const pointer = dylib.symbols.get_pointer();
const string = Deno.UnsafePointerView.getCString(pointer); // Function returning a cstring is common enough
const buffer = Deno.UnsafePointerView.getArrayBuffer(pointer); // Getting a zero-copy AB from a pointer is useful, especially if mutations need to be done.
const copyBuffer = new Uint8Array(32);
Deno.UnsafePointerView.copyInto(pointer, copyBuffer); // Less useful perhaps, but if a copy of a buffer is needed, it can be done.

These static methods do not have pointer offsets offered, as those seem like a special case and if offsets are needed, then creating a Deno.UnsafePointerView seems apt to actually "view into" the pointer.

Additionally, I did not create static methods for the numeric return values as it seems unlikely that a function would return just a *const u8 or similar. Again, if viewing into a pointer that contains a struct which contains numeric values is needed, then creating a Deno.UnsafePointerView seems apt.

@aapoalas
Copy link
Collaborator Author

Should I add offset parameters to these APIs? I think they'd be useless but it'd then align with the existing APIs.

Reasons for uselessness:

  1. getCString: I would presume any char pointer with a prefixed header would be quite uncommon. It could be handled using getArrayBuffer for the header + subarray to the end of the header, to UnsafePointer.of() + getCString.
  2. getArrayBuffer / copyInto: Any offset can be handled with a subarray.

@littledivy littledivy merged commit 9c594de into denoland:main Aug 5, 2022
@aapoalas aapoalas deleted the feat-ext-ffi-static-pointer-view-methods branch October 9, 2022 08:01
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.

None yet

2 participants