Browse Source

[js] fixed js syntax on `value.iterator--` (fixes #6637) (#6638)

* [js] fixed js syntax on `value.iterator--` (fixes #6637)

* changelog
Alexander Kuzmenko 8 years ago
parent
commit
d3a807dd4a
2 changed files with 29 additions and 0 deletions
  1. 11 0
      src/generators/genjs.ml
  2. 18 0
      tests/unit/src/unit/issues/Issue6637.hx

+ 11 - 0
src/generators/genjs.ml

@@ -511,6 +511,17 @@ and gen_expr ctx e =
 		print ctx "$iterator(";
 		gen_value ctx x;
 		print ctx ")";
+	(* Don't generate `$iterator(value)` for exprs like `value.iterator--` *)
+	| TUnop (op,flag,({eexpr = TField (x,f)} as fe)) when field_name f = "iterator" && is_dynamic_iterator ctx fe ->
+		(match flag with
+			| Prefix ->
+				spr ctx (Ast.s_unop op);
+				gen_value ctx x;
+				spr ctx ".iterator"
+			| Postfix ->
+				gen_value ctx x;
+				spr ctx ".iterator";
+				spr ctx (Ast.s_unop op))
 	| TField (x,FClosure (Some ({cl_path=[],"Array"},_), {cf_name="push"})) ->
 		(* see https://github.com/HaxeFoundation/haxe/issues/1997 *)
 		add_feature ctx "use.$arrayPush";

+ 18 - 0
tests/unit/src/unit/issues/Issue6637.hx

@@ -0,0 +1,18 @@
+package unit.issues;
+
+class Issue6637 extends unit.Test {
+	function test() {
+		//this is required to enable HxOverrides.iter on js
+		([]:Iterable<Int>);
+
+		var value = {iterator:10};
+		decrease(value);
+
+		eq(value.iterator, 8);
+	}
+
+	static public function decrease<T:{iterator:Int}>(v:T) {
+		v.iterator--;
+		--v.iterator;
+	}
+}