Selaa lähdekoodia

- js : added --js-namespace to create a namespace where root types are defined

Franco Ponticelli 16 vuotta sitten
vanhempi
commit
1b9ee834f2
6 muutettua tiedostoa jossa 60 lisäystä ja 16 poistoa
  1. 2 0
      common.ml
  2. 1 0
      doc/CHANGES.txt
  3. 30 14
      genjs.ml
  4. 5 0
      main.ml
  5. 14 0
      std/Type.hx
  6. 8 2
      std/js/Boot.hx

+ 2 - 0
common.ml

@@ -64,6 +64,7 @@ type context = {
 	mutable package_rules : (string,package_rule) PMap.t;
 	mutable error : string -> pos -> unit;
 	mutable warning : string -> pos -> unit;
+	mutable js_namespace : string option;
 	(* output *)
 	mutable file : string;
 	mutable flash_version : int;
@@ -94,6 +95,7 @@ let create v =
 		flash_version = 8;
 		resources = Hashtbl.create 0;
 		php_front = None;
+		js_namespace = None;
 		warning = (fun _ _ -> assert false);
 		error = (fun _ _ -> assert false);
 		type_api = {

+ 1 - 0
doc/CHANGES.txt

@@ -2,6 +2,7 @@
 	all : allow -D noopt to turn off optimizer
 	js : added js.Scroll
 	js : package names are now checked at runtime to avoid clashes with existing libs
+	js : added --js-namespace to create a namespace for types that are defined in the root
 
 2009-07-26: 2.04
 	flash9 : fixed get_full_path error with -D fdb

+ 30 - 14
genjs.ml

@@ -24,6 +24,7 @@ type ctx = {
 	buf : Buffer.t;
 	packages : (string list,unit) Hashtbl.t;
 	stack : Codegen.stack_context;
+	namespace : string option;
 	mutable current : tclass;
 	mutable statics : (tclass * string * texpr) list;
 	mutable inits : texpr list;
@@ -35,8 +36,12 @@ type ctx = {
 	mutable curmethod : (string * bool);
 }
 
-let s_path = function
+let s_path ctx = function
 	| ([],"@Main") -> "$Main"
+	| ([],p) -> 
+		(match ctx.namespace with
+		| None -> p
+		| Some ns -> ns ^ "." ^ p)
 	| p -> Ast.s_type_path p
 
 let kwds =
@@ -144,7 +149,7 @@ let rec gen_call ctx e el =
 		(match ctx.current.cl_super with
 		| None -> assert false
 		| Some (c,_) ->
-			print ctx "%s.apply(%s,[" (s_path c.cl_path) (this ctx);
+			print ctx "%s.apply(%s,[" (s_path ctx c.cl_path) (this ctx);
 			concat ctx "," (gen_value ctx) params;
 			spr ctx "])";
 		);
@@ -152,7 +157,7 @@ let rec gen_call ctx e el =
 		(match ctx.current.cl_super with
 		| None -> assert false
 		| Some (c,_) ->
-			print ctx "%s.prototype%s.apply(%s,[" (s_path c.cl_path) (field name) (this ctx);
+			print ctx "%s.prototype%s.apply(%s,[" (s_path ctx c.cl_path) (field name) (this ctx);
 			concat ctx "," (gen_value ctx) params;
 			spr ctx "])";
 		);
@@ -206,7 +211,7 @@ and gen_expr ctx e =
 	| TConst c -> gen_constant ctx e.epos c
 	| TLocal s -> spr ctx (ident s)
 	| TEnumField (e,s) ->
-		print ctx "%s%s" (s_path e.e_path) (field s)
+		print ctx "%s%s" (s_path ctx e.e_path) (field s)
 	| TArray (e1,e2) ->
 		gen_value ctx e1;
 		spr ctx "[";
@@ -231,7 +236,7 @@ and gen_expr ctx e =
 		gen_constant ctx e.epos (TString s);
 		spr ctx ")";
 	| TTypeExpr t ->
-		spr ctx (s_path (t_path t))
+		spr ctx (s_path ctx (t_path t))
 	| TParenthesis e ->
 		spr ctx "(";
 		gen_value ctx e;
@@ -295,7 +300,7 @@ and gen_expr ctx e =
 				gen_value ctx e
 		) vl;
 	| TNew (c,_,el) ->
-		print ctx "new %s(" (s_path c.cl_path);
+		print ctx "new %s(" (s_path ctx c.cl_path);
 		concat ctx "," (gen_value ctx) el;
 		spr ctx ")"
 	| TIf (cond,e,eelse) ->
@@ -592,20 +597,20 @@ let generate_package_create ctx (p,_) =
 let gen_class_static_field ctx c f =
 	match f.cf_expr with
 	| None ->
-		print ctx "%s%s = null" (s_path c.cl_path) (field f.cf_name);
+		print ctx "%s%s = null" (s_path ctx c.cl_path) (field f.cf_name);
 		newline ctx
 	| Some e ->
 		match e.eexpr with
 		| TFunction _ ->
 			ctx.curmethod <- (f.cf_name,false);
-			print ctx "%s%s = " (s_path c.cl_path) (field f.cf_name);
+			print ctx "%s%s = " (s_path ctx c.cl_path) (field f.cf_name);
 			gen_value ctx e;
 			newline ctx
 		| _ ->
 			ctx.statics <- (c,f.cf_name,e) :: ctx.statics
 
 let gen_class_field ctx c f =
-	print ctx "%s.prototype%s = " (s_path c.cl_path) (field f.cf_name);
+	print ctx "%s.prototype%s = " (s_path ctx c.cl_path) (field f.cf_name);
 	match f.cf_expr with
 	| None ->
 		print ctx "null";
@@ -618,7 +623,7 @@ let gen_class_field ctx c f =
 let generate_class ctx c =
 	ctx.current <- c;
 	ctx.curmethod <- ("new",true);
-	let p = s_path c.cl_path in
+	let p = s_path ctx c.cl_path in
 	generate_package_create ctx c.cl_path;
 	print ctx "%s = " p;
 	(match c.cl_constructor with
@@ -638,7 +643,7 @@ let generate_class ctx c =
 	(match c.cl_super with
 	| None -> ()
 	| Some (csup,_) ->
-		let psup = s_path csup.cl_path in
+		let psup = s_path ctx csup.cl_path in
 		print ctx "%s.__super__ = %s" p psup;
 		newline ctx;
 		print ctx "for(var k in %s.prototype ) %s.prototype[k] = %s.prototype[k]" psup p psup;
@@ -651,11 +656,11 @@ let generate_class ctx c =
 	match c.cl_implements with
 	| [] -> ()
 	| l ->
-		print ctx "%s.__interfaces__ = [%s]" p (String.concat "," (List.map (fun (i,_) -> s_path i.cl_path) l));
+		print ctx "%s.__interfaces__ = [%s]" p (String.concat "," (List.map (fun (i,_) -> s_path ctx i.cl_path) l));
 		newline ctx
 
 let generate_enum ctx e =
-	let p = s_path e.e_path in
+	let p = s_path ctx e.e_path in
 	generate_package_create ctx e.e_path;
 	let ename = List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst e.e_path @ [snd e.e_path]) in
 	print ctx "%s = { __ename__ : [%s], __constructs__ : [%s] }" p (String.concat "," ename) (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" s) e.e_names));
@@ -677,7 +682,7 @@ let generate_enum ctx e =
 	) e.e_constrs
 
 let generate_static ctx (c,f,e) =
-	print ctx "%s%s = " (s_path c.cl_path) (field f);
+	print ctx "%s%s = " (s_path ctx c.cl_path) (field f);
 	gen_value ctx e;
 	newline ctx
 
@@ -698,6 +703,7 @@ let generate com =
 		stack = Codegen.stack_init com false;
 		buf = Buffer.create 16000;
 		packages = Hashtbl.create 0;
+		namespace = com.js_namespace;
 		statics = [];
 		inits = [];
 		current = null_class;
@@ -711,11 +717,21 @@ let generate com =
 	let t = Common.timer "generate js" in
 	print ctx "$estr = function() { return js.Boot.__string_rec(this,''); }";
 	newline ctx;
+	(match ctx.namespace with
+		| None -> ()
+		| Some ns -> 
+			print ctx "if(typeof %s=='undefined') %s = {}" ns ns;
+			newline ctx);
 	List.iter (generate_type ctx) com.types;
 	print ctx "$_ = {}";
 	newline ctx;
 	print ctx "js.Boot.__res = {}";
 	newline ctx;
+	(match ctx.namespace with
+		| None -> ()
+		| Some ns -> 
+			print ctx "js.Boot.__ns = '%s'" ns;
+			newline ctx);
 	if com.debug then begin
 		print ctx "%s = []" ctx.stack.Codegen.stack_var;
 		newline ctx;

+ 5 - 0
main.ml

@@ -400,6 +400,11 @@ try
 			if com.php_front <> None then raise (Arg.Bad "Multiple --php-front");
 			com.php_front <- Some f;
 		),"<filename> : select the name for the php front file");
+		("--js-namespace",Arg.String (fun f ->
+			if com.js_namespace <> None then raise (Arg.Bad "Multiple --js-namespace");
+			com.js_namespace <- Some f;
+			Common.define com "js_namespace";
+		),"<namespace> : create a namespace where root types are defined");
 		("--remap", Arg.String (fun s ->
 			let pack, target = (try ExtString.String.split s ":" with _ -> raise (Arg.Bad "Invalid format")) in
 			com.package_rules <- PMap.add pack (Remap target) com.package_rules;

+ 14 - 0
std/Type.hx

@@ -209,7 +209,14 @@ class Type {
 			cl = __eval__(name);
 		#elseif js
 			try {
+				#if js_namespace
+				if (name.indexOf('.') < 0)
+					cl = eval(js.Boot.__ns + '.' + name);
+				else
+					cl = eval(name);
+				#else
 				cl = eval(name);
+				#end
 			} catch( e : Dynamic ) {
 				cl = null;
 			}
@@ -261,7 +268,14 @@ class Type {
 			e = __eval__(name);
 		#elseif js
 			try {
+				#if js_namespace
+				if (name.indexOf('.') < 0)
+					e = eval(js.Boot.__ns + '.' + name);
+				else
+					e = eval(name);
+				#else
 				e = eval(name);
+				#end
 			} catch( err : Dynamic ) {
 				e = null;
 			}

+ 8 - 2
std/js/Boot.hx

@@ -193,8 +193,14 @@ class Boot {
 
 	private static function __init() {
 		untyped {
-			Lib.isIE = (document && document.all != null && window && window.opera == null );
-			Lib.isOpera = (window && window.opera != null );
+			Lib.isIE = (__js__("typeof document!='undefined'") && document.all != null && __js__("typeof window!='undefined'") && window.opera == null );
+			Lib.isOpera = (__js__("typeof window!='undefined'") && window.opera != null );
+#if js_namespace
+			__js__("eval(js.Boot.__ns).Array = Array");
+			__js__("eval(js.Boot.__ns).String = String");
+			__js__("eval(js.Boot.__ns).Math = Math");
+			__js__("eval(js.Boot.__ns).Date = Date");
+#end
 			Array.prototype.copy = Array.prototype.slice;
 			Array.prototype.insert = function(i,x) {
 				this.splice(i,0,x);