mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-24 12:20:20 +02:00
+ for strings, global js object, new Foo() works
* module/language/ecmascript/array.scm (*array-prototype*): Declare the constructor. * module/language/ecmascript/base.scm (pput, pdel): Remove some needless checks. (new): Move definition of new here, and use the constructor. * module/language/ecmascript/compile-ghil.scm (compile-ghil): Add a stub so that when we load a compiled JS program, we make sure the runtime has been booted. * module/language/ecmascript/function.scm (js-constructor): Export a js-constructor method instead of a new method. * module/language/ecmascript/impl.scm (<js-global-object>): Define a new class for the global "this" object, wrapping bindings from the current module. (init-js-bindings!): Define the dozen or so global properties, in the current module. (+): Define addition operations for non-numbers. This is efficient because the generics are only dispatched if the fast-path fails.
This commit is contained in:
parent
aec8febc46
commit
8c306808c2
6 changed files with 97 additions and 35 deletions
|
@ -28,7 +28,7 @@
|
|||
pget pput pdel has-property?
|
||||
->boolean
|
||||
new-object new new-array)
|
||||
#:export (get-this
|
||||
#:export (js-init get-this
|
||||
typeof
|
||||
bitwise-not logical-not
|
||||
shift
|
||||
|
@ -36,6 +36,44 @@
|
|||
band bxor bior))
|
||||
|
||||
|
||||
(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 (init-js-bindings! mod)
|
||||
(module-define! mod 'NaN +nan.0)
|
||||
(module-define! mod 'Infinity +inf.0)
|
||||
(module-define! mod 'undefined *undefined*)
|
||||
;; isNAN, isFinite, parseFloat, parseInt, eval
|
||||
;; decodeURI, decodeURIComponent, encodeURI, encodeURIComponent
|
||||
;; Object Function Array String Boolean Number Date RegExp Error EvalError
|
||||
;; RangeError ReferenceError SyntaxError TypeError URIError
|
||||
(module-define! mod 'Object *object-prototype*)
|
||||
(module-define! mod 'Array *array-prototype*))
|
||||
|
||||
(define (js-init)
|
||||
(cond ((get-this))
|
||||
(else
|
||||
(fluid-set! *this* (make <js-global-object>))
|
||||
(init-js-bindings! (current-module)))))
|
||||
|
||||
(define (get-this)
|
||||
(fluid-ref *this*))
|
||||
|
||||
|
@ -60,3 +98,15 @@
|
|||
(define bior logior)
|
||||
|
||||
(define mod modulo)
|
||||
|
||||
(define-method (+ (a <string>) (b <string>))
|
||||
(string-append a b))
|
||||
|
||||
(define-method (+ (a <string>) b)
|
||||
(string-append a (->string b)))
|
||||
|
||||
(define-method (+ a (b <string>))
|
||||
(string-append (->string a) b))
|
||||
|
||||
(define-method (+ a b)
|
||||
(+ (->number a) (->number b)))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue