aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/interop.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/js/interop.js b/js/interop.js
new file mode 100644
index 0000000..89da4a6
--- /dev/null
+++ b/js/interop.js
@@ -0,0 +1,36 @@
+// Node vs browser behavior
+var interop = {};
+if (typeof module === 'undefined') {
+ var exports = interop,
+ GLOBAL = window;
+}
+
+function resolve_js(str) {
+ if (str.match(/\./)) {
+ var re = /^(.*)\.\([^\.]*)$/,
+ match = re.exec(str);
+ return [eval(match[0]), eval(str)];
+ } else {
+ return [GLOBAL, eval(str)];
+ }
+}
+
+function js_to_mal(obj) {
+ var cache = [];
+ var str = JSON.stringify(obj, function(key, value) {
+ if (typeof value === 'object' && value !== null) {
+ if (cache.indexOf(value) !== -1) {
+ // Circular reference found, discard key
+ return;
+ }
+ // Store value in our collection
+ cache.push(value);
+ }
+ return value;
+ });
+ cache = null; // Enable garbage collection
+ return JSON.parse(str);
+}
+
+exports.resolve_js = interop.resolve_js = resolve_js;
+exports.js_to_mal = interop.js_to_mal = js_to_mal;