openpeeps / denim Goto Github PK
View Code? Open in Web Editor NEWNode ๐ Nim = Denim! Build powerful NodeJS / BunJS addons with Nim language via Node API (NAPI)
Home Page: https://openpeeps.github.io/denim/
License: MIT License
Node ๐ Nim = Denim! Build powerful NodeJS / BunJS addons with Nim language via Node API (NAPI)
Home Page: https://openpeeps.github.io/denim/
License: MIT License
I installed the latest denim@#head to try the high level API, but unfortunately it doesn't work for me, and the low-level API seems to be broken too.
Low-level API has two problems:
Error: type mismatch: got <napi_env, seq[napi_value], string, (string, NapiValueType), (string, NapiValueType)>
but expected one of:
proc expect(env: napi_env; n: napi_value; expectKind: NapiValueType): bool
first type mismatch at position: 2
required type for n: napi_value
but expression 'args' is of type: seq[napi_value]
proc expect(env: napi_env; v: seq[napi_value]; errorName, fnIdent: string): bool
first type mismatch at position: 4
required type for fnIdent: string
but expression '("accountUserId", napi_number)' is of type: (string, NapiValueType)
Error: unhandled exception: field 'vStr' is not accessible for type 'Parameter' using 'ptype = LongFlag' [FieldDefect]
The high-level API didn't work for me either, also two problems:
Error: expression expected, but found 'A simple function from Nim'
Here's my test Nim program:
import myprogramapppkg/submodule
when defined napibuild:
# Running `denim build myprogram.nim` compiles with `-d:napibuild`
import denim # NAPI bindings
import jsony
init proc(module: Module) =
module.registerFn(0, "getWelcomeMessage"):
return %* getWelcomeMessage()
elif isMainModule:
echo(getWelcomeMessage())
When I run the binary created in the bin directory I get a segfault 11. I suspect node-gyp has a problem somewhere. When I searched on this problem I saw that people logged similar issues for previous versions of node-gyp, but those are fixed by now.
Version information:
$ node --version
v18.16.0
$ node-gyp --version
v9.4.0
$ nim --version
Nim Compiler Version 1.6.12 [Linux: amd64]
Compiled at 2023-03-10
Copyright (c) 2006-2023 by Andreas Rumpf
git hash: 1aa9273640c0c51486cf3a7b67282fe58f360e91
active boot switches: -d:release
It would be great to use macros to simplify the syntax of repetitive code.
E.g. instead of module.registerFn(3, "testWithArgs"):
you could write registerFn testWithArgs (3 args):
.
There are other possibilities too. Just an idea, not high priority.
I'm logging this issue in case anyone has a similar problem.
This caused nimble install denim to fail because it couldn't find node_api.h.
I found the file here: $HOME/.cache/node-gyp/18.16.0/include/node/node_api.h
So to fix the error, I copied the node directory with: cp -r /home/jasonfi/.cache/node-gyp/18.16.0/include/* /usr/include
After that denim installed fine via nimble.
At JS side I pass some functions in Nim procedure:
...
lib.callFunc(() => {
console.log(1);
});
...
But at Nim side I have error on denim build
:
Error: internal error: getTypeDescAux(tyEmpty)
No stack traceback available
To create a stacktrace, rerun compilation with './koch temp c <file>', see https://nim-lang.github.io/Nim/intern.html#debugging-the-compiler for details
Nim code:
init proc(module: Module) =
proc callFunc(callback: napi_function): void {.export_napi.} =
discard callFunction(args.get("callback"), [])
Nim version:
Nim Compiler Version 2.0.0 [Linux: amd64]
Compiled at 2023-08-01
Copyright (c) 2006-2023 by Andreas Rumpf
git hash: a488067a4130f029000be4550a0fb1b39e0e9e7c
active boot switches: -d:release
UPD: on 1.6.14 I got same error
UPD2: same on 1.6.8
I see that this library builds Node addons only on Linux and only for Linux.
It possible to create builds for Windows?
I successfully build an add-on, but when I copy it to an external project, I can't get it to load. This is a Next.js project. Every technique I try encounters a different error.
Do I need to wait for the ability to build an npm from the add-on?
I ran: denim -h
but there was no output. Running echo $?
returned 0 so the program ran OK.
This is with v.0.1.4.
init proc(a: Module):
var x {.export_napi.} = "100"
It would be very useful if Denim included a utility to build a binding.gyp file given a package name.
I would also like to know: must I list every .c file in the project's cache? Or only the main .c file? Thanks.
My node module is causing Node to die quite often. It is typically (always?) a Nim GC function:
$HOME/.choosenim/toolchains/nim-1.6.14/lib/system/gc.nim(486) newObj
$HOME/.choosenim/toolchains/nim-1.6.14/lib/system/gc_common.nim(422) prepareDealloc
I'm not sure if a different GC would help, I think Denim uses the default? I usually specify the ORC GC when compiling the code I'm using for the node addon. I think it might help if Denim had an option to pass in extra args to the Nim compiler called by the Denim build command.
I noticed that when an exception is raised from the Nim code in the addon, the exception not displayed in the console and any future Nim calls don't work. The node process has to be restarted.
It might be useful for Denim to catch all exceptions and log them to the console in a harmless way.
I'm installing and testing a Denim add-on on a new server (Debian 12). I kept getting segfaults when running my Next.js web app with an add-on. So I used a minimal test program and js script, the one that prints a message to the console (only), and it's still segfaulting.
I'm running Nim v.2.0.4 and Denim v0.1.9 (latest).
This happens with both Node v20.14.0 (lts) and v22.2.0 (current).
However it works with Bun v1.1.12 (latest)!
Any idea why this could be?
With Node v18 and v19 it prints the message, but then aborts (SIGABRT).
In nim.cfg:
--path:"."
--passC:"-I/usr/include/node -I/usr/local/include/node"
--gc:orc
--define:release
test.nim:
proc getWelcomeMessage(): string =
return "Hi!"
when defined napibuild:
# Running `denim build myprogram.nim` compiles with `-d:napibuild`
import denim # NAPI bindings
init proc(module: Module) =
# Test
proc getWelcomeMessageNim(): string {.export_napi} =
return %* getWelcomeMessage()
elif isMainModule:
echo(getWelcomeMessage())
test.js:
const nim = require('../bin/test.node')
// Welcome message
console.log(nim.getWelcomeMessageNim()) // Hello, World!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.