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

Hot Reload: fix new global not initialized #725

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yuxiaomao
Copy link
Collaborator

@yuxiaomao yuxiaomao commented Nov 5, 2024

In the following code based on #145 , uncomment trace after launch will

  • successfully print once after the first reload (4 changes detected: modified foo, entrypoint, new haxe.Log.formatOutput, haxe.Log.trace),
    • note that if we do not run any hl.Api.checkReload after the first reload, the program runs correctly.
  • then read access violation after the second reload (2 changes detected, even no change on hl bytecode: new haxe.Log.formatOutput, haxe.Log.trace) at the beginning of foo(), probably global 2, 9 field 1,2[6] with global 9: haxe.$Log.
class Main {
	static var g1 = 0;
	static function foo() {
		// haxe.Log.trace("additional print"); // <- after launch, uncomment this line and save
		return 10000 + g1++;
	}

	static function reload() {
		Sys.sleep(1); // make sure timestamp is different
		Sys.command("haxe",["-hl","main.hl","-main","Main"]);
		Sys.println(hl.Api.checkReload() ? "Module Reloaded" : "Module not reloaded");
	}

	static function main() {
		while( true ) {
			var val = foo();
			Sys.println(val);
			reload();
		}
	}
}

When debugging, I saw that additional global values are forced reset in hl_module_patch every time memset(m2->globals_data+m1->globals_size,0,gsize - m1->globals_size) and are initialized by hl_module_init_constant(m2, c) and m2->functions_ptrs[c->entrypoint].
However, on the second reload, entrypoint function was not modified and is not called, leaving some global values to 0 (likely haxe.$Log, hl.CoreType, hl.CoreEnum).

This PR try to fix it by re-call the last entrypoint function.
(I think it does not work if no change is detected, for example if new functions are memorized by m1 in the future; but for now if a new type is added, there are likely some new functions)

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

Successfully merging this pull request may close these issues.

1 participant