zignapi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,213 @@
1
+ #ifndef SRC_JS_NATIVE_API_TYPES_H_
2
+ #define SRC_JS_NATIVE_API_TYPES_H_
3
+
4
+ // Use INT_MAX, this should only be consumed by the pre-processor anyway.
5
+ #define NAPI_VERSION_EXPERIMENTAL 2147483647
6
+ #ifndef NAPI_VERSION
7
+ // The baseline version for Node-API.
8
+ // NAPI_VERSION controls which version is used by default when compiling
9
+ // a native addon. If the addon developer wants to use functions from a
10
+ // newer Node-API version not yet available in all LTS versions, they can
11
+ // set NAPI_VERSION to explicitly depend on that version.
12
+ #define NAPI_VERSION 8
13
+ #endif
14
+
15
+
16
+ // This file needs to be compatible with C compilers.
17
+ // This is a public include file, and these includes have essentially
18
+ // become part of its API.
19
+ #include <stddef.h> // NOLINT(modernize-deprecated-headers)
20
+ #include <stdint.h> // NOLINT(modernize-deprecated-headers)
21
+
22
+ #if !defined __cplusplus || (defined(_MSC_VER) && _MSC_VER < 1900)
23
+ typedef uint16_t char16_t;
24
+ #endif
25
+
26
+ #ifndef NAPI_CDECL
27
+ #ifdef _WIN32
28
+ #define NAPI_CDECL __cdecl
29
+ #else
30
+ #define NAPI_CDECL
31
+ #endif
32
+ #endif
33
+
34
+ // JSVM API types are all opaque pointers for ABI stability
35
+ // typedef undefined structs instead of void* for compile time type safety
36
+ typedef struct napi_env__* napi_env;
37
+
38
+ // We need to mark APIs which can be called during garbage collection (GC),
39
+ // meaning that they do not affect the state of the JS engine, and can
40
+ // therefore be called synchronously from a finalizer that itself runs
41
+ // synchronously during GC. Such APIs can receive either a `napi_env` or a
42
+ // `node_api_basic_env` as their first parameter, because we should be able to
43
+ // also call them during normal, non-garbage-collecting operations, whereas
44
+ // APIs that affect the state of the JS engine can only receive a `napi_env` as
45
+ // their first parameter, because we must not call them during GC. In lieu of
46
+ // inheritance, we use the properties of the const qualifier to accomplish
47
+ // this, because both a const and a non-const value can be passed to an API
48
+ // expecting a const value, but only a non-const value can be passed to an API
49
+ // expecting a non-const value.
50
+ //
51
+ // In conjunction with appropriate CFLAGS to warn us if we're passing a const
52
+ // (basic) environment into an API that expects a non-const environment, and
53
+ // the definition of basic finalizer function pointer types below, which
54
+ // receive a basic environment as their first parameter, and can thus only call
55
+ // basic APIs (unless the user explicitly casts the environment), we achieve
56
+ // the ability to ensure at compile time that we do not call APIs that affect
57
+ // the state of the JS engine from a synchronous (basic) finalizer.
58
+ typedef struct napi_env__* node_api_nogc_env;
59
+ typedef node_api_nogc_env node_api_basic_env;
60
+
61
+ typedef struct napi_value__* napi_value;
62
+ typedef struct napi_ref__* napi_ref;
63
+ typedef struct napi_handle_scope__* napi_handle_scope;
64
+ typedef struct napi_escapable_handle_scope__* napi_escapable_handle_scope;
65
+ typedef struct napi_callback_info__* napi_callback_info;
66
+ typedef struct napi_deferred__* napi_deferred;
67
+
68
+ typedef enum {
69
+ napi_default = 0,
70
+ napi_writable = 1 << 0,
71
+ napi_enumerable = 1 << 1,
72
+ napi_configurable = 1 << 2,
73
+
74
+ // Used with napi_define_class to distinguish static properties
75
+ // from instance properties. Ignored by napi_define_properties.
76
+ napi_static = 1 << 10,
77
+
78
+ #if NAPI_VERSION >= 8
79
+ // Default for class methods.
80
+ napi_default_method = napi_writable | napi_configurable,
81
+
82
+ // Default for object properties, like in JS obj[prop].
83
+ napi_default_jsproperty = napi_writable | napi_enumerable | napi_configurable,
84
+ #endif // NAPI_VERSION >= 8
85
+ } napi_property_attributes;
86
+
87
+ typedef enum {
88
+ // ES6 types (corresponds to typeof)
89
+ napi_undefined,
90
+ napi_null,
91
+ napi_boolean,
92
+ napi_number,
93
+ napi_string,
94
+ napi_symbol,
95
+ napi_object,
96
+ napi_function,
97
+ napi_external,
98
+ napi_bigint,
99
+ } napi_valuetype;
100
+
101
+ typedef enum {
102
+ napi_int8_array,
103
+ napi_uint8_array,
104
+ napi_uint8_clamped_array,
105
+ napi_int16_array,
106
+ napi_uint16_array,
107
+ napi_int32_array,
108
+ napi_uint32_array,
109
+ napi_float32_array,
110
+ napi_float64_array,
111
+ napi_bigint64_array,
112
+ napi_biguint64_array,
113
+ #define NODE_API_HAS_FLOAT16_ARRAY
114
+ napi_float16_array,
115
+ } napi_typedarray_type;
116
+
117
+ typedef enum {
118
+ napi_ok,
119
+ napi_invalid_arg,
120
+ napi_object_expected,
121
+ napi_string_expected,
122
+ napi_name_expected,
123
+ napi_function_expected,
124
+ napi_number_expected,
125
+ napi_boolean_expected,
126
+ napi_array_expected,
127
+ napi_generic_failure,
128
+ napi_pending_exception,
129
+ napi_cancelled,
130
+ napi_escape_called_twice,
131
+ napi_handle_scope_mismatch,
132
+ napi_callback_scope_mismatch,
133
+ napi_queue_full,
134
+ napi_closing,
135
+ napi_bigint_expected,
136
+ napi_date_expected,
137
+ napi_arraybuffer_expected,
138
+ napi_detachable_arraybuffer_expected,
139
+ napi_would_deadlock, // unused
140
+ napi_no_external_buffers_allowed,
141
+ napi_cannot_run_js,
142
+ } napi_status;
143
+ // Note: when adding a new enum value to `napi_status`, please also update
144
+ // * `const int last_status` in the definition of `napi_get_last_error_info()'
145
+ // in file js_native_api_v8.cc.
146
+ // * `const char* error_messages[]` in file js_native_api_v8.cc with a brief
147
+ // message explaining the error.
148
+ // * the definition of `napi_status` in doc/api/n-api.md to reflect the newly
149
+ // added value(s).
150
+
151
+ typedef napi_value(NAPI_CDECL* napi_callback)(napi_env env,
152
+ napi_callback_info info);
153
+ typedef void(NAPI_CDECL* napi_finalize)(napi_env env,
154
+ void* finalize_data,
155
+ void* finalize_hint);
156
+
157
+ typedef napi_finalize node_api_nogc_finalize;
158
+ typedef node_api_nogc_finalize node_api_basic_finalize;
159
+
160
+ // A finalizer that can be called from any thread and at any time.
161
+ typedef void(NAPI_CDECL* node_api_noenv_finalize)(void* finalize_data,
162
+ void* finalize_hint);
163
+
164
+ typedef struct {
165
+ // One of utf8name or name should be NULL.
166
+ const char* utf8name;
167
+ napi_value name;
168
+
169
+ napi_callback method;
170
+ napi_callback getter;
171
+ napi_callback setter;
172
+ napi_value value;
173
+
174
+ napi_property_attributes attributes;
175
+ void* data;
176
+ } napi_property_descriptor;
177
+
178
+ typedef struct {
179
+ const char* error_message;
180
+ void* engine_reserved;
181
+ uint32_t engine_error_code;
182
+ napi_status error_code;
183
+ } napi_extended_error_info;
184
+
185
+ #if NAPI_VERSION >= 6
186
+ typedef enum {
187
+ napi_key_include_prototypes,
188
+ napi_key_own_only
189
+ } napi_key_collection_mode;
190
+
191
+ typedef enum {
192
+ napi_key_all_properties = 0,
193
+ napi_key_writable = 1,
194
+ napi_key_enumerable = 1 << 1,
195
+ napi_key_configurable = 1 << 2,
196
+ napi_key_skip_strings = 1 << 3,
197
+ napi_key_skip_symbols = 1 << 4
198
+ } napi_key_filter;
199
+
200
+ typedef enum {
201
+ napi_key_keep_numbers,
202
+ napi_key_numbers_to_strings
203
+ } napi_key_conversion;
204
+ #endif // NAPI_VERSION >= 6
205
+
206
+ #if NAPI_VERSION >= 8
207
+ typedef struct {
208
+ uint64_t lower;
209
+ uint64_t upper;
210
+ } napi_type_tag;
211
+ #endif // NAPI_VERSION >= 8
212
+
213
+ #endif // SRC_JS_NATIVE_API_TYPES_H_
@@ -0,0 +1,265 @@
1
+ #ifndef SRC_NODE_API_H_
2
+ #define SRC_NODE_API_H_
3
+
4
+ #if defined(BUILDING_NODE_EXTENSION) && !defined(NAPI_EXTERN)
5
+ #ifdef _WIN32
6
+ // Building native addon against node
7
+ #define NAPI_EXTERN __declspec(dllimport)
8
+ #elif defined(__wasm__)
9
+ #define NAPI_EXTERN __attribute__((__import_module__("napi")))
10
+ #endif
11
+ #endif
12
+ #include "js_native_api.h"
13
+ #include "node_api_types.h"
14
+
15
+ struct uv_loop_s; // Forward declaration.
16
+
17
+ #ifdef _WIN32
18
+ #define NAPI_MODULE_EXPORT __declspec(dllexport)
19
+ #else
20
+ #ifdef __EMSCRIPTEN__
21
+ #define NAPI_MODULE_EXPORT \
22
+ __attribute__((visibility("default"))) __attribute__((used))
23
+ #else
24
+ #define NAPI_MODULE_EXPORT __attribute__((visibility("default")))
25
+ #endif
26
+ #endif
27
+
28
+ #if defined(__GNUC__)
29
+ #define NAPI_NO_RETURN __attribute__((noreturn))
30
+ #elif defined(_WIN32)
31
+ #define NAPI_NO_RETURN __declspec(noreturn)
32
+ #else
33
+ #define NAPI_NO_RETURN
34
+ #endif
35
+
36
+ // Used by deprecated registration method napi_module_register.
37
+ typedef struct napi_module {
38
+ int nm_version;
39
+ unsigned int nm_flags;
40
+ const char* nm_filename;
41
+ napi_addon_register_func nm_register_func;
42
+ const char* nm_modname;
43
+ void* nm_priv;
44
+ void* reserved[4];
45
+ } napi_module;
46
+
47
+ #define NAPI_MODULE_VERSION 1
48
+
49
+ #define NAPI_MODULE_INITIALIZER_X(base, version) \
50
+ NAPI_MODULE_INITIALIZER_X_HELPER(base, version)
51
+ #define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version
52
+
53
+ #ifdef __wasm__
54
+ #define NAPI_MODULE_INITIALIZER_BASE napi_register_wasm_v
55
+ #else
56
+ #define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v
57
+ #endif
58
+
59
+ #define NODE_API_MODULE_GET_API_VERSION_BASE node_api_module_get_api_version_v
60
+
61
+ #define NAPI_MODULE_INITIALIZER \
62
+ NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, NAPI_MODULE_VERSION)
63
+
64
+ #define NODE_API_MODULE_GET_API_VERSION \
65
+ NAPI_MODULE_INITIALIZER_X(NODE_API_MODULE_GET_API_VERSION_BASE, \
66
+ NAPI_MODULE_VERSION)
67
+
68
+ #define NAPI_MODULE_INIT() \
69
+ EXTERN_C_START \
70
+ NAPI_MODULE_EXPORT int32_t NODE_API_MODULE_GET_API_VERSION(void) { \
71
+ return NAPI_VERSION; \
72
+ } \
73
+ NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
74
+ napi_value exports); \
75
+ EXTERN_C_END \
76
+ napi_value NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports)
77
+
78
+ #define NAPI_MODULE(modname, regfunc) \
79
+ NAPI_MODULE_INIT() { return regfunc(env, exports); }
80
+
81
+ // Deprecated. Use NAPI_MODULE.
82
+ #define NAPI_MODULE_X(modname, regfunc, priv, flags) \
83
+ NAPI_MODULE(modname, regfunc)
84
+
85
+ EXTERN_C_START
86
+
87
+ // Deprecated. Replaced by symbol-based registration defined by NAPI_MODULE
88
+ // and NAPI_MODULE_INIT macros.
89
+ NAPI_EXTERN void NAPI_CDECL
90
+ napi_module_register(napi_module* mod);
91
+
92
+ NAPI_EXTERN NAPI_NO_RETURN void NAPI_CDECL
93
+ napi_fatal_error(const char* location,
94
+ size_t location_len,
95
+ const char* message,
96
+ size_t message_len);
97
+
98
+ // Methods for custom handling of async operations
99
+ NAPI_EXTERN napi_status NAPI_CDECL
100
+ napi_async_init(napi_env env,
101
+ napi_value async_resource,
102
+ napi_value async_resource_name,
103
+ napi_async_context* result);
104
+
105
+ NAPI_EXTERN napi_status NAPI_CDECL
106
+ napi_async_destroy(napi_env env, napi_async_context async_context);
107
+
108
+ NAPI_EXTERN napi_status NAPI_CDECL
109
+ napi_make_callback(napi_env env,
110
+ napi_async_context async_context,
111
+ napi_value recv,
112
+ napi_value func,
113
+ size_t argc,
114
+ const napi_value* argv,
115
+ napi_value* result);
116
+
117
+ // Methods to provide node::Buffer functionality with napi types
118
+ NAPI_EXTERN napi_status NAPI_CDECL napi_create_buffer(napi_env env,
119
+ size_t length,
120
+ void** data,
121
+ napi_value* result);
122
+ #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
123
+ NAPI_EXTERN napi_status NAPI_CDECL
124
+ napi_create_external_buffer(napi_env env,
125
+ size_t length,
126
+ void* data,
127
+ node_api_basic_finalize finalize_cb,
128
+ void* finalize_hint,
129
+ napi_value* result);
130
+ #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
131
+
132
+ #if NAPI_VERSION >= 10
133
+
134
+ NAPI_EXTERN napi_status NAPI_CDECL
135
+ node_api_create_buffer_from_arraybuffer(napi_env env,
136
+ napi_value arraybuffer,
137
+ size_t byte_offset,
138
+ size_t byte_length,
139
+ napi_value* result);
140
+ #endif // NAPI_VERSION >= 10
141
+
142
+ NAPI_EXTERN napi_status NAPI_CDECL napi_create_buffer_copy(napi_env env,
143
+ size_t length,
144
+ const void* data,
145
+ void** result_data,
146
+ napi_value* result);
147
+ NAPI_EXTERN napi_status NAPI_CDECL napi_is_buffer(napi_env env,
148
+ napi_value value,
149
+ bool* result);
150
+ NAPI_EXTERN napi_status NAPI_CDECL napi_get_buffer_info(napi_env env,
151
+ napi_value value,
152
+ void** data,
153
+ size_t* length);
154
+
155
+ // Methods to manage simple async operations
156
+ NAPI_EXTERN napi_status NAPI_CDECL
157
+ napi_create_async_work(napi_env env,
158
+ napi_value async_resource,
159
+ napi_value async_resource_name,
160
+ napi_async_execute_callback execute,
161
+ napi_async_complete_callback complete,
162
+ void* data,
163
+ napi_async_work* result);
164
+ NAPI_EXTERN napi_status NAPI_CDECL napi_delete_async_work(napi_env env,
165
+ napi_async_work work);
166
+ NAPI_EXTERN napi_status NAPI_CDECL napi_queue_async_work(node_api_basic_env env,
167
+ napi_async_work work);
168
+ NAPI_EXTERN napi_status NAPI_CDECL
169
+ napi_cancel_async_work(node_api_basic_env env, napi_async_work work);
170
+
171
+ // version management
172
+ NAPI_EXTERN napi_status NAPI_CDECL napi_get_node_version(
173
+ node_api_basic_env env, const napi_node_version** version);
174
+
175
+ #if NAPI_VERSION >= 2
176
+
177
+ // Return the current libuv event loop for a given environment
178
+ NAPI_EXTERN napi_status NAPI_CDECL
179
+ napi_get_uv_event_loop(node_api_basic_env env, struct uv_loop_s** loop);
180
+
181
+ #endif // NAPI_VERSION >= 2
182
+
183
+ #if NAPI_VERSION >= 3
184
+
185
+ NAPI_EXTERN napi_status NAPI_CDECL napi_fatal_exception(napi_env env,
186
+ napi_value err);
187
+
188
+ NAPI_EXTERN napi_status NAPI_CDECL napi_add_env_cleanup_hook(
189
+ node_api_basic_env env, napi_cleanup_hook fun, void* arg);
190
+
191
+ NAPI_EXTERN napi_status NAPI_CDECL napi_remove_env_cleanup_hook(
192
+ node_api_basic_env env, napi_cleanup_hook fun, void* arg);
193
+
194
+ NAPI_EXTERN napi_status NAPI_CDECL
195
+ napi_open_callback_scope(napi_env env,
196
+ napi_value resource_object,
197
+ napi_async_context context,
198
+ napi_callback_scope* result);
199
+
200
+ NAPI_EXTERN napi_status NAPI_CDECL
201
+ napi_close_callback_scope(napi_env env, napi_callback_scope scope);
202
+
203
+ #endif // NAPI_VERSION >= 3
204
+
205
+ #if NAPI_VERSION >= 4
206
+
207
+ // Calling into JS from other threads
208
+ NAPI_EXTERN napi_status NAPI_CDECL
209
+ napi_create_threadsafe_function(napi_env env,
210
+ napi_value func,
211
+ napi_value async_resource,
212
+ napi_value async_resource_name,
213
+ size_t max_queue_size,
214
+ size_t initial_thread_count,
215
+ void* thread_finalize_data,
216
+ napi_finalize thread_finalize_cb,
217
+ void* context,
218
+ napi_threadsafe_function_call_js call_js_cb,
219
+ napi_threadsafe_function* result);
220
+
221
+ NAPI_EXTERN napi_status NAPI_CDECL napi_get_threadsafe_function_context(
222
+ napi_threadsafe_function func, void** result);
223
+
224
+ NAPI_EXTERN napi_status NAPI_CDECL
225
+ napi_call_threadsafe_function(napi_threadsafe_function func,
226
+ void* data,
227
+ napi_threadsafe_function_call_mode is_blocking);
228
+
229
+ NAPI_EXTERN napi_status NAPI_CDECL
230
+ napi_acquire_threadsafe_function(napi_threadsafe_function func);
231
+
232
+ NAPI_EXTERN napi_status NAPI_CDECL napi_release_threadsafe_function(
233
+ napi_threadsafe_function func, napi_threadsafe_function_release_mode mode);
234
+
235
+ NAPI_EXTERN napi_status NAPI_CDECL napi_unref_threadsafe_function(
236
+ node_api_basic_env env, napi_threadsafe_function func);
237
+
238
+ NAPI_EXTERN napi_status NAPI_CDECL napi_ref_threadsafe_function(
239
+ node_api_basic_env env, napi_threadsafe_function func);
240
+
241
+ #endif // NAPI_VERSION >= 4
242
+
243
+ #if NAPI_VERSION >= 8
244
+
245
+ NAPI_EXTERN napi_status NAPI_CDECL
246
+ napi_add_async_cleanup_hook(node_api_basic_env env,
247
+ napi_async_cleanup_hook hook,
248
+ void* arg,
249
+ napi_async_cleanup_hook_handle* remove_handle);
250
+
251
+ NAPI_EXTERN napi_status NAPI_CDECL
252
+ napi_remove_async_cleanup_hook(napi_async_cleanup_hook_handle remove_handle);
253
+
254
+ #endif // NAPI_VERSION >= 8
255
+
256
+ #if NAPI_VERSION >= 9
257
+
258
+ NAPI_EXTERN napi_status NAPI_CDECL
259
+ node_api_get_module_file_name(node_api_basic_env env, const char** result);
260
+
261
+ #endif // NAPI_VERSION >= 9
262
+
263
+ EXTERN_C_END
264
+
265
+ #endif // SRC_NODE_API_H_
@@ -0,0 +1,58 @@
1
+ #ifndef SRC_NODE_API_TYPES_H_
2
+ #define SRC_NODE_API_TYPES_H_
3
+
4
+ #include "js_native_api_types.h"
5
+
6
+ typedef napi_value(NAPI_CDECL* napi_addon_register_func)(napi_env env,
7
+ napi_value exports);
8
+ // False positive: https://github.com/cpplint/cpplint/issues/409
9
+ // NOLINTNEXTLINE (readability/casting)
10
+ typedef int32_t(NAPI_CDECL* node_api_addon_get_api_version_func)(void);
11
+
12
+ typedef struct napi_callback_scope__* napi_callback_scope;
13
+ typedef struct napi_async_context__* napi_async_context;
14
+ typedef struct napi_async_work__* napi_async_work;
15
+
16
+ #if NAPI_VERSION >= 3
17
+ typedef void(NAPI_CDECL* napi_cleanup_hook)(void* arg);
18
+ #endif // NAPI_VERSION >= 3
19
+
20
+ #if NAPI_VERSION >= 4
21
+ typedef struct napi_threadsafe_function__* napi_threadsafe_function;
22
+ #endif // NAPI_VERSION >= 4
23
+
24
+ #if NAPI_VERSION >= 4
25
+ typedef enum {
26
+ napi_tsfn_release,
27
+ napi_tsfn_abort
28
+ } napi_threadsafe_function_release_mode;
29
+
30
+ typedef enum {
31
+ napi_tsfn_nonblocking,
32
+ napi_tsfn_blocking
33
+ } napi_threadsafe_function_call_mode;
34
+ #endif // NAPI_VERSION >= 4
35
+
36
+ typedef void(NAPI_CDECL* napi_async_execute_callback)(napi_env env, void* data);
37
+ typedef void(NAPI_CDECL* napi_async_complete_callback)(napi_env env,
38
+ napi_status status,
39
+ void* data);
40
+ #if NAPI_VERSION >= 4
41
+ typedef void(NAPI_CDECL* napi_threadsafe_function_call_js)(
42
+ napi_env env, napi_value js_callback, void* context, void* data);
43
+ #endif // NAPI_VERSION >= 4
44
+
45
+ typedef struct {
46
+ uint32_t major;
47
+ uint32_t minor;
48
+ uint32_t patch;
49
+ const char* release;
50
+ } napi_node_version;
51
+
52
+ #if NAPI_VERSION >= 8
53
+ typedef struct napi_async_cleanup_hook_handle__* napi_async_cleanup_hook_handle;
54
+ typedef void(NAPI_CDECL* napi_async_cleanup_hook)(
55
+ napi_async_cleanup_hook_handle handle, void* data);
56
+ #endif // NAPI_VERSION >= 8
57
+
58
+ #endif // SRC_NODE_API_TYPES_H_
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "zignapi",
3
+ "version": "0.1.0",
4
+ "description": "CLI to scaffold and build native Node.js addons written in Zig (the `zignapi` command)",
5
+ "keywords": [
6
+ "zig",
7
+ "napi",
8
+ "node-api",
9
+ "native",
10
+ "addon",
11
+ "zigbind"
12
+ ],
13
+ "homepage": "https://github.com/zigbind/zigbind",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/zigbind/zigbind.git",
17
+ "directory": "packages/zigbind"
18
+ },
19
+ "type": "module",
20
+ "bin": {
21
+ "zignapi": "dist/cli.js"
22
+ },
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "templates",
29
+ "native"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsc",
33
+ "prepack": "node scripts/prepack.mjs"
34
+ },
35
+ "license": "MIT"
36
+ }
@@ -0,0 +1,32 @@
1
+ # __NAME__
2
+
3
+ A native Node.js addon written in [Zig](https://ziglang.org/) with
4
+ [zigbind](https://github.com/) — requires **Zig 0.16.0** and **Node >= 18**.
5
+
6
+ ```sh
7
+ zignapi build # compiles src/main.zig into __NAME__.node (and zig-out/lib/)
8
+ node --test # runs test.js
9
+ ```
10
+
11
+ Edit `src/main.zig` to add functions, then list them in the `zigbind.register`
12
+ call at the bottom of that file.
13
+
14
+ ## The zigbind dependency
15
+
16
+ `zignapi new` added the `zignapi` Zig module to `build.zig.zon` with
17
+ `zig fetch --save`, which pins it by content hash:
18
+
19
+ ```zig
20
+ .dependencies = .{
21
+ .zignapi = .{ .url = "…", .hash = "zignapi-…" },
22
+ },
23
+ ```
24
+
25
+ The `url` points at the zignapi sources on the machine where you scaffolded.
26
+ The content is cached globally by hash, so builds don't need that path again.
27
+ To share this project across machines/CI, re-point `url` at a hosted tarball
28
+ of the zignapi `native/` sources (the `hash` stays the same) with:
29
+
30
+ ```sh
31
+ zig fetch --save=zignapi https://…/zignapi-native.tar.gz
32
+ ```
@@ -0,0 +1,33 @@
1
+ const std = @import("std");
2
+
3
+ /// Builds the addon into `zig-out/lib/__NAME__.node`.
4
+ ///
5
+ /// The addon is a dynamic library importing the `zigbind` module. N-API symbols
6
+ /// are left undefined at link time and resolved by Node when it loads the addon.
7
+ pub fn build(b: *std.Build) void {
8
+ const target = b.standardTargetOptions(.{});
9
+ const optimize = b.standardOptimizeOption(.{});
10
+
11
+ const zigbind = b.dependency("zigbind", .{}).module("zigbind");
12
+
13
+ const addon = b.addLibrary(.{
14
+ .name = "__NAME__",
15
+ .linkage = .dynamic,
16
+ .root_module = b.createModule(.{
17
+ .root_source_file = b.path("src/main.zig"),
18
+ .target = target,
19
+ .optimize = optimize,
20
+ .link_libc = true,
21
+ .imports = &.{
22
+ .{ .name = "zigbind", .module = zigbind },
23
+ },
24
+ }),
25
+ });
26
+
27
+ // Portable equivalent of macOS `-undefined dynamic_lookup`: let N-API
28
+ // symbols stay undefined so Node resolves them when it loads the addon.
29
+ addon.linker_allow_shlib_undefined = true;
30
+
31
+ const install = b.addInstallFileWithDir(addon.getEmittedBin(), .lib, "__NAME__.node");
32
+ b.getInstallStep().dependOn(&install.step);
33
+ }
@@ -0,0 +1,13 @@
1
+ .{
2
+ .name = .__NAME__,
3
+ .version = "0.1.0",
4
+ .minimum_zig_version = "0.16.0",
5
+ // `zignapi new` fills in `.fingerprint` and adds the `zigbind` dependency
6
+ // below via `zig fetch --save` (which pins zigbind by content hash).
7
+ .dependencies = .{},
8
+ .paths = .{
9
+ "build.zig",
10
+ "build.zig.zon",
11
+ "src",
12
+ },
13
+ }
@@ -0,0 +1,4 @@
1
+ node_modules/
2
+ zig-out/
3
+ .zig-cache/
4
+ *.node
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "__NAME__",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "scripts": {
6
+ "build": "zignapi build",
7
+ "test": "node --test"
8
+ }
9
+ }
@@ -0,0 +1,21 @@
1
+ const zigbind = @import("zigbind");
2
+
3
+ /// Exposed to JS as `addon.add(a, b)`.
4
+ pub fn add(a: i32, b: i32) i32 {
5
+ return a + b;
6
+ }
7
+
8
+ /// Exposed to JS as `addon.greet(name)`.
9
+ pub fn greet(name: []const u8) []const u8 {
10
+ _ = name;
11
+ return "hello from Zig";
12
+ }
13
+
14
+ // Emits `napi_register_module_v1`. Each field becomes a property on the addon's
15
+ // `exports`. Add your own Zig functions here.
16
+ comptime {
17
+ zigbind.register(.{
18
+ .add = add,
19
+ .greet = greet,
20
+ });
21
+ }