Simon Krajewski пре 11 година
родитељ
комит
1943881f4e

+ 4 - 0
tests/optimization/run.hxml

@@ -0,0 +1,4 @@
+-cp src
+-js testopt.js
+--macro Macro.register('Test')
+-dce std

+ 84 - 0
tests/optimization/src/Macro.hx

@@ -0,0 +1,84 @@
+import haxe.macro.Context;
+import haxe.macro.Expr;
+import haxe.macro.Type;
+
+using StringTools;
+
+class Macro {
+	static var classes = [];
+	static var output;
+	static var lines;
+	static var tests = 0;
+	static var failures = 0;
+	
+	static function register(className:String) {
+		if (classes.length == 0) {
+			Context.onAfterGenerate(run);
+		}
+		Context.getType(className);
+		classes.push(className);
+	}
+	
+	static function run() {
+		output = sys.io.File.getContent(haxe.macro.Compiler.getOutput());
+		lines = output.replace("\r", "").split("\n");
+		for (className in classes) {
+			test(className);
+		}
+		trace('Done $tests tests ($failures failures)');
+		trace("SUCCESS: " + (failures == 0));
+	}
+	
+	static function test(className:String) {
+		var c = switch(Context.getType(className)) {
+			case TInst(c, _): c.get();
+			case _: Context.error('$className should be a class', Context.currentPos());
+		}
+		var fields = [];
+		function checkField(cf:ClassField) {
+			if (cf.meta.has(":js")) {
+				fields.push({name: cf.name, js: extractJs(cf.meta.get()), pos: cf.pos});
+			}
+		}
+		for (cf in c.statics.get()) {
+			checkField(cf);
+		}
+		for (field in fields) {
+			var name = '$className.${field.name}';
+			var output = getOutput(name);
+			++tests;
+			if (output != field.js) {
+				++failures;
+				Context.warning('$output should be ${field.js}', field.pos);
+			}
+		}
+	}
+	
+	static function extractJs(meta:Metadata) {
+		for (m in meta) {
+			if (m.name == ":js") {
+				switch(m.params[0]) {
+					case macro $v{(s:String)}: return s;
+					case e: Context.error("String expected", e.pos);
+				}
+			}
+		}
+		throw false;
+	}
+	
+	static function getOutput(identifier:String) {
+		var buf = new StringBuf();
+		for (i in 0...lines.length) {
+			if (lines[i].startsWith(identifier)) {
+				for (k in (i + 1)...lines.length) {
+					if (lines[k].startsWith("\t")) {
+						buf.add(lines[k].trim());
+					} else {
+						return buf.toString();
+					}
+				}
+			}
+		}
+		return Context.error('Could not find $identifier in output', Context.currentPos());
+	}
+}

+ 56 - 0
tests/optimization/src/Test.hx

@@ -0,0 +1,56 @@
+class InlineCtor {
+	public var x:Int;
+	public var y:String;
+	
+	public inline function new(x, y) {
+		this.x = x;
+		this.y = y;
+	}
+}
+
+class Test {
+	@:js("3;")
+	static function testNoOpRemoval() {
+		1;
+		2;
+		{}
+		3;
+	}
+	
+	@:js("var a = 3;var b = 27;")
+	static function testConstMath() {
+		var a = 1 + 2;
+		var b = 9 * 3;
+	}
+	
+	@:js("var c_x = 12;var c_y = \"foo\";var x = c_x;c_x = 13;x = c_x;var y = c_y;")
+	static function testInlineCtor1() {
+		var c = new InlineCtor(12, "foo");
+		var x = c.x;
+		c.x = 13;
+		x = c.x;
+		var y = c.y;
+	}
+	
+	@:js("var a = 0;a = 1;a = 2;var c_x = 12;var c_y = \"foo\";a = c_x;")
+	static function testInlineCtor2() {
+		var a = 0;
+		var c = {
+			a = 1;
+			a = 2;
+			new InlineCtor(12, "foo");
+		}
+		a = c.x;
+	}
+	
+	@:js("var a = 0;var c_x = 1;var c_y = \"c\";a = 1;var b_x = 2;var b_y = \"b\";b_x = a;")
+	static function testInlineCtor3() {
+		var a = 0;
+		var b = {
+			var c = new InlineCtor(1, "c");
+			a = 1;
+			new InlineCtor(2, "b");
+		}
+		b.x = a;
+	}
+}

+ 53 - 0
tests/optimization/testopt.hxproj

@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project version="2">
+  <!-- Output SWF options -->
+  <output>
+    <movie outputType="CustomBuild" />
+    <movie input="" />
+    <movie path="testopt.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="src" />
+  </classpaths>
+  <!-- Build options -->
+  <build>
+    <option directives="" />
+    <option flashStrict="False" />
+    <option noInlineOnDebug="False" />
+    <option mainClass="Main" />
+    <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" />
+  </compileTargets>
+  <!-- Paths to exclude from the Project Explorer tree -->
+  <hiddenPaths>
+    <hidden path="obj" />
+  </hiddenPaths>
+  <!-- Executed before build -->
+  <preBuildCommand>haxe run.hxml</preBuildCommand>
+  <!-- Executed after build -->
+  <postBuildCommand alwaysRun="False" />
+  <!-- Other project options -->
+  <options>
+    <option showHiddenPaths="False" />
+    <option testMovie="Custom" />
+    <option testMovieCommand="" />
+  </options>
+  <!-- Plugin storage -->
+  <storage />
+</project>