Skip to content

Commit

Permalink
Implement Check (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Jun 7, 2016
1 parent 80cd4ab commit 2cda2b6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/test-uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ p(uv.Prepare.prototype);
print("Handle.prototype (via Prepare.prototype)");
p(Object.getPrototypeOf(uv.Prepare.prototype));

print("\nCheck.prototype");
p(uv.Check.prototype);
print("Handle.prototype (via Check.prototype)");
p(Object.getPrototypeOf(uv.Check.prototype));

print("\nTcp.prototype");
p(uv.Tcp.prototype);
print("Stream.prototype (via Tcp.prototype)");
Expand All @@ -32,6 +37,12 @@ prepare.start(function () {
});
prepare.unref();

var check = new uv.Check();
check.start(function () {
print("check...");
});
check.unref();

print("\nTesting simple timeout");
var timer = new uv.Timer();
timer.start(100, 0, function () {
Expand Down
4 changes: 4 additions & 0 deletions implementations/duktape/duv/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ void duv_on_prepare(uv_prepare_t *prepare) {
duv_call_callback((uv_handle_t*)prepare, "\xffon-prepare", 0);
}

void duv_on_check(uv_check_t *check) {
duv_call_callback((uv_handle_t*)check, "\xffon-check", 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 @@ -13,6 +13,9 @@ void duv_on_timeout(uv_timer_t *timer);
// Prepare method callbacks
void duv_on_prepare(uv_prepare_t *prepare);

// Check method callbacks
void duv_on_check(uv_check_t *check);

// 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
27 changes: 27 additions & 0 deletions implementations/duktape/duv/check.c
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
#include "check.h"
#include "utils.h"
#include "callbacks.h"

duk_ret_t duv_new_check(duk_context *ctx) {
uv_check_t *check = duk_push_fixed_buffer(ctx, sizeof(uv_check_t));
duv_check(ctx, uv_check_init(duv_loop(ctx), check));
duv_setup_handle(ctx, (uv_handle_t*)check, DUV_CHECK);
return 1;
}

duk_ret_t duv_check_start(duk_context *ctx) {
dschema_check(ctx, (const duv_schema_entry[]) {
{"callback", duk_is_function},
{0,0}
});
uv_check_t *check = duv_require_this_handle(ctx, DUV_CHECK_MASK);
duk_put_prop_string(ctx, 0, "\xffon-check");
duv_check(ctx, uv_check_start(check, duv_on_check));
return 0;
}

duk_ret_t duv_check_stop(duk_context *ctx) {
uv_check_t *check = duv_require_this_handle(ctx, DUV_CHECK_MASK);
duk_del_prop_string(ctx, -1, "\xffon-check");
duv_check(ctx, uv_check_stop(check));
return 0;
}
4 changes: 4 additions & 0 deletions implementations/duktape/duv/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

#include "duv.h"

duk_ret_t duv_new_check(duk_context *ctx);
duk_ret_t duv_check_start(duk_context *ctx);
duk_ret_t duv_check_stop(duk_context *ctx);

#endif
19 changes: 19 additions & 0 deletions implementations/duktape/duv/duv.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "handle.h"
#include "timer.h"
#include "prepare.h"
#include "check.h"
#include "stream.h"
#include "tcp.h"

Expand Down Expand Up @@ -34,6 +35,12 @@ static const duk_function_list_entry duv_prepare_methods[] = {
{0,0,0}
};

static const duk_function_list_entry duv_check_methods[] = {
{"start", duv_check_start, 1},
{"stop", duv_check_stop, 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 @@ -70,6 +77,7 @@ static const duk_function_list_entry duv_funcs[] = {
// libuv handle constructors
{"Timer", duv_timer, 0},
{"Prepare", duv_prepare, 0},
{"Check", duv_new_check, 0},
{"Tcp", duv_tcp, 0},

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

// stack: nucleus uv Handle.prototype

// uv.Check.prototype
duk_get_prop_string(ctx, -2, "Check");
duk_push_object(ctx);
duk_put_function_list(ctx, -1, duv_check_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
3 changes: 3 additions & 0 deletions implementations/duktape/duv/duv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
typedef enum {
DUV_TIMER,
DUV_PREPARE,
DUV_CHECK,
DUV_TCP,
DUV_PIPE,
} duv_type_t;
Expand All @@ -15,10 +16,12 @@ typedef enum {
DUV_HANDLE_MASK =
1 << DUV_TIMER |
1 << DUV_PREPARE |
1 << DUV_CHECK |
1 << DUV_TCP |
1 << DUV_PIPE,
DUV_TIMER_MASK = 1 << DUV_TIMER,
DUV_PREPARE_MASK = 1 << DUV_PREPARE,
DUV_CHECK_MASK = 1 << DUV_CHECK,
DUV_STREAM_MASK = 1 << DUV_TCP | 1 << DUV_PIPE,
DUV_TCP_MASK = 1 << DUV_TCP,
DUV_PIPE_MASK = 1 << DUV_PIPE,
Expand Down

0 comments on commit 2cda2b6

Please sign in to comment.