Skip to content

Instantly share code, notes, and snippets.

@haruiz
Last active May 11, 2017 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haruiz/de2caf7a2ee84fbfe48406639c7ff246 to your computer and use it in GitHub Desktop.
Save haruiz/de2caf7a2ee84fbfe48406639c7ff246 to your computer and use it in GitHub Desktop.
basic native mocule node.js
#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