Browse files

Add in a rough-draft of the require() mechanism. exports does not wor…

…k with the store/fetch_global mechanism so is bound to cause confusion
  • Loading branch information...
1 parent de51ea7 commit 7f1ea779b788bec8c08ac275b36403b471801fb8 @Whiteknight committed Mar 31, 2012
Showing with 53 additions and 2 deletions.
  1. +3 −1 stage0/js/wast.js
  2. +46 −0 stage0/runtime/common.winxed
  3. +4 −1 stage0/runtime/jsobject.winxed
View
4 stage0/js/wast.js
@@ -160,7 +160,7 @@ def(wast, "MainFunctionDecl", {
toWinxed : function() {
var st = new SymbolTable(null);
st.declareVarsLocally(false);
- var wx = "function __js_main__[anon](var arguments)\n" +
+ var wx = "function __js_main__[anon,tag('js_main')](var arguments)\n" +
"{\n" +
FUNC_ENTRY;
emitter.increase_indent();
@@ -178,9 +178,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") +
"}";
emitter.decrease_indent();
return wx;
View
46 stage0/runtime/common.winxed
@@ -1,5 +1,47 @@
namespace JavaScript
{
+ namespace Modules
+ {
+ function __get_module_registry[anon]()
+ {
+ var export_registry = JavaScript.__fetch_global("!!!MODULE_EXPORTS!!!");
+ if (export_registry == null) {
+ export_registry = {};
+ JavaScript.__store_global("!!!MODULE_EXPORTS!!!");
+ }
+ return export_registry;
+ }
+
+ function require(string 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";
+ var lib = load_packfile(fullname);
+ if (!lib.is_initialized("load")) {
+ for (var s in lib.subs_by_tag("load"))
+ s();
+ lib.mark_initialized("load");
+ }
+
+ if (!lib.is_initialized("js_main")) {
+ var subs = lib.subs_by_tag("js_main");
+ if (elements(subs) > 0)
+ exports = subs[0]();
+ lib.mark_initialized("js_main");
+ }
+ export_registry[modulename] = exports;
+ } catch(e) {
+ // TODO: How to handle error here?
+ }
+ return exports;
+ }
+ }
+
namespace Process
{
function get_Process_prototype(var obj_proto)
@@ -111,5 +153,9 @@ namespace JavaScript
var p_proto = JavaScript.Process.get_Process_prototype(object_proto);
var p = JavaScript.JSObject.box_function(Process, p_proto);
JavaScript.__store_global("Process", p);
+
+ using JavaScript.Modules.require;
+ var r = JavaScript.JSObject.box_function(require);
+ JavaScript.__store_global("require", r);
}
}
View
5 stage0/runtime/jsobject.winxed
@@ -25,7 +25,10 @@ namespace JavaScript
function __fetch_global(string name)
{
- return __global_store()[name];
+ var s = __global_store();
+ if (exists s[name])
+ return s[name];
+ return null;
}
/* JSObject

0 comments on commit 7f1ea77

Please sign in to comment.