X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fprelude.js;h=6f5feeaa9c15efcf60799a9dee9351b6a6e86ab7;hb=68cd2db6542fa3442d46b0331ecf8be8f86c09c2;hp=38a13d7fdb52ce9d7d011b820d885498eed2ad8e;hpb=261c79ce0f1b20b7f917a4139239facbd3e89eed;p=jscl.git diff --git a/src/prelude.js b/src/prelude.js index 38a13d7..6f5feea 100644 --- a/src/prelude.js +++ b/src/prelude.js @@ -1,12 +1,10 @@ -// This file is prepended to the result of compile ecmalisp.lisp, and -// contain runtime code that ecmalisp assumes to exist. +// This file is prepended to the result of compile jscl.lisp, and +// contain runtime code that jscl assumes to exist. var window = this; var nil; -function globalEval (x) { - return eval.call (window, x); -} +globalEval = eval; // Just an indirect eval function pv (x) { return x==undefined? nil: x; } @@ -27,11 +25,11 @@ function forcemv (x) { var values = mv; function checkArgsAtLeast(args, n){ - if (args.length < n) throw 'too few arguments'; + if (args < n) throw 'too few arguments'; } function checkArgsAtMost(args, n){ - if (args.length > n) throw 'too many arguments'; + if (args > n) throw 'too many arguments'; } function checkArgs(args, n){ @@ -52,3 +50,49 @@ function QIList(){ return r; } } + + +// Create and return a lisp string for the Javascript string STRING. +function make_lisp_string (string){ + var array = string.split(""); + array.type = 'character' + return array; +} + +function xstring(x){ return x.join(''); } + + +function Symbol(name, package_name){ + this.name = name; + if (package_name) + this['package'] = package_name; +} + +function lisp_to_js (x) { + if (typeof x == 'object' && 'length' in x && x.type == 'character') + return xstring(x); + else if (typeof x == 'function'){ + // Trampoline calling the Lisp function + return (function(){ + var args = Array.prototype.slice.call(arguments); + for (var i in args) + args[i] = js_to_lisp(args[i]); + return lisp_to_js(x.apply(this, [pv, arguments.length].concat(args))); + }); + } + else return x; +} + +function js_to_lisp (x) { + if (typeof x == 'string') + return make_lisp_string(x); + else if (typeof x == 'function'){ + // Trampoline calling the JS function + return (function(values, nargs){ + var args = Array.prototype.slice.call(arguments, 2); + for (var i in args) + args[i] = lisp_to_js(args[i]); + return values(js_to_lisp(x.apply(this, args))); + }); + } else return x; +}