-
Notifications
You must be signed in to change notification settings - Fork 5k
bun:ffi: throw validation errors from viewSource and JSCallback instead of returning them #34396
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
Merged
+83
−10
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,63 @@ | ||
| import { viewSource } from "bun:ffi"; | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { isArm64, isWindows } from "harness"; | ||
|
|
||
| const isFFIUnavailable = isWindows && isArm64; | ||
|
|
||
| // Captures what a call throws, or undefined if it returned normally. Written | ||
| // explicitly so the assertions below distinguish a thrown Error from a | ||
| // returned one regardless of toThrow()'s handling of returned Errors. | ||
| function thrown(fn: () => unknown): unknown { | ||
| try { | ||
| fn(); | ||
| } catch (e) { | ||
| return e; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| describe.skipIf(isFFIUnavailable)("FFI viewSource", () => { | ||
| test("rejects non-object symbol descriptor values", () => { | ||
| // These should throw a TypeError because each symbol descriptor | ||
| // must be an object like { args: [...], returns: "void" }. | ||
| // Previously, non-object values like numbers or strings would | ||
| // cause a debug assertion failure (crash) in generateSymbolForFunction. | ||
| expect(() => Bun.FFI.viewSource({ myFunc: 42 })).toThrow("Expected an object"); | ||
| expect(() => Bun.FFI.viewSource({ myFunc: "not_an_object" })).toThrow("Expected an object"); | ||
| expect(() => Bun.FFI.viewSource({ myFunc: true })).toThrow("Expected an object"); | ||
| test("throws on non-object symbol descriptor values", () => { | ||
| // Each symbol descriptor must be an object like { args: [...], returns: "void" }. | ||
| // Previously, non-object values like numbers or strings would cause a debug | ||
| // assertion failure (crash) in generateSymbolForFunction; after that crash | ||
| // was fixed, viewSource returned the TypeError instead of throwing it. | ||
| for (const value of [42, "not_an_object", true] as const) { | ||
| const err = thrown(() => viewSource({ myFunc: value as any })); | ||
| expect(err).toBeInstanceOf(TypeError); | ||
| expect((err as TypeError).message).toContain("Expected an object"); | ||
| } | ||
| }); | ||
|
|
||
| test("throws on an unknown FFI type", () => { | ||
| const err = thrown(() => viewSource({ foo: { args: ["bogus_type" as any], returns: "void" } })); | ||
| expect(err).toBeInstanceOf(TypeError); | ||
| expect((err as TypeError).message).toContain("bogus_type"); | ||
| }); | ||
|
|
||
| test("throws on a non-object options argument", () => { | ||
| for (const value of [null, undefined, 42] as const) { | ||
| const err = thrown(() => viewSource(value as any)); | ||
| expect(err).toBeInstanceOf(TypeError); | ||
| } | ||
| }); | ||
|
|
||
| test("throws on a non-object callback descriptor", () => { | ||
| for (const value of [null, undefined, 42, "str"] as const) { | ||
| const err = thrown(() => viewSource(value as any, true)); | ||
| expect(err).toBeInstanceOf(TypeError); | ||
| expect((err as TypeError).message).toContain("Expected an object"); | ||
| } | ||
| }); | ||
|
|
||
| test("returns the generated source for a valid descriptor", () => { | ||
| const src = viewSource({ foo: { args: ["i32"], returns: "i32" } }); | ||
| expect(src).toBeArray(); | ||
| expect(src).toHaveLength(1); | ||
| expect(src[0]).toContain("JSFunctionCall"); | ||
|
|
||
| const cbSrc = viewSource({ args: ["i32"], returns: "i32" }, true); | ||
| expect(typeof cbSrc).toBe("string"); | ||
| expect(cbSrc).toContain("my_callback_function"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.