Skip to content

Commit

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

print("\nIdle.prototype");
p(uv.Idle.prototype);
print("Handle.prototype (via Idle.prototype)");
p(Object.getPrototypeOf(uv.Idle.prototype));

print("\nTcp.prototype");
p(uv.Tcp.prototype);
print("Stream.prototype (via Tcp.prototype)");
Expand Down Expand Up @@ -43,6 +48,13 @@ check.start(function () {
});
check.unref();

var idle = new uv.Idle();
idle.start(function () {
print("idle...");
idle.stop();
});
idle.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 @@ -27,6 +27,10 @@ void duv_on_check(uv_check_t *check) {
duv_call_callback((uv_handle_t*)check, "\xffon-check", 0);
}

void duv_on_idle(uv_idle_t *idle) {
duv_call_callback((uv_handle_t*)idle, "\xffon-idle", 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 @@ -16,6 +16,9 @@ void duv_on_prepare(uv_prepare_t *prepare);
// Check method callbacks
void duv_on_check(uv_check_t *check);

// Idle method callbacks
void duv_on_idle(uv_idle_t *idle);

// 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
19 changes: 19 additions & 0 deletions implementations/duktape/duv/duv.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "timer.h"
#include "prepare.h"
#include "check.h"
#include "idle.h"
#include "stream.h"
#include "tcp.h"

Expand Down Expand Up @@ -41,6 +42,12 @@ static const duk_function_list_entry duv_check_methods[] = {
{0,0,0}
};

static const duk_function_list_entry duv_idle_methods[] = {
{"start", duv_idle_start, 1},
{"stop", duv_idle_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 @@ -78,6 +85,7 @@ static const duk_function_list_entry duv_funcs[] = {
{"Timer", duv_timer, 0},
{"Prepare", duv_prepare, 0},
{"Check", duv_new_check, 0},
{"Idle", duv_idle, 0},
{"Tcp", duv_tcp, 0},

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

// stack: nucleus uv Handle.prototype

// uv.Idle.prototype
duk_get_prop_string(ctx, -2, "Idle");
duk_push_object(ctx);
duk_put_function_list(ctx, -1, duv_idle_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 @@ -8,6 +8,7 @@ typedef enum {
DUV_TIMER,
DUV_PREPARE,
DUV_CHECK,
DUV_IDLE,
DUV_TCP,
DUV_PIPE,
} duv_type_t;
Expand All @@ -17,11 +18,13 @@ typedef enum {
1 << DUV_TIMER |
1 << DUV_PREPARE |
1 << DUV_CHECK |
1 << DUV_IDLE |
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_IDLE_MASK = 1 << DUV_IDLE,
DUV_STREAM_MASK = 1 << DUV_TCP | 1 << DUV_PIPE,
DUV_TCP_MASK = 1 << DUV_TCP,
DUV_PIPE_MASK = 1 << DUV_PIPE,
Expand Down
27 changes: 27 additions & 0 deletions implementations/duktape/duv/idle.c
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
#include "idle.h"
#include "utils.h"
#include "callbacks.h"

duk_ret_t duv_idle(duk_context *ctx) {
uv_idle_t *idle = duk_push_fixed_buffer(ctx, sizeof(uv_idle_t));
duv_check(ctx, uv_idle_init(duv_loop(ctx), idle));
duv_setup_handle(ctx, (uv_handle_t*)idle, DUV_IDLE);
return 1;
}

duk_ret_t duv_idle_start(duk_context *ctx) {
dschema_check(ctx, (const duv_schema_entry[]) {
{"callback", duk_is_function},
{0,0}
});
uv_idle_t *idle = duv_require_this_handle(ctx, DUV_IDLE_MASK);
duk_put_prop_string(ctx, 0, "\xffon-idle");
duv_check(ctx, uv_idle_start(idle, duv_on_idle));
return 0;
}

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

#include "duv.h"

duk_ret_t duv_idle(duk_context *ctx);
duk_ret_t duv_idle_start(duk_context *ctx);
duk_ret_t duv_idle_stop(duk_context *ctx);

#endif

0 comments on commit fd07205

Please sign in to comment.