summaryrefslogtreecommitdiff
path: root/module/language/ecmascript/impl.scm
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2009-02-22 16:01:11 +0100
committerAndy Wingo <wingo@pobox.com>2009-02-22 16:01:11 +0100
commite05320fa549af175c8cbb7bed8cd4ece873033da (patch)
treefaaaca78867b6d3fd14ea6600f5f24b86343880c /module/language/ecmascript/impl.scm
parentbb67fe27ab5cda3fcefaa1c471f75c8241cc12b2 (diff)
downloadguile-e05320fa549af175c8cbb7bed8cd4ece873033da.tar.gz
compile for-in
* module/language/ecmascript/base.scm (prop-keys): New method, returns the list of keys of props of this instance. * module/language/ecmascript/impl.scm: Refactor the global object into a special kind of module object. Provide a prop-keys implementation for module objects. * module/language/ecmascript/compile-ghil.scm (comp): Compile for-in. * module/language/ecmascript/impl.scm: Reshuffly things, and implement make-enumerator, a helper for use in for-in statements. * module/language/ecmascript/parse.scm (parse-ecmascript): Fix parsing of for (var foo in bar) {}...
Diffstat (limited to 'module/language/ecmascript/impl.scm')
-rw-r--r--module/language/ecmascript/impl.scm51
1 files changed, 27 insertions, 24 deletions
diff --git a/module/language/ecmascript/impl.scm b/module/language/ecmascript/impl.scm
index 1307a7f27..be4c751cb 100644
--- a/module/language/ecmascript/impl.scm
+++ b/module/language/ecmascript/impl.scm
@@ -33,32 +33,12 @@
bitwise-not logical-not
shift
mod
- band bxor bior))
+ band bxor bior
+ make-enumerator))
-(define-class <js-global-object> (<js-object>))
-(define-method (pget (o <js-global-object>) (p <string>))
- (pget o (string->symbol p)))
-(define-method (pget (o <js-global-object>) (p <symbol>))
- (let ((v (module-variable (current-module) p)))
- (if v
- (variable-ref v)
- (next-method))))
-(define-method (pput (o <js-global-object>) (p <string>) v)
- (pput o (string->symbol p) v))
-(define-method (pput (o <js-global-object>) (p <symbol>) v)
- (module-define! (current-module) p v))
-(define-method (prop-attrs (o <js-global-object>) (p <symbol>))
- (cond ((module-local-variable (current-module) p)
- '())
- ((module-variable (current-module) p)
- '(DontDelete ReadOnly))
- (else (next-method))))
-(define-method (prop-attrs (o <js-global-object>) (p <string>))
- (prop-attrs o (string->symbol p)))
-
(define-class <js-module-object> (<js-object>)
- (module #:init-form (js-module o) #:init-keyword #:module
+ (module #:init-form (current-module) #:init-keyword #:module
#:getter js-module))
(define-method (pget (o <js-module-object>) (p <string>))
(pget o (string->symbol p)))
@@ -72,16 +52,24 @@
(define-method (pput (o <js-module-object>) (p <symbol>) v)
(module-define! (js-module o) p v))
(define-method (prop-attrs (o <js-module-object>) (p <symbol>))
- (cond ((module-variable (js-module o) p) '())
+ (cond ((module-local-variable (js-module o) p) '())
+ ((module-variable (js-module o) p) '(DontDelete ReadOnly))
(else (next-method))))
(define-method (prop-attrs (o <js-module-object>) (p <string>))
(prop-attrs o (string->symbol p)))
+(define-method (prop-keys (o <js-module-object>))
+ (append (hash-map->list (lambda (k v) k) (module-obarray (js-module o)))
+ (next-method)))
;; we could make a renamer, but having obj['foo-bar'] should be enough
(define (js-require modstr)
(make <js-module-object> #:module
(resolve-interface (map string->symbol (string-split modstr #\.)))))
+(define-class <js-global-object> (<js-module-object>))
+(define-method (js-module (o <js-global-object>))
+ (current-module))
+
(define (init-js-bindings! mod)
(module-define! mod 'NaN +nan.0)
(module-define! mod 'Infinity +inf.0)
@@ -165,3 +153,18 @@
(> (->number a) (->number b)))
(define-method (> (a <string>) (b <string>))
(string> a b))
+
+(define (obj-and-prototypes o)
+ (if o
+ (cons o (obj-and-prototypes (js-prototype o)))
+ '()))
+
+(define (make-enumerator obj)
+ (let ((props (make-hash-table 23)))
+ (for-each (lambda (o)
+ (for-each (lambda (k) (hashq-set! props k #t))
+ (prop-keys o)))
+ (obj-and-prototypes obj))
+ (apply new-array (filter (lambda (p)
+ (not (prop-has-attr? obj p 'DontEnum)))
+ (hash-map->list (lambda (k v) k) props)))))