Skip to content

Commit

Permalink
Merge pull request #2 from piscisaureus/windows1
Browse files Browse the repository at this point in the history
Towards windows support
  • Loading branch information
creationix committed Mar 14, 2012
2 parents ae92afe + 8cecf41 commit 48547f9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 53 deletions.
21 changes: 18 additions & 3 deletions .gitignore
@@ -1,4 +1,19 @@
/out
/deps
*Makefile
*.a
*.Makefile
*.mk
*.o
*.opensdf
*.sdf
*.sln
*.suo
*.vcxproj
*.vcxproj.filters
*.vcxproj.user
/build/gyp
/Debug
/deps
/gyp-mac-tool
/ipch
/Makefile
/out
/Release
9 changes: 4 additions & 5 deletions src/luv.c
Expand Up @@ -64,12 +64,11 @@ static JSBool luv_get_total_memory(JSContext *cx, unsigned argc, jsval *vp) {

static JSBool luv_loadavg(JSContext *cx, unsigned argc, jsval *vp) {
double avg[3];
jsval values[3];
uv_loadavg(avg);
jsval values[] = {
DOUBLE_TO_JSVAL(avg[0]),
DOUBLE_TO_JSVAL(avg[1]),
DOUBLE_TO_JSVAL(avg[2])
};
values[0] = DOUBLE_TO_JSVAL(avg[0]);
values[1] = DOUBLE_TO_JSVAL(avg[1]),
values[2] = DOUBLE_TO_JSVAL(avg[2]);
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(JS_NewArrayObject(cx, 3, values)));
return JS_TRUE;
}
Expand Down
8 changes: 5 additions & 3 deletions src/luv_handle.c
Expand Up @@ -27,9 +27,10 @@ void luv_on_close(uv_handle_t* handle) {
static JSBool luv_close(JSContext *cx, unsigned argc, jsval *vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_handle_t* handle;
JSObject* callback;

handle = (uv_handle_t*)JS_GetPrivate(this);

JSObject* callback;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "o", &callback)) {
return JS_FALSE;
}
Expand All @@ -49,15 +50,16 @@ static JSFunctionSpec Handle_methods[] = {

int luv_handle_init(JSContext* cx, JSObject *uv) {
Handle_prototype = JS_InitClass(cx, uv, NULL,
&Handle_class, Handle_constructor, 0,
&Handle_class, Handle_constructor, 0,
NULL, Handle_methods, NULL, NULL);
return 0;
}

/* Store an async callback in an object and put the object in the gc root for safekeeping */
JSBool luv_store_callback(JSContext* cx, JSObject *this, const char* name, JSObject* callback) {
jsval callback_val;
if (!JS_AddObjectRoot(cx, &this)) return JS_FALSE;
jsval callback_val = OBJECT_TO_JSVAL(callback);
callback_val = OBJECT_TO_JSVAL(callback);
return JS_SetProperty(cx, this, name, &callback_val);
}

Expand Down
40 changes: 25 additions & 15 deletions src/luv_stream.c
Expand Up @@ -28,10 +28,11 @@ static void luv_on_connection(uv_stream_t* server, int status) {
static JSBool luv_listen(JSContext* cx, unsigned argc, jsval *vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_stream_t* stream;
stream = (uv_stream_t*)JS_GetPrivate(this);

int backlog;
JSObject* callback;

stream = (uv_stream_t*)JS_GetPrivate(this);

if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "io", &backlog, &callback)) {
return JS_FALSE;
}
Expand All @@ -48,9 +49,10 @@ static JSBool luv_accept(JSContext* cx, unsigned argc, jsval *vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_stream_t* server;
uv_stream_t* client;
JSObject* obj;

server = (uv_stream_t*)JS_GetPrivate(this);

JSObject* obj;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "o", &obj)) {
return JS_FALSE;
}
Expand All @@ -74,9 +76,9 @@ static void luv_on_read(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
ref = (luv_ref_t*)stream->data;

if (nread >= 0) {
jsval chunk[1] = { STRING_TO_JSVAL(JS_NewStringCopyN(ref->cx, buf.base, nread)) };
jsval chunk = STRING_TO_JSVAL(JS_NewStringCopyN(ref->cx, buf.base, nread));

if (!luv_call_callback(ref->cx, ref->obj, "onData", 1, chunk)) {
if (!luv_call_callback(ref->cx, ref->obj, "onData", 1, &chunk)) {
/* TODO: report properly */
printf("Error in onData callback\n");
}
Expand Down Expand Up @@ -128,16 +130,19 @@ static JSBool luv_read_stop(JSContext* cx, unsigned argc, jsval* vp) {

static void luv_on_write(uv_write_t* req, int status) {
luv_ref_t* ref;
JSContext* cx;
JSObject* this;
JSObject* callback;

/* Get the context, instance, and handle */
ref = (luv_ref_t*)req->handle->data;
JSContext* cx = ref->cx;
JSObject* this = ref->obj;
cx = ref->cx;
this = ref->obj;

/* Get the callback */
ref = (luv_ref_t*)req->data;
assert(ref->cx == cx);
JSObject* callback = ref->obj;
callback = ref->obj;
free(ref);

if (JS_ObjectIsFunction(cx, callback)) {
Expand All @@ -155,22 +160,27 @@ static void luv_on_write(uv_write_t* req, int status) {
static JSBool luv_write(JSContext* cx, unsigned argc, jsval* vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_stream_t* stream;
stream = (uv_stream_t*)JS_GetPrivate(this);

JSString* str;
JSObject* callback;
uv_buf_t* buf;
size_t len;
luv_ref_t* ref;
uv_write_t* req;

stream = (uv_stream_t*)JS_GetPrivate(this);

if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "So", &str, &callback)) {
return JS_FALSE;
}
/* Put the string in a new uv_buf_t */
uv_buf_t* buf = (uv_buf_t*)malloc(sizeof(uv_buf_t));
size_t len = JS_GetStringEncodingLength(cx, str);
buf = (uv_buf_t*)malloc(sizeof(uv_buf_t));
len = JS_GetStringEncodingLength(cx, str);
buf->base = (char*)malloc(len);
buf->len = JS_EncodeStringToBuffer(str, buf->base, len);

/* Store a reference to the callback in the write request */
luv_ref_t* ref = LUV_REF(cx, callback);
uv_write_t* req = (uv_write_t*)malloc(sizeof(uv_write_t));
ref = LUV_REF(cx, callback);
req = (uv_write_t*)malloc(sizeof(uv_write_t));
req->data = ref;

UV_CALL(uv_write, req, stream, buf, 1, luv_on_write);
Expand All @@ -190,7 +200,7 @@ static JSFunctionSpec Stream_methods[] = {

int luv_stream_init(JSContext* cx, JSObject *uv) {
Stream_prototype = JS_InitClass(cx, uv, Handle_prototype,
&Stream_class, Stream_constructor, 0,
&Stream_class, Stream_constructor, 0,
NULL, Stream_methods, NULL, NULL);
return 0;
}
23 changes: 14 additions & 9 deletions src/luv_tcp.c
Expand Up @@ -13,13 +13,14 @@ static JSClass Tcp_class = {

static JSBool Tcp_constructor(JSContext *cx, unsigned argc, jsval *vp) {
JSObject* obj = JS_NewObject(cx, &Tcp_class, Tcp_prototype, NULL);
luv_ref_t* ref;

uv_tcp_t* handle = malloc(sizeof(uv_tcp_t));
uv_tcp_init(uv_default_loop(), handle);
JS_SetPrivate(obj, handle);

/* Store a reference to the object in the handle */
luv_ref_t* ref = LUV_REF(cx, obj);
ref = LUV_REF(cx, obj);
handle->data = ref;

JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
Expand All @@ -34,16 +35,18 @@ static void Tcp_finalize(JSContext *cx, JSObject *this) {
static JSBool luv_tcp_bind(JSContext *cx, unsigned argc, jsval *vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_tcp_t* handle;
handle = (uv_tcp_t*)JS_GetInstancePrivate(cx, this, &Tcp_class, NULL);


JSString* str;
int port;
char *host;
struct sockaddr_in address;

handle = (uv_tcp_t*)JS_GetInstancePrivate(cx, this, &Tcp_class, NULL);

if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "Si", &str, &port)) {
return JS_FALSE;
}
char *host = JS_EncodeString(cx, str);
struct sockaddr_in address = uv_ip4_addr(host, port);
host = JS_EncodeString(cx, str);
address = uv_ip4_addr(host, port);
JS_free(cx, host);

UV_CALL(uv_tcp_bind, handle, address);
Expand All @@ -55,11 +58,12 @@ static JSBool luv_tcp_bind(JSContext *cx, unsigned argc, jsval *vp) {
static JSBool luv_tcp_nodelay(JSContext *cx, unsigned argc, jsval *vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_tcp_t* handle;
handle = (uv_tcp_t*)JS_GetInstancePrivate(cx, this, &Tcp_class, NULL);

/* TODO: don't hardcode */
int enable = 1;

handle = (uv_tcp_t*)JS_GetInstancePrivate(cx, this, &Tcp_class, NULL);

UV_CALL(uv_tcp_nodelay, handle, enable);

JS_SET_RVAL(cx, vp, JSVAL_VOID);
Expand All @@ -69,12 +73,13 @@ static JSBool luv_tcp_nodelay(JSContext *cx, unsigned argc, jsval *vp) {
static JSBool luv_tcp_keepalive(JSContext *cx, unsigned argc, jsval *vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_tcp_t* handle;
handle = (uv_tcp_t*)JS_GetInstancePrivate(cx, this, &Tcp_class, NULL);

/* TODO: don't hardcode */
int enable = 1;
int delay = 500;

handle = (uv_tcp_t*)JS_GetInstancePrivate(cx, this, &Tcp_class, NULL);

UV_CALL(uv_tcp_keepalive, handle, enable, delay);

JS_SET_RVAL(cx, vp, JSVAL_VOID);
Expand All @@ -90,7 +95,7 @@ static JSFunctionSpec Tcp_methods[] = {

int luv_tcp_init(JSContext* cx, JSObject *uv) {
Tcp_prototype = JS_InitClass(cx, uv, Stream_prototype,
&Tcp_class, Tcp_constructor, 0,
&Tcp_class, Tcp_constructor, 0,
NULL, Tcp_methods, NULL, NULL);
return 0;
}
20 changes: 13 additions & 7 deletions src/luv_timer.c
Expand Up @@ -13,13 +13,15 @@ static JSClass Timer_class = {

static JSBool Timer_constructor(JSContext *cx, unsigned argc, jsval *vp) {
JSObject* obj = JS_NewObject(cx, &Timer_class, Timer_prototype, NULL);
uv_timer_t* handle;
luv_ref_t* ref;

uv_timer_t* handle = malloc(sizeof(uv_timer_t));
handle = malloc(sizeof(uv_timer_t));
uv_timer_init(uv_default_loop(), handle);
JS_SetPrivate(obj, handle);

/* Store a reference to the object in the handle */
luv_ref_t* ref = LUV_REF(cx, obj);
ref = LUV_REF(cx, obj);
handle->data = ref;

JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
Expand All @@ -44,11 +46,12 @@ void luv_on_timer(uv_timer_t* handle, int status) {
static JSBool luv_timer_start(JSContext* cx, unsigned argc, jsval *vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_timer_t* handle;
handle = (uv_timer_t*)JS_GetInstancePrivate(cx, this, &Timer_class, NULL);

int32_t timeout;
int32_t repeat;
JSObject* callback;

handle = (uv_timer_t*)JS_GetInstancePrivate(cx, this, &Timer_class, NULL);

if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "uuo", &timeout, &repeat, &callback)) {
return JS_FALSE;
}
Expand Down Expand Up @@ -86,9 +89,10 @@ static JSBool luv_timer_again(JSContext* cx, unsigned argc, jsval* vp) {
static JSBool luv_timer_set_repeat(JSContext* cx, unsigned argc, jsval* vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_timer_t* handle;
int32_t repeat;

handle = (uv_timer_t*)JS_GetInstancePrivate(cx, this, &Timer_class, NULL);

int32_t repeat;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "u", &repeat)) {
return JS_FALSE;
}
Expand All @@ -102,9 +106,11 @@ static JSBool luv_timer_set_repeat(JSContext* cx, unsigned argc, jsval* vp) {
static JSBool luv_timer_get_repeat(JSContext* cx, unsigned argc, jsval* vp) {
JSObject* this = JS_THIS_OBJECT(cx, vp);
uv_timer_t* handle;
int64_t repeat;

handle = (uv_timer_t*)JS_GetInstancePrivate(cx, this, &Timer_class, NULL);

int64_t repeat = uv_timer_get_repeat(handle);
repeat = uv_timer_get_repeat(handle);

JS_SET_RVAL(cx, vp, INT_TO_JSVAL(repeat));
return JS_TRUE;
Expand All @@ -122,7 +128,7 @@ static JSFunctionSpec Timer_methods[] = {

int luv_timer_init(JSContext* cx, JSObject *uv) {
Timer_prototype = JS_InitClass(cx, uv, Handle_prototype,
&Timer_class, Timer_constructor, 0,
&Timer_class, Timer_constructor, 0,
NULL, Timer_methods, NULL, NULL);
return 0;
}
27 changes: 17 additions & 10 deletions src/main.c
Expand Up @@ -69,21 +69,23 @@ static JSBool Exit(JSContext *cx, unsigned argc, jsval *vp) {
}

static JSBool executeFile(JSContext *cx, unsigned argc, jsval *vp) {

JSString* str;
JSObject* obj;
char *filename;
JSScript* script;
jsval result;

if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "So", &str, &obj)) {
return JS_FALSE;
}
char *filename = JS_EncodeString(cx, str);
filename = JS_EncodeString(cx, str);

JSScript* script = JS_CompileUTF8File(cx, JS_THIS_OBJECT(cx, vp), filename);
script = JS_CompileUTF8File(cx, JS_THIS_OBJECT(cx, vp), filename);
JS_free(cx, filename);
if (!script) {
return JS_FALSE;
}

jsval result;
if (!JS_ExecuteScript(cx, obj, script, &result)) {
return JS_FALSE;
}
Expand All @@ -107,11 +109,17 @@ static JSFunctionSpec binding_functions[] = {

int main(int argc, const char *argv[])
{
int index;

/* JS variables. */
JSRuntime* rt;
JSContext* cx;
JSObject* global;
JSObject* alpha;
JSObject* bindings;
jsval global_val;
JSObject* args;
jsval args_val;

/* Create a JS runtime. */
rt = JS_NewRuntime(8L * 1024L * 1024L);
Expand All @@ -134,24 +142,23 @@ int main(int argc, const char *argv[])
like Object and Array. */
if (!JS_InitStandardClasses(cx, global)) return 1;

JSObject* alpha = JS_DefineObject(cx, global, "alpha", NULL, NULL, 0);
alpha = JS_DefineObject(cx, global, "alpha", NULL, NULL, 0);

/* Attach the global functions */
if (!JS_DefineFunctions(cx, alpha, global_functions)) return 1;

/* expose the binding functions for require to use */
JSObject* bindings = JS_DefineObject(cx, alpha, "bindings", NULL, NULL, 0);
bindings = JS_DefineObject(cx, alpha, "bindings", NULL, NULL, 0);
if (!JS_DefineFunctions(cx, bindings, binding_functions)) return 1;

/* Set global on alpha */
jsval global_val = OBJECT_TO_JSVAL(global);
global_val = OBJECT_TO_JSVAL(global);
if (!JS_SetProperty(cx, alpha, "global", &global_val)) return 1;

/* Set args on alpha */
JSObject* args = JS_NewArrayObject(cx, 0, NULL);
jsval args_val = OBJECT_TO_JSVAL(args);
args = JS_NewArrayObject(cx, 0, NULL);
args_val = OBJECT_TO_JSVAL(args);
if (!JS_SetProperty(cx, alpha, "args", &args_val)) return 1;
int index;
for (index = 0; index < argc; index++) {
jsval arg = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, argv[index]));
if (!JS_SetElement(cx, args, index, &arg)) return 1;
Expand Down

0 comments on commit 48547f9

Please sign in to comment.