fixing the closure capturing for the web

This commit is contained in:
Danny Yoo 2011-03-13 21:56:47 -04:00
parent 8f2d928e08
commit a78b51e624
2 changed files with 30 additions and 7 deletions

View File

@ -257,8 +257,11 @@ EOF
"MACHINE.control.pop();"] "MACHINE.control.pop();"]
[(PushEnvironment? stmt) [(PushEnvironment? stmt)
(format "MACHINE.env.push(~a);" (string-join (format "MACHINE.env.push(~a);" (string-join
(build-list (PushEnvironment-n stmt) (lambda: ([i : Natural]) (build-list (PushEnvironment-n stmt)
"undefined")) (lambda: ([i : Natural])
(if (PushEnvironment-unbox? stmt)
"[]"
"undefined")))
", "))] ", "))]
[(PopEnvironment? stmt) [(PopEnvironment? stmt)
(format "MACHINE.env.splice(MACHINE.env.length-(~a),~a);" (format "MACHINE.env.splice(MACHINE.env.length-(~a),~a);"
@ -321,13 +324,17 @@ EOF
(EnvWholePrefixReference-depth a-prefix-ref))) (EnvWholePrefixReference-depth a-prefix-ref)))
(: assemble-env-reference (EnvReference -> String)) (: assemble-env-reference/closure-capture (EnvReference -> String))
(define (assemble-env-reference ref) ;; When we're capturing the values for a closure, we need to not unbox
;; lexical references: they must remain boxes.
(define (assemble-env-reference/closure-capture ref)
(cond (cond
[(EnvLexicalReference? ref) [(EnvLexicalReference? ref)
(assemble-lexical-reference ref)] (format "MACHINE.env[MACHINE.env.length - 1 - ~a]"
(EnvLexicalReference-depth ref))]
[(EnvWholePrefixReference? ref) [(EnvWholePrefixReference? ref)
(assemble-whole-prefix-reference ref)])) (format "MACHINE.env[MACHINE.env.length - 1 - ~a]"
(EnvWholePrefixReference-depth ref))]))
(: assemble-op-expression (PrimitiveOperator -> String)) (: assemble-op-expression (PrimitiveOperator -> String))
@ -340,7 +347,7 @@ EOF
(format "new Closure(~a, ~a, [~a], ~s)" (format "new Closure(~a, ~a, [~a], ~s)"
(MakeCompiledProcedure-label op) (MakeCompiledProcedure-label op)
(MakeCompiledProcedure-arity op) (MakeCompiledProcedure-arity op)
(string-join (map assemble-env-reference (string-join (map assemble-env-reference/closure-capture
;; The closure values are in reverse order ;; The closure values are in reverse order
;; to make it easier to push, in bulk, into ;; to make it easier to push, in bulk, into
;; the environment (which is also in reversed order) ;; the environment (which is also in reversed order)

View File

@ -125,3 +125,19 @@
y)))))))) y))))))))
(displayln (ctak 18 12 6))) (displayln (ctak 18 12 6)))
"7\n") "7\n")
(test '(letrec ([f (lambda (x)
(if (= x 0)
1
(* x (f (sub1 x)))))])
(display (f 10)))
"3628800")
(test '(letrec ([tak (lambda (x y z)
(if (>= y x)
z
(tak (tak (- x 1) y z)
(tak (- y 1) z x)
(tak (- z 1) x y))))])
(displayln (tak 18 12 6)))
"7\n")