cwebber pushed a commit to branch compile-to-js-merge
in repository guile.
commit b3c0fcdb25b4217a526661f1e9989f1a1f533da2
Author: Ian Price <[email protected]>
AuthorDate: Thu Jun 22 14:55:07 2017 +0100
Implement cached-module-box
* module/language/js-il/runtime.js (scheme): Add module_cache field.
(scheme.primitives) Add cached-module-box primitive.
(def_guile0) Convenience for adding to (guile) module cache.
---
module/language/js-il/runtime.js | 59 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 58 insertions(+), 1 deletion(-)
diff --git a/module/language/js-il/runtime.js b/module/language/js-il/runtime.js
index 85669da..cccccc6 100644
--- a/module/language/js-il/runtime.js
+++ b/module/language/js-il/runtime.js
@@ -4,6 +4,7 @@ var scheme = {
utils : {},
env : {},
cache: [],
+ module_cache: {},
builtins: [],
dynstack : [],
// TODO: placeholders
@@ -314,7 +315,21 @@ scheme.primitives["cached-toplevel-box"] = function
(scope, sym, is_bound) {
return scheme.cache[scope][sym.name];
};
-scheme.primitives["cached-module-box"] = not_implemented_yet;
+scheme.primitives["cached-module-box"] = function (module_name, sym,
is_public, is_bound) {
+ var cache = scheme.module_cache;
+
+ while (scheme.EMPTY != module_name.cdr) {
+ cache = cache[module_name.car.name];
+ }
+
+ cache = cache[module_name.car.name];
+ var r = cache[sym.name];
+ if (typeof r === 'undefined') {
+ throw {r : "cached-module-box", s : sym, m : module_name};
+ } else {
+ return r;
+ }
+};
scheme.primitives["current-module"] = function () {
return scheme.env;
@@ -493,3 +508,45 @@ scheme.frame.Fluid = function(fluid, old_value) {
this.fluid = fluid;
this.old_value = old_value;
};
+
+// Module Cache
+scheme.module_cache["guile"] = scheme.env;
+
+function def_guile0 (name, fn) {
+ var sym = new scheme.Symbol(name); // put in obarray
+ var clos = new scheme.Closure(fn, 0);
+ var box = new scheme.Box(clos);
+ scheme.module_cache["guile"][name] = box;
+};
+
+function scm_list (self, cont) {
+ var l = scheme.EMPTY;
+ for (var i = arguments.length - 1; i >= 2; i--){
+ l = scheme.primitives.cons(arguments[i],l);
+ };
+ return cont(l);
+};
+def_guile0("list", scm_list);
+
+function scm_add(self, cont) {
+
+ var total = 0;
+ for (var i = arguments.length - 1; i >= 2; i--){
+ total += arguments[i];
+ };
+ return cont(total);
+
+};
+def_guile0("+", scm_add);
+
+function scm_mul(self, cont) {
+
+ var total = 1;
+ for (var i = arguments.length - 1; i >= 2; i--){
+ total *= arguments[i];
+ };
+ return cont(total);
+
+};
+def_guile0("*", scm_mul);
+