Rewrite FFI structs section with a runnable example#3107
Open
lunadogbot wants to merge 2 commits into
Open
Conversation
Contributor
Author
|
@bartlomieju ready for review when you have a moment. |
1 task
bartlomieju
reviewed
May 14, 2026
Member
bartlomieju
left a comment
There was a problem hiding this comment.
Nice work, please remove em dashes from prose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The "Working with structs" section in
runtime/fundamentals/ffi.mdhad anexample that did not run: it defined the struct as
{ fields: { x: "f64", y: "f64" } }(the supported-types table on the same page documents{ struct: [...] }as an array of field types), callednew Deno.UnsafePointer(...)asa constructor, and built struct values by passing
BigInt(Float64Array.of(1.0) .buffer)— which throwsTypeError: Cannot convert [object ArrayBuffer] to a BigInt. A reader copying it would hit that error before getting anywhere.This PR replaces the section with a complete walkthrough: a small C
Pointlibrary, the
cccommand to build it, and a Deno script that callsdistanceandmidpointusing{ struct: ["f64", "f64"] }, passing valuesas a
Uint8Arrayview of aFloat64Arrayand decoding the returned structback through a
Float64Array. The observedconsole.logoutput is shownverbatim. A short "things to keep in mind" list calls out padding, positional
fields, and how returned structs come back as bytes.
Verified by building
libpoint.sowithcc 13.3.0and running the exactscript from the page inside
denoland/deno:latest(2.7.14 — no:canarytag is currently published) with
--allow-ffi. Output matched:distance = 5,midpoint = { x: 2.5, y: 4 }.deno fmtwas run on the file. Thesection's only change is to itself; no other sections were touched.
Closes bartlomieju/orchid-inbox#46