11///! This is an example napi module that exercises various napi features.
22const std = @import ("std" );
3- const napi = @import ("napi " );
3+ const zapi = @import ("zapi " );
44const allocator = std .heap .page_allocator ;
55
66comptime {
77 // The module must be registered with napi via `register`
8- napi .module .register (exampleMod );
8+ zapi .module .register (exampleMod );
99}
1010
1111// This is the top-level module registration function for this module.
1212// It is called by napi when the module is loaded.
13- fn exampleMod (env : napi .Env , module : napi .Value ) anyerror ! void {
13+ fn exampleMod (env : zapi .Env , module : zapi .Value ) anyerror ! void {
1414 // Example of a string property
1515 try module .setNamedProperty ("helloWorld" , try env .createStringUtf8 (hello_world ));
1616
@@ -29,7 +29,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
2929 try module .setNamedProperty ("add" , try env .createFunction (
3030 "add" ,
3131 2 ,
32- napi .createCallback (2 , add , .{}),
32+ zapi .createCallback (2 , add , .{}),
3333 null ,
3434 ));
3535
@@ -40,7 +40,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
4040 try module .setNamedProperty ("add_semimanual" , try env .createFunction (
4141 "add_semimanual" ,
4242 2 ,
43- napi .createCallback (2 , add_semimanual , .{
43+ zapi .createCallback (2 , add_semimanual , .{
4444 .args = .{ .env , .auto , .value },
4545 .returns = .value ,
4646 }),
@@ -51,7 +51,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
5151 try module .setNamedProperty ("surprise" , try env .createFunction (
5252 "surprise" ,
5353 0 ,
54- napi .createCallback (0 , surprise , .{
54+ zapi .createCallback (0 , surprise , .{
5555 .returns = .string ,
5656 }),
5757 null ,
@@ -60,7 +60,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
6060 try module .setNamedProperty ("update" , try env .createFunction (
6161 "update" ,
6262 1 ,
63- napi .createCallback (1 , S .update , .{
63+ zapi .createCallback (1 , S .update , .{
6464 .args = .{ .data , .auto },
6565 }),
6666 & s ,
@@ -74,15 +74,15 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
7474 0 ,
7575 Timer_ctor ,
7676 null ,
77- &[_ ]napi .c.napi_property_descriptor { .{
77+ &[_ ]zapi .c.napi_property_descriptor { .{
7878 .utf8name = "reset" ,
79- .method = napi .wrapCallback (0 , Timer_reset ),
79+ .method = zapi .wrapCallback (0 , Timer_reset ),
8080 }, .{
8181 .utf8name = "read" ,
82- .method = napi .wrapCallback (0 , Timer_read ),
82+ .method = zapi .wrapCallback (0 , Timer_read ),
8383 }, .{
8484 .utf8name = "lap" ,
85- .method = napi .wrapCallback (0 , Timer_lap ),
85+ .method = zapi .wrapCallback (0 , Timer_lap ),
8686 } },
8787 ),
8888 );
@@ -110,7 +110,7 @@ comptime {
110110 // std.debug.assert(@TypeOf(&add_manual) == napi.Callback(2));
111111}
112112
113- fn add_manual (env : napi .Env , cb : napi .CallbackInfo (2 )) ! napi .Value {
113+ fn add_manual (env : zapi .Env , cb : zapi .CallbackInfo (2 )) ! zapi .Value {
114114 const a = try cb .arg (0 ).getValueInt32 ();
115115 const b = try cb .arg (1 ).getValueInt32 ();
116116
@@ -123,7 +123,7 @@ fn add(a: i32, b: i32) !i32 {
123123 return a + b ;
124124}
125125
126- fn add_semimanual (env : napi .Env , a : i32 , b : napi .Value ) ! napi .Value {
126+ fn add_semimanual (env : zapi .Env , a : i32 , b : zapi .Value ) ! zapi .Value {
127127 const b_int = try b .getValueInt32 ();
128128
129129 const result = a + b_int ;
@@ -153,12 +153,12 @@ var s: S = S{
153153
154154// Wrapped class example (std.time.Timer)
155155
156- fn Timer_finalize (_ : napi .Env , timer : * std.time.Timer , _ : ? * anyopaque ) void {
156+ fn Timer_finalize (_ : zapi .Env , timer : * std.time.Timer , _ : ? * anyopaque ) void {
157157 std .debug .print ("Destroying timer {any}\n " , .{timer });
158158 allocator .destroy (timer );
159159}
160160
161- fn Timer_ctor (env : napi .Env , cb : napi .CallbackInfo (0 )) ! napi .Value {
161+ fn Timer_ctor (env : zapi .Env , cb : zapi .CallbackInfo (0 )) ! zapi .Value {
162162 const timer = try allocator .create (std .time .Timer );
163163 timer .* = try std .time .Timer .start ();
164164 _ = try env .wrap (
@@ -172,18 +172,18 @@ fn Timer_ctor(env: napi.Env, cb: napi.CallbackInfo(0)) !napi.Value {
172172 return cb .this ();
173173}
174174
175- fn Timer_reset (env : napi .Env , cb : napi .CallbackInfo (0 )) ! napi .Value {
175+ fn Timer_reset (env : zapi .Env , cb : zapi .CallbackInfo (0 )) ! zapi .Value {
176176 const timer = try env .unwrap (std .time .Timer , cb .this ());
177177 timer .reset ();
178178 return try env .getUndefined ();
179179}
180180
181- fn Timer_read (env : napi .Env , cb : napi .CallbackInfo (0 )) ! napi .Value {
181+ fn Timer_read (env : zapi .Env , cb : zapi .CallbackInfo (0 )) ! zapi .Value {
182182 const timer = try env .unwrap (std .time .Timer , cb .this ());
183183 return try env .createInt64 (@intCast (timer .read ()));
184184}
185185
186- fn Timer_lap (env : napi .Env , cb : napi .CallbackInfo (0 )) ! napi .Value {
186+ fn Timer_lap (env : zapi .Env , cb : zapi .CallbackInfo (0 )) ! zapi .Value {
187187 const timer = try env .unwrap (std .time .Timer , cb .this ());
188188 return try env .createInt64 (@intCast (timer .lap ()));
189189}
@@ -194,16 +194,16 @@ const AsyncAddData = struct {
194194 a : i32 ,
195195 b : i32 ,
196196 result : i32 ,
197- deferred : napi .Deferred ,
198- work : napi .AsyncWork (AsyncAddData ),
197+ deferred : zapi .Deferred ,
198+ work : zapi .AsyncWork (AsyncAddData ),
199199};
200200
201- fn asyncAddExecute (_ : napi .Env , data : * AsyncAddData ) void {
201+ fn asyncAddExecute (_ : zapi .Env , data : * AsyncAddData ) void {
202202 std .time .sleep (1_000_000_000 ); // 1 second
203203 data .result = data .a + data .b ;
204204}
205205
206- fn asyncAddComplete (env : napi .Env , status : napi .status.Status , data : * AsyncAddData ) void {
206+ fn asyncAddComplete (env : zapi .Env , status : zapi .status.Status , data : * AsyncAddData ) void {
207207 defer {
208208 data .work .delete () catch undefined ;
209209 allocator .destroy (data );
@@ -220,7 +220,7 @@ fn asyncAddComplete(env: napi.Env, status: napi.status.Status, data: *AsyncAddDa
220220 data .deferred .resolve (env .createInt32 (data .result ) catch unreachable ) catch unreachable ;
221221}
222222
223- fn asyncAdd (env : napi .Env , cb : napi .CallbackInfo (2 )) ! napi .Value {
223+ fn asyncAdd (env : zapi .Env , cb : zapi .CallbackInfo (2 )) ! zapi .Value {
224224 const a = try cb .arg (0 ).getValueInt32 ();
225225 const b = try cb .arg (1 ).getValueInt32 ();
226226
@@ -256,7 +256,7 @@ const TsfnData = struct {
256256 count : i32 ,
257257};
258258
259- fn startThread (env : napi .Env , cb : napi .CallbackInfo (1 )) ! napi .Value {
259+ fn startThread (env : zapi .Env , cb : zapi .CallbackInfo (1 )) ! zapi .Value {
260260 const context = try allocator .create (TsfnContext );
261261
262262 // Create the thread-safe function
@@ -279,7 +279,7 @@ fn startThread(env: napi.Env, cb: napi.CallbackInfo(1)) !napi.Value {
279279 return try env .getUndefined ();
280280}
281281
282- fn threadMain (tsfn : napi .ThreadSafeFunction (TsfnContext , TsfnData )) void {
282+ fn threadMain (tsfn : zapi .ThreadSafeFunction (TsfnContext , TsfnData )) void {
283283 var i : i32 = 0 ;
284284 while (i < 5 ) : (i += 1 ) {
285285 const data = allocator .create (TsfnData ) catch return ;
@@ -295,7 +295,7 @@ fn threadMain(tsfn: napi.ThreadSafeFunction(TsfnContext, TsfnData)) void {
295295 tsfn .release (.release ) catch {};
296296}
297297
298- fn callJs (env : napi .Env , cb : napi .Value , _ : * TsfnContext , data : * TsfnData ) void {
298+ fn callJs (env : zapi .Env , cb : zapi .Value , _ : * TsfnContext , data : * TsfnData ) void {
299299 defer allocator .destroy (data );
300300
301301 _ = env .callFunction (
@@ -305,7 +305,7 @@ fn callJs(env: napi.Env, cb: napi.Value, _: *TsfnContext, data: *TsfnData) void
305305 ) catch {};
306306}
307307
308- fn finalizeTsfn (_ : napi .Env , context : * TsfnContext ) void {
308+ fn finalizeTsfn (_ : zapi .Env , context : * TsfnContext ) void {
309309 defer {
310310 context .thread .join ();
311311 allocator .destroy (context );
0 commit comments