Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random Coordinates C++ Node.JS AddOn
#include <node.h>
#include <v8.h>
#include <stdio.h>
#include <stdlib.h>
using namespace v8;
Handle<Value> getRandomCoords2D(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
Local<Object> obj = Object::New();
obj->Set(String::NewSymbol("x"), Number::New( 1 + (rand() % xBound->IntegerValue() )));
obj->Set(String::NewSymbol("y"), Number::New( 1 + (rand() % yBound->IntegerValue() )));
return scope.Close(obj);
}
Handle<Value> getRandomCoords3D(const Arguments& args) {
HandleScope scope;
if (args.Length() < 3) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
Local<Number> xBound = args[0]->ToNumber();
Local<Number> yBound = args[1]->ToNumber();
Local<Number> zBound = args[2]->ToNumber();
Local<Object> obj = Object::New();
obj->Set(String::NewSymbol("x"), Number::New( 1 + (rand() % xBound->IntegerValue() )));
obj->Set(String::NewSymbol("y"), Number::New( 1 + (rand() % yBound->IntegerValue() )));
obj->Set(String::NewSymbol("z"), Number::New( 1 + (rand() % zBound->IntegerValue() )));
return scope.Close(obj);
}
void init(Handle<Object> target) {
target->Set(String::NewSymbol("getRandomCoords3D"),
FunctionTemplate::New(getRandomCoords3D)->GetFunction());
target->Set(String::NewSymbol("getRandomCoords2D"),
FunctionTemplate::New(getRandomCoords2D)->GetFunction());
}
NODE_MODULE(randomcoords, init)
@MaxwellRebo

This comment has been minimized.

Copy link

commented Feb 3, 2014

Nice example

@alexellis

This comment has been minimized.

Copy link

commented Feb 5, 2014

How do I build this? I have node installed an GCC.

@odinpowerglove

This comment has been minimized.

Copy link

commented Jun 3, 2014

what does you binding file look like ?

@Julie728

This comment has been minimized.

Copy link

commented Jun 5, 2014

Good example, thanks for sharing!

@octalmage

This comment has been minimized.

@ze-phyr-us

This comment has been minimized.

Copy link

commented Sep 15, 2014

Thanks for the useful article. Haven't tried to run this yet but it seems getRandomCoords2D is missing these lines:

Local<Number> xBound = args[0]->ToNumber();
Local<Number> yBound = args[1]->ToNumber();

In fact you can do fine with just the 3D function. Whenever you need 2D coordinates you just ignore the z axis. Cuts down on code duplication.

@felipebcs

This comment has been minimized.

Copy link

commented Feb 13, 2015

That's great! I'm still preparing myself for my first Node project, and I'll sure build some nasty addons to boost performance in some heavy calculations

@SamanShafigh

This comment has been minimized.

Copy link

commented Jun 19, 2015

Nice example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.