From 348fe83434814be45c4b86ad7ca666393d8cd4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Wed, 11 Nov 2020 14:54:14 +0100 Subject: [PATCH] Make module context-aware to be included in electron Fixes #172 --- lib/index.cpp | 22 ++++++------- lib/nodejieba.cpp | 82 +++++++++++++++++++---------------------------- package.json | 5 +-- 3 files changed, 47 insertions(+), 62 deletions(-) diff --git a/lib/index.cpp b/lib/index.cpp index 9f2dcf1..0bc297e 100644 --- a/lib/index.cpp +++ b/lib/index.cpp @@ -1,24 +1,24 @@ #include "nodejieba.h" -void init(Local exports) { - Nan::Set(exports,Nan::New("load").ToLocalChecked(), +NAN_MODULE_INIT(init) { + Nan::Set(target,Nan::New("load").ToLocalChecked(), Nan::GetFunction(Nan::New(load)).ToLocalChecked()); - Nan::Set(exports,Nan::New("cut").ToLocalChecked(), + Nan::Set(target,Nan::New("cut").ToLocalChecked(), Nan::GetFunction(Nan::New(cut)).ToLocalChecked()); - Nan::Set(exports,Nan::New("cutAll").ToLocalChecked(), + Nan::Set(target,Nan::New("cutAll").ToLocalChecked(), Nan::GetFunction(Nan::New(cutAll)).ToLocalChecked()); - Nan::Set(exports,Nan::New("cutHMM").ToLocalChecked(), + Nan::Set(target,Nan::New("cutHMM").ToLocalChecked(), Nan::GetFunction(Nan::New(cutHMM)).ToLocalChecked()); - Nan::Set(exports,Nan::New("cutForSearch").ToLocalChecked(), + Nan::Set(target,Nan::New("cutForSearch").ToLocalChecked(), Nan::GetFunction(Nan::New(cutForSearch)).ToLocalChecked()); - Nan::Set(exports,Nan::New("cutSmall").ToLocalChecked(), + Nan::Set(target,Nan::New("cutSmall").ToLocalChecked(), Nan::GetFunction(Nan::New(cutSmall)).ToLocalChecked()); - Nan::Set(exports,Nan::New("tag").ToLocalChecked(), + Nan::Set(target,Nan::New("tag").ToLocalChecked(), Nan::GetFunction(Nan::New(tag)).ToLocalChecked()); - Nan::Set(exports,Nan::New("extract").ToLocalChecked(), + Nan::Set(target,Nan::New("extract").ToLocalChecked(), Nan::GetFunction(Nan::New(extract)).ToLocalChecked()); - Nan::Set(exports,Nan::New("insertWord").ToLocalChecked(), + Nan::Set(target,Nan::New("insertWord").ToLocalChecked(), Nan::GetFunction(Nan::New(insertWord)).ToLocalChecked()); } -NODE_MODULE(nodejieba, init) +NAN_MODULE_WORKER_ENABLED(NODE_GYP_MODULE_NAME, init) diff --git a/lib/nodejieba.cpp b/lib/nodejieba.cpp index 9156eb1..5667cba 100644 --- a/lib/nodejieba.cpp +++ b/lib/nodejieba.cpp @@ -3,8 +3,6 @@ #include "cppjieba/Jieba.hpp" #include "cppjieba/KeywordExtractor.hpp" -cppjieba::Jieba* global_jieba_handle; - NAN_METHOD(load) { if(info.Length() != 5) { info.GetReturnValue().Set(Nan::New(false)); @@ -17,12 +15,15 @@ NAN_METHOD(load) { Nan::Utf8String idfPath(Nan::To((info[3])).ToLocalChecked()); Nan::Utf8String stopWordsPath(Nan::To((info[4])).ToLocalChecked()); - delete global_jieba_handle; - global_jieba_handle = new cppjieba::Jieba(*dictPath, - *modelPath, + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + delete _jieba_handle; + _jieba_handle = new cppjieba::Jieba(*dictPath, + *modelPath, *userDictPath, *idfPath, *stopWordsPath); + Nan::SetIsolateData(isolate, _jieba_handle); info.GetReturnValue().Set(Nan::New(true)); } @@ -41,9 +42,11 @@ NAN_METHOD(insertWord) { tag = *(Nan::Utf8String(Nan::To((info[1])).ToLocalChecked())); } - assert(global_jieba_handle); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + assert(_jieba_handle); - if(!global_jieba_handle->InsertUserWord(word, tag)) { + if(!_jieba_handle->InsertUserWord(word, tag)) { info.GetReturnValue().Set(Nan::New(false)); return; } @@ -51,39 +54,6 @@ NAN_METHOD(insertWord) { info.GetReturnValue().Set(Nan::New(true)); } -//NAN_METHOD(cut) { -// if (info.Length() == 0) { -// info.GetReturnValue().Set(Nan::New(false)); -// return; -// } -// string sentence = *(Nan::Utf8String(Nan::To((info[0])).ToLocalChecked())); -// vector words; -// -// assert(global_jieba_handle); -// if (info.Length() >= 2) { -// string method = *(String::Utf8Value(info[1]->ToString())); -// if ("MP" == method) { -// global_jieba_handle->Cut(sentence, words, false); -// if (info.Length() == 3) { -// global_jieba_handle->CutSmall(sentence, words, info[2]->Int32Value()); -// } -// } else if ("HMM" == method) { -// global_jieba_handle->CutHMM(sentence, words); -// } else if ("MIX" == method) { -// global_jieba_handle->Cut(sentence, words, true); -// } else { -// global_jieba_handle->Cut(sentence, words, true); -// } -// } else { -// global_jieba_handle->Cut(sentence, words, true); -// } -// -// Local outArray; -// WrapVector(words, outArray); -// -// info.GetReturnValue().Set(outArray); -//} - NAN_METHOD(cut) { if (info.Length() == 0) { info.GetReturnValue().Set(Nan::New(false)); @@ -95,7 +65,9 @@ NAN_METHOD(cut) { if (info.Length() > 1) { useHMM = *Nan::To((info[1])).ToLocalChecked(); } - global_jieba_handle->Cut(sentence, words, useHMM); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + _jieba_handle->Cut(sentence, words, useHMM); Local outArray; WrapVector(words, outArray); info.GetReturnValue().Set(outArray); @@ -108,7 +80,9 @@ NAN_METHOD(cutHMM) { } string sentence = *(Nan::Utf8String(Nan::To((info[0])).ToLocalChecked())); vector words; - global_jieba_handle->CutHMM(sentence, words); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + _jieba_handle->CutHMM(sentence, words); Local outArray; WrapVector(words, outArray); info.GetReturnValue().Set(outArray); @@ -121,7 +95,9 @@ NAN_METHOD(cutAll) { } string sentence = *(Nan::Utf8String(Nan::To((info[0])).ToLocalChecked())); vector words; - global_jieba_handle->CutAll(sentence, words); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + _jieba_handle->CutAll(sentence, words); Local outArray; WrapVector(words, outArray); info.GetReturnValue().Set(outArray); @@ -138,7 +114,9 @@ NAN_METHOD(cutForSearch) { if (info.Length() > 1) { useHMM = *Nan::To((info[1])).ToLocalChecked(); } - global_jieba_handle->CutForSearch(sentence, words, useHMM); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + _jieba_handle->CutForSearch(sentence, words, useHMM); Local outArray; WrapVector(words, outArray); info.GetReturnValue().Set(outArray); @@ -152,7 +130,9 @@ NAN_METHOD(cutSmall) { string sentence = *(Nan::Utf8String(Nan::To((info[0])).ToLocalChecked())); vector words; size_t word_len_limit = Nan::To((info[1])).FromJust(); - global_jieba_handle->CutSmall(sentence, words, word_len_limit); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + _jieba_handle->CutSmall(sentence, words, word_len_limit); Local outArray; WrapVector(words, outArray); info.GetReturnValue().Set(outArray); @@ -166,8 +146,10 @@ NAN_METHOD(tag) { vector > words; string sentence = *(Nan::Utf8String(Nan::To((info[0])).ToLocalChecked())); - assert(global_jieba_handle); - global_jieba_handle->Tag(sentence, words); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + assert(_jieba_handle); + _jieba_handle->Tag(sentence, words); Local outArray; WrapPairVector(words, outArray); @@ -185,8 +167,10 @@ NAN_METHOD(extract) { size_t topN = Nan::To((info[1])).FromJust(); vector > words; - assert(global_jieba_handle); - global_jieba_handle->extractor.Extract(sentence, words, topN); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData(isolate); + assert(_jieba_handle); + _jieba_handle->extractor.Extract(sentence, words, topN); Local outArray; WrapPairVector(words, outArray); diff --git a/package.json b/package.json index 737ceb4..8f49fb6 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "main": "./index.js", "typings": "types/index.d.ts", "engines": { - "node": ">= 0.10.0", + "node": ">= 9.3.0", "iojs": ">= 1.0.0" }, "repository": { @@ -38,7 +38,8 @@ }, "scripts": { "test": "node test/demo.js && node test/load_dict_demo.js && mocha --timeout 10s -R spec test/test.js && mocha --timeout 10s -R spec test/load_dict_test.js", - "install": "node-pre-gyp install --fallback-to-build" + "install": "node-pre-gyp install --fallback-to-build", + "rebuild": "node-pre-gyp rebuild" }, "binary": { "module_name": "nodejieba",