compile ecmascript's parser. yay! instant load-time!
[bpt/guile.git] / module / language / ecmascript / base.scm
CommitLineData
e80ce73d
AW
1;;; ECMAScript for Guile
2
3;; Copyright (C) 2009 Free Software Foundation, Inc.
4
5;; This program is free software; you can redistribute it and/or modify
6;; it under the terms of the GNU General Public License as published by
7;; the Free Software Foundation; either version 2, or (at your option)
8;; any later version.
9;;
10;; This program is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;; GNU General Public License for more details.
14;;
15;; You should have received a copy of the GNU General Public License
16;; along with this program; see the file COPYING. If not, write to
17;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18;; Boston, MA 02111-1307, USA.
19
20;;; Code:
21
22(define-module (language ecmascript base)
23 #:use-module (oop goops)
24 #:export (*undefined* *this*
25 <js-object> *object-prototype*
785fb107 26 js-prototype js-props js-prop-attrs js-value js-constructor js-class
e80ce73d
AW
27 pget prop-attrs prop-has-attr? pput has-property? pdel
28
29 object->string object->number object->value/string
30 object->value/number object->value
31
32 ->primitive ->boolean ->number ->integer ->int32 ->uint32
33 ->uint16 ->string ->object
34
785fb107 35 call/this* call/this lambda/this define-js-method
e80ce73d
AW
36
37 new-object))
38
39(define *undefined* ((@@ (oop goops) make-unbound)))
40(define *this* (make-fluid))
41
42(define-class <js-object> ()
43 (prototype #:getter js-prototype #:init-keyword #:prototype
44 #:init-thunk (lambda () *object-prototype*))
45 (props #:getter js-props #:init-form (make-hash-table 7))
46 (prop-attrs #:getter js-prop-attrs #:init-value #f)
47 (value #:getter js-value #:init-value #f #:init-keyword #:value)
48 (constructor #:getter js-constructor #:init-value #f #:init-keyword #:constructor)
49 (class #:getter js-class #:init-value "Object" #:init-keyword #:class))
50
b358fe65
AW
51(define-method (pget (o <js-object>) (p <string>))
52 (pget o (string->symbol p)))
53
e80ce73d 54(define-method (pget (o <js-object>) p)
b358fe65
AW
55 (let ((h (hashq-get-handle (js-props o) p)))
56 (if h
57 (cdr h)
58 (let ((proto (js-prototype o)))
59 (if proto
60 (pget proto p)
61 *undefined*)))))
e80ce73d
AW
62
63(define-method (prop-attrs (o <js-object>) p)
64 (or (let ((attrs (js-prop-attrs o)))
65 (and attrs (hashq-ref (js-prop-attrs o) p)))
66 (let ((proto (js-prototype o)))
67 (if proto
68 (prop-attrs proto p)
69 '()))))
70
71(define-method (prop-has-attr? (o <js-object>) p attr)
72 (memq attr (prop-attrs o p)))
73
74(define-method (pput (o <js-object>) p v)
75 (let ((p (if (string? p) (string->symbol p) p)))
76 (if (prop-has-attr? o p 'ReadOnly)
77 (throw 'ReferenceError o p)
78 (hashq-set! (js-props o) p v))))
79
b358fe65
AW
80(define-method (pput (o <js-object>) (p <string>) v)
81 (pput o (string->symbol p) v))
82
e80ce73d
AW
83(define-method (pdel (o <js-object>) p)
84 (let ((p (if (string? p) (string->symbol p) p)))
85 (if (prop-has-attr? o p 'DontDelete)
86 #f
87 (begin
88 (pput o p *undefined*)
89 #t))))
90
b358fe65
AW
91(define-method (pdel (o <js-object>) (p <string>) v)
92 (pdel o (string->symbol p)))
93
94(define-method (has-property? (o <js-object>) p)
95 (if (hashq-get-handle (js-props o) v)
96 #t
97 (let ((proto (js-prototype o)))
98 (if proto
99 (has-property? proto p)
100 #f))))
101
785fb107
AW
102(define (call/this* this f)
103 (with-fluid* *this* this f))
104
e80ce73d 105(define-macro (call/this this f . args)
785fb107 106 `(with-fluid* *this* ,this (lambda () (,f . ,args))))
e80ce73d
AW
107(define-macro (lambda/this formals . body)
108 `(lambda ,formals (let ((this (fluid-ref *this*))) . ,body)))
109(define-macro (define-js-method object name-and-args . body)
110 `(pput ,object ',(car name-and-args) (lambda/this ,(cdr name-and-args) . ,body)))
111
112(define *object-prototype* #f)
113(set! *object-prototype* (make <js-object>))
114
115(define-js-method *object-prototype* (toString)
116 (format #f "[object ~A]" (js-class this)))
117(define-js-method *object-prototype* (toLocaleString . args)
118 ((pget *object-prototype* 'toString)))
119(define-js-method *object-prototype* (valueOf)
120 this)
121(define-js-method *object-prototype* (hasOwnProperty p)
122 (and (hashq-get-handle (js-props this) p) #t))
123(define-js-method *object-prototype* (isPrototypeOf v)
124 (eq? this (js-prototype v)))
125(define-js-method *object-prototype* (propertyIsEnumerable p)
126 (and (hashq-get-handle (js-props this) p)
127 (not (prop-has-attr? this p 'DontEnum))))
128
129(define (object->string o error?)
130 (let ((toString (pget o 'toString)))
131 (if (procedure? toString)
132 (let ((x (call/this o toString)))
133 (if (and error? (is-a? x <js-object>))
134 (throw 'TypeError o 'default-value)
135 x))
136 (if error?
137 (throw 'TypeError o 'default-value)
138 o))))
139
140(define (object->number o error?)
141 (let ((valueOf (pget o 'valueOf)))
142 (if (procedure? valueOf)
143 (let ((x (call/this o valueOf)))
144 (if (and error? (is-a? x <js-object>))
145 (throw 'TypeError o 'default-value)
146 x))
147 (if error?
148 (throw 'TypeError o 'default-value)
149 o))))
150
151(define (object->value/string o)
152 (let ((v (object->string o #f)))
153 (if (is-a? x <js-object>)
154 (object->number o #t)
155 x)))
156
157(define (object->value/number o)
158 (let ((v (object->number o #f)))
159 (if (is-a? x <js-object>)
160 (object->string o #t)
161 x)))
162
163(define (object->value o)
164 ;; FIXME: if it's a date, we should try numbers first
165 (object->value/string o))
166
167(define (->primitive x)
168 (if (is-a? x <js-object>)
169 (object->value x)
170 x))
171
172(define (->boolean x)
173 (not (or (not x) (null? x) (eq? x *undefined*) (zero? x) (nan? x)
174 (and (string? x) (= (string-length x) 0)))))
175
176(define (->number x)
177 (cond ((number? x) x)
178 ((boolean? x) (if x 1 0))
179 ((null? x) 0)
180 ((eq? x *undefined*) +nan.0)
181 ((is-a? x <js-object>) (object->number o))
182 ((string? x) (string->number x))
183 (else (throw 'TypeError o '->number))))
184
185(define (->integer x)
186 (let ((n (->number x)))
187 (cond ((nan? n) 0)
188 ((zero? n) n)
189 ((inf? n) n)
190 (else (inexact->exact (round n))))))
191
192(define (->int32 x)
193 (let ((n (->number x)))
194 (if (or (nan? n) (zero? n) (inf? n))
195 0
196 (let ((m (logand (1- (ash 1 32)) (inexact->exact (round n)))))
197 (if (negative? n)
198 (- m (ash 1 32))
199 m)))))
200
201(define (->uint32 x)
202 (let ((n (->number x)))
203 (if (or (nan? n) (zero? n) (inf? n))
204 0
205 (logand (1- (ash 1 32)) (inexact->exact (round n))))))
206
207(define (->uint16 x)
208 (let ((n (->number x)))
209 (if (or (nan? n) (zero? n) (inf? n))
210 0
211 (logand (1- (ash 1 16)) (inexact->exact (round n))))))
212
213(define (->string x)
214 (cond ((eq? x *undefined*) "undefined")
215 ((null? x) "null")
216 ((boolean? x) (if x "true" "false"))
217 ((string? x) x)
218 ((number? x)
219 (cond ((nan? x) "NaN")
220 ((zero? x) "0")
221 ((inf? x) "Infinity")
222 (else (number->string x))))
223 (else (->string (object->value/string x)))))
224
225(define (->object x)
226 (cond ((eq? x *undefined*) (throw 'TypeError x '->object))
227 ((null? x) (throw 'TypeError x '->object))
228 ((boolean? x) (make <js-object> #:prototype Boolean #:value x))
229 ((number? x) (make <js-object> #:prototype String #:value x))
230 ((string? x) (make <js-object> #:prototype Number #:value x))
231 (else x)))
232
233(define (new-object . pairs)
234 (let ((o (make <js-object>)))
235 (map (lambda (pair)
236 (pput o (car pair) (cdr pair)))
237 pairs)
238 o))