Commit | Line | Data |
---|---|---|
e80ce73d AW |
1 | ;;; ECMAScript for Guile |
2 | ||
3 | ;; Copyright (C) 2009 Free Software Foundation, Inc. | |
4 | ||
53befeb7 NJ |
5 | ;;;; This library is free software; you can redistribute it and/or |
6 | ;;;; modify it under the terms of the GNU Lesser General Public | |
7 | ;;;; License as published by the Free Software Foundation; either | |
8 | ;;;; version 3 of the License, or (at your option) any later version. | |
9 | ;;;; | |
10 | ;;;; This library 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 GNU | |
13 | ;;;; Lesser General Public License for more details. | |
14 | ;;;; | |
15 | ;;;; You should have received a copy of the GNU Lesser General Public | |
16 | ;;;; License along with this library; if not, write to the Free Software | |
17 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
e80ce73d AW |
18 | |
19 | ;;; Code: | |
20 | ||
21 | (define-module (language ecmascript function) | |
22 | #:use-module (oop goops) | |
23 | #:use-module (language ecmascript base) | |
8c306808 | 24 | #:export (*function-prototype* *program-wrappers*)) |
e80ce73d AW |
25 | |
26 | ||
27 | (define-class <js-program-wrapper> (<js-object>)) | |
28 | ||
29 | (define *program-wrappers* (make-doubly-weak-hash-table 31)) | |
30 | ||
31 | (define *function-prototype* (make <js-object> #:class "Function" | |
32 | #:value (lambda args *undefined*))) | |
33 | ||
34 | (define-js-method *function-prototype* (toString) | |
35 | (format #f "~A" (js-value this))) | |
36 | ||
37 | (define-js-method *function-prototype* (apply this-arg array) | |
38 | (cond ((or (null? array) (eq? array *undefined*)) | |
39 | (call/this this-arg (js-value this))) | |
40 | ((is-a? array <js-array-object>) | |
41 | (call/this this-arg | |
42 | (lambda () | |
43 | (apply (js-value this) | |
44 | (vector->list (js-array-vector array)))))) | |
45 | (else | |
46 | (throw 'TypeError 'apply array)))) | |
47 | ||
48 | (define-js-method *function-prototype* (call this-arg . args) | |
49 | (call/this this-arg | |
50 | (lambda () | |
51 | (apply (js-value this) args)))) | |
52 | ||
53 | (define-method (pget (o <applicable>) p) | |
54 | (let ((wrapper (hashq-ref *program-wrappers* o))) | |
55 | (if wrapper | |
56 | (pget wrapper p) | |
57 | (pget *function-prototype* p)))) | |
58 | ||
59 | (define-method (pput (o <applicable>) p v) | |
60 | (let ((wrapper (hashq-ref *program-wrappers* o))) | |
61 | (if wrapper | |
62 | (pput wrapper p v) | |
63 | (let ((wrapper (make <js-program-wrapper> #:value o #:class "Function" | |
64 | #:prototype *function-prototype*))) | |
65 | (hashq-set! *program-wrappers* o wrapper) | |
66 | (pput wrapper p v))))) | |
67 | ||
68 | (define-method (js-prototype (o <applicable>)) | |
69 | (let ((wrapper (hashq-ref *program-wrappers* o))) | |
70 | (if wrapper | |
71 | (js-prototype wrapper) | |
72 | #f))) | |
73 | ||
8c306808 AW |
74 | (define-method (js-constructor (o <applicable>)) |
75 | (let ((wrapper (hashq-ref *program-wrappers* o))) | |
76 | (if wrapper | |
77 | (js-constructor wrapper) | |
78 | #f))) |