Last active
May 11, 2017 00:06
-
-
Save haruiz/de2caf7a2ee84fbfe48406639c7ff246 to your computer and use it in GitHub Desktop.
basic native mocule node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <nan.h> | |
using namespace v8; | |
//definimos nuestra función (suma=>nombre) | |
NAN_METHOD(suma){ | |
//validamos los parametros de entrada que le enviaremos desde js | |
if(!info[0]->IsNumber() && !info[2]->IsNumber() ){ | |
//si los argumentos son invalidos arrojamos una exception | |
Nan::ThrowError("argumentos invalidos"); | |
return; | |
} | |
//obtenemos los valores | |
double a = info[0]->NumberValue(); | |
double b = info[1]->NumberValue(); | |
Local<Number> result = Nan::New(a + b);//calculamos la suma | |
info.GetReturnValue().Set(result);//finalmente retornamos el resultado | |
} | |
NAN_MODULE_INIT(Init) { | |
//exportamos la función suma | |
Nan::Set(target, Nan::New("suma").ToLocalChecked(), | |
Nan::GetFunction(Nan::New<FunctionTemplate>(suma)).ToLocalChecked()); | |
} | |
NODE_MODULE(mymodule, Init) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment