├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── example.zig └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | zig-cache 2 | example.node 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2021 Isaac Freund 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any 4 | purpose with or without fee is hereby granted. 5 | 6 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 7 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 8 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 9 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 10 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 11 | OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 12 | PERFORMANCE OF THIS SOFTWARE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zig-napi-example 2 | 3 | An example of calling Zig code from nodejs using Node-API. 4 | 5 | This example is compatible with Zig version `0.9`. 6 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const example = require('./example.node'); 2 | console.log(example.foo()); 3 | -------------------------------------------------------------------------------- /example.zig: -------------------------------------------------------------------------------- 1 | const c = @cImport({ 2 | @cInclude("node_api.h"); 3 | }); 4 | 5 | export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value { 6 | var function: c.napi_value = undefined; 7 | if (c.napi_create_function(env, null, 0, foo, null, &function) != c.napi_ok) { 8 | _ = c.napi_throw_error(env, null, "Failed to create function"); 9 | return null; 10 | } 11 | 12 | if (c.napi_set_named_property(env, exports, "foo", function) != c.napi_ok) { 13 | _ = c.napi_throw_error(env, null, "Failed to add function to exports"); 14 | return null; 15 | } 16 | 17 | return exports; 18 | } 19 | 20 | fn foo(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value { 21 | _ = info; 22 | 23 | var result: c.napi_value = undefined; 24 | if (c.napi_create_int32(env, 42, &result) != c.napi_ok) { 25 | _ = c.napi_throw_error(env, null, "Failed to create return value"); 26 | return null; 27 | } 28 | 29 | return result; 30 | } 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zig-napi-example", 3 | "version": "0.0.0", 4 | "description": "example napi package in zig", 5 | "main": "example.js", 6 | "scripts": { 7 | "build": "zig build-lib -dynamic -lc -isystem /usr/include/node example.zig -femit-bin=example.node" 8 | }, 9 | "author": "Isaac Freund ", 10 | "license": "0BSD" 11 | } 12 | --------------------------------------------------------------------------------