4 #include "../src/opencc.h"
8 char* ToUtf8String(
const Local<String>& str) {
9 char* utf8 =
new char[str->Utf8Length() + 1];
10 utf8[str->Utf8Length()] =
'\0';
16 struct ConvertRequest {
20 Persistent<Function> callback;
28 if (handler_ != (opencc_t) -1)
32 operator bool()
const {
33 return handler_ != (opencc_t) -1;
36 static Handle<Value> New(
const Arguments& args) {
40 if (args.Length() >= 1 && args[0]->IsString()) {
41 char* config_file = ToUtf8String(args[0]->ToString());
49 if (!*opencc_instance) {
50 ThrowException(Exception::Error(
51 String::New(
"Can not create opencc instance")));
52 return scope.Close(Undefined());
54 opencc_instance->Wrap(args.This());
58 static Handle<Value> Convert(
const Arguments& args) {
60 if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsFunction()) {
61 ThrowException(Exception::TypeError(String::New(
"Wrong arguments")));
62 return scope.Close(Undefined());
65 ConvertRequest* conv_data =
new ConvertRequest;
66 conv_data->opencc_instance = ObjectWrap::Unwrap<OpenccBinding>(args.This());
67 conv_data->input = ToUtf8String(args[0]->ToString());
68 conv_data->callback = Persistent<Function>::New(Local<Function>::Cast(args[1]));
69 uv_work_t* req =
new uv_work_t;
70 req->data = conv_data;
71 uv_queue_work(uv_default_loop(), req, DoConnect, (uv_after_work_cb)AfterConvert);
76 static void DoConnect(uv_work_t* req) {
77 ConvertRequest* conv_data =
static_cast<ConvertRequest*
>(req->data);
78 opencc_t opencc_handler = conv_data->opencc_instance->handler_;
82 static void AfterConvert(uv_work_t* req) {
84 ConvertRequest* conv_data =
static_cast<ConvertRequest*
>(req->data);
85 Local<String> converted = String::New(conv_data->output);
86 const unsigned argc = 2;
87 Local<Value> argv[argc] = {
88 Local<Value>::New(Undefined()),
89 Local<Value>::New(converted)
91 conv_data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
92 conv_data->callback.Dispose();
93 delete[] conv_data->input;
99 static Handle<Value> ConvertSync(
const Arguments& args) {
101 if (args.Length() < 1 || !args[0]->IsString()) {
102 ThrowException(Exception::TypeError(String::New(
"Wrong arguments")));
103 return scope.Close(Undefined());
106 OpenccBinding* opencc_instance = ObjectWrap::Unwrap<OpenccBinding>(args.This());
107 opencc_t opencc_handler = opencc_instance->handler_;
108 char* input = ToUtf8String(args[0]->ToString());
111 Local<String> converted = String::New(output);
114 return scope.Close(converted);
117 static Handle<Value> SetConversionMode(
const Arguments& args) {
119 if (args.Length() < 1 || !args[0]->IsInt32()) {
120 ThrowException(Exception::TypeError(String::New(
"Wrong arguments")));
121 return scope.Close(Undefined());
124 OpenccBinding* opencc_instance = ObjectWrap::Unwrap<OpenccBinding>(args.This());
125 opencc_t opencc_handler = opencc_instance->handler_;
126 int conversion_mode = args[0]->ToInt32()->Value();
127 if (conversion_mode < 0 || conversion_mode > 2) {
128 ThrowException(Exception::Error(
129 String::New(
"conversion_mode must between 0 and 2")));
130 return scope.Close(Undefined());
134 (opencc_conversion_mode) conversion_mode);
135 return scope.Close(Boolean::New(
true));
138 static void init(Handle<Object> target) {
140 Local<FunctionTemplate> tpl = FunctionTemplate::New(OpenccBinding::New);
141 tpl->SetClassName(String::NewSymbol(
"Opencc"));
142 tpl->InstanceTemplate()->SetInternalFieldCount(1);
144 tpl->PrototypeTemplate()->Set(String::NewSymbol(
"convert"),
145 FunctionTemplate::New(Convert)->GetFunction());
146 tpl->PrototypeTemplate()->Set(String::NewSymbol(
"convertSync"),
147 FunctionTemplate::New(ConvertSync)->GetFunction());
148 tpl->PrototypeTemplate()->Set(String::NewSymbol(
"setConversionMode"),
149 FunctionTemplate::New(SetConversionMode)->GetFunction());
151 Persistent<Function> constructor = Persistent<Function>::New(
153 target->Set(String::NewSymbol(
"Opencc"), constructor);
159 void init(Handle<Object> target) {
160 OpenccBinding::init(target);
163 NODE_MODULE(binding, init);