Permalink
Browse files

mq, bugfix: params lost in asynchronous js handler.

  • Loading branch information...
xicilion committed Nov 3, 2017
1 parent 25c6e68 commit 4fc8482a9994b4fb265fd4b004d6a0738306d739
Showing with 38 additions and 41 deletions.
  1. +23 −11 fibjs/src/mq/JSHandler.cpp
  2. +15 −30 test/mq_test.js
View
@@ -37,30 +37,42 @@ result_t JSHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
if (ac->isAsync())
return CHECK_ERROR(CALL_E_NOASYNC);
v8::Local<v8::Object> o = v->wrap();
Isolate* isolate = holder();
obj_ptr<Message_base> msg = Message_base::getInstance(v);
v8::Local<v8::Value> a = v8::Local<v8::Value>::New(isolate->m_isolate, o);
if (m_async) {
v8::Local<v8::Value> v1 = GetPrivate("handler");
if (IsEmpty(v1))
return CALL_RETURN_NULL;
Isolate* isolate = holder();
retVal = new AsyncWaitHandler();
v8::Local<v8::Function> proc = v8::Local<v8::Function>::Cast(v1);
v8::Local<v8::Value> args[2];
obj_ptr<NArray> params;
std::vector<v8::Local<v8::Value>> argv;
int32_t len = 0, i;
args[0] = v->wrap();
args[1] = isolate->NewFunction("done", _done, retVal->wrap());
if (msg != NULL) {
msg->get_params(params);
params->get_length(len);
}
argv.resize(len + 2);
argv[0] = a;
for (i = 0; i < len; i++) {
Variant v;
params->_indexed_getter(i, v);
argv[i + 1] = v;
}
argv[len + 1] = isolate->NewFunction("done", _done, retVal->wrap());
proc->Call(args[0], 2, args);
proc->Call(v8::Undefined(isolate->m_isolate), len + 2, argv.data());
return 0;
}
v8::Local<v8::Object> o = v->wrap();
Isolate* isolate = holder();
obj_ptr<Message_base> msg = Message_base::getInstance(v);
v8::Local<v8::Value> a = v8::Local<v8::Value>::New(isolate->m_isolate, o);
v8::Local<v8::Value> hdlr = GetPrivate("handler");
while (true) {
View
@@ -1083,36 +1083,6 @@ describe("mq", () => {
});
});
it("await", () => {
var n = 100;
mq.invoke((r) => {
var aw = mq.await();
function delayend() {
assert.equal(n, 100);
n = 200;
aw.end();
}
coroutine.start(delayend);
return aw;
}, m);
assert.equal(n, 200);
n = 300;
mq.invoke((r) => {
var aw = mq.await();
assert.equal(n, 300);
n = 400;
aw.end();
return aw;
}, m);
assert.equal(n, 400);
});
it("sync(func)", () => {
var n = 100;
@@ -1134,6 +1104,21 @@ describe("mq", () => {
}), m);
assert.equal(n, 400);
});
it("sync(func) with params", () => {
var n;
var r = new mq.Routing({
'/:a/:b': util.sync(function (req, a, b, done) {
n = a + b;
done();
})
});
var msg = new mq.Message();
msg.value = '/123/456';
mq.invoke(r, msg);
assert.equal(n, '123456');
});
});
repl && test.run(console.DEBUG);

0 comments on commit 4fc8482

Please sign in to comment.