Skip to content

Commit

Permalink
Implement Async (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Jun 7, 2016
1 parent ee75382 commit eceb817
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
28 changes: 20 additions & 8 deletions app/test-uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ p(uv.Idle.prototype);
print("Handle.prototype (via Idle.prototype)");
p(Object.getPrototypeOf(uv.Idle.prototype));

print("\nAsync.prototype");
p(uv.Async.prototype);
print("Handle.prototype (via Async.prototype)");
p(Object.getPrototypeOf(uv.Async.prototype));

print("\nTcp.prototype");
p(uv.Tcp.prototype);
print("Stream.prototype (via Tcp.prototype)");
Expand All @@ -29,13 +34,6 @@ p(streamProto);
print("Handle.prototype (via Stream.prototype)");
p(Object.getPrototypeOf(streamProto));

print("\nTesting uv.walk");
var timer = new uv.Timer();
var tcp = new uv.Tcp();
uv.walk(p);
timer.close();
tcp.close();

var prepare = new uv.Prepare();
prepare.start(function () {
print("prepare...");
Expand All @@ -46,14 +44,28 @@ var check = new uv.Check();
check.start(function () {
print("check...");
});
check.unref();

var idle = new uv.Idle();
idle.start(function () {
print("idle...");
idle.stop();
});

var async = new uv.Async(function () {
print("async...");
});
p("async", async);
async.send();

print("\nTesting uv.walk");
var timer = new uv.Timer();
var tcp = new uv.Tcp();
uv.walk(p);
timer.close();
tcp.close();
check.unref();
idle.unref();
async.unref();

print("\nTesting simple timeout");
var timer = new uv.Timer();
Expand Down
21 changes: 21 additions & 0 deletions implementations/duktape/duv/async.c
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
#include "async.h"
#include "utils.h"
#include "callbacks.h"

duk_ret_t duv_async(duk_context *ctx) {
dschema_check(ctx, (const duv_schema_entry[]) {
{"callback", duk_is_function},
{0,0}
});
uv_async_t *async = duk_push_fixed_buffer(ctx, sizeof(uv_async_t));
duv_check(ctx, uv_async_init(duv_loop(ctx), async, duv_on_async));
duv_setup_handle(ctx, (uv_handle_t*)async, DUV_ASYNC);
duk_insert(ctx, 0);
duk_put_prop_string(ctx, 0, "\xffon-async");
return 1;
}

duk_ret_t duv_async_send(duk_context *ctx) {
uv_async_t *async = duv_require_this_handle(ctx, DUV_ASYNC_MASK);
duv_check(ctx, uv_async_send(async));
return 0;
}
3 changes: 3 additions & 0 deletions implementations/duktape/duv/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@

#include "duv.h"

duk_ret_t duv_async(duk_context *ctx);
duk_ret_t duv_async_send(duk_context *ctx);

#endif
4 changes: 4 additions & 0 deletions implementations/duktape/duv/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void duv_on_idle(uv_idle_t *idle) {
duv_call_callback((uv_handle_t*)idle, "\xffon-idle", 0);
}

void duv_on_async(uv_async_t *async) {
duv_call_callback((uv_handle_t*)async, "\xffon-async", 0);
}

void duv_on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
duk_context *ctx = stream->data;
if (nread >= 0) {
Expand Down
3 changes: 3 additions & 0 deletions implementations/duktape/duv/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ void duv_on_check(uv_check_t *check);
// Idle method callbacks
void duv_on_idle(uv_idle_t *idle);

// Async method callbacks
void duv_on_async(uv_async_t *async);

// Stream method callbacks
void duv_on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
void duv_on_write(uv_write_t *shutdown, int status);
Expand Down
18 changes: 18 additions & 0 deletions implementations/duktape/duv/duv.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "prepare.h"
#include "check.h"
#include "idle.h"
#include "async.h"
#include "stream.h"
#include "tcp.h"

Expand Down Expand Up @@ -48,6 +49,11 @@ static const duk_function_list_entry duv_idle_methods[] = {
{0,0,0}
};

static const duk_function_list_entry duv_async_methods[] = {
{"send", duv_async_send, 0},
{0,0,0}
};

static const duk_function_list_entry duv_stream_methods[] = {
{"shutdown", duv_shutdown, 1},
{"listen", duv_listen, 2},
Expand Down Expand Up @@ -86,6 +92,7 @@ static const duk_function_list_entry duv_funcs[] = {
{"Prepare", duv_prepare, 0},
{"Check", duv_new_check, 0},
{"Idle", duv_idle, 0},
{"Async", duv_async, 1},
{"Tcp", duv_tcp, 0},

// // pipe.c
Expand Down Expand Up @@ -223,6 +230,17 @@ duk_ret_t duv_push_module(duk_context *ctx) {

// stack: nucleus uv Handle.prototype

// uv.Async.prototype
duk_get_prop_string(ctx, -2, "Async");
duk_push_object(ctx);
duk_put_function_list(ctx, -1, duv_async_methods);
duk_dup(ctx, -3);
duk_set_prototype(ctx, -2);
duk_put_prop_string(ctx, -2, "prototype");
duk_pop(ctx);

// stack: nucleus uv Handle.prototype

// uv.Stream.prototype
duk_push_object(ctx);
duk_put_function_list(ctx, -1, duv_stream_methods);
Expand Down
2 changes: 1 addition & 1 deletion implementations/duktape/duv/duv.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ typedef enum {
DUV_PREPARE_MASK = 1 << DUV_PREPARE,
DUV_CHECK_MASK = 1 << DUV_CHECK,
DUV_IDLE_MASK = 1 << DUV_IDLE,
DUV_ASYNC_MASK = 1 << DUV_ASYNC,
DUV_POLL_MASK = 1 << DUV_POLL,
DUV_SIGNAL_MASK = 1 << DUV_SIGNAL,
DUV_PROCESS_MASK = 1 << DUV_PROCESS,
DUV_ASYNC_MASK = 1 << DUV_ASYNC,
DUV_STREAM_MASK = 1 << DUV_TCP | 1 << DUV_PIPE | 1 << DUV_TTY,
DUV_TCP_MASK = 1 << DUV_TCP,
DUV_PIPE_MASK = 1 << DUV_PIPE,
Expand Down

0 comments on commit eceb817

Please sign in to comment.