Skip to content

Commit

Permalink
Merge pull request #7 from lovell/nan
Browse files Browse the repository at this point in the history
Migrate to Native Abstractions (rvagg/nan)
  • Loading branch information
dachev committed Jul 7, 2014
2 parents 5e50ee7 + dacc99d commit b8d8579
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 94 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: node_js
node_js:
- '0.10'
- '0.11'
6 changes: 4 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"cld/binding.gyp:cld"
],
"include_dirs": [
"cld/public"
"cld/public",
"<!(node -e \"require('nan')\")"
],
"sources": ["src/constants.cc", "src/cld.cc"],
"defines": []
"defines": [],
"cflags_cc": ["-Wall"]
}
]
}
3 changes: 2 additions & 1 deletion cld/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"internal/cld2_generated_distinctocta0122.cc",
"internal/cld_generated_score_quad_octa_0122.cc"
],
"defines": []
"defines": [],
"cflags_cc": ["-w"]
}
]
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"cld",
"cld2"
],
"version": "2.0.0",
"version": "2.1.0",
"main": "./index",
"dependencies": {
"underscore": "^1.6.0"
"underscore": "^1.6.0",
"nan": "^1.2.0"
},
"scripts": {
"test": "test/runner.js"
Expand All @@ -21,6 +22,9 @@
"email": "[email protected]",
"url": "http://www.dachev.com"
},
"contributors": [
"Lovell Fuller <[email protected]>"
],
"repository": {
"type": "git",
"url": "git://github.com/dachev/cld.git"
Expand Down
126 changes: 56 additions & 70 deletions src/cld.cc
Original file line number Diff line number Diff line change
@@ -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<v8::FunctionTemplate> detect;
static v8::Persistent<v8::Array> detected;
static v8::Persistent<v8::Object> languages;
static v8::Persistent<v8::Array> encodings;

void
Cld::Init(v8::Handle<v8::Object> target) {
v8::HandleScope scope;

detected = v8::Persistent<v8::Array>(v8::Array::New());
languages = v8::Persistent<v8::Object>(v8::Object::New());
encodings = v8::Persistent<v8::Array>(v8::Array::New());

// set detected languages
vector<NodeCldDetected>* rawDetected = Constants::getInstance().getDetected();
for(vector<NodeCldDetected>::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<NodeCldLanguage>* rawLanguages = Constants::getInstance().getLanguages();
for(vector<NodeCldLanguage>::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<NodeCldEncoding>* rawEncodings = Constants::getInstance().getEncodings();
for(vector<NodeCldEncoding>::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<v8::FunctionTemplate> func = v8::FunctionTemplate::New(Detect);
detect = v8::Persistent<v8::FunctionTemplate>::New(func);
target->Set(v8::String::NewSymbol("detect"), detect->GetFunction());
}

v8::Handle<v8::Value>
Cld::Detect(const v8::Arguments& args) {
v8::HandleScope scope;

v8::Local<v8::Object> results = v8::Object::New();
v8::Local<v8::Object> results = NanNew<v8::Object>();
v8::String::Utf8Value text(args[0]->ToString());

char *bytes = *text;
Expand Down Expand Up @@ -105,24 +60,24 @@ namespace NodeCld {
&isReliable
);

v8::Local<v8::Array> languages = v8::Local<v8::Array>(v8::Array::New());
v8::Local<v8::Array> languages = v8::Local<v8::Array>(NanNew<v8::Array>());
for(int idx = 0; idx < 3; idx++) {
CLD2::Language lang = language3[idx];

if (lang == CLD2::UNKNOWN_LANGUAGE) {
continue;
}

v8::Local<v8::Object> 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<v8::Object> item = NanNew<v8::Object>();
item->Set(NanNew<v8::String>("name"), NanNew<v8::String>(Constants::getInstance().getLanguageName(lang)));
item->Set(NanNew<v8::String>("code"), NanNew<v8::String>(Constants::getInstance().getLanguageCode(lang)));
item->Set(NanNew<v8::String>("percent"), NanNew<v8::Number>(percent3[idx]));
item->Set(NanNew<v8::String>("score"), NanNew<v8::Number>(normalized_score3[idx]));

languages->Set(v8::Integer::New(idx), item);
languages->Set(NanNew<v8::Integer>(idx), item);
}

v8::Local<v8::Array> chunks = v8::Local<v8::Array>(v8::Array::New());
v8::Local<v8::Array> chunks = v8::Local<v8::Array>(NanNew<v8::Array>());
for(unsigned int idx = 0; idx < resultChunkVector.size(); idx++) {
CLD2::ResultChunk chunk = resultChunkVector.at(idx);
CLD2::Language lang = static_cast<CLD2::Language>(chunk.lang1);
Expand All @@ -131,27 +86,58 @@ namespace NodeCld {
continue;
}

v8::Local<v8::Object> 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<v8::Object> item = NanNew<v8::Object>();
item->Set(NanNew<v8::String>("name"), NanNew<v8::String>(Constants::getInstance().getLanguageName(lang)));
item->Set(NanNew<v8::String>("code"), NanNew<v8::String>(Constants::getInstance().getLanguageCode(lang)));
item->Set(NanNew<v8::String>("offset"), NanNew<v8::Number>(chunk.offset));
item->Set(NanNew<v8::String>("bytes"), NanNew<v8::Number>(chunk.bytes));

chunks->Set(v8::Integer::New(idx), item);
chunks->Set(NanNew<v8::Integer>(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<v8::String>("reliable"), NanNew<v8::Boolean>(isReliable));
results->Set(NanNew<v8::String>("textBytes"), NanNew<v8::Number>(textBytesFound));
results->Set(NanNew<v8::String>("languages"), languages);
results->Set(NanNew<v8::String>("chunks"), chunks);

return scope.Close(results);
NanReturnValue(results);
}

extern "C" void init (v8::Handle<v8::Object> target) {
Cld::Init(target);
NanScope();

// set detected languages
v8::Local<v8::Array> detected = NanNew<v8::Array>();
vector<NodeCldDetected>* rawDetected = Constants::getInstance().getDetected();
for(vector<NodeCldDetected>::size_type i = 0; i < rawDetected->size(); i++) {
NodeCldDetected rawLanguage = rawDetected->at(i);
detected->Set(static_cast<uint32_t>(i), NanNew<v8::String>(rawLanguage.name));
}
//NanAssignPersistent(detected, NanNew<v8::Array>());
target->Set(NanNew<v8::String>("DETECTED_LANGUAGES"), detected);

// set all languages
v8::Local<v8::Object> languages = NanNew<v8::Object>();
vector<NodeCldLanguage>* rawLanguages = Constants::getInstance().getLanguages();
for(vector<NodeCldLanguage>::size_type i = 0; i < rawLanguages->size(); i++) {
NodeCldLanguage rawLanguage = rawLanguages->at(i);
languages->Set(NanNew<v8::String>(rawLanguage.name), NanNew<v8::String>(rawLanguage.code));
}
//NanAssignPersistent(languages, NanNew<v8::Object>());
target->Set(NanNew<v8::String>("LANGUAGES"), languages);

// set encodings
v8::Local<v8::Array> encodings = NanNew<v8::Array>();
vector<NodeCldEncoding>* rawEncodings = Constants::getInstance().getEncodings();
for(vector<NodeCldEncoding>::size_type i = 0; i < rawEncodings->size(); i++) {
NodeCldEncoding rawEncoding = rawEncodings->at(i);
encodings->Set(static_cast<uint32_t>(i), NanNew<v8::String>(rawEncoding.name));
}
//NanAssignPersistent(encodings, NanNew<v8::Array>());
target->Set(NanNew<v8::String>("ENCODINGS"), encodings);

NODE_SET_METHOD(target, "detect", Detect);
}

NODE_MODULE(cld, init);
}

19 changes: 0 additions & 19 deletions src/cld.h

This file was deleted.

0 comments on commit b8d8579

Please sign in to comment.