summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan <ry@tinyclouds.org>2009-04-21 13:52:21 +0200
committerRyan <ry@tinyclouds.org>2009-04-21 13:52:21 +0200
commit408526a1c130dec847a50fb538657b70d6c45296 (patch)
treea1c704d4f64f4d8a0fb33a7cd4c567c2eeaca772
parentec9697b1ba442a21be2ad8046d96ace09ce95ac3 (diff)
downloadnode-new-408526a1c130dec847a50fb538657b70d6c45296.tar.gz
debugging/improving the module framework
-rw-r--r--src/main.js28
-rw-r--r--src/node.cc18
-rw-r--r--test/fixtures/a.js5
-rw-r--r--test/fixtures/b/c.js3
-rw-r--r--test/fixtures/x.txt1
-rw-r--r--test/test-file-open.js13
-rw-r--r--test/test-setTimeout.js12
-rw-r--r--test/test-test.js12
8 files changed, 81 insertions, 11 deletions
diff --git a/src/main.js b/src/main.js
index 53e9bd0332..e777ab080e 100644
--- a/src/main.js
+++ b/src/main.js
@@ -25,8 +25,10 @@ node.path = new function () {
};
this.dirname = function (path) {
+ if (path.charAt(0) !== "/")
+ path = "./" + path;
var parts = path.split("/");
- return parts.slice(0, parts.length-1);
+ return parts.slice(0, parts.length-1).join("/");
};
};
@@ -38,6 +40,7 @@ node.path = new function () {
throw "absolute module paths are not yet supported.";
var filename = node.path.join(base_directory, name) + ".js";
+
File.exists(filename, function (status) {
callback(status ? filename : null);
});
@@ -51,6 +54,7 @@ node.path = new function () {
this.target = target;
this.load = function (base_directory, callback) {
+ node.debug("sub.load from <" + base_directory + "> " + this.toString());
findScript(base_directory, name, function (filename) {
if (filename === null) {
stderr.puts("Cannot find a script matching: " + name);
@@ -79,6 +83,10 @@ node.path = new function () {
// returns the function
var compiled = node.compile(source, filename);
+ if (module.__on_load) {
+ node.debug("<"+ filename+"> has onload! this is bad");
+ }
+
module.__subs = [];
module.__require = function (name) {
var target = {};
@@ -92,6 +100,7 @@ node.path = new function () {
compiled.apply(module, [filename]);
// The module still needs to have its submodules loaded.
+ this.filename = filename;
this.module = module;
this.subs = module.__subs;
this.on_load = module.__on_load;
@@ -112,9 +121,15 @@ node.path = new function () {
var scaffold = new Scaffold(content, filename, target);
+ node.debug("after scaffold <" + filename + ">");
+
function finish() {
- if (scaffold.on_load instanceof Function)
+ node.debug("finish 1 load <" + filename + ">");
+ if (scaffold.on_load instanceof Function) {
+ node.debug("foo bar <" + filename + ">");
scaffold.on_load();
+ }
+ node.debug("finish 2 load <" + filename + ">");
if (callback instanceof Function)
callback();
@@ -126,10 +141,13 @@ node.path = new function () {
if (scaffold.subs.length == 0) {
finish();
} else {
- while (scaffold.subs.length > 0) {
- var sub = scaffold.subs.shift();
+ var ncomplete = 0;
+ for (var i = 0; i < scaffold.subs.length; i++) {
+ var sub = scaffold.subs[i];
sub.load(node.path.dirname(filename), function () {
- if(scaffold.subs.length == 0)
+ ncomplete += 1;
+ node.debug("<" + filename + "> ncomplete = " + ncomplete.toString() + " scaffold.subs.length = " + scaffold.subs.length.toString());
+ if (ncomplete === scaffold.subs.length)
finish();
});
}
diff --git a/src/node.cc b/src/node.cc
index 480dd77624..96578393cb 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -70,14 +70,18 @@ ExecuteString(v8::Handle<v8::String> source,
v8::Handle<v8::Value> filename)
{
HandleScope scope;
+ TryCatch try_catch;
+
Handle<Script> script = Script::Compile(source, filename);
if (script.IsEmpty()) {
- return ThrowException(String::New("Error compiling string"));
+ ReportException(&try_catch);
+ exit(1);
}
Handle<Value> result = script->Run();
if (result.IsEmpty()) {
- return ThrowException(String::New("Error running string"));
+ ReportException(&try_catch);
+ exit(1);
}
return scope.Close(result);
@@ -98,6 +102,15 @@ JS_METHOD(compile)
return scope.Close(result);
}
+JS_METHOD(debug)
+{
+ if (args.Length() < 1)
+ return Undefined();
+ HandleScope scope;
+ String::Utf8Value msg(args[0]->ToString());
+ fprintf(stderr, "DEBUG: %s\n", *msg);
+ return Undefined();
+}
static void
OnFatalError (const char* location, const char* message)
@@ -182,6 +195,7 @@ main (int argc, char *argv[])
g->Set(String::New("node"), node);
JS_SET_METHOD(node, "compile", compile);
+ JS_SET_METHOD(node, "debug", debug);
Local<Array> arguments = Array::New(argc);
for (int i = 0; i < argc; i++) {
diff --git a/test/fixtures/a.js b/test/fixtures/a.js
new file mode 100644
index 0000000000..111417ac1c
--- /dev/null
+++ b/test/fixtures/a.js
@@ -0,0 +1,5 @@
+var c = require("b/c");
+exports.A = function () {
+ return "A";
+}
+exports.C = function () { return c.C(); }
diff --git a/test/fixtures/b/c.js b/test/fixtures/b/c.js
new file mode 100644
index 0000000000..3524a64601
--- /dev/null
+++ b/test/fixtures/b/c.js
@@ -0,0 +1,3 @@
+exports.C = function () {
+ return "C";
+}
diff --git a/test/fixtures/x.txt b/test/fixtures/x.txt
new file mode 100644
index 0000000000..cd470e6190
--- /dev/null
+++ b/test/fixtures/x.txt
@@ -0,0 +1 @@
+xyz
diff --git a/test/test-file-open.js b/test/test-file-open.js
new file mode 100644
index 0000000000..6a738f1a75
--- /dev/null
+++ b/test/test-file-open.js
@@ -0,0 +1,13 @@
+include("mjsunit");
+var assert_count = 0;
+
+function onload () {
+ var fixtures = node.path.join(script.dirname, "fixtures");
+ var x = node.path.join(fixtures, "x.txt");
+
+ file = new File;
+ file.open(x, "r", function (status) {
+ assertTrue(status == 0);
+ assert_count += 1;
+ });
+};
diff --git a/test/test-setTimeout.js b/test/test-setTimeout.js
new file mode 100644
index 0000000000..4a05be25b4
--- /dev/null
+++ b/test/test-setTimeout.js
@@ -0,0 +1,12 @@
+include("mjsunit");
+
+function on_load () {
+ assertInstanceof(setTimeout, Function);
+ var starttime = new Date;
+ setTimeout(function () {
+ var endtime = new Date;
+ var diff = endtime - starttime;
+ if (diff < 0) diff = -diff;
+ assertTrue(900 < diff || diff < 1100);
+ }, 1000);
+}
diff --git a/test/test-test.js b/test/test-test.js
index ac9d89bbf2..e8bd677aa8 100644
--- a/test/test-test.js
+++ b/test/test-test.js
@@ -1,9 +1,13 @@
-puts(__filename);
include("mjsunit");
-puts(__filename);
+var a = require("fixtures/a");
function on_load () {
+ stderr.puts("hello world");
assertFalse(false, "testing the test program.");
- puts("i think everything is okay.");
- //mjsunit.assertEquals("test-test.js", __file__);
+
+ assertInstanceof(a.A, Function);
+ assertEquals("A", a.A());
+
+ assertInstanceof(a.C, Function);
+ assertEquals("C", a.C());
}