1 // This file is prepended to the result of compile jscl.lisp, and
2 // contain runtime code that jscl assumes to exist.
9 globalEval = eval; // Just an indirect eval
11 function pv (x) { return x==undefined? nil: x; }
14 var r = [].slice.call(arguments);
15 r['multiple-value'] = true;
19 function forcemv (x) {
20 return typeof x == 'object' && 'multiple-value' in x? x: mv(x);
23 // NOTE: Define VALUES to be MV for toplevel forms. It is because
24 // `eval' compiles the forms and execute the Javascript code at
25 // toplevel with `js-eval', so it is necessary to return multiple
26 // values from the eval function.
29 function checkArgsAtLeast(args, n){
30 if (args < n) throw 'too few arguments';
33 function checkArgsAtMost(args, n){
34 if (args > n) throw 'too many arguments';
37 function checkArgs(args, n){
38 checkArgsAtLeast(args, n);
39 checkArgsAtMost(args, n);
42 // Improper list constructor (like LIST*)
44 if (arguments.length == 1)
47 var i = arguments.length-1;
48 var r = arguments[i--];
50 r = {car: arguments[i], cdr: r};
56 // Return a new Array of strings, each either length-1, or length-2 (a UTF-16 surrogate pair).
57 function codepoints(string) {
58 return string.split(/(?![\udc00-\udfff])/);
61 // Create and return a lisp string for the Javascript string STRING.
62 function make_lisp_string (string){
63 var array = codepoints(string);
68 function char_to_codepoint(ch) {
70 return ch.charCodeAt(0);
72 var xh = ch.charCodeAt(0) - 0xD800;
73 var xl = ch.charCodeAt(1) - 0xDC00;
74 return 0x10000 + (xh << 10) + (xl);
78 function char_from_codepoint(x) {
80 return String.fromCharCode(x);
85 return String.fromCharCode(0xD800 + xh) + String.fromCharCode(0xDC00 + xl);
89 // if a char (JS string) has the same number of codepoints after .toUpperCase(), return that, else the original.
90 function safe_char_upcase(x) {
91 var xu = x.toUpperCase();
92 if (codepoints(xu).length == 1) {
98 function safe_char_downcase(x) {
99 var xl = x.toLowerCase();
100 if (codepoints(xl).length == 1) {
107 function xstring(x){ return x.join(''); }
110 function Symbol(name, package_name){
113 this['package'] = package_name;
116 function lisp_to_js (x) {
117 if (typeof x == 'object' && 'length' in x && x.stringp == 1)
119 else if (typeof x == 'function'){
120 // Trampoline calling the Lisp function
122 var args = Array.prototype.slice.call(arguments);
124 args[i] = js_to_lisp(args[i]);
125 return lisp_to_js(x.apply(this, [pv, arguments.length].concat(args)));
131 function js_to_lisp (x) {
132 if (typeof x == 'string')
133 return make_lisp_string(x);
134 else if (typeof x == 'function'){
135 // Trampoline calling the JS function
136 return (function(values, nargs){
137 var args = Array.prototype.slice.call(arguments, 2);
139 args[i] = lisp_to_js(args[i]);
140 return values(js_to_lisp(x.apply(this, args)));