From bc239e6931441439be5afd9c6221a532340e5eb1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20V=C3=A1zquez?= Date: Tue, 4 Jun 2013 20:23:36 +0100 Subject: [PATCH] OSET* and OGET* type-preserving variants and *root* Javascript object --- src/compiler.lisp | 68 ++++++++++++++++++++++++++++++----------------------- src/ffi.lisp | 2 ++ 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/compiler.lisp b/src/compiler.lisp index 0ad7637..c5787c8 100644 --- a/src/compiler.lisp +++ b/src/compiler.lisp @@ -1242,35 +1242,6 @@ (define-builtin %throw (string) (js!selfcall "throw " string ";" *newline*)) -(define-builtin new () "{}") - -(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 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')"))) @@ -1336,6 +1307,45 @@ ;;; Javascript FFI +(define-builtin new () "{}") + +(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 oget (object key) + (js!selfcall + "var tmp = " "(" object ")[xstring(" key ")];" *newline* + "return tmp == undefined? " (ls-compile nil) ": js_to_lisp(tmp);" *newline*)) + +(define-builtin oset (object key value) + (code "((" object ")[xstring(" key ")] = lisp_to_js(" value "))")) + + +(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 ")")) diff --git a/src/ffi.lisp b/src/ffi.lisp index 29862a8..32b9f21 100644 --- a/src/ffi.lisp +++ b/src/ffi.lisp @@ -32,3 +32,5 @@ (%define-symbol-macro symbol `(%js-vref ,(string symbol)))))) (setq *intern-hook* #'ffi-intern-hook) + +(defvar *root* (%js-vref "window")) -- 1.7.10.4