Permalink
Browse files

Add in a stage0/modules directory and a sys.js. Implement require and…

… exports. Add tests for require. A handful of fixes
  • Loading branch information...
1 parent 7f1ea77 commit b1309839a83ff28303394cd9a771fb82391592c8 @Whiteknight committed Apr 1, 2012
Showing with 106 additions and 31 deletions.
  1. +26 −12 Makefile
  2. +6 −0 js0cc.sh
  3. +2 −1 js2wxst0.js
  4. +1 −1 stage0/js/nodes.js
  5. +12 −7 stage0/js/wast.js
  6. +3 −0 stage0/modules/sys.js
  7. +10 −3 stage0/runtime/common.winxed
  8. +18 −4 stage0/runtime/jsobject.winxed
  9. +4 −3 t/harness
  10. +14 −0 t/stage0/require.t
  11. +10 −0 t/stage0_modules/sys.t
View
38 Makefile
@@ -1,3 +1,6 @@
+
+# Basic Make Targets
+
all: build
build: build-stage0 build-test
@@ -6,13 +9,32 @@ test: build
winxed --nowarn t/harness
clean:
- rm stage0/runtime/*.pir stage0/runtime/*.pbc
+ rm stage0/runtime/*.pir stage0/runtime/*.pbc stage0/modules/*.pbc
rm t/*.pir t/*.pbc
-build-stage0: stage0/js/parser.js stage0/runtime/jsobject.pbc stage0/runtime/common.pbc
+# Build Test Library
build-test: t/testlib.pbc t/harnesslib.pbc
+t/testlib.pbc: t/testlib.pir
+ parrot -o t/testlib.pbc t/testlib.pir
+
+t/testlib.pir: t/testlib.winxed
+ winxed -c -o t/testlib.pir t/testlib.winxed
+
+t/harnesslib.pbc: t/harnesslib.pir
+ parrot -o t/harnesslib.pbc t/harnesslib.pir
+
+t/harnesslib.pir: t/harnesslib.winxed
+ winxed -c -o t/harnesslib.pir t/harnesslib.winxed
+
+# Stage 0
+
+STAGE_ZERO_MODULES = \
+ stage0/modules/sys.pbc
+
+build-stage0: stage0/js/parser.js stage0/runtime/jsobject.pbc stage0/runtime/common.pbc $(STAGE_ZERO_MODULES)
+
stage0/js/parser.js: stage0/js/grammar.jiy stage0/js/lexer.jil
jison stage0/js/grammar.jiy stage0/js/lexer.jil
mv grammar.js stage0/js/parser.js
@@ -29,16 +51,8 @@ stage0/runtime/common.pbc: stage0/runtime/common.pir
stage0/runtime/common.pir: stage0/runtime/common.winxed
winxed -c -o stage0/runtime/common.pir stage0/runtime/common.winxed
-t/testlib.pbc: t/testlib.pir
- parrot -o t/testlib.pbc t/testlib.pir
+stage0/modules/sys.pbc: stage0/modules/sys.js
+ ./js0cc.sh stage0/modules/sys.js stage0/modules/sys.pbc
-t/testlib.pir: t/testlib.winxed
- winxed -c -o t/testlib.pir t/testlib.winxed
-
-t/harnesslib.pbc: t/harnesslib.pir
- parrot -o t/harnesslib.pbc t/harnesslib.pir
-
-t/harnesslib.pir: t/harnesslib.winxed
- winxed -c -o t/harnesslib.pir t/harnesslib.winxed
View
6 js0cc.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+node js2wxst0.js -o temp.winxed $1
+#cat temp.winxed
+winxed --nowarn -c -o temp.pir temp.winxed
+parrot -o $2 temp.pir
+#rm temp.winxed
View
3 js2wxst0.js
@@ -36,7 +36,8 @@ function main(args) {
}
}
- var infileText = fs.readFileSync(infile).toString();
+ var inbuffer = fs.readFileSync(infile);
+ var infileText = inbuffer.toString();
// Parse JavaScript to AST
var ast = compiler.parse(infileText);
View
2 stage0/js/nodes.js
@@ -145,7 +145,7 @@ def(expr,'LiteralExpr', {
v = 'null';
break;
case 'string':
- v = "'"+this.value+"'";
+ v = "\"" + this.value + "\"";
break;
default:
v = this.value.toString();
View
19 stage0/js/wast.js
@@ -121,6 +121,7 @@ def(wast, "Program", {
" using JavaScript.JSObject.box_function;\n" +
" using JavaScript.__store_global;\n" +
" using JavaScript.__fetch_global;\n" +
+ " var __OBJECT_CONSTRUCTOR__ = __fetch_global('Object');\n" +
" try {\n" +
" /* Setup the process */\n" +
" var __process_const = __fetch_global('Process');\n" +
@@ -133,8 +134,8 @@ def(wast, "Program", {
var name = c.name.toWinxed(st);
st.addGlobal(name);
return " using " + name + ";\n" +
- " __f = box_function(" + name + ");\n" +
- " __store_global('" + name + "', __f);\n";
+ " __f = box_function(" + name + ");\n" +
+ " __store_global('" + name + "', __f);\n";
} else
return "";
}).join("\n");
@@ -160,9 +161,13 @@ def(wast, "MainFunctionDecl", {
toWinxed : function() {
var st = new SymbolTable(null);
st.declareVarsLocally(false);
- var wx = "function __js_main__[anon,tag('js_main')](var arguments)\n" +
+ var wx = "function __js_main__[anon,tag('js_main')](var arguments = null)\n" +
"{\n" +
- FUNC_ENTRY;
+ FUNC_ENTRY +
+ " /* Setup 'exports' */\n" +
+ " var __old_exports = __fetch_global('exports');\n" +
+ " var __exports = new JavaScript.JSObject(null, __OBJECT_CONSTRUCTOR__);\n" +
+ " __store_global('exports', __exports);\n\n";
emitter.increase_indent();
var stmts = this.children.map(function(c) { return emitter.emit(c.toWinxed(st)) + ";\n"; }).join("");
@@ -178,11 +183,11 @@ def(wast, "MainFunctionDecl", {
if (fwd_fetch != "")
fwd_fetch = emitter.emit("/* Declare and fetch global values */\n") + fwd_fetch + "\n";
wx += fwd_fetch;
- wx += emitter.emit("var exports = new JavaScript.JSObject(null, __OBJECT_CONSTRUCTOR__);\n");
wx += emitter.emit("/* Begin user code */\n") +
stmts + "\n" +
emitter.emit("/* End user code */\n") +
- emitter.emit("return exports;\n") +
+ " __store_global('exports', __old_exports);\n" +
+ " return __exports;\n" +
"}";
emitter.decrease_indent();
return wx;
@@ -474,7 +479,7 @@ def(wast, "MemberExpr", {
var wx = this.children[0].toWinxed(st);
var child = this.children[1].toWinxed(st);
if (this.children[1].nodeType == "Literal") {
- if (child.substring(0, 1) == "'" && child.substring(child.length - 1, child.length) == "'")
+ if (child.substring(0, 1) == "\"" && child.substring(child.length - 1, child.length) == "\"")
wx += ".*" + child;
else
wx += ".*'" + child + "'";
View
3 stage0/modules/sys.js
@@ -0,0 +1,3 @@
+exports.puts = function(s) {
+ WX->say(s);
+};
View
13 stage0/runtime/common.winxed
@@ -7,20 +7,22 @@ namespace JavaScript
var export_registry = JavaScript.__fetch_global("!!!MODULE_EXPORTS!!!");
if (export_registry == null) {
export_registry = {};
- JavaScript.__store_global("!!!MODULE_EXPORTS!!!");
+ JavaScript.__store_global("!!!MODULE_EXPORTS!!!", export_registry);
}
return export_registry;
}
- function require(string modulename)
+ function require(string modulename, var this [named('this')])
{
+ //say("Requiring module " + modulename);
var export_registry = __get_module_registry();
if (exists export_registry[modulename])
return export_registry[modulename];
var exports = null;
try {
string fullname = "stage0/modules/" + modulename + ".pbc";
+ //say("Searcing for bytecode " + fullname);
var lib = load_packfile(fullname);
if (!lib.is_initialized("load")) {
for (var s in lib.subs_by_tag("load"))
@@ -36,6 +38,9 @@ namespace JavaScript
}
export_registry[modulename] = exports;
} catch(e) {
+ say("Have Error: " + string(e.message));
+ for (string bt in e.backtrace_strings())
+ say(bt);
// TODO: How to handle error here?
}
return exports;
@@ -56,8 +61,10 @@ namespace JavaScript
p.*"openFile" = box_function(function(string path, string mode, var this [named('this')]) {
var f = new 'FileHandle';
f.open(path, mode);
- return box_stream(f);
+ return JavaScript.FileStream.box_stream(f);
});
+
+ return p;
}
function Process(var argv, var this [named('this')])
View
22 stage0/runtime/jsobject.winxed
@@ -7,15 +7,27 @@ namespace JavaScript
const string ATTR_NAME_CONSTRUCTOR = "constructor";
const string ATTR_NAME_ATTRIBUTES = "attributes";
const string PROP_NAME_JSFUNC = "!!!jsfunction!!!";
+ const string JAESOP_ALL_GLOBALS = "JAESOP_ALL_GLOBALS";
+ const string JAESOP_CURRENT_GLOBALS = "JAESOP_CURRENT_GLOBALS";
/* Global value store
*/
- function __global_store[anon]()
+ function __setup_globals[anon, load, init]()
{
- var store = {};
- while(1)
- yield store;
+ var globals;
+ ${ get_global globals, JAESOP_ALL_GLOBALS };
+ if (globals == null) {
+ globals = {};
+ ${ set_global JAESOP_ALL_GLOBALS, globals };
+ }
+ }
+
+ inline __global_store() return var
+ {
+ var globals;
+ ${ get_global globals, JAESOP_ALL_GLOBALS };
+ return globals;
}
function __store_global(string name, var jsobj)
@@ -31,6 +43,8 @@ namespace JavaScript
return null;
}
+
+
/* JSObject
*/
View
7 t/harness
@@ -15,14 +15,15 @@ function harness_main(var args)
if (elements(args) == 0) {
args = [
"t/stage0",
- "t/stage0_runtime"
+ "t/stage0_runtime",
+ "t/stage0_modules"
];
}
harness.add_testfile_type("t/stage0", class JavaScriptStage0File);
for (string path in args) {
var parts = split("_", path);
- if (elements(parts) > 1)
+ if (path == "t/stage0_runtime")
harness.add_test_dirs("Winxed", path, 1:[named("recurse")]);
else
harness.add_test_dirs(string(parts[0]), path, 1:[named("recurse")]);
@@ -55,7 +56,7 @@ Parrot/Winxed TAP Harness for Jaesop. Execute test suite in folders. If
<DIRS> is passed execute tests in those directories only. Otherwise, execute
tests in default directories.
- default directories: ( "t/stage0" ).
+ default directories: ( "t/stage0" "t/stage0_runtime" "t/stage0_modules").
<DIRS> A list of test directories to execute
--help Show this help message
View
14 t/stage0/require.t
@@ -0,0 +1,14 @@
+// Test require
+var t = new TestObject();
+test_list([
+ function() {
+ var sys = require('sys');
+ t.not_null(sys);
+ },
+
+ function() {
+ var sys_a = require('sys');
+ var sys_b = require('sys');
+ t.same(sys_a, sys_b);
+ }
+]);
View
10 t/stage0_modules/sys.t
@@ -0,0 +1,10 @@
+// Test sys module
+var t = new TestObject();
+test_list([
+ function() {
+ t.output_is(function() {
+ var sys = require('sys');
+ sys.puts("Hello world!");
+ }, "Hello world!\n");
+ }
+]);

0 comments on commit b130983

Please sign in to comment.