Skip to content

Commit

Permalink
compiler: add CPod.flattenDepends and CPod.orderByDepends static util…
Browse files Browse the repository at this point in the history
…ity methods

refactor compilerEs to use those instead
  • Loading branch information
Matthew Giannini authored and Matthew Giannini committed Jun 25, 2024
1 parent 155a2b4 commit 5042530
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
57 changes: 57 additions & 0 deletions src/compiler/fan/namespace/CPod.fan
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,62 @@ mixin CPod
return name
}

**
** Expand a set of pods to include all their recursive dependencies.
** This method does not order them; see `orderByDepends`.
**
static CPod[] flattenDepends(CPod[] pods)
{
acc := Str:CPod[:]
pods.each |pod| { doFlattenDepends(acc, pod) }
return acc.vals
}

private static Void doFlattenDepends([Str:CPod] acc, CPod pod)
{
if (acc.containsKey(pod.name)) return
acc[pod.name] = pod
pod.depends.each |CDepend depend|
{
doFlattenDepends(acc, pod.ns.resolvePod(depend.name, null))
}
}

**
** Order a list of pods by their dependencies.
** This method does not flatten dependencies - see `flattenDepends`.
**
static CPod[] orderByDepends(CPod[] pods)
{
left := pods.dup.sort
ordered := CPod[,]
while (!left.isEmpty)
{
i := 0
for (i = 0; i<left.size; ++i)
{
if (noDependsInLeft(left, left[i])) break
}
ordered.add(left.removeAt(i))
}
return ordered
}

private static Bool noDependsInLeft(CPod[] left, CPod p)
{
depends := p.depends
for (i := 0; i<depends.size; ++i)
{
d := depends[i]
for (j := 0; j<left.size; ++j)
{
if (d.name == left[j].name)
return false
}
}
return true
}


}

55 changes: 1 addition & 54 deletions src/compilerEs/fan/ast/JsPod.fan
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ class JsPod : JsNode
if (p.name.startsWith("[java]")) return null
return c.ns.resolvePod(p.name, null)
}
// jacked this implementation straight from Pod.java.
// see https://fantom.org/forum/topic/2922 for reason
pods = orderByDepends(flattenDepends(pods))
pods.each |depend|
CPod.orderByDepends(CPod.flattenDepends(pods)).each |depend|
{
if (depend.name == "sys") return
if (!c.ns.resolvePod(depend.name, null).hasJs) return
Expand All @@ -86,56 +83,6 @@ class JsPod : JsNode
js.wl("const js = (typeof window !== 'undefined') ? window : global;")
}

** Use CNamespace to flatten depends (they are not ordered)
private CPod[] flattenDepends(CPod[] pods)
{
acc := Str:CPod[:]
pods.each |pod| { doFlattenDepends(acc, pod) }
return acc.vals
}

private Void doFlattenDepends([Str:CPod] acc, CPod pod)
{
if (acc.containsKey(pod.name)) return
acc[pod.name] = pod
pod.depends.each |CDepend depend|
{
doFlattenDepends(acc, c.ns.resolvePod(depend.name, null))
}
}

** Order depends using namespace
private CPod[] orderByDepends(CPod[] pods)
{
left := pods.dup.sort
ordered := CPod[,]
while (!left.isEmpty)
{
i := 0
for (i = 0; i<left.size; ++i)
{
if (noDependsInLeft(left, left[i])) break
}
ordered.add(left.removeAt(i))
}
return ordered
}

private static Bool noDependsInLeft(CPod[] left, CPod p)
{
depends := p.depends
for (i := 0; i<depends.size; ++i)
{
d := depends[i]
for (j := 0; j<left.size; ++j)
{
if (d.name == left[j].name)
return false
}
}
return true
}

private Void writeTypes()
{
types.each |JsType t|
Expand Down

0 comments on commit 5042530

Please sign in to comment.