Improved add-on, now links to libzet.so and can open the specified index file
authorNick Downing <downing.nick@gmail.com>
Thu, 4 Jan 2018 05:53:22 +0000 (16:53 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 18 Nov 2018 02:37:55 +0000 (13:37 +1100)
binding.gyp
test.js
zetjs.cpp

index 900bb1f..df75e80 100644 (file)
@@ -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 (executable)
--- 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());
index b87a4cf..6bcf653 100644 (file)
--- a/zetjs.cpp
+++ b/zetjs.cpp
 #include <node.h>
 #include <node_object_wrap.h>
 
+#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<v8::Object> 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<v8::Value>& args);
   static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
   static v8::Persistent<v8::Function> constructor;
-  double value_;
 };
 
-Persistent<Function> MyObject::constructor;
+v8::Persistent<v8::Function> 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<Object> exports) {
-  Isolate* isolate = exports->GetIsolate();
+void IndexObject::Init(v8::Local<v8::Object> exports) {
+  v8::Isolate* isolate = exports->GetIsolate();
 
   // Prepare constructor template
-  Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
-  tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
+  v8::Local<v8::FunctionTemplate> 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<Value>& args) {
-  Isolate* isolate = args.GetIsolate();
+void IndexObject::New(const v8::FunctionCallbackInfo<v8::Value>& 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<Value> argv[argc] = { args[0] };
-    Local<Context> context = isolate->GetCurrentContext();
-    Local<Function> cons = Local<Function>::New(isolate, constructor);
-    Local<Object> result =
-        cons->NewInstance(context, argc, argv).ToLocalChecked();
+    v8::Local<v8::Value> argv[argc] = { args[0] };
+    v8::Local<v8::Context> context = isolate->GetCurrentContext();
+    v8::Local<v8::Function> cons =
+      v8::Local<v8::Function>::New(isolate, constructor);
+    v8::Local<v8::Object> result =
+      cons->NewInstance(context, argc, argv).ToLocalChecked();
     args.GetReturnValue().Set(result);
   }
 }
 
-void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
-  Isolate* isolate = args.GetIsolate();
+void IndexObject::PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args) {
+  v8::Isolate* isolate = args.GetIsolate();
 
-  MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
+  IndexObject* obj = node::ObjectWrap::Unwrap<IndexObject>(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<Object> exports) {
-  MyObject::Init(exports);
+void InitAll(v8::Local<v8::Object> exports) {
+  IndexObject::Init(exports);
 }
 
 NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)