Просмотр исходного кода

[macro] changed Context.getResource() to actually return `Bytes` instances (fixes #9838)

Aleksandr Kuzmenko 5 лет назад
Родитель
Сommit
e0e1e19b56

+ 1 - 0
extra/CHANGES.txt

@@ -7,6 +7,7 @@
 	Bugfixes:
 
 	all : fixed `switch` typing error for arrow functions with `Void` return type (#9813)
+	macro : fixed `haxe.macro.Context.getResources()` (#9838)
 	php : fixed false detection of `catch` vars in anonymous functions as captured from outer scope
 	php : fixed return type of extern definition for `fseek` function
 	cs,java : fixed generation of `@:generic` classes with anonymous functions (#9799)

+ 3 - 1
src/macro/macroApi.ml

@@ -1757,7 +1757,9 @@ let macro_api ccom get_api =
 			vnull
 		);
 		"get_resources", vfun0 (fun() ->
-			encode_string_map encode_string (Hashtbl.fold (fun k v acc -> PMap.add k v acc) (ccom()).resources PMap.empty)
+			let pmap_resources = Hashtbl.fold (fun k v acc -> PMap.add k v acc) (ccom()).resources PMap.empty in
+			let encode_string_to_bytes s = encode_bytes (Bytes.of_string s) in
+			encode_string_map encode_string_to_bytes pmap_resources
 		);
 		"get_local_module", vfun0 (fun() ->
 			let m = (get_api()).current_module() in

+ 12 - 0
tests/misc/projects/Issue9838/Macro.hx

@@ -0,0 +1,12 @@
+import haxe.macro.Context;
+import haxe.io.Bytes;
+
+class Macro {
+	public static macro function include() {
+		Context.addResource("foo", Bytes.ofString("hi"));
+		if(!Std.isOfType(Context.getResources()["foo"], Bytes)) {
+			Context.error('Invalid resource encoding/decoding', (macro {}).pos);
+		}
+		return macro {};
+	}
+}

+ 1 - 0
tests/misc/projects/Issue9838/compile.hxml

@@ -0,0 +1 @@
+--macro Macro.include()