Skip to content

Commit

Permalink
Fix capability passing for extern definitions (#633)
Browse files Browse the repository at this point in the history
Resolves #546
  • Loading branch information
dvdvgt authored Oct 14, 2024
1 parent afb9ef0 commit 8911e76
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import effekt.symbols.*
import effekt.context.assertions.*
import effekt.source.Tree.Rewrite


/**
* Transformation on source trees that translates programs into explicit capability-passing style
*
Expand Down Expand Up @@ -34,6 +33,9 @@ object ExplicitCapabilities extends Phase[Typechecked, Typechecked], Rewrite {
val capabilities = Context.annotation(Annotations.BoundCapabilities, f)
val capParams = capabilities.map(definitionFor)
f.copy(bparams = bps ++ capParams, body = rewrite(body))
case extDef @ ExternDef(capture, id, tparams, vparams, bparams, ret, bodies) =>
val rewrittenBodies = bodies.map { rewrite }
extDef.copy(bodies = rewrittenBodies)
}

override def expr(using Context) = {
Expand Down Expand Up @@ -127,6 +129,20 @@ object ExplicitCapabilities extends Phase[Typechecked, Typechecked], Rewrite {
source.BlockLiteral(tps, vps, bps ++ capParams, rewrite(body))
}

override def rewrite(body: ExternBody)(using context.Context): ExternBody =
body match {
case b @ source.ExternBody.StringExternBody(ff, body) =>
val rewrittenTemplate =
body.copy(
args = body.args.map { rewrite }
)
b.copy(template = rewrittenTemplate)
case b @ source.ExternBody.EffektExternBody(ff, body) =>
val rewrittenBody = rewrite(body)
b.copy(body = rewrittenBody)
case u: source.ExternBody.Unsupported => u
}

def referenceToCapability(capability: BlockParam)(using C: Context): Var =
val id = IdRef(Nil, capability.name.name)
C.assignSymbol(id, capability)
Expand Down
1 change: 1 addition & 0 deletions examples/pos/extern-cap-passing.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typechecks
7 changes: 7 additions & 0 deletions examples/pos/extern-cap-passing.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
effect IOException(msg: String): Nothing
extern io def three: Nothing =
js "${box { (msg: String) => do IOException(msg) }}('oops')}"

def main() = {
println("typechecks")
}

0 comments on commit 8911e76

Please sign in to comment.