@@ -3,23 +3,46 @@ module Fable.Transforms.FableTransforms
33open Fable
44open Fable.AST .Fable
55
6+ let private walkCapturedIdents ( f : string -> bool ) expr =
7+ let exprs = FSharp.Collections.ResizeArray [| struct ( false , expr) |]
8+ let mutable index = 0
9+ let mutable found = false
10+
11+ while not found && index < exprs.Count do
12+ let struct ( isClosure , expr ) = exprs[ index]
13+ index <- index + 1
14+
15+ match expr with
16+ | IdentExpr ident when isClosure -> found <- f ident.Name
17+ | Lambda(_, body, _) -> exprs.Add( struct ( true , body))
18+ | Delegate(_, body, _, _) -> exprs.Add( struct ( true , body))
19+ | ObjectExpr( members, _, baseCall) ->
20+ members
21+ |> List.iter ( fun memberDecl -> exprs.Add( struct ( true , memberDecl.Body)))
22+
23+ baseCall
24+ |> Option.iter ( fun baseCall -> exprs.Add( struct ( isClosure, baseCall)))
25+ | e ->
26+ getSubExpressions e
27+ |> List.iter ( fun subExpr -> exprs.Add( struct ( isClosure, subExpr)))
28+
29+ found
30+
31+ let getCapturedNames expr =
32+ let capturedNames = System.Collections.Generic.HashSet< string>()
33+
34+ walkCapturedIdents
35+ ( fun identName ->
36+ capturedNames.Add( identName) |> ignore
37+ false
38+ )
39+ expr
40+ |> ignore
41+
42+ capturedNames |> Seq.toList
43+
644let isIdentCaptured identName expr =
7- let rec loop isClosure exprs =
8- match exprs with
9- | [] -> false
10- | expr :: restExprs ->
11- match expr with
12- | IdentExpr i when i.Name = identName -> isClosure || loop isClosure restExprs
13- | Lambda(_, body, _) -> loop true [ body ] || loop isClosure restExprs
14- | Delegate(_, body, _, _) -> loop true [ body ] || loop isClosure restExprs
15- | ObjectExpr( members, _, baseCall) ->
16- let memberExprs = members |> List.map ( fun m -> m.Body)
17- loop true memberExprs || loop isClosure ( Option.toList baseCall @ restExprs)
18- | e ->
19- let sub = getSubExpressions e
20- loop isClosure ( sub @ restExprs)
21-
22- loop false [ expr ]
45+ walkCapturedIdents ( fun candidate -> candidate = identName) expr
2346
2447let isTailRecursive identName expr =
2548 let mutable isTailRec = true
0 commit comments