-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagger.cc
80 lines (63 loc) · 2.21 KB
/
tagger.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "node-mecab.h"
namespace node_mecab {
using namespace std;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;
const char* ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
Tagger::Tagger(MeCab::Tagger *tagger) : tagger_(tagger) {
}
Tagger::~Tagger() {
delete tagger_;
}
void Tagger::Init(Local<Object> exports) {
Isolate* isolate = exports->GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<ObjectTemplate> addon_data_tpl = ObjectTemplate::New(isolate);
addon_data_tpl->SetInternalFieldCount(1); // 1 field for the Tagger::New()
Local<Object> addon_data =
addon_data_tpl->NewInstance(context).ToLocalChecked();
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New, addon_data);
tpl->SetClassName(String::NewFromUtf8(isolate, "Tagger"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "parse", Parse);
Local<Function> constructor = tpl->GetFunction(context).ToLocalChecked();
addon_data->SetInternalField(0, constructor);
exports->Set(
context,
String::NewFromUtf8(isolate, "Tagger"),
constructor
).FromJust();
}
void Tagger::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
v8::String::Utf8Value str(isolate, args[0]);
std::string cppStr(*str);
const char *str_char = ToCString(str);
Tagger* tagger = new Tagger(MeCab::createTagger(str_char));
tagger->Wrap(args.This());
args.GetReturnValue().Set(args.This());
}
void Tagger::Parse (const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Tagger* tagger = ObjectWrap::Unwrap<Tagger>(args.Holder());
v8::String::Utf8Value str(isolate, args[0]);
std::string cppStr(*str);
const char *str_char = tocstring(str);
const char *result = tagger->tagger_->parse(str_char);
args.GetReturnValue().Set(String::NewFromUtf8(isolate, result));
}
}