diff --git a/data/ffi.ts b/data/ffi.ts new file mode 100644 index 0000000..126af0f --- /dev/null +++ b/data/ffi.ts @@ -0,0 +1,56 @@ +/** + * @title Foreign Function Interface + * @difficulty intermediate + * @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. + */ + +// 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`, + 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", ],