node_dlang 0.4.3

Native NodeJS® module creator


To use this package, run the following command in your project's root directory:


This package provides sub packages which can be used individually:

node_dlang:type_examples - A minimal D application.

Actions CI License

Node dlang

Note: This is currently in early state of development, expect breaking changes!

Package to create native NodeJS modules based on N-API. Tested on 64 bit Linux and Windows with LDC and DMD compilers.

Requirements

Just a D compiler and DUB package manager (usually included with the compiler). JavaScript is not necessary to generate the modules but NodeJS is needed to test the generated file.

Usage

Create a DUB project with:

dub init

Assuming JSON format, add the following fields to dub.json:

"dependencies": {
	"node_dlang": "*"
},
"configurations": [
	{
		"name": "example_windows",
		"platforms": ["windows"],
		"targetType": "dynamicLibrary",
		"targetPath" : ".",
		"targetName" : "module.node",
		"postGenerateCommands": ["move module.node.dll module.node"]
	}, {
		"name": "example_posix",
		"platforms": ["posix"],
		"targetName" : "module.node",
		"targetType": "dynamicLibrary",
		"postGenerateCommands": ["mv libmodule.node.so module.node"]
	}
]

You can check the example folder for a reference dub.json.

Compile with

dub build

The resulting module.node file can be require'd from JavaScript.

Code example

You probably want to check the code at examples/type_examples.

D side

Add at the beginning of your D file:

module your_module_name;
import node_dlang;
extern (C): // We need no mangling

Then add your functions as normal D code (note: they are using extern (C)):

auto foo (int first, long second) {
	return [first, second * 4, 0];
}

// Functions that you want executed on load must be void (napi_env)
void atStart (napi_env env) {
	import std.stdio;
	writeln ("Hello from D!");
}

At the end of your file use a mixin to do all the magic:

// MainFunction is used to execute on load instead of registering to exports.
mixin exportToJs! (foo, MainFunction!atStart);

Add to exportToJs template args all the functions that you want to be able to use from JavaScript.

Javascript side

Make sure NodeJS is installed on your system.

If you used MainFunction you can run your generated module.node directly:

node module.node

You can also require the module from JS. Example file:

// Use relative paths if you haven't made an NPM package yet
const mymodule = require ('./module.node');
console.log (mymodule.foo (1, 3));

Run with

node example.js
Authors:
  • Nemo
Sub packages:
node_dlang:type_examples
Dependencies:
none
Versions:
0.4.6 2020-Sep-16
0.4.5 2020-Sep-14
0.4.4 2020-Sep-12
0.4.3 2020-Sep-06
0.4.2 2020-Sep-06
Show all 31 versions
Download Stats:
  • 0 downloads today

  • 2 downloads this week

  • 4 downloads this month

  • 11 downloads total

Score:
0.6
Short URL:
node_dlang.dub.pm