Permalink
Browse files

Refactor to use new module system

  • Loading branch information...
1 parent 723769e commit 3cb44fd0c70acdbfe1f26e73edebf0d5473e2294 @creationix creationix committed Mar 20, 2012
Showing with 219 additions and 221 deletions.
  1. +1 −1 deps/candor
  2. +136 −29 src/cio.cc
  3. +1 −1 src/cio.h
  4. +4 −2 src/cio_string.cc
  5. +1 −1 src/cio_string.h
  6. +12 −12 src/lhttp_parser.cc
  7. +1 −1 src/lhttp_parser.h
  8. +13 −11 src/luv.cc
  9. +1 −1 src/luv.h
  10. +0 −16 src/luv_misc.cc
  11. +0 −9 src/luv_misc.h
  12. +2 −2 src/luv_tcp.cc
  13. +1 −1 src/luv_tcp.h
  14. +2 −2 src/luv_timer.cc
  15. +1 −1 src/luv_timer.h
  16. +17 −13 src/main.cc
  17. +2 −49 test-http.can
  18. +12 −60 test-tcp.can
  19. +12 −9 test-timer.can
View
@@ -3,43 +3,124 @@
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
-#include "cio_string.h"
+
+#include <assert.h>
+#include <stdio.h>
using namespace candor;
+static Object* builtins;
+static Object* builtinCache;
+
+// Print a value to a fd
+static void printValue(FILE* fd, Value* value, bool shallow) {
+ if (value->Is<String>()) {
+ String* string = value->ToString();
+ fprintf(fd, "\033[1;32m\"\033[0;32m%.*s\033[1;32m\"\033[0m", string->Length(), string->Value());
+ return;
+ }
+ if (value->Is<Number>()) {
+ String* string = value->ToString();
+ fprintf(fd, "\033[0;34m%.*s\033[0m", string->Length(), string->Value());
+ return;
+ }
+ if (value->Is<Boolean>()) {
+ String* string = value->ToString();
+ fprintf(fd, "\033[0;33m%.*s\033[0m", string->Length(), string->Value());
+ return;
+ }
+ if (value->Is<Function>()) {
+ fprintf(fd, "\033[0;36mfunction: %p\033[0m", value);
+ return;
+ }
+
+ if (value->Is<Object>()) {
+ if (shallow) {
+ fprintf(fd, "\033[0;35mobject: %p\033[0m", value);
+ return;
+ }
+ Object* obj = value->As<Object>();
+ Array* keys = obj->Keys();
+ int64_t length = keys->Length();
+ int64_t i = 0;
+ fprintf(fd, "\033[1;37m{\033[0m ");
+ while (i < length) {
+ Value* key = keys->Get(i);
+ if (key->Is<String>()) {
+ String* string = key->ToString();
+ fprintf(fd, "%.*s", string->Length(), string->Value());
+ } else {
+ fprintf(fd, "[");
+ printValue(fd, key, true);
+ fprintf(fd, "]");
+ }
+
+ fprintf(fd, "\033[1;37m:\033[0m ");
+ printValue(fd, obj->Get(key), true);
+ if (i < length - 1) {
+ fprintf(fd, "\033[1;37m,\033[0m ");
+ }
+ i++;
+ }
+ fprintf(fd, " \033[1;37m}\033[0m");
+ return;
+ }
+
+ if (value->Is<Array>()) {
+ if (shallow) {
+ fprintf(fd, "\033[0;36marray: %p\033[0m", value);
+ return;
+ }
+ Array* array = value->As<Array>();
+ int64_t length = array->Length();
+ int64_t i = 0;
+ fprintf(fd, "[ ");
+ while (i < length) {
+ printValue(fd, array->Get(i), true);
+ if (i < length - 1) {
+ fprintf(fd, ", ");
+ }
+ i++;
+ }
+ fprintf(fd, " ]");
+ return;
+ }
+
+ if (value->Is<CData>()) {
+ fprintf(fd, "\033[0;31mcdata: %p\033[0m", value);
+ return;
+ }
+
+ if (value->Is<Nil>()) {
+ fprintf(fd, "\033[1;30mnil\033[0m");
+ return;
+ }
+
+ assert(false);
+}
+
static Value* Print(uint32_t argc, Arguments& argv) {
// Print all arguments as strings with spaces and a newline.
for (uint32_t i = 0; i < argc; i++) {
- Value* value = argv[i];
- const char* part;
- uint32_t length;
- if (value->Is<Function>()) {
- part = "[Function]";
- length = 10;
- } else if (value->Is<Object>()) {
- part = "[Object]";
- length = 8;
- } else if (value->Is<Array>()) {
- part = "[Array]";
- length = 7;
- } else if (value->Is<CData>()) {
- part = "[CData]";
- length = 7;
- } else if (value->Is<Nil>()) {
- part = "nil";
- length = 3;
- } else {
- String* string = value->ToString();
- part = string->Value();
- length = string->Length();
+ String* string = argv[i]->ToString();
+ fprintf(stdout, "%.*s", string->Length(), string->Value());
+ if (i < argc - 1) {
+ fprintf(stdout, ", ");
}
+ }
+ fprintf(stdout, "\n");
+ return Nil::New();
+}
- if (i == argc - 1) {
- printf("%.*s\n", length, part);
- } else {
- printf("%.*s ", length, part);
+static Value* PrettyPrint(uint32_t argc, Arguments& argv) {
+ // Print all arguments as strings with spaces and a newline.
+ for (uint32_t i = 0; i < argc; i++) {
+ printValue(stdout, argv[i], false);
+ if (i < argc - 1) {
+ fprintf(stdout, ", ");
}
}
+ fprintf(stdout, "\n");
return Nil::New();
}
@@ -52,8 +133,34 @@ static Value* Exit(uint32_t argc, Arguments& argv) {
return Nil::New();
}
-void cio_init(Object* global) {
+static Value* LoadBuiltin(uint32_t argc, Arguments& argv) {
+ assert(argc == 1 && argv[0]->Is<String>());
+ String* name = argv[0]->As<String>();
+ Value* cache = builtinCache->Get(name);
+ if (!cache->Is<Nil>()) {
+ return cache;
+ }
+ Value* obj = builtins->Get(name);
+ assert(obj->Is<Function>());
+ Function* setup = obj->As<Function>();
+ Value* module = setup->Call(0, NULL);
+ if (!module->Is<Nil>()) {
+ builtinCache->Set(name, module);
+ }
+ return module;
+}
+
+Object* cio_init(Object* global) {
global->Set("print", Function::New(Print));
+ global->Set("prettyPrint", Function::New(PrettyPrint));
global->Set("exit", Function::New(Exit));
- cio_string_init(global);
+ global->Set("require", Function::New(LoadBuiltin));
+ builtins = Object::New();
+ builtinCache = Object::New();
+ global->Set("builtins", builtins);
+ // Make the objects persistent
+ new Handle<Object>(builtins);
+ new Handle<Object>(builtinCache);
+
+ return builtins;
}
View
@@ -5,6 +5,6 @@
using namespace candor;
-void cio_init(Object* global);
+Object* cio_init(Object* global);
#endif
View
@@ -68,9 +68,10 @@ static Value* readInt64(uint32_t argc, Arguments& argv) {
}
-void cio_string_init(Object* global) {
+Value* cio_string_module(uint32_t argc, Arguments& argv) {
Object* string = Object::New();
- global->Set("String", string);
+ // These match the endianess of the host.
+ // TODO: add functions using explicit endianess
string->Set("readUInt8", Function::New(readUInt8));
string->Set("readInt8", Function::New(readInt8));
string->Set("readUInt16", Function::New(readUInt16));
@@ -79,4 +80,5 @@ void cio_string_init(Object* global) {
string->Set("readInt32", Function::New(readInt32));
string->Set("readUInt64", Function::New(readUInt64));
string->Set("readInt64", Function::New(readInt64));
+ return string;
}
View
@@ -4,6 +4,6 @@
#include "candor.h"
using namespace candor;
-void cio_string_init(Object* global);
+Value* cio_string_module(uint32_t argc, Arguments& argv);
#endif
View
@@ -5,9 +5,11 @@
#include <string.h>
#include <stdio.h>
-http_parser_settings settings;
+using namespace candor;
using namespace candorIO;
+static http_parser_settings settings;
+
static int lhttp_on_message_begin(http_parser* parser) {
return (reinterpret_cast<HttpParser*>(parser->data))->Emit("messageBegin");
}
@@ -90,8 +92,7 @@ static Value* lhttp_parse_url(uint32_t argc, Arguments& argv) {
return url;
}
-void lhttp_parser_init(Object* global) {
-
+Value* http_parser_module(uint32_t argc, Arguments& argv) {
// Initialize the settings
settings.on_message_begin = lhttp_on_message_begin;
settings.on_url = lhttp_on_url;
@@ -101,15 +102,14 @@ void lhttp_parser_init(Object* global) {
settings.on_body = lhttp_on_body;
settings.on_message_complete = lhttp_on_message_complete;
- Object* http_parser = Object::New();
- global->Set("HttpParser", http_parser);
- http_parser->Set("parseUrl", Function::New(lhttp_parse_url));
- http_parser->Set("create", Function::New(lhttp_create));
- http_parser->Set("init", Function::New(lhttp_init));
- http_parser->Set("execute", Function::New(lhttp_execute));
- http_parser->Set("finish", Function::New(lhttp_finish));
- http_parser->Set("pause", Function::New(lhttp_pause));
-
+ Object* module = Object::New();
+ module->Set("parseUrl", Function::New(lhttp_parse_url));
+ module->Set("create", Function::New(lhttp_create));
+ module->Set("init", Function::New(lhttp_init));
+ module->Set("execute", Function::New(lhttp_execute));
+ module->Set("finish", Function::New(lhttp_finish));
+ module->Set("pause", Function::New(lhttp_pause));
+ return module;
}
View
@@ -23,6 +23,6 @@ namespace candorIO {
}
-void lhttp_parser_init(Object* global);
+Value* http_parser_module(uint32_t argc, Arguments& argv);
#endif
View
@@ -1,19 +1,21 @@
#include "luv.h"
#include "candor.h"
#include "uv.h"
-#include "luv_misc.h"
-#include "luv_timer.h"
-#include "luv_tcp.h"
-using namespace candor;
+#include <stdio.h>
-void luv_init(Object* global) {
- // Create a global args array.
- Object* uv = Object::New();
- global->Set("uv", uv);
+using namespace candor;
- luv_misc_init(uv);
- luv_timer_init(uv);
- luv_tcp_init(uv);
+static Value* luv_last_error(uint32_t argc, Arguments& argv) {
+ Object* error = Object::New();
+ uv_err_t err = uv_last_error(uv_default_loop());
+ error->Set("name", String::New(uv_err_name(err)));
+ error->Set("error", String::New(uv_strerror(err)));
+ return error;
+}
+Value* uv_base_module(uint32_t argc, Arguments& argv) {
+ Object* uv = Object::New();
+ uv->Set("lastError", Function::New(luv_last_error));
+ return uv;
}
View
@@ -28,6 +28,6 @@ enum kTYPE {
kPROCESS = isHANDLE | isPROCESS
};
-void luv_init(Object* global);
+Value* uv_base_module(uint32_t argc, Arguments& argv);
#endif
View
@@ -1,16 +0,0 @@
-#include "candor.h"
-#include "uv.h"
-#include "luv.h"
-#include "luv_misc.h"
-
-static Value* luv_last_error(uint32_t argc, Arguments& argv) {
- Object* error = Object::New();
- uv_err_t err = uv_last_error(uv_default_loop());
- error->Set("name", String::New(uv_err_name(err)));
- error->Set("error", String::New(uv_strerror(err)));
- return error;
-}
-
-void luv_misc_init(Object* uv) {
- uv->Set("lastError", Function::New(luv_last_error));
-}
View
@@ -1,9 +0,0 @@
-#ifndef LUV_MISC_H
-#define LUV_MISC_H
-
-#include "candor.h"
-using namespace candor;
-
-void luv_misc_init(Object* uv);
-
-#endif
View
@@ -118,9 +118,8 @@ static Value* luv_close(uint32_t argc, Arguments& argv) {
}
// Create the Timer object that wraps the C functions
-void luv_tcp_init(Object* uv) {
+Value* uv_tcp_module(uint32_t argc, Arguments& argv) {
Object* tcp = Object::New();
- uv->Set("Tcp", tcp);
tcp->Set("create", Function::New(luv_create_tcp));
tcp->Set("nodelay", Function::New(luv_tcp_nodelay));
@@ -140,6 +139,7 @@ void luv_tcp_init(Object* uv) {
tcp->Set("isWritable", Function::New(luv_is_writable));
tcp->Set("close", Function::New(luv_close));
+ return tcp;
}
// Implement class methods.
View
@@ -45,6 +45,6 @@ namespace candorIO {
};
}
-void luv_tcp_init(Object* uv);
+Value* uv_tcp_module(uint32_t argc, Arguments& argv);
#endif
Oops, something went wrong.

0 comments on commit 3cb44fd

Please sign in to comment.