Browse Source

[js][lua] @:expose static vars (closes #8710)

Aleksandr Kuzmenko 6 years ago
parent
commit
a69fe95324
3 changed files with 30 additions and 6 deletions
  1. 5 3
      src/generators/genjs.ml
  2. 7 3
      src/generators/genlua.ml
  3. 18 0
      tests/unit/src/unit/issues/Issue8710.hx

+ 5 - 3
src/generators/genjs.ml

@@ -52,7 +52,7 @@ type ctx = {
 	has_interface_check : bool;
 	has_interface_check : bool;
 	es_version : int;
 	es_version : int;
 	mutable current : tclass;
 	mutable current : tclass;
-	mutable statics : (tclass * string * texpr) list;
+	mutable statics : (tclass * tclass_field * texpr) list;
 	mutable inits : texpr list;
 	mutable inits : texpr list;
 	mutable tabs : string;
 	mutable tabs : string;
 	mutable in_value : tvar option;
 	mutable in_value : tvar option;
@@ -1042,7 +1042,7 @@ let gen_class_static_field ctx c f =
 			gen_value ctx e;
 			gen_value ctx e;
 			newline ctx;
 			newline ctx;
 		| _ ->
 		| _ ->
-			ctx.statics <- (c,f.cf_name,e) :: ctx.statics
+			ctx.statics <- (c,f,e) :: ctx.statics
 
 
 let can_gen_class_field ctx = function
 let can_gen_class_field ctx = function
 	| { cf_expr = (None | Some { eexpr = TConst TNull }) } when not (has_feature ctx "Type.getInstanceFields") ->
 	| { cf_expr = (None | Some { eexpr = TConst TNull }) } when not (has_feature ctx "Type.getInstanceFields") ->
@@ -1446,7 +1446,9 @@ let generate_enum ctx e =
 	flush ctx
 	flush ctx
 
 
 let generate_static ctx (c,f,e) =
 let generate_static ctx (c,f,e) =
-	print ctx "%s%s = " (s_path ctx c.cl_path) (static_field ctx c f);
+	let dot_path = (dot_path c.cl_path) ^ (static_field ctx c f.cf_name) in
+	(match (get_exposed ctx dot_path f.cf_meta) with [s] -> print ctx "$hx_exports%s = " (path_to_brackets s) | _ -> ());
+	print ctx "%s%s = " (s_path ctx c.cl_path) (static_field ctx c f.cf_name);
 	gen_value ctx e;
 	gen_value ctx e;
 	newline ctx
 	newline ctx
 
 

+ 7 - 3
src/generators/genlua.ml

@@ -33,7 +33,7 @@ type ctx = {
     buf : Buffer.t;
     buf : Buffer.t;
     packages : (string list,unit) Hashtbl.t;
     packages : (string list,unit) Hashtbl.t;
     mutable current : tclass;
     mutable current : tclass;
-    mutable statics : (tclass * string * texpr) list;
+    mutable statics : (tclass * tclass_field * texpr) list;
     mutable inits : texpr list;
     mutable inits : texpr list;
     mutable tabs : string;
     mutable tabs : string;
     mutable in_value : tvar option;
     mutable in_value : tvar option;
@@ -1541,7 +1541,7 @@ let gen_class_static_field ctx c f =
             newline ctx;
             newline ctx;
             (match (get_exposed ctx dot_path f.cf_meta) with [s] -> (print ctx "_hx_exports%s = %s" (path_to_brackets s) path; newline ctx) | _ -> ());
             (match (get_exposed ctx dot_path f.cf_meta) with [s] -> (print ctx "_hx_exports%s = %s" (path_to_brackets s) path; newline ctx) | _ -> ());
         | _ ->
         | _ ->
-            ctx.statics <- (c,f.cf_name,e) :: ctx.statics
+            ctx.statics <- (c,f,e) :: ctx.statics
 
 
 let gen_class_field ctx c f =
 let gen_class_field ctx c f =
     let p = s_path ctx c.cl_path in
     let p = s_path ctx c.cl_path in
@@ -1769,9 +1769,13 @@ let generate_enum ctx e =
     end
     end
 
 
 let generate_static ctx (c,f,e) =
 let generate_static ctx (c,f,e) =
-    print ctx "%s%s = " (s_path ctx c.cl_path) (field f);
+    let dot_path = (dot_path c.cl_path) ^ (static_field c f.cf_name)
+    and path = (s_path ctx c.cl_path) ^ (field f.cf_name) in
+    print ctx "%s = " path;
     gen_value ctx e;
     gen_value ctx e;
     semicolon ctx;
     semicolon ctx;
+    newline ctx;
+    (match (get_exposed ctx dot_path f.cf_meta) with [s] -> (print ctx "_hx_exports%s = %s" (path_to_brackets s) path; semicolon ctx) | _ -> ());
     newline ctx
     newline ctx
 
 
 let generate_enumMeta_fields ctx = function
 let generate_enumMeta_fields ctx = function

+ 18 - 0
tests/unit/src/unit/issues/Issue8710.hx

@@ -0,0 +1,18 @@
+package unit.issues;
+
+class Issue8710 extends unit.Test {
+#if (js || lua)
+	@:expose('exposed')
+	static var field = 10 + Std.random(1);
+
+	function test() {
+		var actual =
+			#if js
+				js.Syntax.code("$hx_exports[\"exposed\"]");
+			#elseif lua
+				untyped __lua__("_hx_exports[\"exposed\"]");
+			#end
+		eq(10, actual);
+	}
+#end
+}