Skip to content
Browse files

EIO_Moo gets messed up req->data pointer

  • Loading branch information...
0 parents commit a9f869e0f7e0e674dd12ea1b684aeb80d0a9ef37 @pkrumins committed Aug 4, 2010
Showing with 139 additions and 0 deletions.
  1. +104 −0 async.cpp
  2. +8 −0 tests/a.js
  3. +27 −0 wscript
104 async.cpp
@@ -0,0 +1,104 @@
+#include <cstdlib>
+#include <node.h>
+#include <node_buffer.h>
+
+using namespace v8;
+using namespace node;
+
+class Async : public ObjectWrap {
+ static Persistent<FunctionTemplate> constructor_template;
+ int x, y;
+
+public:
+ Async(int xx, int yy) : x(xx), y(yy) {}
+
+ static void Initialize(Handle<Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(New);
+ constructor_template = Persistent<FunctionTemplate>::New(t);
+ constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor_template->SetClassName(String::NewSymbol("Async"));
+
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "moo", Moo);
+ target->Set(String::NewSymbol("Async"), constructor_template->GetFunction());
+ }
+
+ static Handle<Value> New(const Arguments &args) {
+ HandleScope scope;
+
+ Async *async = new Async(args[0]->Int32Value(), args[1]->Int32Value());
+ async->Wrap(args.This());
+ return args.This();
+ }
+
+ struct moo_request {
+ Persistent<Function> cb;
+ Async *async;
+ int x, y;
+ };
+
+ static int EIO_Moo(eio_req *req) {
+ //
+ // req->data pointer gets messed up!!!
+ //
+ printf("%x %x\n", req, req->data);
+ moo_request *moo_req = (moo_request *)req->data;
+ moo_req->x = 11;
+ moo_req->y = 4;
+ return 0;
+ }
+
+ static int EIO_MooAfter(eio_req *req) {
+ HandleScope scope;
+
+ ev_unref(EV_DEFAULT_UC);
+ moo_request *moo_req = (moo_request *)req->data;
+
+ Local<Value> argv[1];
+ argv[0] = Integer::New(moo_req->x * moo_req->y);
+
+ TryCatch try_catch;
+
+ moo_req->cb->Call(Context::GetCurrent()->Global(), 1, argv);
+
+ if (try_catch.HasCaught())
+ FatalException(try_catch);
+
+ moo_req->cb.Dispose();
+
+ moo_req->async->Unref();
+ free(moo_req);
+
+ return 0;
+ }
+
+ static Handle<Value> Moo(const Arguments &args) {
+ HandleScope scope;
+
+ Local<Function> cb = Local<Function>::Cast(args[0]);
+ Async *async = ObjectWrap::Unwrap<Async>(args.This());
+
+ moo_request *moo_req = (moo_request *)malloc(sizeof(moo_request));
+ moo_req->cb = Persistent<Function>::New(cb);
+ moo_req->async = async;
+
+ eio_custom(EIO_Moo, EIO_PRI_DEFAULT, EIO_MooAfter, moo_req);
+
+ ev_ref(EV_DEFAULT_UC);
+ async->Ref();
+
+ return Undefined();
+ }
+};
+
+Persistent<FunctionTemplate> Async::constructor_template;
+
+extern "C" void
+init(Handle<Object> target)
+{
+ HandleScope scope;
+
+ Async::Initialize(target);
+}
+
8 tests/a.js
@@ -0,0 +1,8 @@
+var Async = require('../async').Async;
+
+var async = new Async(1,2);
+
+async.moo(function (x) {
+ console.log(x);
+});
+
27 wscript
@@ -0,0 +1,27 @@
+import Options
+from os import unlink, symlink, popen
+from os.path import exists
+
+srcdir = "."
+blddir = "build"
+VERSION = "0.0.1"
+
+def set_options(opt):
+ opt.tool_options("compiler_cxx")
+
+def configure(conf):
+ conf.check_tool("compiler_cxx")
+ conf.check_tool("node_addon")
+
+def build(bld):
+ obj = bld.new_task_gen("cxx", "shlib", "node_addon")
+ obj.target = "async"
+ obj.source = "async.cpp"
+
+def shutdown():
+ if Options.commands['clean']:
+ if exists('async.node'): unlink('async.node')
+ else:
+ if exists('build/default/async.node') and not exists('async.node'):
+ symlink('build/default/async.node', 'async.node')
+

0 comments on commit a9f869e

Please sign in to comment.
Something went wrong with that request. Please try again.