Переглянути джерело

[js] add js.Syntax.code (no deprecation warning for now)

Dan Korostelev 8 роки тому
батько
коміт
fc036bd37d
2 змінених файлів з 37 додано та 7 видалено
  1. 21 7
      src/generators/genjs.ml
  2. 16 0
      std/js/Syntax.hx

+ 21 - 7
src/generators/genjs.ml

@@ -369,12 +369,9 @@ let rec gen_call ctx e el in_value =
 	| TIdent "__new__", args ->
 		print_deprecation_message ctx.com "__new__ is deprecated, use js.Syntax.new_ instead" e.epos;
 		gen_syntax ctx "new_" args e.epos
-	| TIdent "__js__", [{ eexpr = TConst (TString "this") }] ->
-		spr ctx (this ctx)
-	| TIdent "__js__", [{ eexpr = TConst (TString code) }] ->
-		spr ctx (String.concat "\n" (ExtString.String.nsplit code "\r\n"))
-	| TIdent "__js__", { eexpr = TConst (TString code); epos = p } :: tl ->
-		Codegen.interpolate_code ctx.com code tl (spr ctx) (gen_expr ctx) p
+	| TIdent "__js__", args ->
+		(* TODO: add deprecation warning when we figure out what to do with purity here *)
+		gen_syntax ctx "code" args e.epos
 	| TIdent "__instanceof__",  args ->
 		print_deprecation_message ctx.com "__instanceof__ is deprecated, use js.Syntax.instanceof instead" e.epos;
 		gen_syntax ctx "instanceof" args e.epos
@@ -922,7 +919,24 @@ and gen_syntax ctx meth args pos =
 		gen_value ctx f;
 		spr ctx "]";
 		spr ctx ")";
-	| _ -> abort (Printf.sprintf "Unknown js.Syntax method %s with %d arguments" meth (List.length args)) pos
+	| "code", code :: args ->
+		let code, code_pos =
+			match code.eexpr with
+			| TConst (TString s) -> s, code.epos
+			| _ -> abort "The `code` argument for js.Syntax must be a string constant" code.epos
+		in
+		begin
+			match args with
+			| [] ->
+				if code = "this" then
+					spr ctx (this ctx)
+				else
+					spr ctx (String.concat "\n" (ExtString.String.nsplit code "\r\n"))
+			| _ ->
+				Codegen.interpolate_code ctx.com code args (spr ctx) (gen_expr ctx) code_pos
+		end
+	| _ ->
+		abort (Printf.sprintf "Unknown js.Syntax method `%s` with %d arguments" meth (List.length args)) pos
 
 let generate_package_create ctx (p,_) =
 	let rec loop acc = function

+ 16 - 0
std/js/Syntax.hx

@@ -7,6 +7,22 @@ import haxe.extern.Rest;
 	Use only at low-level when specific target-specific code-generation is required.
 **/
 extern class Syntax {
+	/**
+		Inject `code` directly into generated source.
+
+		`code` must be a string constant.
+
+		Additional `args` are supported to provide code interpolation, for example:
+		```
+		Syntax.code("console.log({0}, {1})", "hi", 42);
+		```
+		will generate
+		```
+		console.log("hi", 42);
+		```
+	**/
+	static function code(code:String, args:Rest<Dynamic>):Dynamic;
+
 	/**
 		Generate `new cl(...args)` expression.
 	**/