1 ;;; ECMAScript for Guile
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
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)
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.
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.
22 (define-module (language ecmascript function)
23 #:use-module (oop goops)
24 #:use-module (language ecmascript base)
25 #:export (*function-prototype* *program-wrappers*))
28 (define-class <js-program-wrapper> (<js-object>))
30 (define *program-wrappers* (make-doubly-weak-hash-table 31))
32 (define *function-prototype* (make <js-object> #:class "Function"
33 #:value (lambda args *undefined*)))
35 (define-js-method *function-prototype* (toString)
36 (format #f "~A" (js-value this)))
38 (define-js-method *function-prototype* (apply this-arg array)
39 (cond ((or (null? array) (eq? array *undefined*))
40 (call/this this-arg (js-value this)))
41 ((is-a? array <js-array-object>)
44 (apply (js-value this)
45 (vector->list (js-array-vector array))))))
47 (throw 'TypeError 'apply array))))
49 (define-js-method *function-prototype* (call this-arg . args)
52 (apply (js-value this) args))))
54 (define-method (pget (o <applicable>) p)
55 (let ((wrapper (hashq-ref *program-wrappers* o)))
58 (pget *function-prototype* p))))
60 (define-method (pput (o <applicable>) p v)
61 (let ((wrapper (hashq-ref *program-wrappers* o)))
64 (let ((wrapper (make <js-program-wrapper> #:value o #:class "Function"
65 #:prototype *function-prototype*)))
66 (hashq-set! *program-wrappers* o wrapper)
67 (pput wrapper p v)))))
69 (define-method (js-prototype (o <applicable>))
70 (let ((wrapper (hashq-ref *program-wrappers* o)))
72 (js-prototype wrapper)
75 (define-method (js-constructor (o <applicable>))
76 (let ((wrapper (hashq-ref *program-wrappers* o)))
78 (js-constructor wrapper)