Browse Source

debugging/improving the module framework

v0.7.4-release
Ryan 16 years ago
parent
commit
408526a1c1
  1. 28
      src/main.js
  2. 18
      src/node.cc
  3. 5
      test/fixtures/a.js
  4. 3
      test/fixtures/b/c.js
  5. 1
      test/fixtures/x.txt
  6. 13
      test/test-file-open.js
  7. 12
      test/test-setTimeout.js
  8. 12
      test/test-test.js

28
src/main.js

@ -25,8 +25,10 @@ node.path = new function () {
}; };
this.dirname = function (path) { this.dirname = function (path) {
if (path.charAt(0) !== "/")
path = "./" + path;
var parts = path.split("/"); 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."; throw "absolute module paths are not yet supported.";
var filename = node.path.join(base_directory, name) + ".js"; var filename = node.path.join(base_directory, name) + ".js";
File.exists(filename, function (status) { File.exists(filename, function (status) {
callback(status ? filename : null); callback(status ? filename : null);
}); });
@ -51,6 +54,7 @@ node.path = new function () {
this.target = target; this.target = target;
this.load = function (base_directory, callback) { this.load = function (base_directory, callback) {
node.debug("sub.load from <" + base_directory + "> " + this.toString());
findScript(base_directory, name, function (filename) { findScript(base_directory, name, function (filename) {
if (filename === null) { if (filename === null) {
stderr.puts("Cannot find a script matching: " + name); stderr.puts("Cannot find a script matching: " + name);
@ -79,6 +83,10 @@ node.path = new function () {
// returns the function // returns the function
var compiled = node.compile(source, filename); var compiled = node.compile(source, filename);
if (module.__on_load) {
node.debug("<"+ filename+"> has onload! this is bad");
}
module.__subs = []; module.__subs = [];
module.__require = function (name) { module.__require = function (name) {
var target = {}; var target = {};
@ -92,6 +100,7 @@ node.path = new function () {
compiled.apply(module, [filename]); compiled.apply(module, [filename]);
// The module still needs to have its submodules loaded. // The module still needs to have its submodules loaded.
this.filename = filename;
this.module = module; this.module = module;
this.subs = module.__subs; this.subs = module.__subs;
this.on_load = module.__on_load; this.on_load = module.__on_load;
@ -112,9 +121,15 @@ node.path = new function () {
var scaffold = new Scaffold(content, filename, target); var scaffold = new Scaffold(content, filename, target);
node.debug("after scaffold <" + filename + ">");
function finish() { 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(); scaffold.on_load();
}
node.debug("finish 2 load <" + filename + ">");
if (callback instanceof Function) if (callback instanceof Function)
callback(); callback();
@ -126,10 +141,13 @@ node.path = new function () {
if (scaffold.subs.length == 0) { if (scaffold.subs.length == 0) {
finish(); finish();
} else { } else {
while (scaffold.subs.length > 0) { var ncomplete = 0;
var sub = scaffold.subs.shift(); for (var i = 0; i < scaffold.subs.length; i++) {
var sub = scaffold.subs[i];
sub.load(node.path.dirname(filename), function () { 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(); finish();
}); });
} }

18
src/node.cc

@ -70,14 +70,18 @@ ExecuteString(v8::Handle<v8::String> source,
v8::Handle<v8::Value> filename) v8::Handle<v8::Value> filename)
{ {
HandleScope scope; HandleScope scope;
TryCatch try_catch;
Handle<Script> script = Script::Compile(source, filename); Handle<Script> script = Script::Compile(source, filename);
if (script.IsEmpty()) { if (script.IsEmpty()) {
return ThrowException(String::New("Error compiling string")); ReportException(&try_catch);
exit(1);
} }
Handle<Value> result = script->Run(); Handle<Value> result = script->Run();
if (result.IsEmpty()) { if (result.IsEmpty()) {
return ThrowException(String::New("Error running string")); ReportException(&try_catch);
exit(1);
} }
return scope.Close(result); return scope.Close(result);
@ -98,6 +102,15 @@ JS_METHOD(compile)
return scope.Close(result); 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 static void
OnFatalError (const char* location, const char* message) OnFatalError (const char* location, const char* message)
@ -182,6 +195,7 @@ main (int argc, char *argv[])
g->Set(String::New("node"), node); g->Set(String::New("node"), node);
JS_SET_METHOD(node, "compile", compile); JS_SET_METHOD(node, "compile", compile);
JS_SET_METHOD(node, "debug", debug);
Local<Array> arguments = Array::New(argc); Local<Array> arguments = Array::New(argc);
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {

5
test/fixtures/a.js

@ -0,0 +1,5 @@
var c = require("b/c");
exports.A = function () {
return "A";
}
exports.C = function () { return c.C(); }

3
test/fixtures/b/c.js

@ -0,0 +1,3 @@
exports.C = function () {
return "C";
}

1
test/fixtures/x.txt

@ -0,0 +1 @@
xyz

13
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;
});
};

12
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);
}

12
test/test-test.js

@ -1,9 +1,13 @@
puts(__filename);
include("mjsunit"); include("mjsunit");
puts(__filename); var a = require("fixtures/a");
function on_load () { function on_load () {
stderr.puts("hello world");
assertFalse(false, "testing the test program."); 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());
} }

Loading…
Cancel
Save