cwebber pushed a commit to branch compile-to-js-merge
in repository guile.
commit 84aa3697cc7f45dd98fcfa7bb8440cb04b935bcc
Author: Ian Price <[email protected]>
AuthorDate: Wed Aug 16 21:34:10 2017 +0100
Search for variables in imports.
* module/language/js-il/runtime.js:
(scm_module_variable): Look in imports if not in obarray.
(module_imported_variable): New procedure.
(scm_module_index_obarray, scm_module_index_uses,
scm_module_index_import_obarray): New variables.
---
module/language/js-il/runtime.js | 45 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/module/language/js-il/runtime.js b/module/language/js-il/runtime.js
index 299a0d3..cfc1faa 100644
--- a/module/language/js-il/runtime.js
+++ b/module/language/js-il/runtime.js
@@ -531,7 +531,16 @@ function scm_module_variable(module, sym) {
// 1. Check Module Obarray
if (module instanceof scheme.Struct) {
var obarray = module.fields[0];
- return obarray.lookup(sym, scheme.UNDEFINED);
+ var v = obarray.lookup(sym, scheme.UNDEFINED);
+ if (v === scheme.UNDEFINED) { // is_false
+ // 2. Search among the imported variables
+ v = module_imported_variable(module, sym);
+ return v;
+ // can't be imported
+ not_implemented_yet();
+ } else {
+ return v;
+ }
}
// 2. Search among the imported variables
// 3. Query the custom binder
@@ -542,6 +551,40 @@ function scm_module_variable(module, sym) {
return scm_pre_modules_obarray.lookup(sym, scheme.UNDEFINED);
}
+var scm_module_index_obarray = 0;
+var scm_module_index_uses = 1;
+var scm_module_index_import_obarray = 8;
+function module_imported_variable(module, sym) {
+ // search cached imported bindings
+ var imports = module.fields[scm_module_index_import_obarray];
+ var v = imports.lookup(sym, scheme.UNDEFINED);
+ if (!(scheme.UNDEFINED === (v))) {
+ return v;
+ }
+ // search use list
+ var uses = module.fields[scm_module_index_uses];
+ var found_var = scheme.FALSE;
+ var found_iface = scheme.FALSE;
+ for (; uses instanceof scheme.Pair; uses = uses.cdr) {
+ var iface = uses.car;
+ var v = scm_module_variable(iface, sym);
+ if (scheme.is_true(v)) {
+ if (scheme.is_true(found_var)) {
+ console.log("resolve duplicate binding");
+ not_implemented_yet();
+ } else {
+ found_var = v;
+ found_iface = iface;
+ }
+ }
+ }
+ if (scheme.is_true(found_var)) {
+ imports.set(sym, found_var);
+ return found_var;
+ }
+ return scheme.FALSE;
+}
+
function scm_module_define(module, sym, val) {
var v = scm_module_ensure_local_variable(module, sym);
v.x = val;