1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00
mal/js/interop.js
Dov Murik d953db84fc js: Fix interop, update web REPL page
* js* renamed to js-eval and is no longer a special form (just a normal
  function in core.js)
* . is no longer a special form (just a normal function in core.js)
* fixed object/method parsing of first argument to .
* added tests for .
* updated mal.html: "Mal at a glance" shows all Mal functions
2016-06-14 23:21:38 -04:00

40 lines
1.0 KiB
JavaScript

// 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[1]), eval(str)];
} else {
return [GLOBAL, eval(str)];
}
}
function js_to_mal(obj) {
if (obj === null || obj === undefined) {
return null;
}
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;