|
| 1 | +// Under the JIT, a cast to a class or interface whose value goes unused, as a |
| 2 | +// statement, checked nothing, so a cast that fails ran on, where the |
| 3 | +// interpreter and compiled code throw. Haxe's TestCasts.testInterfaces makes |
| 4 | +// such casts in closures, which the JIT compiles since local functions are |
| 5 | +// compiled. |
| 6 | +// The same cases run as compiled code, then as a script with the JIT off and on, |
| 7 | +// and print the same lines each time. |
| 8 | +function main() { |
| 9 | + Sys.println("-- compiled"); |
| 10 | + Compiled.run(); |
| 11 | + var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]); |
| 12 | + for (jit in [false, true]) { |
| 13 | + Sys.println(jit ? "-- script, JIT on" : "-- script, JIT off"); |
| 14 | + cpp.cppia.Host.enableJit(jit); |
| 15 | + try { |
| 16 | + cpp.cppia.Host.runFile(script); |
| 17 | + } catch (e:Dynamic) { |
| 18 | + Sys.println("error: " + e); |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// The same cases as Script.hx, compiled into the host |
| 24 | +interface CompiledFirst {} |
| 25 | +interface CompiledSecond {} |
| 26 | + |
| 27 | +class CompiledPlain implements CompiledFirst { |
| 28 | + public function new() {} |
| 29 | +} |
| 30 | + |
| 31 | +class CompiledBoth extends CompiledPlain implements CompiledSecond { |
| 32 | + public function new() { |
| 33 | + super(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class Compiled { |
| 38 | + static function attempt(label:String, f:Void->Void) { |
| 39 | + try { |
| 40 | + f(); |
| 41 | + Sys.println('$label passes'); |
| 42 | + } catch (e:Dynamic) { |
| 43 | + Sys.println('$label throws'); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // A cast whose value goes unused, as a statement of a method |
| 48 | + static function toSecond(o:Dynamic) { |
| 49 | + cast(o, CompiledSecond); |
| 50 | + } |
| 51 | + |
| 52 | + static function toBoth(o:Dynamic) { |
| 53 | + cast(o, CompiledBoth); |
| 54 | + } |
| 55 | + |
| 56 | + public static function run() { |
| 57 | + var plain = new CompiledPlain(); |
| 58 | + var both = new CompiledBoth(); |
| 59 | + attempt("plain to first in a closure", function() cast(plain, CompiledFirst)); |
| 60 | + attempt("plain to second in a closure", function() cast(plain, CompiledSecond)); |
| 61 | + attempt("both to second in a closure", function() cast(both, CompiledSecond)); |
| 62 | + attempt("plain to both in a closure", function() cast(plain, CompiledBoth)); |
| 63 | + attempt("plain to second in a method", () -> toSecond(plain)); |
| 64 | + attempt("both to second in a method", () -> toSecond(both)); |
| 65 | + attempt("plain to both in a method", () -> toBoth(plain)); |
| 66 | + attempt("both to both in a method", () -> toBoth(both)); |
| 67 | + attempt("plain to second, used", function() { |
| 68 | + var s:CompiledSecond = cast(plain, CompiledSecond); |
| 69 | + Sys.println('used ${s == null}'); |
| 70 | + }); |
| 71 | + var after = 0; |
| 72 | + attempt("a statement after a failed cast", function() { |
| 73 | + cast(plain, CompiledSecond); |
| 74 | + after++; |
| 75 | + }); |
| 76 | + Sys.println('ran on past it $after'); |
| 77 | + } |
| 78 | +} |
0 commit comments