Skip to content

Commit

Permalink
Fix result values from condition expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed May 27, 2013
1 parent 27ec7aa commit 341a878
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 61 deletions.
3 changes: 3 additions & 0 deletions TestIfElse.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-cp .
-js bin/TestIfElse.js
-main tests.TestIfElse
53 changes: 53 additions & 0 deletions TestIfElse.hxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<project version="2">
<!-- Output SWF options -->
<output>
<movie outputType="Application" />
<movie input="" />
<movie path="bin\TestIfElse.js" />
<movie fps="0" />
<movie width="0" />
<movie height="0" />
<movie version="1" />
<movie minorVersion="0" />
<movie platform="JavaScript" />
<movie background="#FFFFFF" />
</output>
<!-- Other classes to be compiled into your SWF -->
<classpaths>
<class path="." />
</classpaths>
<!-- Build options -->
<build>
<option directives="" />
<option flashStrict="False" />
<option mainClass="tests.TestIfElse" />
<option enabledebug="False" />
<option additional="" />
</build>
<!-- haxelib libraries -->
<haxelib>
<!-- example: <library name="..." /> -->
</haxelib>
<!-- Class files to compile (other referenced classes will automatically be included) -->
<compileTargets>
<compile path="src\Main.hx" />
<compile path="tests\TestIfElse.hx" />
</compileTargets>
<!-- Paths to exclude from the Project Explorer tree -->
<hiddenPaths>
<!-- example: <hidden path="..." /> -->
</hiddenPaths>
<!-- Executed before build -->
<preBuildCommand />
<!-- Executed after build -->
<postBuildCommand alwaysRun="False" />
<!-- Other project options -->
<options>
<option showHiddenPaths="False" />
<option testMovie="OpenDocument" />
<option testMovieCommand="bin/index.html" />
</options>
<!-- Plugin storage -->
<storage />
</project>
150 changes: 91 additions & 59 deletions com/dongxiguo/continuation/Continuation.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ import haxe.macro.Context;
import haxe.macro.Type;
import haxe.macro.Expr;
#end
#if haxe3
import haxe.ds.GenericStack;
#else
import haxe.FastList;
typedef GenericCell<T> = FastCell<T>;
#end
using Lambda;

/**
Expand Down Expand Up @@ -196,6 +202,27 @@ class Continuation
class ContinuationDetail
{
#if macro

static function pushMulti(gc:GenericCell<Expr>, a:Array<Expr>):GenericCell<Expr>
{
for (e in a)
{
gc = new GenericCell<Expr>(e, gc);
}
return gc;
}

static function toReverseArray<E>(gc:GenericCell<E>):Array<E>
{
var result = [];
while (gc != null)
{
result.unshift(gc.elt);
gc = gc.next;
}
return result;
}

static var seed:Int = 0;

static function unpack(exprs: Array<Expr>, pos: Position):Expr
Expand Down Expand Up @@ -629,13 +656,16 @@ class ContinuationDetail
case ECall(e, originParams):
{
// 优化 e 是另一个异步函数的情况
function transformNext(i:Int, transformedParameters:Array<Expr>):Expr
function transformNext(i:Int, transformedParameters:Null<GenericCell<Expr>>):Expr
{
if (i == originParams.length)
{
return transformNoDelay(e, function(functionResult)
{
transformedParameters.push(
var a =
toReverseArray(
transformedParameters);
a.push(
{
expr: EConst(CIdent("__return")),
pos: origin.pos
Expand All @@ -645,7 +675,7 @@ class ContinuationDetail
pos: origin.pos,
expr: ECall(
unpack(functionResult, origin.pos),
transformedParameters),
a),
};
});
}
Expand All @@ -655,15 +685,15 @@ class ContinuationDetail
originParams[i],
function(parameterResult:Array<Expr>):Expr
{
for (e in parameterResult)
{
transformedParameters.push(e);
}
return transformNext(i + 1, transformedParameters);
return transformNext(
i + 1,
pushMulti(
transformedParameters,
parameterResult));
});
}
}
return transformNext(0, []);
return transformNext(0, null);
}
default:
}
Expand Down Expand Up @@ -697,15 +727,15 @@ class ContinuationDetail
}
case EObjectDecl(originFields):
{
function transformNext(i:Int, transformedFields:Array<{ field : String, expr : Expr }>):Expr
function transformNext(i:Int, transformedFields:Null<GenericCell<{ field : String, expr : Expr }>>):Expr
{
if (i == originFields.length)
{
return rest(
[
{
pos: origin.pos,
expr: EObjectDecl(transformedFields),
expr: EObjectDecl(toReverseArray(transformedFields)),
}
]);
}
Expand All @@ -716,23 +746,25 @@ class ContinuationDetail
originField.expr,
function(valueResult:Array<Expr>):Expr
{
var t = transformedFields;
for (e in valueResult)
{
transformedFields.push(
t = new GenericCell(
{
field: originField.field,
expr: unpack(valueResult, originField.expr.pos),
});
},
t);
}
return transformNext(i + 1, transformedFields);
return transformNext(i + 1, t);
});
}
}
return transformNext(0, []);
return transformNext(0, null);
}
case ENew(t, originParams):
{
function transformNext(i:Int, transformedParameters:Array<Expr>):Expr
function transformNext(i:Int, transformedParameters:Null<GenericCell<Expr>>):Expr
{
if (i == originParams.length)
{
Expand All @@ -742,7 +774,7 @@ class ContinuationDetail
pos: origin.pos,
expr: ENew(
t,
transformedParameters),
toReverseArray(transformedParameters)),
}
]);
}
Expand All @@ -752,15 +784,15 @@ class ContinuationDetail
originParams[i],
function(parameterResult:Array<Expr>):Expr
{
for (e in parameterResult)
{
transformedParameters.push(e);
}
return transformNext(i + 1, transformedParameters);
return transformNext(
i + 1,
pushMulti(
transformedParameters,
parameterResult));
});
}
}
return transformNext(0, []);
return transformNext(0, null);
}
case EIn(_, _):
{
Expand Down Expand Up @@ -917,7 +949,7 @@ class ContinuationDetail
{
case ECall(e, originParams):
{
function transformNext(i:Int, transformedParameters:Array<Expr>):Expr
function transformNext(i:Int, transformedParameters:Null<GenericCell<Expr>>):Expr
{
if (i == originParams.length)
{
Expand Down Expand Up @@ -1033,23 +1065,22 @@ class ContinuationDetail
}
}
}
transformedParameters.push(
{
pos: origin.pos,
expr: EFunction(null,
var a = toReverseArray(transformedParameters);
a.push(
{
ret: null,
params: [],
expr: rest(handlerArgResult),
args: handlerArgDefs
})
});
pos: origin.pos,
expr: EFunction(null,
{
ret: null,
params: [],
expr: rest(handlerArgResult),
args: handlerArgDefs
})
});
return
{
pos: origin.pos,
expr: ECall(
transformedCalleeExpr,
transformedParameters),
expr: ECall(transformedCalleeExpr, a),
};
});
}
Expand All @@ -1059,15 +1090,15 @@ class ContinuationDetail
originParams[i],
function(parameterResult:Array<Expr>):Expr
{
for (e in parameterResult)
{
transformedParameters.push(e);
}
return transformNext(i + 1, transformedParameters);
return transformNext(
i + 1,
pushMulti(
transformedParameters,
parameterResult));
});
}
}
return transformNext(0, []);
return transformNext(0, null);
}
default:
}
Expand All @@ -1076,7 +1107,7 @@ class ContinuationDetail
default:
}
}
function transformNext(i:Int, transformedParameters:Array<Expr>):Expr
function transformNext(i:Int, transformedParameters:Null<GenericCell<Expr>>):Expr
{
if (i == originParams.length)
{
Expand All @@ -1089,7 +1120,7 @@ class ContinuationDetail
pos: origin.pos,
expr: ECall(
unpack(functionResult, origin.pos),
transformedParameters),
toReverseArray(transformedParameters)),
}]);
});
}
Expand All @@ -1099,15 +1130,15 @@ class ContinuationDetail
originParams[i],
function(parameterResult:Array<Expr>):Expr
{
for (e in parameterResult)
{
transformedParameters.push(e);
}
return transformNext(i + 1, transformedParameters);
return transformNext(
i + 1,
pushMulti(
transformedParameters,
parameterResult));
});
}
}
return transformNext(0, []);
return transformNext(0, null);
}
case EBreak:
{
Expand Down Expand Up @@ -1177,15 +1208,16 @@ class ContinuationDetail
}
case EArrayDecl(originParams):
{
function transformNext(i:Int, transformedParameters:Array<Expr>):Expr
function transformNext(i:Int, transformedParameters:Null<GenericCell<Expr>>):Expr
{
if (i == originParams.length)
{
return rest(
[
{
pos: origin.pos,
expr: EArrayDecl(transformedParameters),
expr: EArrayDecl(
toReverseArray(transformedParameters)),
}
]);
}
Expand All @@ -1195,15 +1227,15 @@ class ContinuationDetail
originParams[i],
function(parameterResult:Array<Expr>):Expr
{
for (e in parameterResult)
{
transformedParameters.push(e);
}
return transformNext(i + 1, transformedParameters);
return transformNext(
i + 1,
pushMulti(
transformedParameters,
parameterResult));
});
}
}
return transformNext(0, []);
return transformNext(0, null);
}
case EArray(e1, e2):
{
Expand Down
1 change: 1 addition & 0 deletions tests/TestContinuation.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using com.dongxiguo.continuation.Continuation;
@:build(com.dongxiguo.continuation.Continuation.cpsByMeta("cps"))
class TestContinuation
{


@cps static function forkJoin():Int
{
Expand Down
Loading

0 comments on commit 341a878

Please sign in to comment.