From dacc99d1086b8a2f52e42d102d71ef7f88cbb83f Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Sun, 6 Jul 2014 13:42:54 +0100 Subject: [PATCH] Migrate to Native Abstractions (rvagg/nan) --- .travis.yml | 1 + binding.gyp | 6 ++- cld/binding.gyp | 3 +- package.json | 8 ++- src/cld.cc | 126 +++++++++++++++++++++--------------------------- src/cld.h | 19 -------- 6 files changed, 69 insertions(+), 94 deletions(-) delete mode 100644 src/cld.h diff --git a/.travis.yml b/.travis.yml index 244b7e8..3c6757d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ language: node_js node_js: - '0.10' + - '0.11' diff --git a/binding.gyp b/binding.gyp index 11cafab..d43b6ed 100644 --- a/binding.gyp +++ b/binding.gyp @@ -6,10 +6,12 @@ "cld/binding.gyp:cld" ], "include_dirs": [ - "cld/public" + "cld/public", + "" + ], "repository": { "type": "git", "url": "git://github.com/dachev/cld.git" diff --git a/src/cld.cc b/src/cld.cc index b32c95e..b5d5529 100644 --- a/src/cld.cc +++ b/src/cld.cc @@ -1,59 +1,14 @@ #include "compact_lang_det.h" #include "encodings.h" #include "constants.h" -#include "cld.h" +#include "nan.h" namespace NodeCld { - static v8::Persistent detect; - static v8::Persistent detected; - static v8::Persistent languages; - static v8::Persistent encodings; - - void - Cld::Init(v8::Handle target) { - v8::HandleScope scope; - - detected = v8::Persistent(v8::Array::New()); - languages = v8::Persistent(v8::Object::New()); - encodings = v8::Persistent(v8::Array::New()); - // set detected languages - vector* rawDetected = Constants::getInstance().getDetected(); - for(vector::size_type i = 0; i < rawDetected->size(); i++) { - NodeCldDetected rawLanguage = rawDetected->at(i); - - detected->Set(v8::Integer::New(i), v8::String::New(rawLanguage.name)); - } - target->Set(v8::String::NewSymbol("DETECTED_LANGUAGES"), detected); - - // set all languages - vector* rawLanguages = Constants::getInstance().getLanguages(); - for(vector::size_type i = 0; i < rawLanguages->size(); i++) { - NodeCldLanguage rawLanguage = rawLanguages->at(i); - - languages->Set(v8::String::New(rawLanguage.name), v8::String::New(rawLanguage.code)); - } - target->Set(v8::String::NewSymbol("LANGUAGES"), languages); + NAN_METHOD(Detect) { + NanScope(); - // set encodings - vector* rawEncodings = Constants::getInstance().getEncodings(); - for(vector::size_type i = 0; i < rawEncodings->size(); i++) { - NodeCldEncoding rawEncoding = rawEncodings->at(i); - - encodings->Set(v8::Integer::New(i), v8::String::New(rawEncoding.name)); - } - target->Set(v8::String::NewSymbol("ENCODINGS"), encodings); - - v8::Local func = v8::FunctionTemplate::New(Detect); - detect = v8::Persistent::New(func); - target->Set(v8::String::NewSymbol("detect"), detect->GetFunction()); - } - - v8::Handle - Cld::Detect(const v8::Arguments& args) { - v8::HandleScope scope; - - v8::Local results = v8::Object::New(); + v8::Local results = NanNew(); v8::String::Utf8Value text(args[0]->ToString()); char *bytes = *text; @@ -105,7 +60,7 @@ namespace NodeCld { &isReliable ); - v8::Local languages = v8::Local(v8::Array::New()); + v8::Local languages = v8::Local(NanNew()); for(int idx = 0; idx < 3; idx++) { CLD2::Language lang = language3[idx]; @@ -113,16 +68,16 @@ namespace NodeCld { continue; } - v8::Local item = v8::Object::New(); - item->Set(v8::String::New("name"), v8::String::New(Constants::getInstance().getLanguageName(lang))); - item->Set(v8::String::New("code"), v8::String::New(Constants::getInstance().getLanguageCode(lang))); - item->Set(v8::String::New("percent"), v8::Number::New(percent3[idx])); - item->Set(v8::String::New("score"), v8::Number::New(normalized_score3[idx])); + v8::Local item = NanNew(); + item->Set(NanNew("name"), NanNew(Constants::getInstance().getLanguageName(lang))); + item->Set(NanNew("code"), NanNew(Constants::getInstance().getLanguageCode(lang))); + item->Set(NanNew("percent"), NanNew(percent3[idx])); + item->Set(NanNew("score"), NanNew(normalized_score3[idx])); - languages->Set(v8::Integer::New(idx), item); + languages->Set(NanNew(idx), item); } - v8::Local chunks = v8::Local(v8::Array::New()); + v8::Local chunks = v8::Local(NanNew()); for(unsigned int idx = 0; idx < resultChunkVector.size(); idx++) { CLD2::ResultChunk chunk = resultChunkVector.at(idx); CLD2::Language lang = static_cast(chunk.lang1); @@ -131,27 +86,58 @@ namespace NodeCld { continue; } - v8::Local item = v8::Object::New(); - item->Set(v8::String::New("name"), v8::String::New(Constants::getInstance().getLanguageName(lang))); - item->Set(v8::String::New("code"), v8::String::New(Constants::getInstance().getLanguageCode(lang))); - item->Set(v8::String::New("offset"), v8::Number::New(chunk.offset)); - item->Set(v8::String::New("bytes"), v8::Number::New(chunk.bytes)); + v8::Local item = NanNew(); + item->Set(NanNew("name"), NanNew(Constants::getInstance().getLanguageName(lang))); + item->Set(NanNew("code"), NanNew(Constants::getInstance().getLanguageCode(lang))); + item->Set(NanNew("offset"), NanNew(chunk.offset)); + item->Set(NanNew("bytes"), NanNew(chunk.bytes)); - chunks->Set(v8::Integer::New(idx), item); + chunks->Set(NanNew(idx), item); } - results->Set(v8::String::New("reliable"), v8::Boolean::New(isReliable)); - results->Set(v8::String::New("textBytes"), v8::Number::New(textBytesFound)); - results->Set(v8::String::New("languages"), languages); - results->Set(v8::String::New("chunks"), chunks); + results->Set(NanNew("reliable"), NanNew(isReliable)); + results->Set(NanNew("textBytes"), NanNew(textBytesFound)); + results->Set(NanNew("languages"), languages); + results->Set(NanNew("chunks"), chunks); - return scope.Close(results); + NanReturnValue(results); } extern "C" void init (v8::Handle target) { - Cld::Init(target); + NanScope(); + + // set detected languages + v8::Local detected = NanNew(); + vector* rawDetected = Constants::getInstance().getDetected(); + for(vector::size_type i = 0; i < rawDetected->size(); i++) { + NodeCldDetected rawLanguage = rawDetected->at(i); + detected->Set(static_cast(i), NanNew(rawLanguage.name)); + } + //NanAssignPersistent(detected, NanNew()); + target->Set(NanNew("DETECTED_LANGUAGES"), detected); + + // set all languages + v8::Local languages = NanNew(); + vector* rawLanguages = Constants::getInstance().getLanguages(); + for(vector::size_type i = 0; i < rawLanguages->size(); i++) { + NodeCldLanguage rawLanguage = rawLanguages->at(i); + languages->Set(NanNew(rawLanguage.name), NanNew(rawLanguage.code)); + } + //NanAssignPersistent(languages, NanNew()); + target->Set(NanNew("LANGUAGES"), languages); + + // set encodings + v8::Local encodings = NanNew(); + vector* rawEncodings = Constants::getInstance().getEncodings(); + for(vector::size_type i = 0; i < rawEncodings->size(); i++) { + NodeCldEncoding rawEncoding = rawEncodings->at(i); + encodings->Set(static_cast(i), NanNew(rawEncoding.name)); + } + //NanAssignPersistent(encodings, NanNew()); + target->Set(NanNew("ENCODINGS"), encodings); + + NODE_SET_METHOD(target, "detect", Detect); } NODE_MODULE(cld, init); } - diff --git a/src/cld.h b/src/cld.h deleted file mode 100644 index 6125633..0000000 --- a/src/cld.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef NODE_CLD_H -#define NODE_CLD_H - -#include -#include - -namespace NodeCld { - class Cld { - public: - static void Init(v8::Handle target); - static v8::Handle Detect(const v8::Arguments& args); - - protected: - Cld() {} - ~Cld() {} - }; -} - -#endif \ No newline at end of file