|
1 | 1 | /** |
2 | | - * Module that provides exports for serialized binary files. |
| 2 | + * Module that provides serialized binaries and FFI metadata. |
| 3 | + * |
| 4 | + * The matching C API metadata, is available as |
| 5 | + * `ffi` property in every runtime. |
3 | 6 | * |
4 | 7 | * @example |
5 | | - * Import the corresponding binary and write the file to disk. |
| 8 | + * Usage with Deno |
| 9 | + * |
| 10 | + * Deno provides native JSR imports and the built-in `Deno.dlopen` FFI API: |
6 | 11 | * |
7 | 12 | * ```ts |
8 | 13 | * import { x86_64 } from "@serial/cpp-bindings-windows/bin"; |
| 14 | + * |
| 15 | + * const binary = x86_64; |
| 16 | + * const path = `./${binary.filename}`; |
| 17 | + * |
| 18 | + * Deno.writeFileSync(path, Uint8Array.fromBase64(binary.data)); |
| 19 | + * |
| 20 | + * const library = Deno.dlopen(path, { |
| 21 | + * serialOpen: { |
| 22 | + * parameters: ["pointer", "i32", "i32", "i32", "i32", "pointer"], |
| 23 | + * result: "i64", |
| 24 | + * }, |
| 25 | + * }); |
| 26 | + * library.close(); |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * @example |
| 30 | + * Usage with Bun |
9 | 31 | * |
10 | | - * Deno.writeFileSync(`./${x86_64.filename}`, Uint8Array.fromBase64(x86_64.data)); |
| 32 | + * Use Bun's built-in `bun:ffi` and `Bun.write` APIs: |
| 33 | + * |
| 34 | + * ```ts |
| 35 | + * import { dlopen } from "bun:ffi"; |
| 36 | + * import { resolve } from "node:path"; |
| 37 | + * import { x86_64 } from "@serial/cpp-bindings-windows/bin"; |
| 38 | + * |
| 39 | + * const binary = x86_64; |
| 40 | + * |
| 41 | + * const path = resolve(binary.filename); |
| 42 | + * await Bun.write(path, Buffer.from(binary.data, "base64")); |
| 43 | + * |
| 44 | + * const library = dlopen(path, { |
| 45 | + * serialOpen: { |
| 46 | + * args: ["ptr", "i32", "i32", "i32", "i32", "ptr"], |
| 47 | + * returns: "i64", |
| 48 | + * }, |
| 49 | + * }); |
| 50 | + * library.close(); |
11 | 51 | * ``` |
| 52 | + * |
| 53 | + * @example |
| 54 | + * Usage with Node.js |
| 55 | + * |
| 56 | + * Node.js does not provide a general-purpose C FFI API. This example uses |
| 57 | + * [Koffi](https://koffi.dev/), together with JSR's npm compatibility layer: |
| 58 | + * |
| 59 | + * ```js |
| 60 | + * import { writeFileSync } from "node:fs"; |
| 61 | + * import { resolve } from "node:path"; |
| 62 | + * import koffi from "koffi"; |
| 63 | + * import { x86_64 } from "@serial/cpp-bindings-windows/bin"; |
| 64 | + * |
| 65 | + * const binary = x86_64; |
| 66 | + * |
| 67 | + * const path = resolve(binary.filename); |
| 68 | + * writeFileSync(path, Buffer.from(binary.data, "base64")); |
| 69 | + * |
| 70 | + * const library = koffi.load(path); |
| 71 | + * library.func("serialOpen", "int64_t", [ |
| 72 | + * "void *", |
| 73 | + * "int", |
| 74 | + * "int", |
| 75 | + * "int", |
| 76 | + * "int", |
| 77 | + * "void *", |
| 78 | + * ]); |
| 79 | + * library.unload(); |
| 80 | + * ``` |
| 81 | + * |
12 | 82 | * @module |
13 | 83 | */ |
14 | 84 |
|
15 | | -import x86_64 from "../../bin/x86_64.json" with { type: "json" }; |
| 85 | +import x86_64Library from "../../bin/x86_64/library.json" with { type: "json" }; |
| 86 | +import x86_64ffi from "../../bin/x86_64/ffi.json" with { type: "json" }; |
| 87 | + |
| 88 | +/** |
| 89 | + * The serialized `x86_64-windows` shared library and its FFI metadata. |
| 90 | + * |
| 91 | + * The library targets the generic x86-64 baseline. Decode `data` from base64 |
| 92 | + * and write it to `filename` before loading it using the filesystem |
| 93 | + * and native FFI APIs provided by your runtime. |
| 94 | + */ |
| 95 | +const x86_64 = { |
| 96 | + ...x86_64Library, |
| 97 | + /** |
| 98 | + * ASTrein-generated metadata describing the library's exported C API. |
| 99 | + * |
| 100 | + * It contains symbols, parameter and return types, callbacks, struct |
| 101 | + * definitions, default values, and API documentation for generating |
| 102 | + * runtime-specific FFI adapters. |
| 103 | + */ |
| 104 | + ffi: x86_64ffi, |
| 105 | +}; |
16 | 106 |
|
17 | 107 | export { x86_64 }; |
0 commit comments