浏览代码

[js] support js require

Dan Korostelev 11 年之前
父节点
当前提交
80b28977d6
共有 3 个文件被更改,包括 25 次插入1 次删除
  1. 1 0
      ast.ml
  2. 1 0
      common.ml
  3. 23 1
      genjs.ml

+ 1 - 0
ast.ml

@@ -89,6 +89,7 @@ module Meta = struct
 		| Internal
 		| Internal
 		| IsVar
 		| IsVar
 		| JavaNative
 		| JavaNative
+		| JsRequire
 		| Keep
 		| Keep
 		| KeepInit
 		| KeepInit
 		| KeepSub
 		| KeepSub

+ 1 - 0
common.ml

@@ -391,6 +391,7 @@ module MetaInfo = struct
 		| Meta.Internal -> ":internal",("Generates the annotated field/class with 'internal' access",[Platforms [Java;Cs]; UsedOnEither[TClass;TEnum;TClassField]])
 		| Meta.Internal -> ":internal",("Generates the annotated field/class with 'internal' access",[Platforms [Java;Cs]; UsedOnEither[TClass;TEnum;TClassField]])
 		| IsVar -> ":isVar",("Forces a physical field to be generated for properties that otherwise would not require one",[UsedOn TClassField])
 		| IsVar -> ":isVar",("Forces a physical field to be generated for properties that otherwise would not require one",[UsedOn TClassField])
 		| JavaNative -> ":javaNative",("Automatically added by -java-lib on classes generated from JAR/class files",[Platform Java; UsedOnEither[TClass;TEnum]; Internal])
 		| JavaNative -> ":javaNative",("Automatically added by -java-lib on classes generated from JAR/class files",[Platform Java; UsedOnEither[TClass;TEnum]; Internal])
+		| JsRequire -> ":jsRequire",("Generate javascript module require expression for given extern",[Platform Js; UsedOn TClass])
 		| Keep -> ":keep",("Causes a field or type to be kept by DCE",[])
 		| Keep -> ":keep",("Causes a field or type to be kept by DCE",[])
 		| KeepInit -> ":keepInit",("Causes a class to be kept by DCE even if all its field are removed",[UsedOn TClass])
 		| KeepInit -> ":keepInit",("Causes a class to be kept by DCE even if all its field are removed",[UsedOn TClass])
 		| KeepSub -> ":keepSub",("Extends @:keep metadata to all implementing and extending classes",[UsedOn TClass])
 		| KeepSub -> ":keepSub",("Extends @:keep metadata to all implementing and extending classes",[UsedOn TClass])

+ 23 - 1
genjs.ml

@@ -1105,6 +1105,25 @@ let generate_static ctx (c,f,e) =
 	gen_value ctx e;
 	gen_value ctx e;
 	newline ctx
 	newline ctx
 
 
+let generate_require ctx c =
+	let _, args, mp = Meta.get Meta.JsRequire c.cl_meta in
+	let p = (s_path ctx c.cl_path) in
+
+	if ctx.js_flatten then
+		spr ctx "var "
+	else
+		generate_package_create ctx c.cl_path;
+
+	(match args with
+	| [(EConst(String(module_name)),_)] ->
+		print ctx "%s = require(\"%s\")" p module_name
+	| [(EConst(String(module_name)),_) ; (EConst(String(object_path)),_)] ->
+		print ctx "%s = require(\"%s\").%s" p module_name object_path
+	| _ ->
+		error "Unsupported @:jsRequire format" mp);
+
+	newline ctx
+
 let generate_type ctx = function
 let generate_type ctx = function
 	| TClassDecl c ->
 	| TClassDecl c ->
 		(match c.cl_init with
 		(match c.cl_init with
@@ -1116,6 +1135,8 @@ let generate_type ctx = function
 		if p = "Math" then generate_class___name__ ctx c;
 		if p = "Math" then generate_class___name__ ctx c;
 		if not c.cl_extern then
 		if not c.cl_extern then
 			generate_class ctx c
 			generate_class ctx c
+		else if Meta.has Meta.JsRequire c.cl_meta then
+			generate_require ctx c
 		else if not ctx.js_flatten && Meta.has Meta.InitPackage c.cl_meta then
 		else if not ctx.js_flatten && Meta.has Meta.InitPackage c.cl_meta then
 			(match c.cl_path with
 			(match c.cl_path with
 			| ([],_) -> ()
 			| ([],_) -> ()
@@ -1161,7 +1182,8 @@ let alloc_ctx com =
 	ctx.type_accessor <- (fun t ->
 	ctx.type_accessor <- (fun t ->
 		let p = t_path t in
 		let p = t_path t in
 		match t with
 		match t with
-		| TClassDecl { cl_extern = true }
+		| TClassDecl ({ cl_extern = true } as c) when not (Meta.has Meta.JsRequire c.cl_meta)
+			-> dot_path p
 		| TEnumDecl { e_extern = true }
 		| TEnumDecl { e_extern = true }
 			-> dot_path p
 			-> dot_path p
 		| _ -> s_path ctx p);
 		| _ -> s_path ctx p);