https://github.com/notnite/nodedotnet
Experimental Node.js native bindings for C# using NativeAOT
https://github.com/notnite/nodedotnet
Last synced: 4 months ago
JSON representation
Experimental Node.js native bindings for C# using NativeAOT
- Host: GitHub
- URL: https://github.com/notnite/nodedotnet
- Owner: NotNite
- License: mit
- Created: 2025-01-24T15:22:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-24T15:22:51.000Z (over 1 year ago)
- Last Synced: 2025-04-15T02:58:59.003Z (about 1 year ago)
- Language: C#
- Size: 15.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NodeDotNet
Experimental Node.js native bindings for C# using NativeAOT. These bindings are extremely incomplete and I'm just doing this for fun!
## Usage
This is only tested on Windows for now.
First, obtain the node-gyp headers & library. I have no idea how to do this automatically, so I just went to `%LocalAppData%\node-gyp\Cache` and copied the latest version I had (naming the folder `node-gyp`).
If the bindings need to be regenerated, use [ClangSharpPInvokeGenerator](https://github.com/dotnet/ClangSharp?tab=readme-ov-file#generating-bindings) with the provided `generate.rsp`.
Then, build a project using NodeDotNet as a library. See `NodeDotNet.Test` for an example, with the right project config and the exposed entrypoint.
## Testing
```shell
# Publish as NativeAOT
dotnet publish -r win-x64
# Rename the file to .node so you can require it
cp ./NodeDotNet.Test/bin/Release/net9.0/win-x64/native/NodeDotNet.Test.dll ./NodeDotNet.Test/bin/Release/net9.0/win-x64/native/NodeDotNet.Test.node
# Run it
node -e 'console.log(require("./NodeDotNet.Test/bin/Release/net9.0/win-x64/native/NodeDotNet.Test.node").add(2, 2))'
```
## TODO
(I probably won't ever finish this)
- [ ] Add more wrapper classes for primitives
- [ ] Better binding to method arguments using Action/Func and generics
- [ ] Bind JavaScript objects to C# classes... somehow...
- [ ] Write a source generator or something for the entrypoint