Do not inline builtin funcionts out of a lambda
[jscl.git] / ecmalisp.lisp
index 6a7e019..02aa16e 100644 (file)
@@ -23,7 +23,7 @@
 ;;; language to the compiler to be able to run.
 
 #+ecmalisp
-(js-eval "function id (x) { return x; }")
+(js-eval "function pv (x) { return typeof x === 'object' && 'car' in x ? x.car : x; }")
 
 #+ecmalisp
 (progn
     (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
         (oset exports (symbol-name symb) symb))))
 
   (defun get-universal-time ()
-    (+ (get-unix-time) 2208988800)))
+    (+ (get-unix-time) 2208988800))
+
+  (defun values-list (list)
+    (values-list list))
+
+  (defun values (&rest args)
+    (values-list args)))
 
 
 ;;; The compiler offers some primitives and special forms which are
         "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))
   (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.
 
 (define-raw-builtin funcall (func &rest args)
   (concat "(" (ls-compile func) ")("
-          (join (cons "id" (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 (cons "id" (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-list (list)
+  (concat "values(" list ")"))
 
 (defun macro (x)
   (and (symbolp x)
   (if (and (symbolp function)
            (claimp function 'function 'non-overridable))
       (concat (ls-compile `',function) ".fvalue("
-              (join (cons "id" (mapcar #'ls-compile args))
+              (join (cons "pv" (mapcar #'ls-compile args))
                     ", ")
               ")")
       (concat (ls-compile `#',function) "("
-              (join (cons "id" (mapcar #'ls-compile args))
+              (join (cons "pv" (mapcar #'ls-compile args))
                     ", ")
               ")")))
 
             (apply comp args)))
          ;; Built-in functions
          ((and (assoc name *builtins*)
-               (not (claimp name 'function 'notinline)))
+               (not (claimp name 'function 'notinline))
+               *compiling-lambda-p*)
           (let ((comp (second (assoc name *builtins*))))
             (apply comp args)))
          (t