FFI conversion
[jscl.git] / src / prelude.js
1 // This file is prepended to the result of compile jscl.lisp, and
2 // contain runtime code that jscl assumes to exist.
3
4 var window = this;
5 var nil;
6
7 globalEval = eval;  // Just an indirect eval
8
9 function pv (x) { return x==undefined? nil: x; }
10
11 function mv(){
12     var r = [].slice.call(arguments);
13     r['multiple-value'] = true;
14     return r;
15 }
16
17 function forcemv (x) {
18     return typeof x == 'object' && 'multiple-value' in x? x: mv(x);
19 }
20
21 // NOTE: Define VALUES to be MV for toplevel forms. It is because
22 // `eval' compiles the forms and execute the Javascript code at
23 // toplevel with `js-eval', so it is necessary to return multiple
24 // values from the eval function.
25 var values = mv;
26
27 function checkArgsAtLeast(args, n){
28     if (args < n) throw 'too few arguments';
29 }
30
31 function checkArgsAtMost(args, n){
32     if (args > n) throw 'too many arguments';
33 }
34
35 function checkArgs(args, n){
36     checkArgsAtLeast(args, n);
37     checkArgsAtMost(args, n);
38 }
39
40 // Improper list constructor (like LIST*)
41 function QIList(){
42     if (arguments.length == 1)
43         return arguments[0];
44     else {
45         var i = arguments.length-1;
46         var r = arguments[i--];
47         for (; i>=0; i--){
48             r = {car: arguments[i], cdr: r};
49         }
50         return r;
51     }
52 }
53
54
55 // Create and return a lisp string for the Javascript string STRING.
56 function make_lisp_string (string){
57     var array = string.split("");
58     array.type = 'character'
59     return array;
60 }
61
62 function xstring(x){ return x.join(''); }
63
64
65 function lisp_to_js (x) {
66     if (typeof x == 'object' && 'length' in x && x.type == 'character')
67         return xstring(x);
68     else if (typeof x == 'function'){
69         // Trampoline calling the Lisp function
70         return (function(){
71             var args = Array.prototype.slice.call(arguments);
72             for (var i in args)
73                 args[i] = js_to_lisp(args[i]);
74             return lisp_to_js(x.apply(this, [pv, arguments.length].concat(args)));
75         });
76     }
77     else return x;
78 }
79
80 function js_to_lisp (x) {
81     if (typeof x == 'string')
82         return make_lisp_string(x);
83     else if (typeof x == 'function'){
84         // Trampoline calling the JS function
85         return (function(values, nargs){
86             var args = Array.prototype.slice.call(arguments, 2);
87             for (var i in args)
88                 args[i] = lisp_to_js(args[i]);
89             return values(js_to_lisp(x.apply(this, args)));
90         });
91     } else return x;
92 }