-
Notifications
You must be signed in to change notification settings - Fork 42
Async JavaScript interop #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8502ae1
scaffolding for async
AnsonYeung 984be72
async implementation
AnsonYeung 49b7208
remove noise
AnsonYeung 0aa291b
remove duplication
AnsonYeung eb6e1bd
Error handling
AnsonYeung ef76eda
Add async annotation on terms
AnsonYeung 60ab2b4
revert
AnsonYeung 3fee698
Add extra await for testing
AnsonYeung 33652e6
Merge branch 'hkmc2' into js-async
LPTK b4529db
Merge branch 'hkmc2' into js-async
LPTK af60cca
Address comments
AnsonYeung 34eaa26
Rerun tests
AnsonYeung 163db41
Enable instrumentation on async bodies
AnsonYeung 5926642
Add checking for await
AnsonYeung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package hkmc2 | ||
| package codegen | ||
|
|
||
| import scala.annotation.tailrec | ||
| import scala.collection.mutable | ||
| import scala.util.boundary | ||
| import sourcecode.{ Line, FileName, Name } | ||
|
|
||
| import hkmc2.utils.*, shorthands.* | ||
| import hkmc2.utils.* | ||
| import hkmc2.utils.SymbolSubst | ||
| import hkmc2.Message.MessageContext | ||
|
|
||
| import syntax.{Literal, Tree} | ||
| import semantics.* | ||
| import semantics.Elaborator.ctx | ||
| import semantics.Elaborator.State | ||
| import hkmc2.Config.EffectHandlers | ||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
|
|
||
| class AsyncLowering(using TL, Raise, Elaborator.State, Elaborator.Ctx, Config): | ||
|
|
||
| object Rewriter extends BlockTransformer(SymbolSubst.Id): | ||
| val collectedFunDefn: ArrayBuffer[FunDefn] = new ArrayBuffer | ||
| var inAsyncFun: Bool = false | ||
|
|
||
| inline def wrapAwait[T](innerAllowAwait: Bool)(inline thunk: => T): T = | ||
| val saved = inAsyncFun | ||
| inAsyncFun = innerAllowAwait | ||
| val result = thunk | ||
| inAsyncFun = saved | ||
| result | ||
|
|
||
| override def applyFunDefn(fun: FunDefn): FunDefn = | ||
| if !fun.async then return wrapAwait(false)(super.applyFunDefn(fun)) | ||
| val outerBms = BlockMemberSymbol(fun.sym.nme, Nil, fun.sym.nameIsMeaningful) | ||
| val outerDsym = TermSymbol(syntax.Fun, N, fun.dSym.id) | ||
| val outerParams = fun.params.flatMap: pl => | ||
| pl.allParams.map: p => | ||
| val v = p.sym | ||
| val nv = VarSymbol(v.id) | ||
| (p, p.copy(sym = nv)) | ||
| val symMap = outerParams.iterator.map(p => p._1.sym -> p._2.sym).toMap[SimpleSymbol, SimpleSymbol] | ||
| val thisVar = VarSymbol(Tree.Ident("this")) | ||
| val thisParam = fun.owner.map(_ => Param.simple(thisVar)) | ||
| val vars = fun.params.flatMap(_.paramSyms) | ||
| val noAsync = fun.annotations.filterNot(_ is Annot.Async) | ||
| val transformer = new BlockTransformer(SymbolSubst.Id): | ||
| override def applySimpleSymbol(sym: SimpleSymbol): SimpleSymbol = | ||
| symMap.getOrElse(sym, sym) | ||
| override def applyValue(v: Value)(k: Value => Block): Block = v match | ||
| case Value.This(sym) if fun.owner.contains(sym) => | ||
| k(Value.SimpleRef(thisVar)) | ||
| case _ => super.applyValue(v)(k) | ||
| val newBody = transformer.applyBlock(wrapAwait(true)(applyFunBodyLikeBlock(fun.body))) | ||
| collectedFunDefn += FunDefn(N, outerBms, outerDsym, PlainParamList((thisParam.iterator ++ outerParams.iterator.map(_._2)).toList) :: PlainParamList(Nil) :: Nil, newBody)(fun.configOverride, noAsync) | ||
| val callArgs = (fun.owner.iterator.map(s => Arg(N, Value.This(s))) ++ fun.params.iterator.flatMap(_.allParams.iterator.map(p => Arg(N, Value.SimpleRef(p.sym))))).toList | ||
| val tmp = TempSymbol(N, "tmp") | ||
| val wrapperBody = blockBuilder | ||
| .assignScoped(tmp, Call(Value.MemberRef(outerBms, outerDsym), callArgs ne_:: Nil)(CallMetadata.mlsFunWithEffect)) | ||
| .ret(Call(Value.SimpleRef(State.runtimeSymbol).selSN("toJsAsync"), (tmp.asSimpleRef.asArg :: Nil) ne_:: Nil)(CallMetadata.defaultMlsFun)) | ||
| FunDefn(fun.owner, fun.sym, fun.dSym, fun.params, wrapperBody)(fun.configOverride, noAsync) | ||
|
|
||
| override def applyMainBlock(main: Block): Block = | ||
| collectedFunDefn.foldRight(super.applyMainBlock(main)): (defn, acc) => | ||
| Scoped(Set.single(defn.sym), Define(defn, acc)) | ||
|
|
||
| override def applyPath(p: Path)(k: Path => Block): Block = | ||
| p match | ||
| case s: Select if s.symbol.contains(ctx.builtins.handlers.await) => | ||
| if config.effectHandlers.isEmpty && !inAsyncFun then | ||
| raise(ErrorReport( | ||
| msg"Only await inside of async bodies are allowed if effect handlers are not enabled." -> | ||
| p.toLoc :: Nil, | ||
| source = Diagnostic.Source.Compilation)) | ||
| k(State.runtimeSymbol.asSimpleRef.sel(new Tree.Ident("await"), ctx.builtins.handlers.await)) | ||
| case _ => | ||
| super.applyPath(p)(k) | ||
|
|
||
| def transform(prog: Program): Program = | ||
| Rewriter.applyProgram(prog) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.