`(push (list ',name (lambda ,args (block ,name ,@body)))
*compilations*))
-(define-compilation if (condition true false)
+(define-compilation if (condition true &optional false)
(code "(" (ls-compile condition) " !== " (ls-compile nil)
" ? " (ls-compile true *multiple-value-p*)
" : " (ls-compile false *multiple-value-p*)
(js!selfcall
"var func = " (join strs) ";" *newline*
(when name
- (code "func.fname = \"" (escape-string name) "\";" *newline*))
+ (code "func.fname = " (js-escape-string name) ";" *newline*))
(when docstring
- (code "func.docstring = \"" (escape-string docstring) "\";" *newline*))
+ (code "func.docstring = " (js-escape-string docstring) ";" *newline*))
"return func;" *newline*)
(apply #'code strs)))
(mapconcat #'parse-keyword keyword-arguments))))
;; Check for unknown keywords
(when keyword-arguments
- (code "for (i=" (+ n-required-arguments n-optional-arguments)
- "; i<nargs; i+=2){" *newline*
+ (code "var start = " (+ n-required-arguments n-optional-arguments) ";" *newline*
+ "if ((nargs - start) % 2 == 1){" *newline*
+ (indent "throw 'Odd number of keyword arguments';" *newline*)
+ "}" *newline*
+ "for (i = start; i<nargs; i+=2){" *newline*
(indent "if ("
(join (mapcar (lambda (x)
(concat "arguments[i+2] !== " (ls-compile (caar x))))
" && ")
")" *newline*
(indent
- "throw 'Unknown keyword argument ' + xstring(arguments[i].name);" *newline*))
+ "throw 'Unknown keyword argument ' + xstring(arguments[i+2].name);" *newline*))
"}" *newline*)))))
(defun parse-lambda-list (ll)
(define-compilation setq (&rest pairs)
(let ((result ""))
+ (when (null pairs)
+ (return-from setq (ls-compile nil)))
(while t
(cond
- ((null pairs) (return))
+ ((null pairs)
+ (return))
((null (cdr pairs))
(error "Odd pairs in SETQ"))
(t
;;; Compilation of literals an object dumping
-(defun escape-string (string)
- (let ((output "")
- (index 0)
- (size (length string)))
- (while (< index size)
- (let ((ch (char string index)))
- (when (or (char= ch #\") (char= ch #\\))
- (setq output (concat output "\\")))
- (when (or (char= ch #\newline))
- (setq output (concat output "\\"))
- (setq ch #\n))
- (setq output (concat output (string ch))))
- (incf index))
- output))
-
;;; BOOTSTRAP MAGIC: We record the macro definitions as lists during
;;; the bootstrap. Once everything is compiled, we want to dump the
;;; whole global environment to the output file to reproduce it in the
(concat "[" (join (mapcar #'literal elements) ", ") "]")))
(defun dump-string (string)
- (code "make_lisp_string(\"" (escape-string string) "\")"))
+ (code "make_lisp_string(" (js-escape-string string) ")"))
(defun literal (sexp &optional recursive)
(cond
((integerp sexp) (integer-to-string sexp))
((floatp sexp) (float-to-string sexp))
- ((characterp sexp) (code "\"" (escape-string (string sexp)) "\""))
+ ((characterp sexp) (js-escape-string (string sexp)))
(t
(or (cdr (assoc sexp *literal-table* :test #'eql))
(let ((dumped (typecase sexp
",")
")")))
+(define-compilation macrolet (definitions &rest body)
+ (let ((*environment* (copy-lexenv *environment*)))
+ (dolist (def definitions)
+ (destructuring-bind (name lambda-list &body body) def
+ (let ((binding (make-binding :name name :type 'macro :value
+ (let ((g!form (gensym)))
+ `(lambda (,g!form)
+ (destructuring-bind ,lambda-list ,g!form
+ ,@body))))))
+ (push-to-lexenv binding *environment* 'function))))
+ (ls-compile `(progn ,@body) *multiple-value-p*)))
+
+
(defun special-variable-p (x)
(and (claimp x 'variable 'special) t))
(define-builtin char-code (x)
(type-check (("x" "string" x))
- "x.charCodeAt(0)"))
+ "char_to_codepoint(x)"))
(define-builtin code-char (x)
(type-check (("x" "number" x))
- "String.fromCharCode(x)"))
+ "char_from_codepoint(x)"))
(define-builtin characterp (x)
(js!bool
(js!selfcall
"var x = " x ";" *newline*
- "return (typeof(" x ") == \"string\") && x.length == 1;")))
+ "return (typeof(" x ") == \"string\") && (x.length == 1 || x.length == 2);")))
(define-builtin char-upcase (x)
- (code x ".toUpperCase()"))
+ (code "safe_char_upcase(" x ")"))
(define-builtin char-downcase (x)
- (code x ".toLowerCase()"))
+ (code "safe_char_downcase(" x ")"))
(define-builtin stringp (x)
(js!bool
(define-builtin %throw (string)
(js!selfcall "throw " string ";" *newline*))
-(define-builtin new () "{}")
-
-(define-builtin objectp (x)
- (js!bool (code "(typeof (" x ") === 'object')")))
-
-(define-builtin oget (object key)
- (js!selfcall
- "var tmp = " "(" object ")[xstring(" key ")];" *newline*
- "return tmp == undefined? " (ls-compile nil) ": tmp ;" *newline*))
-
-(define-builtin oset (object key value)
- (code "((" object ")[xstring(" key ")] = " value ")"))
-
-(define-builtin in (key object)
- (js!bool (code "(xstring(" key ") in (" object "))")))
-
-(define-builtin map-for-in (function object)
- (js!selfcall
- "var f = " function ";" *newline*
- "var g = (typeof f === 'function' ? f : f.fvalue);" *newline*
- "var o = " object ";" *newline*
- "for (var key in o){" *newline*
- (indent "g(" (if *multiple-value-p* "values" "pv") ", 1, o[key]);" *newline*)
- "}"
- " return " (ls-compile nil) ";" *newline*))
-
(define-builtin functionp (x)
(js!bool (code "(typeof " x " == 'function')")))
-(define-builtin write-string (x)
+(define-builtin %write-string (x)
(code "lisp.write(" x ")"))
"if (i < 0 || i >= x.length) throw 'Out of range';" *newline*
"return x[i] = " value ";" *newline*))
-
+(define-builtin concatenate-storage-vector (sv1 sv2)
+ (js!selfcall
+ "var sv1 = " sv1 ";" *newline*
+ "var r = sv1.concat(" sv2 ");" *newline*
+ "r.type = sv1.type;" *newline*
+ "r.stringp = sv1.stringp;" *newline*
+ "return r;" *newline*))
(define-builtin get-internal-real-time ()
"(new Date()).getTime()")
;;; Javascript FFI
+(define-builtin new () "{}")
+
+(define-raw-builtin oget* (object key &rest keys)
+ (js!selfcall
+ "var tmp = (" (ls-compile object) ")[xstring(" (ls-compile key) ")];" *newline*
+ (mapconcat (lambda (key)
+ (code "if (tmp === undefined) return " (ls-compile nil) ";" *newline*
+ "tmp = tmp[xstring(" (ls-compile key) ")];" *newline*))
+ keys)
+ "return tmp === undefined? " (ls-compile nil) " : tmp;" *newline*))
+
+(define-raw-builtin oset* (value object key &rest keys)
+ (let ((keys (cons key keys)))
+ (js!selfcall
+ "var obj = " (ls-compile object) ";" *newline*
+ (mapconcat (lambda (key)
+ (code "obj = obj[xstring(" (ls-compile key) ")];"
+ "if (obj === undefined) throw 'Impossible to set Javascript property.';" *newline*))
+ (butlast keys))
+ "var tmp = obj[xstring(" (ls-compile (car (last keys))) ")] = " (ls-compile value) ";" *newline*
+ "return tmp === undefined? " (ls-compile nil) " : tmp;" *newline*)))
+
+(define-raw-builtin oget (object key &rest keys)
+ (code "js_to_lisp(" (ls-compile `(oget* ,object ,key ,@keys)) ")"))
+
+(define-raw-builtin oset (value object key &rest keys)
+ (ls-compile `(oset* (lisp-to-js ,value) ,object ,key ,@keys)))
+
+(define-builtin objectp (x)
+ (js!bool (code "(typeof (" x ") === 'object')")))
+
+(define-builtin lisp-to-js (x) (code "lisp_to_js(" x ")"))
+(define-builtin js-to-lisp (x) (code "js_to_lisp(" x ")"))
+
+
+(define-builtin in (key object)
+ (js!bool (code "(xstring(" key ") in (" object "))")))
+
+(define-builtin map-for-in (function object)
+ (js!selfcall
+ "var f = " function ";" *newline*
+ "var g = (typeof f === 'function' ? f : f.fvalue);" *newline*
+ "var o = " object ";" *newline*
+ "for (var key in o){" *newline*
+ (indent "g(" (if *multiple-value-p* "values" "pv") ", 1, o[key]);" *newline*)
+ "}"
+ " return " (ls-compile nil) ";" *newline*))
+
(define-compilation %js-vref (var)
(code "js_to_lisp(" var ")"))
(mapcar #'ls-compile args)) ", ") ")")))
(unless (or (symbolp function)
(and (consp function)
- (eq (car function) 'lambda)))
+ (member (car function) '(lambda oget))))
(error "Bad function designator `~S'" function))
(cond
((translate-function function)
#+jscl (eq (symbol-package function) (find-package "COMMON-LISP"))
#-jscl t)
(code (ls-compile `',function) ".fvalue" arglist))
+ #+jscl((symbolp function)
+ (code (ls-compile `#',function) arglist))
+ ((and (consp function) (eq (car function) 'lambda))
+ (code (ls-compile `#',function) arglist))
+ ((and (consp function) (eq (car function) 'oget))
+ (code (ls-compile function) arglist))
(t
- (code (ls-compile `#',function) arglist)))))
+ (error "Bad function descriptor")))))
(defun ls-compile-block (sexps &optional return-last-p decls-allowed-p)
(multiple-value-bind (sexps decls)