@@ -546,14 +546,23 @@ def compile_unary(o)
546546
547547 # A function definition. The only node that creates a new Scope.
548548 class CodeNode < Node
549- attr_reader :params , :body
549+ attr_reader :params , :body , :bound
550550
551- def initialize ( params , body )
551+ def initialize ( params , body , tag = nil )
552552 @params = params
553- @body = body
553+ @body = body
554+ @bound = tag == :boundfunc
555+ end
556+
557+ def statement?
558+ @bound
554559 end
555560
556561 def compile_node ( o )
562+ if @bound
563+ o [ :scope ] . assign ( "__this" , "this" )
564+ fvar = o [ :scope ] . free_variable
565+ end
557566 shared_scope = o . delete ( :shared_scope )
558567 o [ :scope ] = shared_scope || Scope . new ( o [ :scope ] , @body )
559568 o [ :return ] = true
@@ -568,9 +577,11 @@ def compile_node(o)
568577 @body . unshift ( splat )
569578 end
570579 @params . each { |id | o [ :scope ] . parameter ( id . to_s ) }
571- code = @body . compile_with_declarations ( o )
580+ code = " \n #{ @body . compile_with_declarations ( o ) } \n "
572581 name_part = name ? " #{ name } " : ''
573- write ( "function#{ name_part } (#{ @params . join ( ', ' ) } ) {\n #{ code } \n #{ idt } }" )
582+ func = "function#{ @bound ? '' : name_part } (#{ @params . join ( ', ' ) } ) {#{ code } #{ idt } }"
583+ return write ( func ) unless @bound
584+ write ( "#{ idt } #{ fvar } = #{ func } ;\n #{ idt } #{ o [ :return ] ? 'return ' : '' } (function#{ name_part } () {\n #{ idt ( 1 ) } return #{ fvar } .apply(__this, arguments);\n #{ idt } });" )
574585 end
575586 end
576587
@@ -726,7 +737,7 @@ def compile_node(o)
726737 body = Expressions . wrap ( IfNode . new ( @filter , body ) )
727738 end
728739 if @object
729- o [ :scope ] . top_level_assign ( "__hasProp" , "Object.prototype.hasOwnProperty" )
740+ o [ :scope ] . assign ( "__hasProp" , "Object.prototype.hasOwnProperty" , true )
730741 body = Expressions . wrap ( IfNode . new (
731742 CallNode . new ( ValueNode . new ( LiteralNode . wrap ( "__hasProp" ) , [ AccessorNode . new ( Value . new ( 'call' ) ) ] ) , [ LiteralNode . wrap ( svar ) , LiteralNode . wrap ( ivar ) ] ) ,
732743 Expressions . wrap ( body ) ,
0 commit comments