This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
feat: ffi example #108
Open
load1n9
wants to merge
3
commits into
denoland:main
Choose a base branch
from
load1n9:ffi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: ffi example #108
Changes from all commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** | ||
| * @title Foreign Function Interface | ||
| * @difficulty intermediate | ||
| * @tags cli | ||
| * @run --unstable --allow-ffi <url> | ||
| * @resource {https://docs.deno.com/runtime/manual/runtime/ffi_api} Manual: FFI | ||
| * @resource {https://denonomicon.deno.dev} Denonomicon | ||
| * | ||
| * Foreign Function Interface (FFI) is a way to call functions written in other languages from JavaScript. | ||
| */ | ||
|
|
||
| // Prerequisites: | ||
| // - Julia from https://julialang.org/downloads/ installed | ||
| // - `julia` in your `PATH` | ||
|
|
||
| // This example shows how to use the julia FFI library to execute Julia code from Deno. | ||
|
|
||
| // First lets define the symbols we want to import from the library. | ||
| const SYMBOLS = { | ||
| // The `jl_init` function initializes the Julia runtime. | ||
| jl_init: { | ||
| // The function takes no parameters so we pass an empty array. | ||
| parameters: [], | ||
| // The function returns a `void` pointer. | ||
| result: "void", | ||
| }, | ||
| // The `jl_eval_string` function takes a string and evaluates it as Julia code. | ||
| jl_eval_string: { | ||
| // The function takes a string so we pass an array with a buffer type | ||
| parameters: ["buffer"], | ||
| // The function results in a `pointer`. | ||
| result: "pointer", | ||
| }, | ||
| } as const; | ||
|
|
||
| // Next, we import the FFI library from Deno. | ||
| const julia = Deno.dlopen( | ||
| // Change .dll to .dylib on macOS and .so on Linux | ||
| `libjulia.dll`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this should be the default, but no strong feelings. |
||
| SYMBOLS, | ||
| ).symbols; | ||
|
|
||
| // Before we can call the functions we need to convert the string to a C string. Let's define a helper function for that. | ||
| export function cstr(str: string): Uint8Array { | ||
| const buf = new Uint8Array(str.length + 1); | ||
| new TextEncoder().encodeInto(str, buf); | ||
| return buf; | ||
| } | ||
|
|
||
| // We can now call the functions we imported. | ||
| julia.jl_init(); | ||
|
|
||
| // We can now evaluate Julia code. | ||
| julia.jl_eval_string( | ||
| cstr("println(sqrt(2.0))"), | ||
| ); | ||
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
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.