-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.cc
49 lines (36 loc) · 1.01 KB
/
node.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <node.h>
#include <nan.h>
#include "src/jade.h"
namespace {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::String;
using v8::Value;
using v8::Handle;
void compileFn(const Nan::FunctionCallbackInfo<v8::Value>& args) {
String::Utf8Value str(args[0]->ToObject());
unsigned char *src = (unsigned char*)*str;
unsigned len;
unsigned char *dst;
const char* err_msg = nullptr;
if (args.Length() != 1) {
err_msg = "Wrong number of arguments: expected 1";
goto err;
}
if (!args[0]->IsString()) {
err_msg = "Input must be String";
goto err;
}
dst = jade_compile(src, &len);
return args.GetReturnValue().Set(Nan::New<String>((char *)dst, len).ToLocalChecked());
err:
Nan::ThrowError(Nan::New<String>(err_msg).ToLocalChecked());
}
NAN_MODULE_INIT(Init) {
v8::Local<v8::Function> _compileFn = Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(compileFn)).ToLocalChecked();
Nan::Set(target, Nan::New("compile").ToLocalChecked(), _compileFn);
}
NODE_MODULE(addon, Init)
}