Skip to content

Commit

Permalink
Make sure to unwrap promises
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallagher committed Jan 20, 2019
1 parent f350ab1 commit a6a37d4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 4 deletions.
79 changes: 77 additions & 2 deletions snap-compile/src/test/java/org/snapscript/compile/AwaitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,82 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = fun(1);\n"+
"println(x.class);\n"+
"x.then(y -> {\n"+
"assert x.then(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get();\n";
"}).get() == 'result for foo()';\n";

private static final String SUCCESS_8 =
"module Mod {\n"+
" async fun(n) {\n"+
" if(n > 0) {\n"+
" const f: String = await foo();\n"+
" println(f);\n"+
" assert f == 'result for foo()';\n"+
" return f;\n"+
" }\n"+
" println('x');\n"+
" }\n"+
" foo() {\n"+
" let n = Thread.currentThread().getName();\n"+
" println(n);\n"+
" return 'result for foo()';\n"+
" }\n"+
"}\n"+
"let x = Mod.fun(1);\n"+
"println(x.class);\n"+
"assert x.then(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";

private static final String SUCCESS_9 =
"class Typ {\n"+
" async static fun(n) {\n"+
" if(n > 0) {\n"+
" const f: String = await foo();\n"+
" println(f);\n"+
" assert f == 'result for foo()';\n"+
" return f;\n"+
" }\n"+
" println('x');\n"+
" }\n"+
" static foo() {\n"+
" let n = Thread.currentThread().getName();\n"+
" println(n);\n"+
" return 'result for foo()';\n"+
" }\n"+
"}\n"+
"let x = Typ.fun(1);\n"+
"println(x.class);\n"+
"assert x.then(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";

private static final String SUCCESS_10 =
"class Typ {\n"+
" async fun(n) {\n"+
" if(n > 0) {\n"+
" const f: String = await foo();\n"+
" println(f);\n"+
" assert f == 'result for foo()';\n"+
" return f;\n"+
" }\n"+
" println('x');\n"+
" }\n"+
" foo() {\n"+
" let n = Thread.currentThread().getName();\n"+
" println(n);\n"+
" return 'result for foo()';\n"+
" }\n"+
"}\n"+
"let x = new Typ().fun(1);\n"+
"println(x.class);\n"+
"assert x.then(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";

public void testAwait() throws Exception {
assertScriptExecutes(SUCCESS_1);
Expand All @@ -132,6 +204,9 @@ public void testAwait() throws Exception {
assertScriptExecutes(SUCCESS_5);
assertScriptExecutes(SUCCESS_6);
assertScriptExecutes(SUCCESS_7);
assertScriptExecutes(SUCCESS_8);
assertScriptExecutes(SUCCESS_9);
assertScriptExecutes(SUCCESS_10);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion snap-parse/src/main/resources/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ module-name = [identifier];
module-import = <import>;
module-property = <assignment-variable>?(':'<constraint>)?('='<assignment-expression>);
module-declaration = <field-modifier-list><module-property>*(','<module-property>)';';
module-function = <annotation-list><access-modifier-list><function-name>'('<parameter-list>')'?(':'<constraint>)<group-statement>;
module-modifier = {'async' | 'public' | 'private'};
module-modifier-list = *(<module-modifier>_);
module-function = <annotation-list><module-modifier-list><function-name>'('<parameter-list>')'?(':'<constraint>)<group-statement>;
module-statement = {<try-statement> | <synchronized-statement> | <conditional-statement> | <type-definition> | <assignment-statement> | <expression-statement>};
module-part = {<module-import> | <module-function> | <module-declaration> | <module-statement>};
module-definition = ?<annotation-list>'module'_<module-name>{'{}' | '{'*<module-part>'}'};
Expand Down
40 changes: 40 additions & 0 deletions snap-tree/src/main/java/org/snapscript/tree/AwaitExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.snapscript.tree;

import org.snapscript.core.Evaluation;
import org.snapscript.core.Promise;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.variable.Value;

public class AwaitExpression extends Evaluation {

private final Evaluation evaluation;

public AwaitExpression(Evaluation evaluation) {
this.evaluation = evaluation;
}

@Override
public void define(Scope scope) throws Exception {
evaluation.define(scope);
}

@Override
public Constraint compile(Scope scope, Constraint left) throws Exception {
return evaluation.compile(scope, left);
}

@Override
public Value evaluate(Scope scope, Value left) throws Exception {
Value value = evaluation.evaluate(scope, left);
Object object = value.getValue();

if(Promise.class.isInstance(object)) {
Promise promise = (Promise)object;
Object result = promise.get();

return Value.getTransient(result);
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static class CompileExecution extends SuspendStatement<Object> {
private final Evaluation left;

public CompileExecution(Evaluation left, Evaluation right){
this.right = right;
this.right = new AwaitExpression(right);
this.left = left;
}

Expand Down
2 changes: 2 additions & 0 deletions snap-tree/src/main/resources/instruction.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ import-static = org.snapscript.tree.ImportStatic
module-name = org.snapscript.tree.define.ModuleName
module-property = org.snapscript.tree.define.ModuleProperty
module-declaration = org.snapscript.tree.define.ModuleDeclaration
module-modifier = org.snapscript.tree.Modifier
module-modifier-list = org.snapscript.tree.ModifierList
module-function = org.snapscript.tree.define.ModuleFunction
module-import = org.snapscript.tree.define.ModuleStatement
module-statement = org.snapscript.tree.define.ModuleStatement
Expand Down

0 comments on commit a6a37d4

Please sign in to comment.