Skip to content

Commit e0b8538

Browse files
committed
doc: add getting started
1 parent 72fbd3c commit e0b8538

2 files changed

Lines changed: 67 additions & 19 deletions

File tree

README.md

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ Thanks to its conventions-based approach it bridges the gap seamlessly, with alm
44
![build-badge](https://img.shields.io/github/actions/workflow/status/typesafe/node-api-zig/ci.yml)
55
![test-badge](https://img.shields.io/endpoint?url=https%3A%2F%2Fgist.githubusercontent.com%2Ftypesafe%2F26882516c7ac38bf94a81784f966bd86%2Fraw%2Fnode-api-zig-test-badge.json)
66

7+
## Features
8+
9+
- **function mapping**, including async support (auto-conversion to Promises)
10+
- **class mapping**, incl. support for fields, instance (async) methods, satic (async) methods.
11+
- **auto-wrapping** of native objects instances, similar to defining classes but for instances created in native Zig code
12+
- **memory management** with convention-based `init`, `deinit` support & `allocator` injection
13+
- `errorunion` support
14+
- mapping JS values
15+
- by value: through (de)serialization or various types
16+
- by reference
17+
- Zig-managed values: through pointers to (wrapped) native Zig values
18+
- JS-managed values: through wrappers types (`NodeValue` et.al.) for read/write
19+
- typesafe callbacks: `NodeFunction(fn (u32, u32) !u32)`
20+
21+
## Example
22+
723
```zig
824
const node_api = @import("node-api");
925
const Options = @import("Options");
@@ -47,23 +63,55 @@ TODO:
4763
- [ ] `ExternalArrayBuffer`
4864
- [ ] Use `Result(T, E)` as alternative to `errorunion`s for improved error messages.
4965

50-
# Features
66+
# Getting started
5167

52-
- **function mapping**, including async support (auto-conversion to Promises)
53-
- **class mapping**, incl. support for fields, instance methods, satic methods
54-
- **auto-wrapping** of native objects instances, similar to defining classes but for instances created in native Zig code
55-
- **memory management** with convention-based `init`, `deinit` support & `allocator` injection
56-
- `errorunion` support
57-
- mapping JS values
58-
- by value: through (de)serialization or various types
59-
- by reference
60-
- Zig-managed values: through pointers to (wrapped) native Zig values
61-
- JS-managed values: through wrappers types (`NodeValue` et.al.) for read/write
62-
- typesafe callbacks: `NodeFunction(fn (u32, u32) !u32)`
68+
Install the `node_api` (note the underscore) dependency
6369

64-
# Getting started
70+
```sh
71+
> zig fetch --save https://github.com/typesafe/node-api-zig/archive/refs/tags/v0.0.3-beta.tar.gz
72+
```
73+
74+
Add the `node-api` (note the hyphen) module to your library.
75+
76+
```zig
77+
const std = @import("std");
78+
79+
pub fn build(b: *std.Build) void {
80+
const target = b.standardTargetOptions(.{});
81+
const optimize = b.standardOptimizeOption(.{});
82+
83+
const mod = b.addModule("root", .{
84+
.root_source_file = b.path("src/root.zig"),
85+
.target = target,
86+
.optimize = optimize,
87+
});
88+
89+
const node_api = b.dependency("node_api", .{});
90+
mod.addImport("node-api", node_api.module("node-api"));
6591
66-
TODO
92+
const lib = b.addLibrary(.{
93+
// needed on Linux
94+
.use_llvm = true,
95+
.name = "my-native-node-library",
96+
.root_module = mod,
97+
});
98+
99+
b.installArtifact(lib);
100+
}
101+
```
102+
103+
Initialize your Node-API extension:
104+
105+
```zig
106+
const std = @import("std");
107+
const node = @import("node-api");
108+
109+
comptime {
110+
node.@"export"(.{
111+
// functions, types (for classes), values
112+
});
113+
}
114+
```
67115

68116
# Features
69117

build.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn build(b: *std.Build) void {
3232
.directory => {
3333
std.log.info("tests/zig_modules/{s}/src/root.zig", .{entry.name});
3434

35-
const mod = b.addLibrary(.{
35+
const lib = b.addLibrary(.{
3636
// imprtant, works without on MacOS, but not on Linux
3737
.use_llvm = true,
3838
.linkage = .dynamic,
@@ -43,11 +43,11 @@ pub fn build(b: *std.Build) void {
4343
.target = target,
4444
}),
4545
});
46-
mod.root_module.addImport("node-api", node_api);
46+
lib.root_module.addImport("node-api", node_api);
4747

4848
// important
49-
mod.linker_allow_shlib_undefined = true;
50-
mod.linkLibC();
49+
lib.linker_allow_shlib_undefined = true;
50+
lib.linkLibC();
5151

5252
const sub_path = std.fmt.allocPrint(b.allocator, "{s}.node", .{entry.name}) catch |err| {
5353
std.log.info("{s}", .{@errorName(err)});
@@ -57,7 +57,7 @@ pub fn build(b: *std.Build) void {
5757

5858
build_mods_step.dependOn(
5959
&b.addInstallArtifact(
60-
mod,
60+
lib,
6161
.{
6262
// custom dir is relative to ./zig-out
6363
.dest_dir = .{ .override = .{

0 commit comments

Comments
 (0)