node_dlang 0.0.4

Native node 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:example - A minimal D application.

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 Linux and Windows with LDC compiler.

[TOCM]

Requirements

Just a D compiler (only tested on LDC) with the DUB package manager that is 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 --compiler=ldc2

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

Code example

D side

Add at the beginning of your D file:

module your_module_name;
import dlang_node;
pragma(LDC_no_moduleinfo);
extern (C):
// Needed to be able to use D's runtime features such as garbage collector
// Omission can lead to crashes.
void initialize () {
	import core.runtime;
	rt_init();
}

Then add your functions as normal D code (note, as they are using extern (C) they won't have mangling):

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

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

mixin exportToJs!(initialize, foo);

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. Example file:

// Use relative paths if you haven't made an NPM package yet
const mymodule = require ('./module.node');
// This specific example doesn't need this because D's runtime isn't used
// But usually you will want to add this before calling other of your
// module's functions
mymodule.initialize ();
console.log(mymodule.foo(1, 3));

Run with

node example.js
Authors:
  • Nemo
Sub packages:
node_dlang:example
Dependencies:
none
Versions:
0.0.13 2020-Aug-04
0.0.12 2020-Aug-03
0.0.11 2020-Aug-03
0.0.10 2020-Aug-03
0.0.9 2020-Aug-02
Show all 14 versions
Download Stats:
  • 0 downloads today

  • 6 downloads this week

  • 7 downloads this month

  • 7 downloads total

Score:
0.5
Short URL:
node_dlang.dub.pm