botsman / hxasync Goto Github PK
View Code? Open in Web Editor NEWThis library allows you to add "async" and "await" keywords in Python and JavaScript code almost the same way you would do it in the native code.
License: MIT License
This library allows you to add "async" and "await" keywords in Python and JavaScript code almost the same way you would do it in the native code.
License: MIT License
Would be good to avoid implementing interface or use build
metadata and just specify argument in the command line when compiling sources.
Would it possible to do so that when @async
is used with a function, its type is change, so if @await
is not used when calling the function type checking would fail?
Example
@async function returnZero(): Int {
return 0;
}
var i: Int = @await returnZero(); // compilation succeeds
var p: Promise<Int> = returnZero(); // compilation succeeds
var i2: Int = returnZero(); // compilation fails
Would be good to have an interface, which end user could implement to be able to use async
/await
keywords.
Hello, I'm using haxe to develop a tool project recently, I'm having some trouble with js.lib.promise syntax, after looking up related solutions in the community, I found haxelib hxasync, but I found that the project can't compile when I use it, the log information seems to be due to the npm Acorn library which was dependent on webpack, and the error output is as follows:
C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:2490
throw err
^
SyntaxError: Unexpected token (536:19)
at Parser.pp$4.raise (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:2488:13)
at Parser.pp.unexpected (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:623:8)
at Parser.pp.expect (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:617:26)
at Parser.pp$3.parseObj (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:2173:14)
at Parser.pp$3.parseExprAtom (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1994:17)
at Parser.pp$3.parseExprSubscripts (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1872:19)
at Parser.pp$3.parseMaybeUnary (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1849:17)
at Parser.pp$3.parseExprOps (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1791:19)
at Parser.pp$3.parseMaybeConditional (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1774:19)
at Parser.pp$3.parseMaybeAssign (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1750:19)
at Parser.pp$3.parseMaybeAssign (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1761:23)
at Parser.pp$3.parseExpression (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1722:19)
at Parser.pp$1.parseStatement (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:777:45)
at Parser.pp$1.parseBlock (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1040:23)
at Parser.pp$3.parseFunctionBody (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:2362:22)
at Parser.pp$1.parseFunction (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1132:8)
at Parser.pp$3.parseExprAtom (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1999:17)
at Parser.pp$3.parseExprSubscripts (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1872:19)
at Parser.pp$3.parseMaybeUnary (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1849:17)
at Parser.pp$3.parseExprOps (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1791:19)
at Parser.pp$3.parseMaybeConditional (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1774:19)
at Parser.pp$3.parseMaybeAssign (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1750:19)
at Parser.pp$3.parseExpression (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1722:19)
at Parser.pp$3.parseParenExpression (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:2025:18)
at Parser.pp$3.parseParenAndDistinguishExpression (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:2085:16)
at Parser.pp$3.parseExprAtom (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1978:41)
at Parser.pp$3.parseExprSubscripts (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1872:19)
at Parser.pp$3.parseMaybeUnary (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1849:17)
at Parser.pp$3.parseExprOps (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1791:19)
at Parser.pp$3.parseMaybeConditional (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1774:19)
at Parser.pp$3.parseMaybeAssign (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1750:19)
at Parser.pp$3.parseExpression (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:1722:19)
at Parser.pp$1.parseStatement (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:777:45)
at Parser.pp$1.parseTopLevel (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:672:23)
at Parser.parse (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:529:15)
at Object.parse (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\node_modules\acorn\dist\acorn.js:3378:37)
at Parser.processInput (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\tool\bin\split.js:1176:29)
at new Parser (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\tool\bin\split.js:1162:7)
at Object.HxSplit.run.$hx_exports.run (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-modular\tool\bin\split.js:1044:15)
at processOutput (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-loader\index.js:116:10)
at exec (C:\Users\Xuzx\Desktop\Haxe\OgmoEditor3-CE\node_modules\haxe-loader\index.js:94:27)
at ChildProcess.exithandler (child_process.js:285:7)
Do you know how to solve it?
You can install the hxasync lib in the Ogmo3 project and write some code in the initialization process for testing
var a = someFunction(@await anotherFunction())
private function some(param: String = "asd") {}
then in JS and Python output Haxe adds something like:
if (param == null) {
param = "asd";
}
and that added code is placed before asyncPlaceholder
_gthis = self
in the beginning of a function body, before async placeholder.
Probably both cases could be fixed with regexes. In the future would be good to get rid of regexes completely.
The errors which are thrown inside macros must be "attached" to the place in real code, not to the macros code.
string interpolation is not handled:
var a = 'asd ${@await someAyncFunction()}';
There are possible errors when trying to get expression type in macros.
Would be good to have a possibility to disable expression typing.
Also .github, build, and dump. All kinds of stuff that doesn't need to be shared.
123
Looks like that the code like the following
var f = (@await func()).field;
is compiled into the following in python
f = await func().field
which raises exception AttributeError 'coroutine' object has no attribute 'field'
Not sure if any changes are needed. Have to check.
Make sure that:
@async
function return type is converted to Awaitable<T>
.@await
can only be applied to Awaitable<T>
(maybe implement this under some flag, because in JS you can await normal function).@await
expression return type is converted to T
.My code:
@async private function myAsyncFuncReturningVoid(): Void {
// do something
// line 244 is here
}
@async private function test() {
@await this.myAsyncFuncReturningVoid(); // line 264 is here
}
Compiler output:
ex.hx:244: characters 5-38 : Dynamic should be Void
ex.hx:244: characters 5-38 : Dynamic should be Void
ex.hx:264: characters 14-33 : Void should be hxasync.Awaitable<Void>
ex.hx:264: characters 14-33 : ... For function argument 'arg'
It seems that currently Void
return type is converted to Dynamic
.
It does not seem to be a problem, but it would be good to fix anyway.
Need to check whether it is true and try to fix that.
I had to return null/None from async functions.
Would be good to keep implementation so that it deletes empty returns.
If @async
is added to the function declaration, convert its return type T
to Awaitable<T>
If @await
is added before Awaitable
function call, convert its return type from Awaitable<T>
to T
Check that @await
is used only inside async context.
Tests for Python, JavaScript and (later) for any other platform.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.