From: Nick Downing Date: Thu, 4 Jan 2018 05:53:22 +0000 (+1100) Subject: Improved add-on, now links to libzet.so and can open the specified index file X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?p=node_zettair.git;a=commitdiff_plain;h=aeab00f33bf27d4f429e98c3705969c3f9f9f657 Improved add-on, now links to libzet.so and can open the specified index file --- diff --git a/binding.gyp b/binding.gyp index 900bb1f..df75e80 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,7 +2,15 @@ "targets": [ { "target_name": "zet", - "sources": ["zetjs.cpp"] + "sources": ["zetjs.cpp"], + "include_dirs": [ + "../../include", + "../include", + "../include/compat", + "../include/linux" + ], + "library_dirs": ["$$HOME/lib"], + "libraries": ["-lzet"] } ] } diff --git a/test.js b/test.js index bb35737..d226268 100755 --- a/test.js +++ b/test.js @@ -2,7 +2,7 @@ const zet = require('./build/Release/zet'); -const obj = new zet.MyObject(10); +const obj = new zet.Index(); console.log(obj.plusOne()); // Prints: 11 console.log(obj.plusOne()); diff --git a/zetjs.cpp b/zetjs.cpp index b87a4cf..6bcf653 100644 --- a/zetjs.cpp +++ b/zetjs.cpp @@ -1,90 +1,118 @@ #include #include +#include "def.h" +#include "fdset.h" +#include "vec.h" +#include "index.h" +#include "_index.h" +#include "iobtree.h" +#include "vocab.h" +#include "mlparse.h" +#include "str.h" +#include "docmap.h" + namespace zetjs { -using v8::Context; -using v8::Function; -using v8::FunctionCallbackInfo; -using v8::FunctionTemplate; -using v8::Isolate; -using v8::Local; -using v8::Number; -using v8::Object; -using v8::Persistent; -using v8::String; -using v8::Value; - -class MyObject : public node::ObjectWrap { - public: +class IndexObject : public node::ObjectWrap { +public: static void Init(v8::Local exports); - private: - explicit MyObject(double value = 0); - ~MyObject(); +private: + struct index* idx; + double value_; + + explicit IndexObject(struct index* idx); + ~IndexObject(); static void New(const v8::FunctionCallbackInfo& args); static void PlusOne(const v8::FunctionCallbackInfo& args); static v8::Persistent constructor; - double value_; }; -Persistent MyObject::constructor; +v8::Persistent IndexObject::constructor; -MyObject::MyObject(double value) : value_(value) { +IndexObject::IndexObject(struct index* idx0) : idx(idx0), value_(0.) { } -MyObject::~MyObject() { +IndexObject::~IndexObject() { } -void MyObject::Init(Local exports) { - Isolate* isolate = exports->GetIsolate(); +void IndexObject::Init(v8::Local exports) { + v8::Isolate* isolate = exports->GetIsolate(); // Prepare constructor template - Local tpl = FunctionTemplate::New(isolate, New); - tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); + v8::Local tpl = + v8::FunctionTemplate::New(isolate, New); + tpl->SetClassName(v8::String::NewFromUtf8(isolate, "Index")); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); constructor.Reset(isolate, tpl->GetFunction()); - exports->Set(String::NewFromUtf8(isolate, "MyObject"), - tpl->GetFunction()); + exports->Set( + v8::String::NewFromUtf8(isolate, "Index"), + tpl->GetFunction() + ); } -void MyObject::New(const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); +void IndexObject::New(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { - // Invoked as constructor: `new MyObject(...)` - double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); - MyObject* obj = new MyObject(value); + // Invoked as constructor: `new Index(...)` + v8::String::Utf8Value* prefix = + args[0]->IsUndefined() ? NULL : new v8::String::Utf8Value(args[0]); + struct index* idx = NULL;; + int lopts = INDEX_LOAD_NOOPT; + struct index_load_opt lopt; + + lopts |= INDEX_LOAD_IGNORE_VERSION; /* quick hack */ + if ( + ( + idx = index_load( + prefix ? **prefix : "index", + MEMORY_DEFAULT, + lopts, + &lopt + ) + ) == NULL + ) { + isolate->ThrowException( + v8::String::NewFromUtf8(isolate, "Unable to load index") + ); + delete prefix; + return; + } + IndexObject* obj = new IndexObject(idx); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); + delete prefix; } else { - // Invoked as plain function `MyObject(...)`, turn into construct call. + // Invoked as plain function `Index(...)`, turn into construct call. const int argc = 1; - Local argv[argc] = { args[0] }; - Local context = isolate->GetCurrentContext(); - Local cons = Local::New(isolate, constructor); - Local result = - cons->NewInstance(context, argc, argv).ToLocalChecked(); + v8::Local argv[argc] = { args[0] }; + v8::Local context = isolate->GetCurrentContext(); + v8::Local cons = + v8::Local::New(isolate, constructor); + v8::Local result = + cons->NewInstance(context, argc, argv).ToLocalChecked(); args.GetReturnValue().Set(result); } } -void MyObject::PlusOne(const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); +void IndexObject::PlusOne(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); - MyObject* obj = ObjectWrap::Unwrap(args.Holder()); + IndexObject* obj = node::ObjectWrap::Unwrap(args.Holder()); obj->value_ += 1; - args.GetReturnValue().Set(Number::New(isolate, obj->value_)); + args.GetReturnValue().Set(v8::Number::New(isolate, obj->value_)); } -void InitAll(Local exports) { - MyObject::Init(exports); +void InitAll(v8::Local exports) { + IndexObject::Init(exports); } NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)