Values is inlined only in functions
[jscl.git] / ecmalisp.lisp
index abef679..0801df4 100644 (file)
@@ -23,6 +23,9 @@
 ;;; language to the compiler to be able to run.
 
 #+ecmalisp
+(js-eval "function pv (x) { return typeof x === 'object' && 'car' in x ? x.car : x; }")
+
+#+ecmalisp
 (progn
   (eval-when-compile
     (%compile-defmacro 'defmacro
     (revappend list '()))
 
   (defmacro psetq (&rest pairs)
-    (let (;; For each pair, we store here a list of the form
+    (let ( ;; For each pair, we store here a list of the form
          ;; (VARIABLE GENSYM VALUE).
          (assignments '()))
       (while t
       (aset v i x)
       (incf i))))
 
+#+ecmalisp
+(progn
+  (defun values-list (list)
+    (values-array (list-to-vector list)))
+
+  (defun values (&rest args)
+    (values-list args)))
+
+
 ;;; Like CONCAT, but prefix each line with four spaces. Two versions
 ;;; of this function are available, because the Ecmalisp version is
 ;;; very slow and bootstraping was annoying.
         "return func;" *newline*)
       (join strs)))
 
+
+(defvar *compiling-lambda-p* nil)
+
 (define-compilation lambda (lambda-list &rest body)
   (let ((required-arguments (lambda-list-required-arguments lambda-list))
         (optional-arguments (lambda-list-optional-arguments lambda-list))
         (rest-argument (lambda-list-rest-argument lambda-list))
+        (*compiling-lambda-p* t)
         documentation)
     ;; Get the documentation string for the lambda function
     (when (and (stringp (car body))
       (lambda-docstring-wrapper
        documentation
        "(function ("
-       (join (mapcar #'translate-variable
-                     (append required-arguments optional-arguments))
+       (join (cons "values"
+                   (mapcar #'translate-variable
+                           (append required-arguments optional-arguments)))
              ",")
        "){" *newline*
        ;; Check number of arguments
        (indent
         (if required-arguments
-            (concat "if (arguments.length < " (integer-to-string n-required-arguments)
+            (concat "if (arguments.length < " (integer-to-string (1+ n-required-arguments))
                     ") throw 'too few arguments';" *newline*)
             "")
         (if (not rest-argument)
             (concat "if (arguments.length > "
-                    (integer-to-string (+ n-required-arguments n-optional-arguments))
+                    (integer-to-string (+ 1 n-required-arguments n-optional-arguments))
                     ") throw 'too many arguments';" *newline*)
             "")
         ;; Optional arguments
         (if optional-arguments
-            (concat "switch(arguments.length){" *newline*
+            (concat "switch(arguments.length-1){" *newline*
                     (let ((optional-and-defaults
                            (lambda-list-optional-arguments-with-default lambda-list))
                           (cases nil)
             (let ((js!rest (translate-variable rest-argument)))
               (concat "var " js!rest "= " (ls-compile nil) ";" *newline*
                       "for (var i = arguments.length-1; i>="
-                      (integer-to-string (+ n-required-arguments n-optional-arguments))
+                      (integer-to-string (+ 1 n-required-arguments n-optional-arguments))
                       "; i--)" *newline*
                       (indent js!rest " = "
                               "{car: arguments[i], cdr: ") js!rest "};"
   (concat "(" var " = " (ls-compile val) ")"))
 
 
+
 ;;; Literals
 (defun escape-string (string)
   (let ((output "")
           "})" *newline*)
         (error (concat "Unknown tag `" n "'.")))))
 
-
 (define-compilation unwind-protect (form &rest clean-up)
   (js!selfcall
     "var ret = " (ls-compile nil) ";" *newline*
     "}" *newline*
     "return ret;" *newline*))
 
+(define-compilation multiple-value-call (func-form &rest forms)
+  (let ((func (ls-compile func-form)))
+    (js!selfcall
+      "var args = [values];" *newline*
+      "function values(){" *newline*
+      (indent "var result = [];" *newline*
+              "for (var i=0; i<arguments.length; i++)" *newline*
+              (indent "result.push(arguments[i]);"))
+      "}" *newline*
+      (mapconcat (lambda (form)
+                   (ls-compile form))
+                 forms)
+      "return (" func ").apply(window, [args]);")))
+
+
 
 ;;; A little backquote implementation without optimizations of any
 ;;; kind for ecmalisp.
   (concat "(" symbol ").value = " value))
 
 (define-builtin fset (symbol value)
-  (concat "(" symbol ").function = " value))
+  (concat "(" symbol ").fvalue = " value))
 
 (define-builtin boundp (x)
   (js!bool (concat "(" x ".value !== undefined)")))
 (define-builtin symbol-function (x)
   (js!selfcall
     "var symbol = " x ";" *newline*
-    "var func = symbol.function;" *newline*
+    "var func = symbol.fvalue;" *newline*
     "if (func === undefined) throw \"Function `\" + symbol.name + \"' is undefined.\";" *newline*
     "return func;" *newline*))
 
 
 (define-raw-builtin funcall (func &rest args)
   (concat "(" (ls-compile func) ")("
-          (join (mapcar #'ls-compile args)
+          (join (cons "pv" (mapcar #'ls-compile args))
                 ", ")
           ")"))
 
             (last (car (last args))))
         (js!selfcall
           "var f = " (ls-compile func) ";" *newline*
-          "var args = [" (join (mapcar #'ls-compile args)
+          "var args = [" (join (cons "pv" (mapcar #'ls-compile args))
                                ", ")
           "];" *newline*
           "var tail = (" (ls-compile last) ");" *newline*
 (define-builtin get-unix-time ()
   (concat "(Math.round(new Date() / 1000))"))
 
+(define-builtin values-array (array)
+  (concat "values.apply(this, " array ")"))
+
+(define-raw-builtin values (&rest args)
+  (if *compiling-lambda-p*
+      (concat "values(" (join (mapcar #'ls-compile args) ", ") ")")
+      (compile-funcall 'values args)))
+
 
 (defun macro (x)
   (and (symbolp x)
 (defun compile-funcall (function args)
   (if (and (symbolp function)
            (claimp function 'function 'non-overridable))
-      (concat (ls-compile `',function) ".function("
-              (join (mapcar #'ls-compile args)
+      (concat (ls-compile `',function) ".fvalue("
+              (join (cons "pv" (mapcar #'ls-compile args))
                     ", ")
               ")")
       (concat (ls-compile `#',function) "("
-              (join (mapcar #'ls-compile args)
+              (join (cons "pv" (mapcar #'ls-compile args))
                     ", ")
               ")")))