X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fprelude.js;h=e49b78181363e5d70a317b96b945c79d1de0da92;hb=14795770b0fd9ad416fe4db121be1c197e338c95;hp=277a9d98f5ae84ad9655d1c384f1719b6c2fd091;hpb=f15bd27f080e8f61fdcb2ad9e306cd94f0fa9b52;p=jscl.git diff --git a/src/prelude.js b/src/prelude.js index 277a9d9..e49b781 100644 --- a/src/prelude.js +++ b/src/prelude.js @@ -25,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){ @@ -50,3 +50,43 @@ 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 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; +}