示例#1
0
void Client::Cas(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 5, "5")
	CHECK_N_ARGS_IS_A_STRING(info, 0, "0")
	CHECK_N_ARGS_IS_A_STRING(info, 1, "1")
	CHECK_N_ARGS_IS_A_NUMBER(info, 2, "2")
	CHECK_N_ARGS_IS_A_STRING(info, 3, "3")
	CHECK_N_ARGS_IS_A_FUNCTION(info, 4, "4")

	const string memcached_key = GET_STRING_FROM_PARAM(info[0]);
	const string memcached_value = GET_STRING_FROM_PARAM(info[1]);
	time_t ttl = info[2]->NumberValue();
	// String to uint64_t
	const string casString = GET_STRING_FROM_PARAM(info[3]);
	uint64_t cas;
	sscanf(casString.c_str(), "%" PRIu64, &cas);
	Callback* callback = GET_CALLBACK_FROM_PARAM(info[4]);

	JobBase* job = new CasJob(callback, memcached_key, memcached_value, ttl, cas);
	job->setDebug(memClient->debug);
	memClient->jobs.insert(job);

	info.GetReturnValue().Set(Nan::Undefined());
}
示例#2
0
void MeCab_Tagger::DictionaryInfo(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  MeCab_Tagger* obj = ObjectWrap::Unwrap<MeCab_Tagger>(info.Holder());
  v8::Local<v8::Function> callback = info[0].As<v8::Function>();
  const MeCab::DictionaryInfo *dic = obj->tagger->dictionary_info();
  std::vector<v8::Local<v8::Object>> info_list;

  for (; dic; dic = dic->next) {
    v8::Local<v8::Object> obj = Nan::New<v8::Object>();
    obj->Set(Nan::New("filename").ToLocalChecked(), Nan::New(dic->filename).ToLocalChecked());
    obj->Set(Nan::New("charset").ToLocalChecked(), Nan::New(dic->charset).ToLocalChecked());
    obj->Set(Nan::New("size").ToLocalChecked(), Nan::New(dic->size));
    obj->Set(Nan::New("type").ToLocalChecked(), Nan::New(dic->type));
    obj->Set(Nan::New("lsize").ToLocalChecked(), Nan::New(dic->lsize));
    obj->Set(Nan::New("rsize").ToLocalChecked(), Nan::New(dic->rsize));
    obj->Set(Nan::New("version").ToLocalChecked(), Nan::New(dic->version));
    info_list.push_back(obj);
  }

  v8::Local<v8::Array> results = Nan::New<v8::Array>(info_list.size());
  for(unsigned int i = 0; i < results->Length(); ++i) {
    results->Set(i, info_list.at(i));
  }

  const unsigned argc = 2;
  v8::Local<v8::Value> argv[] = {
    Nan::Null(),
    results
  };
  Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
}
示例#3
0
void Client::Debug(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 1, "1")
	CHECK_N_ARGS_IS_A_BOOLEAN(info, 0, "0")

	memClient->debug = info[0]->ToBoolean()->Value();
	info.GetReturnValue().Set(Nan::Undefined());
}
示例#4
0
void Client::Stop(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 1, "1")
	CHECK_N_ARGS_IS_A_FUNCTION(info, 0, "0")
	Callback* callback = new Callback(info[0].As<v8::Function>());

	memClient->progressWorker->setCallback(callback);
	memClient->isRunning = false;
}
示例#5
0
void MeCab_Tagger::Parse(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  MeCab_Tagger* mecabTagger = ObjectWrap::Unwrap<MeCab_Tagger>(info.Holder());
  v8::String::Utf8Value input(info[0]);
  v8::Local<v8::Function> callback = info[1].As<v8::Function>();

  const char *result = mecabTagger->tagger->parse(*input);

  const unsigned argc = 2;
  v8::Local<v8::Value> argv[] = {
    Nan::Null(),
    Nan::New(result).ToLocalChecked()
  };
  Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
}
示例#6
0
void Client::FetchResult(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 1, "1")
	CHECK_N_ARGS_IS_A_FUNCTION(info, 0, "0")

	Callback* callback = GET_CALLBACK_FROM_PARAM(info[0]);

	JobBase* job = new FetchResultJob(callback);
	job->setDebug(memClient->debug);
	memClient->jobs.insert(job);

	info.GetReturnValue().Set(Nan::Undefined());
}
示例#7
0
void Client::Exist(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 2, "2")
	CHECK_N_ARGS_IS_A_STRING(info, 0, "0")
	CHECK_N_ARGS_IS_A_FUNCTION(info, 1, "1")

	const string memcached_key = GET_STRING_FROM_PARAM(info[0]);
	Callback* callback = GET_CALLBACK_FROM_PARAM(info[1]);

	JobBase* job = new ExistJob(callback, memcached_key);
	job->setDebug(memClient->debug);
	memClient->jobs.insert(job);

	info.GetReturnValue().Set(Nan::Undefined());
}
示例#8
0
void Client::Delete(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 3, "3")
	CHECK_N_ARGS_IS_A_STRING(info, 0, "0")
	CHECK_N_ARGS_IS_AN_UNSIGNED_INTEGER(info, 1, "1")
	CHECK_N_ARGS_IS_A_FUNCTION(info, 2, "2")

	const string memcached_key = GET_STRING_FROM_PARAM(info[0]);
	uint32_t expirationTime = (uint32_t) GET_NUMBER_FROM_PARAM(info[1]);
	Callback* callback = GET_CALLBACK_FROM_PARAM(info[2]);

	JobBase* job = new DeleteJob(callback, memcached_key, expirationTime);
	job->setDebug(memClient->debug);
	memClient->jobs.insert(job);

	info.GetReturnValue().Set(Nan::Undefined());
}
示例#9
0
void Client::Replace(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 4, "4")
	CHECK_N_ARGS_IS_A_STRING(info, 0, "0")
	CHECK_N_ARGS_IS_A_STRING(info, 1, "1")
	CHECK_N_ARGS_IS_A_NUMBER(info, 2, "2")
	CHECK_N_ARGS_IS_A_FUNCTION(info, 3, "3")

	const string memcached_key = GET_STRING_FROM_PARAM(info[0]);
	const string memcached_value = GET_STRING_FROM_PARAM(info[1]);
	time_t ttl = (time_t) GET_NUMBER_FROM_PARAM(info[2]);
	Callback* callback = GET_CALLBACK_FROM_PARAM(info[3]);

	JobBase* job = new ReplaceJob(callback, memcached_key, memcached_value, ttl);
	job->setDebug(memClient->debug);
	memClient->jobs.insert(job);

	info.GetReturnValue().Set(Nan::Undefined());
}
示例#10
0
void MeCab_Tagger::ParseToNode(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  MeCab_Tagger* mecabTagger = ObjectWrap::Unwrap<MeCab_Tagger>(info.Holder());
  v8::String::Utf8Value input(info[0]);
  v8::Local<v8::Function> callback = info[1].As<v8::Function>();
  std::vector<v8::Local<v8::Object>> node_list;
  const MeCab::Node* node = mecabTagger->tagger->parseToNode(*input);

  for (; node; node = node->next) {
    v8::Local<v8::Object> obj = Nan::New<v8::Object>();
    obj->Set(Nan::New("word").ToLocalChecked(), Nan::New(std::string(node->surface, node->length)).ToLocalChecked());
    obj->Set(Nan::New("id").ToLocalChecked(), Nan::New(node->id));
    obj->Set(Nan::New("surface").ToLocalChecked(), Nan::New(node->surface).ToLocalChecked());
    obj->Set(Nan::New("feature").ToLocalChecked(), Nan::New(node->feature).ToLocalChecked());
    obj->Set(Nan::New("len").ToLocalChecked(), Nan::New(node->length));
    obj->Set(Nan::New("rcAttr").ToLocalChecked(), Nan::New(node->rcAttr));
    obj->Set(Nan::New("lcAttr").ToLocalChecked(), Nan::New(node->lcAttr));
    obj->Set(Nan::New("posid").ToLocalChecked(), Nan::New(node->posid));
    obj->Set(Nan::New("char_type").ToLocalChecked(), Nan::New(node->char_type));
    obj->Set(Nan::New("stat").ToLocalChecked(), Nan::New(node->stat));
    obj->Set(Nan::New("isbest").ToLocalChecked(), Nan::New(node->isbest));
    obj->Set(Nan::New("alpha").ToLocalChecked(), Nan::New(node->alpha));
    obj->Set(Nan::New("beta").ToLocalChecked(), Nan::New(node->beta));
    obj->Set(Nan::New("prob").ToLocalChecked(), Nan::New(node->prob));
    obj->Set(Nan::New("cost").ToLocalChecked(), Nan::New((int)node->cost));
    node_list.push_back(obj);
  }

  v8::Local<v8::Array> results = Nan::New<v8::Array>(node_list.size());
  for(unsigned int i = 0; i < results->Length(); ++i) {
    results->Set(i, node_list.at(i));
  }

  const unsigned argc = 2;
  v8::Local<v8::Value> argv[] = {
    Nan::Null(),
    results
  };
  Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
}
示例#11
0
void Client::MGetAndFetchAll(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());

	CHECK_ARGUMENT_LENGTH(info, 2, "2")
	CHECK_N_ARGS_IS_AN_ARRAY(info, 0, "0")
	CHECK_N_ARGS_IS_A_FUNCTION(info, 1, "1")

	v8::Local<v8::Object> arr = info[0]->ToObject();
	size_t number_of_keys = arr->GetOwnPropertyNames()->Length();
	memClient->debug && printf("%s %lu %s\n", "Array with", number_of_keys, "keys");

	std::vector<string> keys;
	for(size_t i = 0; i < number_of_keys; i++) {
		keys.push_back(GET_STRING_FROM_PARAM(arr->Get(i)));
	}

	Callback* callback = GET_CALLBACK_FROM_PARAM(info[1]);

	JobBase* job = new MGetAndFetchAllJob(callback, keys);
	job->setDebug(memClient->debug);
	memClient->jobs.insert(job);

	info.GetReturnValue().Set(Nan::Undefined());
}
示例#12
0
void Client::Start(const Nan::FunctionCallbackInfo<v8::Value>& info) {
	Client* memClient = ObjectWrap::Unwrap<Client>(info.Holder());
	memClient->isRunning = true;
	memClient->progressWorker = new MemcachedAsyncProgressWorker(memClient, new Callback());
	Nan::AsyncQueueWorker(memClient->progressWorker);
}
示例#13
0
inline void asyncForTiles(const Nan::FunctionCallbackInfo<v8::Value> &info,
                          ParameterParser argsToParams,
                          ServiceMemFn service,
                          bool requires_multiple_coordinates)
{
    auto params = argsToParams(info, requires_multiple_coordinates);
    if (!params)
        return;

    auto pluginParams = argumentsToPluginParameters(info);

    BOOST_ASSERT(params->IsValid());

    if (!info[info.Length() - 1]->IsFunction())
        return Nan::ThrowTypeError("last argument must be a callback function");

    auto *const self = Nan::ObjectWrap::Unwrap<Engine>(info.Holder());
    using ParamPtr = decltype(params);

    struct Worker final : Nan::AsyncWorker
    {
        using Base = Nan::AsyncWorker;

        Worker(std::shared_ptr<osrm::OSRM> osrm_,
               ParamPtr params_,
               ServiceMemFn service,
               Nan::Callback *callback,
               PluginParameters pluginParams_)
            : Base(callback), osrm{std::move(osrm_)}, service{std::move(service)},
              params{std::move(params_)}, pluginParams{std::move(pluginParams_)}
        {
        }

        void Execute() override try
        {
            const auto status = ((*osrm).*(service))(*params, result);
            ParseResult(status, result);
        }
        catch (const std::exception &e)
        {
            SetErrorMessage(e.what());
        }

        void HandleOKCallback() override
        {
            Nan::HandleScope scope;

            const constexpr auto argc = 2u;
            v8::Local<v8::Value> argv[argc] = {Nan::Null(), render(result)};

            callback->Call(argc, argv);
        }

        // Keeps the OSRM object alive even after shutdown until we're done with callback
        std::shared_ptr<osrm::OSRM> osrm;
        ServiceMemFn service;
        const ParamPtr params;
        const PluginParameters pluginParams;

        std::string result;
    };

    auto *callback = new Nan::Callback{info[info.Length() - 1].As<v8::Function>()};
    Nan::AsyncQueueWorker(
        new Worker{self->this_, std::move(params), service, callback, std::move(pluginParams)});
}