浏览代码

added JSGenApi.setCurrentClass

Nicolas Cannasse 13 年之前
父节点
当前提交
d1a2971ce5
共有 4 个文件被更改,包括 14 次插入15 次删除
  1. 5 13
      genjs.ml
  2. 3 2
      std/haxe/macro/ExampleJSGenerator.hx
  3. 2 0
      std/haxe/macro/JSGenApi.hx
  4. 4 0
      typer.ml

+ 5 - 13
genjs.ml

@@ -50,7 +50,6 @@ type ctx = {
 	mutable in_loop : bool;
 	mutable handle_break : bool;
 	mutable id_counter : int;
-	mutable curmethod : (string * bool);
 	mutable type_accessor : module_type -> string;
 	mutable separator : bool;
 }
@@ -293,7 +292,7 @@ let rec gen_call ctx e el =
 	match e.eexpr , el with
 	| TConst TSuper , params ->
 		(match ctx.current.cl_super with
-		| None -> error "Missing setDebugInfos current class" e.epos
+		| None -> error "Missing api.setCurrentClass" e.epos
 		| Some (c,_) ->
 			print ctx "%s.call(%s" (ctx.type_accessor (TClassDecl c)) (this ctx);
 			List.iter (fun p -> print ctx ","; gen_value ctx p) params;
@@ -301,7 +300,7 @@ let rec gen_call ctx e el =
 		);
 	| TField ({ eexpr = TConst TSuper },name) , params ->
 		(match ctx.current.cl_super with
-		| None -> error "Missing setDebugInfos current class" e.epos
+		| None -> error "Missing api.setCurrentClass" e.epos
 		| Some (c,_) ->
 			print ctx "%s.prototype%s.call(%s" (ctx.type_accessor (TClassDecl c)) (field name) (this ctx);
 			List.iter (fun p -> print ctx ","; gen_value ctx p) params;
@@ -404,16 +403,10 @@ and gen_expr ctx e =
 		print ctx "}";
 	| TFunction f ->
 		let old = ctx.in_value, ctx.in_loop in
-		let old_meth = ctx.curmethod in
 		ctx.in_value <- None;
 		ctx.in_loop <- false;
-		if snd ctx.curmethod then
-			ctx.curmethod <- (fst ctx.curmethod ^ "@" ^ string_of_int (Lexer.get_error_line e.epos), true)
-		else
-			ctx.curmethod <- (fst ctx.curmethod, true);
 		print ctx "function(%s) " (String.concat "," (List.map ident (List.map arg_name f.tf_args)));
 		gen_expr ctx (fun_block ctx f e.epos);
-		ctx.curmethod <- old_meth;
 		ctx.in_value <- fst old;
 		ctx.in_loop <- snd old;
 		ctx.separator <- true
@@ -814,7 +807,6 @@ let gen_class_static_field ctx c f =
 	| Some e ->
 		match e.eexpr with
 		| TFunction _ ->
-			ctx.curmethod <- (f.cf_name,false);
 			ctx.id_counter <- 0;
 			print ctx "%s%s = " (s_path ctx c.cl_path) (field f.cf_name);
 			gen_value ctx e;
@@ -831,14 +823,12 @@ let gen_class_field ctx c f =
 	| None ->
 		print ctx "null";
 	| Some e ->
-		ctx.curmethod <- (f.cf_name,false);
 		ctx.id_counter <- 0;
 		gen_value ctx e;
 		ctx.separator <- false
 
 let generate_class ctx c =
 	ctx.current <- c;
-	ctx.curmethod <- ("new",false);
 	ctx.id_counter <- 0;
 	(match c.cl_path with
 	| [],"Function" -> error "This class redefine a native one" c.cl_pos
@@ -953,6 +943,9 @@ let generate_type ctx = function
 	| TEnumDecl e -> generate_enum ctx e
 	| TTypeDecl _ -> ()
 
+let set_current_class ctx c =
+	ctx.current <- c
+
 let alloc_ctx com =
 	let ctx = {
 		com = com;
@@ -978,7 +971,6 @@ let alloc_ctx com =
 		in_loop = false;
 		handle_break = false;
 		id_counter = 0;
-		curmethod = ("",false);
 		type_accessor = (fun _ -> assert false);
 		separator = false;
 	} in

+ 3 - 2
std/haxe/macro/ExampleJSGenerator.hx

@@ -27,7 +27,7 @@ import haxe.macro.Type;
 import haxe.macro.Expr;
 using Lambda;
 
-class DefaultJSGenerator {
+class ExampleJSGenerator {
 
 	var api : JSGenApi;
 	var buf : StringBuf;
@@ -135,6 +135,7 @@ class DefaultJSGenerator {
 
 	function genClass( c : ClassType ) {
 		genPackage(c.pack);
+		api.setCurrentClass(c);
 		var p = getPath(c);
 		fprint("$p = ");
 		if( c.constructor != null )
@@ -257,7 +258,7 @@ class DefaultJSGenerator {
 
 	#if macro
 	public static function use() {
-		Compiler.setCustomJSGenerator(function(api) new DefaultJSGenerator(api).generate());
+		Compiler.setCustomJSGenerator(function(api) new ExampleJSGenerator(api).generate());
 	}
 	#end
 

+ 2 - 0
std/haxe/macro/JSGenApi.hx

@@ -47,4 +47,6 @@ typedef JSGenApi = {
 	function quoteString( s : String ) : String;
 	/** create the metadata expression for the given type **/
 	function buildMetaData( t : BaseType ) : Null<TypedExpr>;
+	/** select the current classe **/
+	function setCurrentClass( c : ClassType ) : Void;
 }

+ 4 - 0
typer.ml

@@ -2354,6 +2354,10 @@ let make_macro_api ctx p =
 						);
 						Interp.VNull
 					));
+					"setCurrentClass", Interp.VFunction (Interp.Fun1 (fun c ->
+						Genjs.set_current_class js_ctx (match Interp.decode_tdecl c with TClassDecl c -> c | _ -> assert false);
+						Interp.VNull
+					));
 				] in
 				let t = macro_timer ctx "jsGenerator" in
 				gen jsctx;