-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddon_interface.cc
71 lines (51 loc) · 2.11 KB
/
addon_interface.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
#include <node.h>
#include <v8.h>
#include <map>
#include <list>
#include <string>
#include "pinyin.h"
using namespace v8;
void getPinyin(const v8::FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
// Check the number of arguments passed.
if (args.Length() < 1) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
// Check the argument types
if (!args[0]->IsString() ) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
// Perform the operation
String::Value inputString(args[0]);
const uint16_t* pArrayStringUnicode = *inputString;
Local<Array> arrayResult = Array::New(isolate);
// convert hanzi to pinyin
list<list<u16string>> listCharPinyin;
Pinyin::getInstance()->convert(pArrayStringUnicode,inputString.length(), listCharPinyin);
// organize output
int charIndex = 0 ;
for(list<list<u16string>>::const_iterator charIter = listCharPinyin.begin(); charIter != listCharPinyin.end(); charIter++) {
const list<u16string> & listSingleCharPinyin = *charIter;
// organize pinyin array for one char
Local<Array> arraySingleCharPinyin = Array::New(isolate);
int pinyinIndex = 0 ;
for(list<u16string>::const_iterator pinyinIter = listSingleCharPinyin.begin(); pinyinIter != listSingleCharPinyin.end(); pinyinIter++) {
Local<String> singlePinyin = String::NewFromTwoByte(isolate,(uint16_t *)((*pinyinIter).c_str()));
arraySingleCharPinyin->Set(pinyinIndex,singlePinyin);
pinyinIndex++;
}
arrayResult->Set(charIndex, arraySingleCharPinyin);
charIndex++;
}
args.GetReturnValue().Set(arrayResult);
}
void Init(Handle<Object> exports) {
Isolate* isolate = Isolate::GetCurrent();
exports->Set(String::NewFromUtf8(isolate, "getPinyin"), FunctionTemplate::New(isolate, getPinyin)->GetFunction());
}
NODE_MODULE(hello, Init)