@@ -40,7 +40,9 @@ namespace ts {
4040 node = node . parent
4141 if ( ! node )
4242 userError ( lf ( "cannot determine parent of {0}" , stringKind ( node0 ) ) )
43- if ( node . kind == SyntaxKind . FunctionDeclaration ) return < FunctionDeclaration > node
43+ if ( node . kind == SyntaxKind . FunctionDeclaration ||
44+ node . kind == SyntaxKind . ArrowFunction ||
45+ node . kind == SyntaxKind . FunctionExpression ) return < FunctionLikeDeclaration > node
4446 if ( node . kind == SyntaxKind . SourceFile ) return null
4547 }
4648 }
@@ -197,6 +199,7 @@ namespace ts {
197199 if ( outer == null || outer == proc . action ) {
198200 // not captured
199201 } else {
202+ debugger ;
200203 if ( proc . info . capturedVars . indexOf ( v ) < 0 )
201204 proc . info . capturedVars . push ( v ) ;
202205 info . captured = true ;
@@ -479,10 +482,8 @@ namespace ts {
479482 proc . emit ( "add sp, #4*" + args . length )
480483 if ( hasRet )
481484 proc . emit ( "push {r0}" ) ;
482-
483485 }
484486
485-
486487 if ( decl && decl . kind == SyntaxKind . FunctionDeclaration ) {
487488 if ( attrs . shim ) {
488489 emitShim ( decl , node , args ) ;
@@ -517,6 +518,23 @@ namespace ts {
517518 unhandled ( node , "non-shim method call" ) ;
518519 }
519520
521+ if ( decl && ( decl . kind == SyntaxKind . VariableDeclaration || decl . kind == SyntaxKind . Parameter ) ) {
522+ if ( args . length > 1 )
523+ userError ( "lambda functions with more than 1 argument not supported" )
524+
525+ if ( hasRet )
526+ userError ( "lambda functions cannot yet return values" )
527+
528+ let suff = args . length + ""
529+ if ( suff == "0" ) suff = ""
530+
531+ args . unshift ( node . expression )
532+ args . forEach ( emit )
533+
534+ proc . emitCall ( "action::run" + suff , getMask ( args ) ) ;
535+ return
536+ }
537+
520538 unhandled ( node , stringKind ( decl ) )
521539 }
522540
@@ -572,7 +590,10 @@ namespace ts {
572590 proc . emitCall ( "action::mk" , 0 )
573591 caps . forEach ( ( l , i ) => {
574592 proc . emitInt ( i )
575- proc . localIndex ( l ) . emitLoad ( proc , true ) // direct load
593+ let loc = proc . localIndex ( l )
594+ if ( ! loc )
595+ userError ( "cannot find captured value: " + checker . symbolToString ( l . symbol ) )
596+ loc . emitLoad ( proc , true ) // direct load
576597 proc . emitCall ( "bitvm::stclo" , 0 )
577598 // already done by emitCall
578599 // proc.emit("push {r0}");
@@ -597,6 +618,13 @@ namespace ts {
597618 proc . emit ( "@stackmark inlfunc" ) ;
598619 proc . emit ( "push {r5, lr}" ) ;
599620 proc . emit ( "mov r5, r1" ) ;
621+
622+ node . parameters . forEach ( ( p , i ) => {
623+ if ( i >= 2 )
624+ userError ( lf ( "only up to two parameters supported in lambdas" ) )
625+ proc . emit ( `push {r${ i + 2 } }` )
626+ } )
627+ proc . emit ( "@stackmark args" ) ;
600628 } else {
601629 proc . emit ( "@stackmark func" ) ;
602630 proc . emit ( "@stackmark args" ) ;
@@ -618,20 +646,24 @@ namespace ts {
618646
619647 if ( proc . hasReturn ( ) ) {
620648 proc . emit ( "push {r0}" ) ;
621- proc . emitClrs ( null , true ) ;
649+ proc . emitClrs ( ) ;
622650 proc . emit ( "pop {r0}" ) ;
623651 } else {
624- proc . emitClrs ( null , true ) ;
652+ proc . emitClrs ( ) ;
625653 }
626654
627655 proc . popLocals ( ) ;
628656
629657 if ( isLambda ) {
658+ proc . emit ( "@stackempty args" )
659+ if ( node . parameters . length )
660+ proc . emit ( "add sp, #4*" + node . parameters . length + " ; pop args" )
630661 proc . emit ( "pop {r5, pc}" ) ;
631662 proc . emit ( "@stackempty inlfunc" ) ;
632663 } else {
633664 proc . emit ( "pop {pc}" ) ;
634665 proc . emit ( "@stackempty func" ) ;
666+ proc . emit ( "@stackempty args" )
635667 }
636668 } )
637669 }
@@ -1559,13 +1591,10 @@ namespace ts {
15591591 ( noargs ? null : this . args . filter ( n => n . def == l ) [ 0 ] )
15601592 }
15611593
1562- emitClrs ( omit : Declaration , inclArgs = false ) {
1563- var lst = this . locals
1564- if ( inclArgs )
1565- lst = lst . concat ( this . args )
1594+ emitClrs ( ) {
1595+ var lst = this . locals . concat ( this . args )
15661596 lst . forEach ( p => {
1567- if ( p . def != omit )
1568- p . emitClrIfRef ( this )
1597+ p . emitClrIfRef ( this )
15691598 } )
15701599 }
15711600
0 commit comments