Permalink
Browse files

Update the luv_stream and luv_tcp to use the new UVData class.

  • Loading branch information...
creationix committed Mar 22, 2012
1 parent 6766625 commit 18bcbfbc4e090bb8fac35fa52c042d6ea811ae0a
Showing with 26 additions and 84 deletions.
  1. +2 −0 src/luv.cc
  2. +13 −12 src/luv_stream.cc
  3. +11 −14 src/luv_tcp.cc
  4. +0 −58 test-tcp.can
View
@@ -7,6 +7,8 @@ using namespace candor;
template uv_handle_t* UVData::ObjectTo<uv_handle_t>(candor::Object* obj);
template uv_timer_t* UVData::ObjectTo<uv_timer_t>(candor::Object* obj);
+template uv_stream_t* UVData::ObjectTo<uv_stream_t>(candor::Object* obj);
+template uv_tcp_t* UVData::ObjectTo<uv_tcp_t>(candor::Object* obj);
UVData::UVData(size_t size, Object* prototype) {
// Create a new data object
View
@@ -1,6 +1,7 @@
#include "luv_handle.h" // uv_handle_prototype
#include "luv_stream.h"
+#include "luv.h"
#include "candor.h"
#include "uv.h"
@@ -9,7 +10,7 @@
using namespace candor;
static void luv_on_shutdown(uv_shutdown_t* req, int status) {
- Object* obj = **((Handle<Object>*)req->data);
+ Object* obj = UVData::VoidToObject(req->data);
Value* callback = obj->Get("onShutdown");
if (callback->Is<Function>()) {
Value* argv[1];
@@ -22,7 +23,7 @@ static void luv_on_shutdown(uv_shutdown_t* req, int status) {
static Value* luv_shutdown(uint32_t argc, Value* argv[]) {
assert(argc >= 1 && argc <= 2);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
if (argc == 2) {
obj->Set("onShutdown", argv[1]->As<Function>());
}
@@ -33,7 +34,7 @@ static Value* luv_shutdown(uint32_t argc, Value* argv[]) {
}
static void luv_on_connection(uv_stream_t* server, int status) {
- Object* obj = **((Handle<Object>*)server->data);
+ Object* obj = UVData::VoidToObject(server->data);
Value* callback = obj->Get("onConnection");
if (callback->Is<Function>()) {
Value* argv[1];
@@ -45,7 +46,7 @@ static void luv_on_connection(uv_stream_t* server, int status) {
static Value* luv_listen(uint32_t argc, Value* argv[]) {
assert(argc >= 2 && argc <= 3);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
int backlog = argv[1]->ToNumber()->IntegralValue();
if (argc == 3) {
obj->Set("onConnection", argv[2]->As<Function>());
@@ -57,9 +58,9 @@ static Value* luv_listen(uint32_t argc, Value* argv[]) {
static Value* luv_accept(uint32_t argc, Value* argv[]) {
assert(argc == 2);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
Object* obj2 = argv[1]->As<Object>();
- uv_stream_t* client = (uv_stream_t*)obj2->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
+ uv_stream_t* client = UVData::ObjectTo<uv_stream_t>(obj2);
int status = uv_accept(handle, client);
return Number::NewIntegral(status);
}
@@ -72,7 +73,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 = **((Handle<Object>*)stream->data);
+ Object* obj = UVData::VoidToObject(stream->data);
Value* callback = obj->Get("onRead");
if (callback->Is<Function>()) {
Value* argv[2];
@@ -90,7 +91,7 @@ static void luv_on_read(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
static Value* luv_read_start(uint32_t argc, Value* argv[]) {
assert(argc >= 1 && argc <= 2);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
if (argc == 2) {
obj->Set("onRead", argv[1]->As<Function>());
}
@@ -101,7 +102,7 @@ static Value* luv_read_start(uint32_t argc, Value* argv[]) {
static Value* luv_read_stop(uint32_t argc, Value* argv[]) {
assert(argc == 1);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
int status = uv_read_stop(handle);
return Number::NewIntegral(status);
}
@@ -121,7 +122,7 @@ static void luv_on_write(uv_write_t* req, int status) {
static Value* luv_write(uint32_t argc, Value* argv[]) {
assert(argc >= 2 && argc <= 3);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
String* str = argv[1]->As<String>();
uv_buf_t* buf = new uv_buf_t;
uint32_t len = str->Length();
@@ -141,7 +142,7 @@ static Value* luv_write(uint32_t argc, Value* argv[]) {
static Value* luv_is_readable(uint32_t argc, Value* argv[]) {
assert(argc == 1);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
return uv_is_readable(handle) ?
Boolean::True() :
Boolean::False();
@@ -150,7 +151,7 @@ static Value* luv_is_readable(uint32_t argc, Value* argv[]) {
static Value* luv_is_writable(uint32_t argc, Value* argv[]) {
assert(argc == 1);
Object* obj = argv[0]->As<Object>();
- uv_stream_t* handle = (uv_stream_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_stream_t* handle = UVData::ObjectTo<uv_stream_t>(obj);
return uv_is_writable(handle) ?
Boolean::True() :
Boolean::False();
View
@@ -1,6 +1,7 @@
#include "luv_stream.h" // uv_stream_prototype
#include "luv_tcp.h"
+#include "luv.h"
#include "candor.h"
#include "uv.h"
@@ -26,19 +27,15 @@ using namespace candor;
static Value* luv_create_tcp(uint32_t argc, Value* argv[]) {
assert(argc == 0);
- 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 = new Handle<Object>(obj);
- obj->Set("cdata", cdata);
- uv_tcp_init(uv_default_loop(), handle);
- return obj;
+ UVData* data = new UVData(sizeof(uv_tcp_t), uv_tcp_prototype());
+ uv_tcp_init(uv_default_loop(), (uv_tcp_t*)data->handle);
+ return *data->obj;
}
static Value* luv_tcp_nodelay(uint32_t argc, Value* argv[]) {
assert(argc == 2);
Object* obj = argv[0]->As<Object>();
- uv_tcp_t* handle = (uv_tcp_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_tcp_t* handle = UVData::ObjectTo<uv_tcp_t>(obj);
int nodelay = argv[1]->ToBoolean()->IsTrue();
int status = uv_tcp_nodelay(handle, nodelay);
return Number::NewIntegral(status);
@@ -47,7 +44,7 @@ static Value* luv_tcp_nodelay(uint32_t argc, Value* argv[]) {
static Value* luv_tcp_keepalive(uint32_t argc, Value* argv[]) {
assert(argc == 3);
Object* obj = argv[0]->As<Object>();
- uv_tcp_t* handle = (uv_tcp_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_tcp_t* handle = UVData::ObjectTo<uv_tcp_t>(obj);
int keepalive = argv[1]->ToBoolean()->IsTrue();
unsigned int delay = argv[2]->ToNumber()->IntegralValue();
int status = uv_tcp_keepalive(handle, keepalive, delay);
@@ -57,7 +54,7 @@ static Value* luv_tcp_keepalive(uint32_t argc, Value* argv[]) {
static Value* luv_tcp_bind(uint32_t argc, Value* argv[]) {
assert(argc == 3);
Object* obj = argv[0]->As<Object>();
- uv_tcp_t* handle = (uv_tcp_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_tcp_t* handle = UVData::ObjectTo<uv_tcp_t>(obj);
const char* host = argv[1]->ToString()->Value();
int port = argv[2]->ToNumber()->IntegralValue();
int status = uv_tcp_bind(handle, uv_ip4_addr(host, port));
@@ -67,7 +64,7 @@ static Value* luv_tcp_bind(uint32_t argc, Value* argv[]) {
static Value* luv_tcp_getsockname(uint32_t argc, Value* argv[]) {
assert(argc == 1);
Object* obj = argv[0]->As<Object>();
- uv_tcp_t* handle = (uv_tcp_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_tcp_t* handle = UVData::ObjectTo<uv_tcp_t>(obj);
int port = 0;
char ip[INET6_ADDRSTRLEN];
int family;
@@ -94,7 +91,7 @@ static Value* luv_tcp_getsockname(uint32_t argc, Value* argv[]) {
static Value* luv_tcp_getpeername(uint32_t argc, Value* argv[]) {
assert(argc == 1);
Object* obj = argv[0]->As<Object>();
- uv_tcp_t* handle = (uv_tcp_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_tcp_t* handle = UVData::ObjectTo<uv_tcp_t>(obj);
int port = 0;
char ip[INET6_ADDRSTRLEN];
int family;
@@ -119,7 +116,7 @@ static Value* luv_tcp_getpeername(uint32_t argc, Value* argv[]) {
}
static void luv_on_connect(uv_connect_t* req, int status) {
- Object* obj = **((Handle<Object>*)req->data);
+ Object* obj = UVData::VoidToObject(req->data);
Value* callback = obj->Get("onConnect");
if (callback->Is<Function>()) {
Value* argv[1];
@@ -132,7 +129,7 @@ static void luv_on_connect(uv_connect_t* req, int status) {
static Value* luv_tcp_connect(uint32_t argc, Value* argv[]) {
assert(argc >= 3 && argc <= 4);
Object* obj = argv[0]->As<Object>();
- uv_tcp_t* handle = (uv_tcp_t*)obj->Get("cdata")->As<CData>()->GetContents();
+ uv_tcp_t* handle = UVData::ObjectTo<uv_tcp_t>(obj);
const char* host = argv[1]->ToString()->Value();
int port = argv[2]->ToNumber()->IntegralValue();
if (argc == 4) {
View
@@ -1,58 +0,0 @@
-print = global.print
-p = global.prettyPrint
-require = global.require
-
-throw = (err) {
- p(err)
- global.exit()
-}
-
-// Used to emit on errors when libuv badness happens
-check = (status) {
- if (status) {
- throw(require('uv').lastError())
- }
-}
-
-Tcp = require('tcp')
-
-p("Tcp", Tcp)
-server = Tcp.create()
-
-check(server:bind("0.0.0.0", 8080))
-
-onConnection = (client) {
- p("new client", client)
-
- check(client:readStart((nread, chunk) {
- if (nread == -1) {
- err = require('uv').lastError()
- if (err.name == "EOF") {
- p("client end", client)
- client:shutdown((status) {
- p("client shutdown", client)
- check(status)
- client:close(() {
- p("client left", client)
- })
- })
- } else {
- throw(err)
- }
- return
- }
- if (nread > 0) {
- client:write(chunk)
- return
- }
- }))
-
-}
-
-check(server:listen(128, () {
- client = Tcp.create()
- check(server:accept(client))
- onConnection(client)
-}))
-p("server", server)
-print("TCP echo server listening on port 8080")

0 comments on commit 18bcbfb

Please sign in to comment.