function.scm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;; ECMAScript for Guile
  2. ;; Copyright (C) 2009 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (language ecmascript function)
  18. #:use-module (oop goops)
  19. #:use-module (language ecmascript base)
  20. #:export (*function-prototype* *program-wrappers*))
  21. (define-class <js-program-wrapper> (<js-object>))
  22. (define *program-wrappers* (make-doubly-weak-hash-table))
  23. (define *function-prototype* (make <js-object> #:class "Function"
  24. #:value (lambda args *undefined*)))
  25. (define-js-method *function-prototype* (toString)
  26. (format #f "~A" (js-value this)))
  27. (define-js-method *function-prototype* (apply this-arg array)
  28. (cond ((or (null? array) (eq? array *undefined*))
  29. (call/this this-arg (js-value this)))
  30. ((is-a? array <js-array-object>)
  31. (call/this this-arg
  32. (lambda ()
  33. (apply (js-value this)
  34. (vector->list (js-array-vector array))))))
  35. (else
  36. (throw 'TypeError 'apply array))))
  37. (define-js-method *function-prototype* (call this-arg . args)
  38. (call/this this-arg
  39. (lambda ()
  40. (apply (js-value this) args))))
  41. (define-method (pget (o <applicable>) p)
  42. (let ((wrapper (hashq-ref *program-wrappers* o)))
  43. (if wrapper
  44. (pget wrapper p)
  45. (pget *function-prototype* p))))
  46. (define-method (pput (o <applicable>) p v)
  47. (let ((wrapper (hashq-ref *program-wrappers* o)))
  48. (if wrapper
  49. (pput wrapper p v)
  50. (let ((wrapper (make <js-program-wrapper> #:value o #:class "Function"
  51. #:prototype *function-prototype*)))
  52. (hashq-set! *program-wrappers* o wrapper)
  53. (pput wrapper p v)))))
  54. (define-method (js-prototype (o <applicable>))
  55. (let ((wrapper (hashq-ref *program-wrappers* o)))
  56. (if wrapper
  57. (js-prototype wrapper)
  58. #f)))
  59. (define-method (js-constructor (o <applicable>))
  60. (let ((wrapper (hashq-ref *program-wrappers* o)))
  61. (if wrapper
  62. (js-constructor wrapper)
  63. #f)))