Explorar o código

[js] use $_ to store enum constructor function for setting __params__ (see #7165)

so we don't repeat it twice
Dan Korostelev %!s(int64=7) %!d(string=hai) anos
pai
achega
e1f188c722
Modificáronse 1 ficheiros con 13 adicións e 9 borrados
  1. 13 9
      src/generators/genjs.ml

+ 13 - 9
src/generators/genjs.ml

@@ -1167,22 +1167,17 @@ let generate_enum ctx e =
 			let sargs = String.concat "," (List.map (fun (n,_,_) -> ident n) args) in begin
 			if as_objects then begin
 				let sfields = String.concat "," (List.map (fun (n,_,_) -> (ident n) ^ ":" ^ (ident n) ) args) in
-				print ctx "function(%s) { return {_hx_index:%d,%s,__enum__:\"%s\"" sargs f.ef_index sfields dotp;
+				let sparams = String.concat "," (List.map (fun (n,_,_) -> "\"" ^ (ident n) ^ "\"" ) args) in
+				print ctx "($_=function(%s) { return {_hx_index:%d,%s,__enum__:\"%s\"" sargs f.ef_index sfields dotp;
 				if has_enum_feature then
 					spr ctx ",toString:$estr";
-				spr ctx "}; }";
+				print ctx "}; },$_.__params__ = [%s],$_)" sparams
 			end else begin
 				print ctx "function(%s) { var $x = [\"%s\",%d,%s]; $x.__enum__ = %s;" sargs f.ef_name f.ef_index sargs p;
 				if has_enum_feature then
 					spr ctx " $x.toString = $estr;";
 				spr ctx " return $x; }";
 			end end;
-			if as_objects then begin
-				let sparams = String.concat "," (List.map (fun (n,_,_) -> "\"" ^ (ident n) ^ "\"" ) args) in
-				newline ctx;
-				print ctx "%s%s.__params__ = [%s];" p (field f.ef_name) sparams
-			end;
-			ctx.separator <- true;
 		| _ ->
 			if as_objects then
 				print ctx "{_hx_index:%d,__enum__:\"%s\"%s};" f.ef_index dotp (if has_enum_feature then ",toString:$estr" else "")
@@ -1467,6 +1462,12 @@ let generate com =
 		then ("$estr = function() { return " ^ (ctx.type_accessor (TClassDecl { null_class with cl_path = ["js"],"Boot" })) ^ ".__string_rec(this,''); }") :: vars
 		else vars in
 	let vars = if enums_as_objects then "$hxEnums = {}" :: vars else vars in
+	let vars,has_dollar_underscore =
+		if List.exists (function TEnumDecl { e_extern = false } -> true | _ -> false) com.types then
+			"$_" :: vars,true
+		else
+			vars,false
+	in
 	(match List.rev vars with
 	| [] -> ()
 	| vl ->
@@ -1504,7 +1505,10 @@ let generate com =
 		newline ctx;
 	end;
 	if has_feature ctx "use.$bind" then begin
-		print ctx "var $_, $fid = 0";
+		if has_dollar_underscore then
+			print ctx "var $fid = 0"
+		else
+			print ctx "var $_, $fid = 0";
 		newline ctx;
 		(if ctx.es_version < 5 then
 			print ctx "function $bind(o,m) { if( m == null ) return null; if( m.__id__ == null ) m.__id__ = $fid++; var f; if( o.hx__closures__ == null ) o.hx__closures__ = {}; else f = o.hx__closures__[m.__id__]; if( f == null ) { f = function(){ return f.method.apply(f.scope, arguments); }; f.scope = o; f.method = m; o.hx__closures__[m.__id__] = f; } return f; }"