From ca51b07278c206220596cd52a24812d10d2b8ac9 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 15 May 2019 14:01:15 -0600 Subject: [PATCH] Tricked tests into working for js/cpp --- hank/HInterface.hx | 55 ++++++++++++++++++++++++++--------------- hxml/all-platforms.hxml | 28 ++++++++------------- hxml/all.hxml | 7 ------ 3 files changed, 45 insertions(+), 45 deletions(-) delete mode 100644 hxml/all.hxml diff --git a/hank/HInterface.hx b/hank/HInterface.hx index cbd3b57..63a0537 100644 --- a/hank/HInterface.hx +++ b/hank/HInterface.hx @@ -30,19 +30,6 @@ class HInterface { this.viewCounts = viewCounts; } - static function isStoryNode(o: Dynamic) { - var type = o.typeof(); - switch (type) { - case TClass(c): - if (c.getClassName() == 'hank.StoryNode') { - return true; - } - default: - } - trace('$o is $type'); - return false; - } - static function resolveInScope(variables: Map, name: String): Dynamic { var scope: Array = cast(variables['scope'], Array); for (container in scope) { @@ -63,7 +50,7 @@ class HInterface { static function resolve(variables: Map, container: Dynamic, name: String): Option { if (variables.exists(name)) { return Some(variables[name]); - } else if (isStoryNode(container)) { + } else if (Std.is(container, StoryNode)) { var node: StoryNode = cast(container, StoryNode); return node.resolve(name); } @@ -78,7 +65,7 @@ class HInterface { } static function isTruthy(viewCounts: Map, v: Dynamic) { - if (isStoryNode(v)) { + if (Std.is(v, StoryNode)) { var node: StoryNode = cast(v, StoryNode); return viewCounts[node] > 0; } @@ -87,8 +74,21 @@ class HInterface { return v; case TInt | TFloat: return v > 0; + // TODO I would love to do away with this hack, but C++ type coercion turns random things into strings. This workaround fixes a specific bug default: - throw '$v cannot be coerced to a boolean'; + if (Std.is(v, String)) { + var val = cast(v, String); + switch (val) { + case "true": + return true; + case "false": + return false; + default: + throw '$v: ${Type.typeof(v)} cannot be coerced to a boolean'; + } + } + else throw '$v: ${Type.typeof(v)} cannot be coerced to a boolean'; + } } @@ -120,12 +120,16 @@ class HInterface { } var val2 = valueOf(viewCounts, val); - trace('$val: ${val.typeof()} became $val2: ${val2.typeof()}'); + var type1 = Std.string(Type.typeof(val)); + var type2 = Std.string(Type.typeof(val2)); + //if (type1 != type2) + trace('$val: $type1 became $val2: $type2'); return val2; } public function cond(h: String, scope: Array): Bool { - return isTruthy(viewCounts, expr(h, scope)); + var val: Dynamic = expr(h, scope); + return isTruthy(viewCounts, val); } public function evaluateExpr(h: String, scope: Array): String { @@ -133,7 +137,7 @@ class HInterface { } static function valueOf(viewCounts: Map, val: Dynamic): Dynamic { - return if (isStoryNode(val)) { + return if (Std.is(val, StoryNode)) { var node: StoryNode = cast(val, StoryNode); viewCounts[node]; } else { @@ -141,11 +145,22 @@ class HInterface { }; } + static function viewCountOf(viewCounts: Map, val: Dynamic): Option { + return if (Std.is(val, StoryNode)) { + var node: StoryNode = cast(val, StoryNode); + Some(viewCounts[node]); + } else { + None; + }; + } + /** Convert numerical value expressions to booleans for binary operations **/ function boolify(expr: Expr): Expr { - return ECall(EIdent('_isTruthy'), [transmute(expr)]); + var newExpr = transmute(expr); + trace(newExpr); + return ECall(EIdent('_isTruthy'), [newExpr]); } function valify(expr: Expr): Expr { diff --git a/hxml/all-platforms.hxml b/hxml/all-platforms.hxml index b4ec239..a209ff6 100644 --- a/hxml/all-platforms.hxml +++ b/hxml/all-platforms.hxml @@ -3,6 +3,16 @@ -lib utest --each +-js js/hank.js +-cmd node js/hank.js +hxml/internals.hxml +--next + +-js js/hank.js +-cmd node js/hank.js +hxml/examples.hxml +--next + -cpp cpp -cmd cpp/Internals hxml/internals.hxml @@ -11,22 +21,4 @@ hxml/internals.hxml -cpp cpp -cmd cpp/Examples hxml/examples.hxml ---next - --js js/hank.js --cmd node js/hank.js -hxml/internals.hxml ---next - --js js/hank.js --cmd node js/hank.js -hxml/examples.hxml ---next - ---interp -hxml/internals.hxml ---next - ---interp -hxml/examples.hxml --next \ No newline at end of file diff --git a/hxml/all.hxml b/hxml/all.hxml deleted file mode 100644 index c7d5c67..0000000 --- a/hxml/all.hxml +++ /dev/null @@ -1,7 +0,0 @@ --cp hank --lib hscript --lib utest ---each -hxml/examples.hxml ---next -hxml/internals.hxml \ No newline at end of file