Permalink
Browse files

Use Handles for objects that need to persist

  • Loading branch information...
1 parent 1566742 commit 98978bfcb0abba1b1c180453f24f9fe7ed69ceb5 @creationix creationix committed Mar 21, 2012
Showing with 36 additions and 36 deletions.
  1. +4 −5 src/cio_string.cc
  2. +4 −4 src/lhttp_parser.cc
  3. +4 −5 src/luv.cc
  4. +1 −1 src/luv_handle.cc
  5. +11 −7 src/luv_stream.cc
  6. +6 −7 src/luv_tcp.cc
  7. +6 −7 src/luv_timer.cc
View
@@ -67,11 +67,10 @@ static Value* readInt64(uint32_t argc, Arguments& argv) {
return Number::NewIntegral(*(int64_t*)((uint8_t*)str->Value() + offset));
}
-static Object* module;
+static Handle<Object> module;
Object* cio_string_module() {
- if (module) return module;
- module = Object::New();
- new Handle<Object>(module);
+ if (!module.IsEmpty()) return *module;
+ module.Wrap(Object::New());
// These match the endianess of the host.
// TODO: add functions using explicit endianess
module->Set("readUInt8", Function::New(readUInt8));
@@ -82,5 +81,5 @@ Object* cio_string_module() {
module->Set("readInt32", Function::New(readInt32));
module->Set("readUInt64", Function::New(readUInt64));
module->Set("readInt64", Function::New(readInt64));
- return module;
+ return *module;
}
View
@@ -148,9 +148,10 @@ static Value* lhttp_parse_url(uint32_t argc, Arguments& argv) {
return url;
}
-static Object* module;
+static Handle<Object> module;
Object* http_parser_module() {
- if (module) return module;
+ if (!module.IsEmpty()) return *module;
+ module.Wrap(Object::New());
// Initialize the settings
settings.on_message_begin = lhttp_on_message_begin;
settings.on_url = lhttp_on_url;
@@ -160,10 +161,9 @@ Object* http_parser_module() {
settings.on_body = lhttp_on_body;
settings.on_message_complete = lhttp_on_message_complete;
- module = Object::New();
module->Set("parseUrl", Function::New(lhttp_parse_url));
module->Set("create", Function::New(lhttp_create));
- return module;
+ return *module;
}
static Object* prototype;
View
@@ -14,11 +14,10 @@ static Value* luv_last_error(uint32_t argc, Arguments& argv) {
return error;
}
-static Object* module;
+static Handle<Object> module;
Object* uv_base_module() {
- if (module) return module;
- module = Object::New();
- new Handle<Object>(module);
+ if (!module.IsEmpty()) return *module;
+ module.Wrap(Object::New());
module->Set("lastError", Function::New(luv_last_error));
- return module;
+ return *module;
}
View
@@ -6,7 +6,7 @@
using namespace candor;
static void luv_on_close(uv_handle_t* handle) {
- Object* obj = (Object*)handle->data;
+ Object* obj = **((Handle<Object>*)handle->data);
Value* callback = obj->Get("onTimer");
if (callback->Is<Function>()) {
callback->As<Function>()->Call(0, NULL);;
View
@@ -9,7 +9,7 @@
using namespace candor;
static void luv_on_shutdown(uv_shutdown_t* req, int status) {
- Object* obj = (Object*)req->data;
+ Object* obj = **((Handle<Object>*)req->data);
Value* callback = obj->Get("onShutdown");
if (callback->Is<Function>()) {
Value* argv[1];
@@ -33,7 +33,7 @@ static Value* luv_shutdown(uint32_t argc, Arguments& argv) {
}
static void luv_on_connection(uv_stream_t* server, int status) {
- Object* obj = (Object*)server->data;
+ Object* obj = **((Handle<Object>*)server->data);
Value* callback = obj->Get("onConnection");
if (callback->Is<Function>()) {
Value* argv[1];
@@ -72,7 +72,7 @@ static uv_buf_t luv_on_alloc(uv_handle_t* handle, size_t suggested_size) {
}
static void luv_on_read(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
- Object* obj = (Object*)stream->data;
+ Object* obj = **((Handle<Object>*)stream->data);
Value* callback = obj->Get("onRead");
if (callback->Is<Function>()) {
Value* argv[2];
@@ -107,11 +107,15 @@ static Value* luv_read_stop(uint32_t argc, Arguments& argv) {
}
static void luv_on_write(uv_write_t* req, int status) {
- // TODO call write callback if there is one
- // Value* argv[1];
- // argv[0] = Number::NewIntegral(status);
- // onWrite->Call(1, argv);
+ // TODO: call write callback if there is one
// memory managent probably needs to happen too.
+ // Object* obj = **((Handle<Object>*)req->data);
+ // Value* callback = obj->Get("onWrite");
+ // if (callback->Is<Function>()) {
+ // Value* argv[1];
+ // argv[0] = Number::NewIntegral(status);
+ // callback->As<Function>()->Call(1, argv);
+ // }
}
static Value* luv_write(uint32_t argc, Arguments& argv) {
View
@@ -31,7 +31,7 @@ static Value* luv_create_tcp(uint32_t argc, Arguments& argv) {
Object* obj = uv_tcp_prototype()->Clone();
CData* cdata = CData::New(sizeof(uv_tcp_t));
uv_tcp_t* handle = (uv_tcp_t*)cdata->GetContents();
- handle->data = obj;
+ handle->data = new Handle<Object>(obj);
obj->Set("cdata", cdata);
uv_tcp_init(uv_default_loop(), handle);
return obj;
@@ -121,7 +121,7 @@ static Value* luv_tcp_getpeername(uint32_t argc, Arguments& argv) {
}
static void luv_on_connect(uv_connect_t* req, int status) {
- Object* obj = (Object*)req->data;
+ Object* obj = **((Handle<Object>*)req->data);
Value* callback = obj->Get("onConnect");
if (callback->Is<Function>()) {
Value* argv[1];
@@ -147,13 +147,12 @@ static Value* luv_tcp_connect(uint32_t argc, Arguments& argv) {
return Number::NewIntegral(status);
}
-static Object* module;
+static Handle<Object> module;
Object* uv_tcp_module() {
- if (module) return module;
- module = Object::New();
- new Handle<Object>(module);
+ if (!module.IsEmpty()) return *module;
+ module.Wrap(Object::New());
module->Set("create", Function::New(luv_create_tcp));
- return module;
+ return *module;
}
static Handle<Object> prototype;
View
@@ -12,14 +12,14 @@ static Value* luv_create_timer(uint32_t argc, Arguments& argv) {
Object* obj = uv_timer_prototype()->Clone();
CData* cdata = CData::New(sizeof(uv_timer_t));
uv_timer_t* handle = (uv_timer_t*)cdata->GetContents();
- handle->data = obj;
+ handle->data = new Handle<Object>(obj);
obj->Set("cdata", cdata);
uv_timer_init(uv_default_loop(), handle);
return obj;
}
static void luv_on_timer(uv_timer_t* handle, int status) {
- Object* obj = (Object*)handle->data;
+ Object* obj = **((Handle<Object>*)handle->data);
Value* callback = obj->Get("onTimer");
if (callback->Is<Function>()) {
Value* argv[1];
@@ -74,13 +74,12 @@ static Value* luv_timer_set_repeat(uint32_t argc, Arguments& argv) {
return Nil::New();
}
-static Object* module;
+static Handle<Object> module;
Object* uv_timer_module() {
- if (module) return module;
- module = Object::New();
- new Handle<Object>(module);
+ if (!module.IsEmpty()) return *module;
+ module.Wrap(Object::New());
module->Set("create", Function::New(luv_create_timer));
- return module;
+ return *module;
}
static Handle<Object> prototype;

0 comments on commit 98978bf

Please sign in to comment.