Kaynağa Gözat

[js] Setup keywords for custom js generator (fixes #8231)

Aleksandr Kuzmenko 6 yıl önce
ebeveyn
işleme
98d86711ba

+ 7 - 4
src/generators/genjs.ml

@@ -83,9 +83,6 @@ let s_path ctx = if ctx.js_flatten then Path.flat_path else dot_path
 
 let kwds = Hashtbl.create 0
 
-let setup_kwds lst =
-	List.iter (fun s -> Hashtbl.add kwds s ()) lst
-
 let es3kwds = [
 	"abstract"; "boolean"; "break"; "byte"; "case"; "catch"; "char"; "class"; "const"; "continue";
 	"debugger"; "default"; "delete"; "do"; "double"; "else"; "enum"; "export"; "extends"; "false"; "final";
@@ -104,6 +101,12 @@ let es5kwds = [
 	"true"; "try"; "typeof"; "var"; "void"; "while"; "with"; "yield"
 ]
 
+let setup_kwds com =
+	Hashtbl.reset kwds;
+	let es_version = get_es_version com in
+	let lst = if es_version >= 5 then es5kwds else es3kwds in
+	List.iter (fun s -> Hashtbl.add kwds s ()) lst
+
 (* Identifiers Haxe reserves to make the JS output cleaner. These can still be used in untyped code (TLocal),
    but are escaped upon declaration. *)
 let kwds2 =
@@ -1590,7 +1593,7 @@ let generate com =
 
 	let nodejs = Common.raw_defined com "nodejs" in
 
-	setup_kwds (if ctx.es_version >= 5 then es5kwds else es3kwds);
+	setup_kwds com;
 
 	let exposed = List.concat (List.map (fun t ->
 		match t with

+ 1 - 0
src/macro/macroApi.ml

@@ -1607,6 +1607,7 @@ let macro_api ccom get_api =
 			let f = prepare_callback f 1 in
 			(get_api()).set_js_generator (fun js_ctx ->
 				let com = ccom() in
+				Genjs.setup_kwds com;
 				let api = encode_obj [
 					"outputFile", encode_string com.file;
 					"types", encode_array (List.map (fun t -> encode_type (type_of_module_type t)) com.types);

+ 15 - 0
tests/misc/projects/Issue8231/Main.hx

@@ -0,0 +1,15 @@
+class Main {
+	static var tmp:Int;
+
+	@:analyzer(ignore)
+	static function main() {
+		var finally = 999;
+		tmp = finally;
+	}
+
+	#if macro
+	static function setupGenerator() {
+		haxe.macro.Compiler.setCustomJSGenerator(api -> new haxe.macro.ExampleJSGenerator(api).generate());
+	}
+	#end
+}

+ 5 - 0
tests/misc/projects/Issue8231/compile.hxml

@@ -0,0 +1,5 @@
+-main Main
+--macro Main.setupGenerator()
+-js bin/test.js
+
+--cmd node bin/test.js