Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want a way to initialize hxbit at the right time in C# #47

Open
kyubuns opened this issue May 26, 2020 · 3 comments
Open

I want a way to initialize hxbit at the right time in C# #47

kyubuns opened this issue May 26, 2020 · 3 comments

Comments

@kyubuns
Copy link

kyubuns commented May 26, 2020

I'm having trouble with the "Too late to register class" error.

#24
HaxeFoundation/haxe#4091
HaxeFoundation/haxe#7561

The answer is "Haxe's problem", but Haxe doesn't seem to have any plans to address it either.
Could you please provide a way for all classes to be initialized when you create the Serializer for hxbit, or manually initialize hxbit?

@kyubuns kyubuns changed the title I want a way to initialize it at the right time in C# I want a way to initialize hxbit at the right time in C# May 26, 2020
@ncannasse
Copy link
Member

Right, I guess we could add some support for this in hxbit, or another way would be a way to "preload" (and thus force statics init) some classes in C#, I'm not sure how this would work since I'm not familiar with the platform.

@kyubuns
Copy link
Author

kyubuns commented May 27, 2020

Thanks for the reply!
In my opinion (I'm using C#), user calls hxbit.Serializer.init(), or initialize when the Serializer is created for the first time, it would be less likely to cause unintelligible errors than force calling static init of C# class.
(I don't know Java. Sorry.)

I'm going to post the code I want to work on for your reference.

class SerializeTest {
	static function main() {
		var serializer = new hxbit.Serializer();

		var a = new Hoge(5);
		trace('a = $a');

		var serializedA = serializer.serialize(a);
		trace('serializedA = ${serializedA.toHex()}');

		var b = serializer.unserialize(serializedA, Hoge);
		trace('b = $b');

		var serializedB = serializer.serialize(b);
		trace('serializedB = ${serializedB.toHex()}');
	}
}

class Hoge implements hxbit.Serializable {
	public function new(x: Int) {
		this.x = x;
	}
	@:s public var x: Int;
}
haxe -main SerializeTest -lib hxbit --interp
SerializeTest.hx:6: a = Hoge
SerializeTest.hx:9: serializedA = 0105
SerializeTest.hx:12: b = Hoge
SerializeTest.hx:15: serializedB = 0105
haxe -main SerializeTest -lib hxbit -cs serialize_test_cs && mono ./serialize_test_cs/bin/SerializeTest.exe
haxelib run hxcs hxcs_build.txt --haxe-version 4005 --feature-level 1
Note: dmcs is deprecated, please use mcs instead!

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'Hoge' threw an exception. ---> haxe.lang.HaxeException: Too late to register class
  at hxbit.Serializer.registerClass (System.Type c) [0x00014] in <c0688911d66645dda3a6efebf799a0fc>:0
  at Hoge..cctor () [0x00000] in <c0688911d66645dda3a6efebf799a0fc>:0
   --- End of inner exception stack trace ---
  at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr)
  at SerializeTest.main () [0x00006] in <c0688911d66645dda3a6efebf799a0fc>:0
  at SerializeTest.Main () [0x00005] in <c0688911d66645dda3a6efebf799a0fc>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'Hoge' threw an exception. ---> haxe.lang.HaxeException: Too late to register class
  at hxbit.Serializer.registerClass (System.Type c) [0x00014] in <c0688911d66645dda3a6efebf799a0fc>:0
  at Hoge..cctor () [0x00000] in <c0688911d66645dda3a6efebf799a0fc>:0
   --- End of inner exception stack trace ---
  at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr)
  at SerializeTest.main () [0x00006] in <c0688911d66645dda3a6efebf799a0fc>:0
  at SerializeTest.Main () [0x00005] in <c0688911d66645dda3a6efebf799a0fc>:0

kyubuns added a commit to kyubuns/hxbitmini that referenced this issue Jun 1, 2020
@kyubuns
Copy link
Author

kyubuns commented Jun 1, 2020

I have tried to fix this problem, but this fix is not very good.
This is because the initialization cost in non-C# languages has increased and the number of dependent libraries has increased.
If anyone else is having trouble and is waiting for an official fix, please refer to it.
kyubuns@9ce35bd and kyubuns@a6fd33d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants