for-in
[jscl.git] / src / compiler.lisp
index d8e9b6d..047f42d 100644 (file)
 
 (define-builtin js-eval (string)
   (if *multiple-value-p*
-      (js!selfcall
-        "var v = globalEval(xstring(" string "));"
-        "return values.apply(this, forcemv(v));" )
-      `(code "globalEval(xstring(" ,string "))")))
+      (js!selfcall*
+        `(var (v (call |globalEval| (call |xstring| ,string))))
+        `(return (call (get |values| "apply") this (call |forcemv| v))))
+      `(call |globalEval| (call |xstring| ,string))))
 
 (define-builtin %throw (string)
   (js!selfcall* `(throw ,string)))
     `(return r)))
 
 (define-builtin get-internal-real-time ()
-  "(new Date()).getTime()")
+  `(call (get (new (call Date)) "getTime")))
 
 (define-builtin values-array (array)
   (if *multiple-value-p*
 
 ;;; Javascript FFI
 
-(define-builtin new () "{}")
+(define-builtin new ()
+  '(object))
 
 (define-raw-builtin oget* (object key &rest keys)
-  (js!selfcall
-    "var tmp = (" (ls-compile object) ")[xstring(" (ls-compile key) ")];"
-    `(code
-      ,@(mapcar (lambda (key)
-                  `(code "if (tmp === undefined) return " ,(ls-compile nil) ";"
-                         "tmp = tmp[xstring(" ,(ls-compile key) ")];" ))
-                keys))
-    "return tmp === undefined? " (ls-compile nil) " : tmp;" ))
+  (js!selfcall*
+    `(progn
+       (var (tmp (get ,(ls-compile object) (call |xstring| ,(ls-compile key)))))
+       ,@(mapcar (lambda (key)
+                   `(progn
+                      (if (=== tmp undefined) (return ,(ls-compile nil)))
+                      (= tmp (get tmp (call |xstring| ,(ls-compile key))))))
+                 keys))
+    `(return (if (=== tmp undefined) ,(ls-compile nil) tmp))))
 
 (define-raw-builtin oset* (value object key &rest keys)
   (let ((keys (cons key keys)))
-    (js!selfcall
-      "var obj = " (ls-compile object) ";"
-      `(code ,@(mapcar (lambda (key)
-                         `(code "obj = obj[xstring(" ,(ls-compile key) ")];"
-                                "if (obj === undefined) throw 'Impossible to set Javascript property.';" ))
-                       (butlast keys)))
-      "var tmp = obj[xstring(" (ls-compile (car (last keys))) ")] = " (ls-compile value) ";"
-      "return tmp === undefined? " (ls-compile nil) " : tmp;" )))
+    (js!selfcall*
+      `(progn
+         (var (obj ,(ls-compile object)))
+         ,@(mapcar (lambda (key)
+                     `(progn
+                        (= obj (get obj (call |xstring| ,(ls-compile key))))
+                        (if (=== object undefined)
+                            (throw "Impossible to set object property."))))
+                   (butlast keys))
+         (var (tmp
+               (= (get obj (call |xstring| ,(ls-compile (car (last keys)))))
+                  ,(ls-compile value))))
+         (return (if (=== tmp undefined)
+                     ,(ls-compile nil)
+                     tmp))))))
 
 (define-raw-builtin oget (object key &rest keys)
   `(call |js_to_lisp| ,(ls-compile `(oget* ,object ,key ,@keys))))