From 90d20eb51b6cb7ea4eac95d586887118bff318da Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Mon, 23 Oct 2023 18:10:45 -0400 Subject: [PATCH 1/3] feat: ffi example --- data/ffi.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ toc.ts | 1 + 2 files changed, 56 insertions(+) create mode 100644 data/ffi.ts diff --git a/data/ffi.ts b/data/ffi.ts new file mode 100644 index 0000000..4c72083 --- /dev/null +++ b/data/ffi.ts @@ -0,0 +1,55 @@ +/** + * @title Foreign Function Interface + * @difficulty intermediate + * @tags cli + * @run --unstable --allow-ffi + * @resource {https://docs.deno.com/runtime/manual/runtime/ffi_api} Manual: FFI + * + * Foreign Function Interface (FFI) is a way to call functions written in other languages from JavaScript. + */ + +// Prerequisites: +// - [Julia](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`, + 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))"), +); diff --git a/toc.ts b/toc.ts index 4e0e81f..ae3e73d 100644 --- a/toc.ts +++ b/toc.ts @@ -129,6 +129,7 @@ export const TOC: TocGroup[] = [ title: "Advanced", icon: IconStars, items: [ + "ffi", "web-workers", "webassembly", ], From fed0bbc916da82828478d9cfc88a5cd07b902b54 Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Tue, 24 Oct 2023 09:34:55 -0400 Subject: [PATCH 2/3] feat: add denonomicon to resources --- data/ffi.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/data/ffi.ts b/data/ffi.ts index 4c72083..00db2aa 100644 --- a/data/ffi.ts +++ b/data/ffi.ts @@ -4,6 +4,7 @@ * @tags cli * @run --unstable --allow-ffi * @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. */ From 8824c94e784e964a3f7720db175974bb97bc5f77 Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Tue, 24 Oct 2023 09:37:51 -0400 Subject: [PATCH 3/3] fix: links --- data/ffi.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/ffi.ts b/data/ffi.ts index 00db2aa..126af0f 100644 --- a/data/ffi.ts +++ b/data/ffi.ts @@ -4,13 +4,13 @@ * @tags cli * @run --unstable --allow-ffi * @resource {https://docs.deno.com/runtime/manual/runtime/ffi_api} Manual: FFI - * @resource {https://denonomicon.deno.dev/} Denonomicon + * @resource {https://denonomicon.deno.dev} Denonomicon * * Foreign Function Interface (FFI) is a way to call functions written in other languages from JavaScript. */ // Prerequisites: -// - [Julia](https://julialang.org/downloads/) installed +// - 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.