You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-11
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,6 @@
3
3
This library creates bindings for accessing Javascript from within a WASM runtime. For example:
4
4
5
5
```zig
6
-
// Example for readme
7
6
const zjb = @import("zjb");
8
7
9
8
export fn main() void {
@@ -32,15 +31,15 @@ This package is clearly inspired by Go's solution to this problem: https://pkg.g
32
31
33
32
## Usage
34
33
35
-
As of March 2024, zjb requires Zig's master version, not 0.11.
34
+
As of May 2024, zjb requires Zig 0.12.0 or greater.
36
35
37
36
Call into Javascript using `zjb`, generate the Javascript side code, and then build an html page to combine them.
38
37
39
38
An end to end example is in the example folder. It includes:
40
39
41
40
-`main.zig` has usage examples for the `zjb` Zig import.
42
41
-`build.zig`'s example for how to set up your build file.
43
-
-`example/static` includes an HTML and a Javascript to run the example.
42
+
-`example/static` includes HTML and Javascript files to run the example.
44
43
45
44
To view the example in action, run `zig build example`. Then host a webserver from `zig-out/bin`.
46
45
@@ -49,12 +48,20 @@ Zjb functions which return a value from Javascript require specifying which type
49
48
-`i32`, `i64`, `f32`, `f64`. These are the only numerical types that are supported by the WASM runtime export function signature, so you must cast to one of these before passing.
50
49
-`comptime_int`, `comptime_float`. These are valid as arguments, and are passed as f64 to Javascript, which is Javascript's main number type.
51
50
-`zjb.Handle`. The Zig side type for referring to Javascript values. Most of the time this will be a Javascript object of some kind, but in some rare cases it might be something else, such as null, a Number, NaN, or undefined. Used as an argument, it is automatically converted into the value that is held in zjb's Javascript `_handles` map. When used as a return value, it is automatically added to zjb's Javascript `_handles` map. It is the caller's responsibility to call `release` to remove it from the `_handles` map when you're done using it.
51
+
-`zjb.ConstHandle` as arguments but not return types. These values are returned by `zjb.constString`, `zjb.global`, and `zjb.fnHandle`. `zjb.ConstHandle` works similarly to `zjb.Handle`, with a few notable exceptions: 1. Values are memoized upon first use on the Zig side, so they can be used any number of times without churning garbage. 2. There is no `release` function. These values are intended to be around for the lifetime of your program, with reduced friction of using them. As the functions which produce ConstHandle values all take only comptime arguments, these cannot balloon uncontrolably at runtime. Some values are always defined as handles, `zjb.ConstHandle.null` is Javascript's `null`, `zjb.ConstHandle.global` is the global scope, and `zjb.ConstHandle.empty_string` is a Javascript empty string.
52
52
-`void` is a valid type for method calls which have no return value.
53
53
54
+
Zjb supports multiple ways to expose Zig functions to Javascript:
55
+
-`zjb.exportFn` exposes the function with the passed name to Javascript. This supports `zjb.Handle`, so if you pass an object from a Javascript function, a handle will automaticlaly be created and passed into Zig. It is the responsibility of the Zig function being called to call `release` on any handles in its arguments at the appropriate time to avoid memory leaks.
56
+
-`zjb.fnHandle` uses `zjb.exportFn` and additionally returns a `zjb.ConstHandle` to that function. This can be used as a callback argument in Javascript functions.
57
+
- Zig's `export` keyword on functions works as it always does in WASM, but doesn't support `zjb.Handle` correctly.
58
+
54
59
A few extra notes:
55
60
56
61
`zjb.string([]const u8)` decodes the slice of memory as a utf-8 string, returning a Handle. The string will NOT update to reflect changes in the slice in Zig.
57
62
63
+
`zjb.global` will be set to the value of that global variable the first time it is called. As it is intended to be used for Javascript objects or classes defined in the global scope, that usage will be safe. For example, `console`, `document` or `Map`. If you use it to retrieve a value or object you've defined in Javascript, ensure it's defined before your program runs and doesn't change.
64
+
58
65
The \_ArrayView functions (`i8ArrayView`, `u8ArrayView`, etc) create the respective JavaScript typed array backed by the same memory as the Zig WASM instance.
59
66
60
67
`dataView` is similar in functionality to the ArrayView functions, but returns a DataView object. Accepts any pointer or slice.
Things that this doesn't have yet, but would be nice:
130
+
Zjb fully works, but has not had enough usage to confidently tag a 1.0. Here are some things which would be nice to add:
120
131
121
-
- Exposing a function that has Handles in arguments, automatically handling everything. Ability to pass in one of those functions as a callback into calls from Zig side. Eg, being able to do setTimeout, or handle async networking tasks entirely from Zig.
122
132
- Tests (need to run from a js environment somehow).
123
133
- Better error handling. (Both handling javascript exceptions as returned errors, but also properly printing panic's error messages).
124
134
- Other random javascript stuff like instanceof, or converting a handle to a number (typically not needed, but might be if you don't know what type something is until after you've got the handle).
0 commit comments