Giter VIP home page Giter VIP logo

openpeeps / denim Goto Github PK

View Code? Open in Web Editor NEW
39.0 39.0 1.0 2.73 MB

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

Nim 98.56% JavaScript 1.44%
bindings cli-tool denim javascript napi napi-nim native-addon nim-lang node node-addon-api nodejs-addon nodejs-native-addon openpeeps toolkit

denim's People

Contributors

ethosa avatar georgelemon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

ethosa

denim's Issues

Code breaks with latest head

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:

  1. The Env.expect call I had no longer works:
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)
  1. Node-gyp gives an unhelpful error: 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:

  1. The example on the GitHub README.md errors on a warning line:
Error: expression expected, but found 'A simple function from Nim'
  1. Calling args.get("name") also didn't work for me.

Compiled binary segfaults when run

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

Macros for cleaner export Nim code

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.

node-gyp put node_api.h in an unexpected place

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.

Calling callback JS functions in Nim

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

Build for Windows?

I see that this library builds Node addons only on Linux and only for Linux.

It possible to create builds for Windows?

Can't use add-on in an external project

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?

denim -h doesn't do anything

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.

Build binding.gyp given a package name

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.

Frequent: SIGSEGV: Illegal storage access. (Attempt to read from nil?)

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.

Nim exceptions break the node process

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.

Denim addon segfaults with Node, but works with Bun

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.