Skip to content

Commit

Permalink
fix get_result(#11)
Browse files Browse the repository at this point in the history
add pointer check before doing segment(close #12)
  • Loading branch information
Zhang Qiang committed Sep 5, 2016
1 parent f78928b commit 35fce61
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
50 changes: 33 additions & 17 deletions src/nodescws.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ void NodeScws::ScwsInit()
void NodeScws::ScwsSegment(const Nan::FunctionCallbackInfo<v8::Value>& info)
{
if (!info[0]->IsString()) {
Nan::ThrowTypeError("[scws ERROR] Argument \
should be the string to segment");
Nan::ThrowTypeError("[scws ERROR] Argument "
"should be the string to segment");
// return undefined
info.GetReturnValue()
.Set(Nan::Undefined());
Expand All @@ -206,35 +206,48 @@ void NodeScws::ScwsSegment(const Nan::FunctionCallbackInfo<v8::Value>& info)

NodeScws *nscwsp = ObjectWrap::Unwrap<NodeScws>(info.Holder());
scws_t scws = nscwsp->scws;
// destroyed?
if (!scws) {
Nan::ThrowError("[scws ERROR] scws instance not found, "
"possibly already been destroy()ed.");
// return undefined
info.GetReturnValue()
.Set(Nan::Undefined());
return;
}

// convert v8::String to char *
std::string Text(*Nan::Utf8String(info[0]->ToString()));
char *text = (char *)Text.c_str();
scws_send_text(scws, text, strlen(text));

scws_res_t res, res0;
scws_res_t res;
int res_wc = 0;
long memsize = RESMEMSTEP * sizeof(scws_result);
int memsteps = 1;

nscwsp->result_raw_ = (scws_result *)malloc(memsize);

res = res0 = scws_get_result(scws);
res = scws_get_result(scws);
while (res != NULL) {
memcpy(&nscwsp->result_raw_[res_wc], res, sizeof(*res));
res_wc++;
if (res_wc >= RESMEMSTEP * memsteps) {
long new_size = RESMEMSTEP * (memsteps + 1) * sizeof(scws_result);
if ((nscwsp->result_raw_ = (scws_result *)realloc(nscwsp->result_raw_, new_size)) == NULL) {
Log(NODESCWS_MSG_ERR, "Failed to allocate memory for results\n");
free(nscwsp->result_raw_);
scws_free(scws);
info.GetReturnValue().Set(Array::New(0));
while (res != NULL) {
memcpy(&nscwsp->result_raw_[res_wc], res, sizeof(*res));
res_wc++;
if (res_wc >= RESMEMSTEP * memsteps) {
long new_size = RESMEMSTEP * (memsteps + 1) * sizeof(scws_result);
if ((nscwsp->result_raw_ = (scws_result *)realloc(nscwsp->result_raw_, new_size)) == NULL) {
Log(NODESCWS_MSG_ERR, "Failed to allocate memory for results\n");
free(nscwsp->result_raw_);
scws_free(scws);
info.GetReturnValue().Set(Array::New(0));
}
memsteps++;
}
memsteps++;
res = res->next;
}
res = res->next;
scws_free_result(res);
res = scws_get_result(scws);
}
scws_free_result(res0);

Local<Array> resArr = Nan::New<Array>(res_wc);
for (int i = 0; i < res_wc; i++) {
Expand All @@ -259,9 +272,12 @@ void NodeScws::ScwsSegment(const Nan::FunctionCallbackInfo<v8::Value>& info)

void NodeScws::ScwsDestroy(const Nan::FunctionCallbackInfo<v8::Value>& info)
{
info.GetReturnValue().Set(Nan::New("destroy").ToLocalChecked());
NodeScws* nscwsp = ObjectWrap::Unwrap<NodeScws>(info.Holder());

// only free scws, other Local values will be GCed by v8
scws_free(nscwsp->scws);
nscwsp->scws = NULL;

info.GetReturnValue().Set(Nan::True());
}

Expand Down
15 changes: 14 additions & 1 deletion test/new_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ catch (e) {
console.log();
}

try {
var scws = new NodeScws({});
scws.destroy();
scws.segment("abc");
}
catch (e) {
console.log("should raise error: already destroy()ed");
console.log(e);
console.log();
}

const conf = {
charset: "utf8",
debug: false,
Expand All @@ -32,11 +43,13 @@ const conf = {
ignorePunct: true,
multi: "duality"
}
var content = fs.readFileSync(__dirname + "/test_doc.txt", {encoding: "utf8"});
var content = fs.readFileSync(__dirname + "/test_doc.txt", {encoding: "utf8"}),
content_en = "引言 VMwareTools 是 VMware 虚拟机中很重要的一个工具包,有些时候在虚拟机中安装完操作系统会缺少网卡驱动,不能上网,这时只要安装 VMwareTools 就可以解决问题,下面以 CentOS 为例,来说明 VMwareTools 的安装方法";
var start = new Date().valueOf(), end;
var scws = new NodeScws(conf);
// console.log(scws.getConfig());
console.log(scws.segment(content));
console.log(scws.segment(content_en));
console.log(scws.destroy());
end = new Date().valueOf();
console.log("time used: " + (end - start) + "ms");

0 comments on commit 35fce61

Please sign in to comment.