-}
-
-void IndexObject::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
- v8::Isolate* isolate = args.GetIsolate();
-
- if (args.IsConstructCall()) {
- // Invoked as constructor: `new Index(...)`
- v8::String::Utf8Value prefix(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(
- args[0]->IsString() ? *prefix : "index",
- MEMORY_DEFAULT,
- lopts,
- &lopt
- )
- ) == NULL
- ) {
- isolate->ThrowException(
- v8::String::NewFromUtf8(isolate, "Unable to load index")
- );
- return;
- }
- IndexObject* obj = new IndexObject(idx);
- obj->Wrap(args.This());
- args.GetReturnValue().Set(args.This());
- } else {
- // Invoked as plain function `Index(...)`, turn into construct call.
- const int argc = 1;
- 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);