I'm currently developing a simulator that runs on a server and should display data in the browser.
For serving files, communication and things like that, I'd like to use Node.js. But, I'm not sure if it will perform as well as I'd want it to in the computation department, so I would like to develop the simulation part in C++.
The simulation is divided into separate "worlds", which all start with some initial parameters.
What is the best way to do this?
Node. js can dynamically load an external C or C++ DLL file at runtime and utilize its API to perform some operations written inside it from a JavaScript program. This is basically how a Native Addon works in Node. js.
js is about twice as fast as C see here.
All C++ compilers also support C linkage, for some compatible C compiler. When you need to access a function compiled with C linkage (for example, a function compiled by the C compiler, or a function written in assembler), declare the function to have C linkage.
It gives the opportunity to make intensive, parallel, and high-accuracy calculations. It also gives the opportunity to use C++ libraries in NodeJS. We can integrate a third-party library written in C/C++ and use it directly in NodeJS.
Well, V8 allows for C++ code to be called from JavaScript.
So you can have 3 parts of your code:
World
is.World
class.First, understand how V8 and C++ communicate. Google provides a guide for this: https://developers.google.com/v8/embed
Then, you need node.js specific glue. See http://www.slideshare.net/nsm.nikhil/writing-native-bindings-to-nodejs-in-c and http://syskall.com/how-to-write-your-own-native-nodejs-extension
From the slideshare link above:
#include <v8.h>
#include <node.h>
using namespace v8;
extern "C" {
static void init(Handle<Object> target) {}
NODE_MODULE(module_name, init)
}
We can expand that into something closer to what you want:
src/world.h
#ifndef WORLD_H_
#define WORLD_H_
class World {
public:
void update();
};
extern World MyWorld;
#endif
src/world.cpp
#include "world.h"
#include <iostream>
using std::cout;
using std::endl;
World MyWorld;
void World::update() {
cout << "Updating World" << endl;
}
src/bind.cpp
#include <v8.h>
#include <node.h>
#include "world.h"
using namespace v8;
static Handle<Value> UpdateBinding(const Arguments& args) {
HandleScope scope;
MyWorld.update();
return Undefined();
}
static Persistent<FunctionTemplate> updateFunction;
extern "C" {
static void init(Handle<Object> obj) {
v8::HandleScope scope;
Local<FunctionTemplate> updateTemplate = FunctionTemplate::New(UpdateBinding);
updateFunction = v8::Persistent<FunctionTemplate>::New(updateTemplate);
obj->Set(String::NewSymbol("update"), updateFunction->GetFunction());
}
NODE_MODULE(world, init)
}
demo/demo.js
var world = require('../build/Release/world.node');
world.update();
wscript
def set_options(opt):
opt.tool_options("compiler_cxx")
def configure(conf):
conf.check_tool("compiler_cxx")
conf.check_tool("node_addon")
def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
# This is the name of our extension.
obj.target = "world"
obj.source = "src/world.cpp src/bind.cpp"
obj.uselib = []
On Linux shell, some setup:
node-waf configure
To build, run:
node-waf
To test:
node demo/demo.js
Output:
Updating World
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With