Skip to content

Commit

Permalink
Merge pull request #12 from botsman/typing
Browse files Browse the repository at this point in the history
Add typing
  • Loading branch information
botsman authored Mar 5, 2021
2 parents 8295707 + 4394f66 commit 35a6728
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
build
.python-version
dump
20 changes: 10 additions & 10 deletions build_tests.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
-D js-es=6
-D shallow-expose

--next
# --next

# python build
--python build/py/tests.py
--cmd cp tests/main.py build/py/
# # python build
# --python build/py/tests.py
# --cmd cp tests/main.py build/py/

--next
# --next

# python sync build (macros meta tags are igored and no changes are made)
--python build/py_sync/tests.py
-D sync
--cmd cp tests/main_sync.py build/py_sync/
# # python sync build (macros meta tags are igored and no changes are made)
# --python build/py_sync/tests.py
# -D sync
# --cmd cp tests/main_sync.py build/py_sync/

--next
# --next

# java (== other) build
# --java build/java/
2 changes: 1 addition & 1 deletion haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"javascript"
],
"description": "This library provides a macros to add 'async' and 'await' keywords for Python and JavaScript and does nothing for other platoforms.",
"version": "0.3.6",
"version": "1.0.0",
"classPath": "src",
"releasenote": "Mare cases handled. Improve throwed errors",
"contributors": [
Expand Down
4 changes: 3 additions & 1 deletion src/hxasync/NoReturn.hx → src/hxasync/Abstracts.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package hxasync;

abstract Awaitable<T>(T) from T {}

abstract NoReturn(Dynamic) to Dynamic from Dynamic {

abstract NoReturn(Dynamic) {
inline public function new(value: Dynamic) {
this = null;
}
Expand Down
52 changes: 44 additions & 8 deletions src/hxasync/AsyncMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class AsyncMacro {
handleAny(variable.expr, isAsyncContext);
}
case EFunction(kind, f):
handleEFunction(f, kind, false, expr.pos);
handleEFunction(f, kind, isAsyncContext, expr.pos);
case EObjectDecl(fields):
for (field in fields) {
handleAny(field.expr, isAsyncContext);
Expand Down Expand Up @@ -183,7 +183,12 @@ class AsyncMacro {
}
}

public static function handleEFunction(fun: Function, kind: FunctionKind, isAsyncContext: Bool, pos: Position) {
public static function handleEFunction(
fun: Function,
kind: FunctionKind,
isAsyncContext: Bool,
pos: Position
) {
if (isAsyncContext) {
switch kind {
case FNamed(name, inlined):
Expand Down Expand Up @@ -329,10 +334,6 @@ class AsyncMacro {
}
}

public static function getModifiedFunctionReturnType(ret: Null<ComplexType>): Null<ComplexType> {
return null; // TODO: fix
}

private static function getPythonEmptyReturn(expr: Expr): Expr {
return {
expr: EReturn(
Expand Down Expand Up @@ -405,20 +406,55 @@ class AsyncMacro {
}
}

public static function inferReturnType(fun: Function): Null<ComplexType> {
if (fun.ret != null) {
return fun.ret;
}
var complexType =
try {
var typed = Context.typeExpr({expr: EFunction(null, fun), pos:fun.expr.pos});
typed.t.followWithAbstracts().toComplexType();
} catch (e) {
null;
};

return switch complexType {
case TFunction(args, ret):
ret;
default:
null;
}
}

public static function getModifiedFunctionReturnType(fun: Function) {
var returnType = inferReturnType(fun);
return switch returnType {
case TPath({name: "StdTypes", sub: "Void"}):
macro: hxasync.Abstracts.Awaitable<hxasync.Abstracts.NoReturn>;
case TPath(p):
macro: hxasync.Abstracts.Awaitable<$returnType>;
case null:
null; // TODO: fix. Temporary fallback solution for cases when we failed to infer return type
default:
trace('Unexpected return type: ${returnType}');
macro: hxasync.Abstracts.Awaitable<$returnType>;
}
}

/**
* Modifies function body (by adding asyncPlaceholder) and (in future) changes return type from T to Promise<T>
* @param {Function} fun -- Function to modify
*/
public static function transformToAsync(fun: Function) {
fun.ret = getModifiedFunctionReturnType(fun);
fun.expr = getModifiedPlatformFunctionBody(fun.expr);
fun.ret = getModifiedFunctionReturnType(fun.ret);
makeExplicitReturn(fun);
}

public static function processAwaitedFuncArgs(expr: Expr) {
switch expr.expr {
case ECall(e, params):
handleAny(e, false);
handleAny(e, true);
for (param in params) {
handleAny(param, false);
}
Expand Down
2 changes: 2 additions & 0 deletions src/hxasync/AsyncMacroUtils.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package hxasync;

import hxasync.Abstracts.Awaitable;

class AsyncMacroUtils {
public static extern inline function await<T>(arg: Awaitable<T>): T {
#if js
Expand Down
3 changes: 0 additions & 3 deletions src/hxasync/Awaitable.hx

This file was deleted.

21 changes: 16 additions & 5 deletions tests/Tests.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package tests;



class Cases {
public var some = "some variable";

public function new() {}
public function new() {
}

@async public static function testBasic() {
return "basic func called";
Expand Down Expand Up @@ -40,15 +43,23 @@ class Cases {
@await funcWithTwoDefaultArgs();
}

@async public function testNestedFunction() {
@async public static function testNestedFunction() {
var nestedFunction = @async function() {
trace(this.some);
trace("asd");
}

@await nestedFunction();
}

@async public function execute() {
@async public function returnDynamic() {
var a = 10;
return {
a: a,
b: this.some
};
}

@async public static function execute() {
@await testBasic();
@await testFuncWithCallback();
@await testArrowFunction();
Expand All @@ -61,6 +72,6 @@ class Cases {
class Tests {
static public function main() {
var cases = new Cases();
cases.execute();
cases.returnDynamic();
}
}

0 comments on commit 35a6728

Please sign in to comment.