Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Commit

Permalink
Why does this break?
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 19, 2010
1 parent d2e5fdc commit c9cdce0
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.lock-wscript
build/
4 changes: 4 additions & 0 deletions README
@@ -1,3 +1,7 @@
This is about the simplest reduction you can get for using eio_custom.

Unfortunately, it blows up on Solaris, and I can't seem to figure out why.

To run:

node-waf configure build && node test.js
86 changes: 86 additions & 0 deletions nas.cc
@@ -0,0 +1,86 @@

#include <v8.h>
#include <node.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

using namespace std;
using namespace node;
using namespace v8;

static Handle<Value> DoSomethingAsync (const Arguments&);
static int DoSomething (eio_req *);
static int DoSomething_After (eio_req *);
extern "C" void init (Handle<Object>);

struct simple_request {
int x;
int y;
char name[1];
Persistent<Function> cb;
};

static Handle<Value> DoSomethingAsync (const Arguments& args) {
HandleScope scope;
const char *usage = "usage: doSomething(x, y, name, cb)";
if (args.Length() != 4) {
return ThrowException(Exception::Error(String::New(usage)));
}
int x = args[0]->Int32Value();
int y = args[1]->Int32Value();
String::Utf8Value name(args[2]);
fprintf(stderr, " >>>DoSomethingAsync %d %d %s\n", x, y, *name);
Local<Function> cb = Local<Function>::Cast(args[3]);

simple_request *sr = (simple_request *)
malloc(sizeof(struct simple_request) + name.length() + 1);

sr->cb = Persistent<Function>::New(cb);
strncpy(sr->name, *name, name.length() + 1);
sr->x = x;
sr->y = y;

fprintf(stderr, " >>>about to eio_custom\n");
eio_custom(DoSomething, EIO_PRI_DEFAULT, DoSomething_After, sr);
ev_ref(EV_DEFAULT_UC);
fprintf(stderr, " >>>returning\n");
return Undefined();
}

// this function happens on the thread pool
static int DoSomething (eio_req *req) {
fprintf(stderr, " >>>DoSomething %d\n", req);
struct simple_request * sr = (struct simple_request *)req->data;
fprintf(stderr, " >>>about to sleep\n");
sleep(2);
fprintf(stderr, " >>>read req, about to set result\n");
// Why does this crash in Solaris?
req->result = sr->x + sr->y;
fprintf(stderr, " >>>returning\n");
return 0;
}

static int DoSomething_After (eio_req *req) {
fprintf(stderr, " >>>DoSomething_After %d\n", req);
HandleScope scope;
ev_unref(EV_DEFAULT_UC);
struct simple_request * sr = (struct simple_request *)req->data;
Local<Value> argv[3];
argv[0] = Local<Value>::New(Null());
argv[1] = Integer::New(req->result);
argv[2] = String::New(sr->name);
TryCatch try_catch;
sr->cb->Call(Context::GetCurrent()->Global(), 3, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
sr->cb.Dispose();
free(sr);
return 0;
}

extern "C" void init (Handle<Object> target) {
HandleScope scope;
NODE_SET_METHOD(target, "doSomething", DoSomethingAsync);
}
18 changes: 18 additions & 0 deletions test.js
@@ -0,0 +1,18 @@
var nas = require("./build/default/nas")
, sys = require("sys")

if (typeof console === "undefined") {
global.console = { log : function () {
sys.debug(sys.inspect([].slice.call(arguments)))
}}
}


console.log(Date.now(), "before hello")
nas.doSomething(1, 2, "hello", function (er, res, n) {
console.log(Date.now(), er, res, n)
})
console.log(Date.now(), "before goodbye")
nas.doSomething(3, 4, "goodbye", function (er, res, n) {
console.log(Date.now(), er, res, n)
})
15 changes: 15 additions & 0 deletions wscript
@@ -0,0 +1,15 @@
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 = "nas"
obj.source = "nas.cc"

0 comments on commit c9cdce0

Please sign in to comment.