Improve the MV function
[jscl.git] / prelude.js
1 // This file is prepended to the result of compile ecmalisp.lisp, and
2 // contain runtime code that ecmalisp assumes to exist.
3
4 var nil;
5
6 function pv (x) { return x==undefined? nil: x; }
7
8 function mv(){
9     var r = [].slice.call(arguments);
10     r['multiple-value'] = true;
11     return r;
12 }
13
14 function forcemv (x) {
15     return typeof x == 'object' && 'multiple-value' in x? x: mv(x);
16 }
17
18 // NOTE: Define VALUES to be MV for toplevel forms. It is because
19 // `eval' compiles the forms and execute the Javascript code at
20 // toplevel with `js-eval', so it is necessary to return multiple
21 // values from the eval function.
22 var values = mv;
23
24 function checkArgsAtLeast(args, n){
25     if (args.length < n) throw 'too few arguments';
26 }
27
28 function checkArgsAtMost(args, n){
29     if (args.length > n) throw 'too many arguments';
30 }
31
32 function checkArgs(args, n){
33     checkArgsAtLeast(args, n);
34     checkArgsAtMost(args, n);
35 }
36
37 // Improper list constructor (like LIST*)
38 function QIList(){
39     if (arguments.length == 1)
40         return arguments[0];
41     else {
42         var i = arguments.length-1;
43         var r = arguments[i--];
44         for (; i>=0; i--){
45             r = {car: arguments[i], cdr: r};
46         }
47         return r;
48     }
49 }