From 94b8810ef189ae1659e3b57f4dfd2d14a8c808d7 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 3 Jan 2025 14:35:13 +0200 Subject: [PATCH] fix and cleanup --- examples/hot_reload/message.v | 8 +++----- vlib/v/gen/c/live.v | 4 ++++ vlib/v/live/sharedlib/live_sharedlib.v | 14 +++++++++++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/hot_reload/message.v b/examples/hot_reload/message.v index 6f53f32cc83fbb..7e619431a56684 100644 --- a/examples/hot_reload/message.v +++ b/examples/hot_reload/message.v @@ -12,12 +12,10 @@ mut: @[live] fn print_message(mut app App) { - info := live.info() - println('1 OK reloads: ${info.reloads_ok:4d} | Total reloads: ${info.reloads:4d} | Hello! Modify this message while the program is running.') - eprintln('>> app: ${voidptr(app)} | g_live_reload_info: ${voidptr(g_live_reload_info)}') - app.x = 341 // try changing this to another value, while the program is running ... + i := live.info() + println('OK reloads: ${i.reloads_ok:4d} | Total reloads: ${i.reloads:4d} | ${voidptr(i)} Hello! Modify this message while the program is running. app: ${app}') + app.x = 2 // try changing this to another value, while the program is running ... app.counter++ - dump(app) } fn main() { diff --git a/vlib/v/gen/c/live.v b/vlib/v/gen/c/live.v index 611c3cc025c94b..c7383b3dbf17de 100644 --- a/vlib/v/gen/c/live.v +++ b/vlib/v/gen/c/live.v @@ -50,6 +50,10 @@ fn (mut g Gen) generate_hotcode_reloader_code() { } phd = windows_hotcode_definitions_1 } + // Ensure that g_live_reload_info from the executable is passed to the DLL/SO . + // See also vlib/v/live/sharedlib/live_sharedlib.v . + load_code << 'void (* fn_set_live_reload_pointer)(void *) = (void *)GetProcAddress(live_lib, "set_live_reload_pointer");' + load_code << 'if(fn_set_live_reload_pointer){ fn_set_live_reload_pointer( g_live_reload_info ); }' g.hotcode_definitions.writeln(phd.replace('@LOAD_FNS@', load_code.join('\n'))) } } diff --git a/vlib/v/live/sharedlib/live_sharedlib.v b/vlib/v/live/sharedlib/live_sharedlib.v index 2a79e84041fe7b..819445235104f9 100644 --- a/vlib/v/live/sharedlib/live_sharedlib.v +++ b/vlib/v/live/sharedlib/live_sharedlib.v @@ -2,6 +2,18 @@ module sharedlib import v.live as _ +@[export: 'set_live_reload_pointer'] +@[markused] pub fn set_live_reload_pointer(p voidptr) { - eprintln('> set_live_reload_pointer, p: ${p}') + // NOTE: the `g_live_reload_info` global on windows, in the DLL, has a different address itself, + // compared to the g_live_reload_info in the main executable. + // + // The code here, ensures that *its value* will be the same, + // since the executable, will make sure to load the DLL, and then call set_live_reload_pointer() + // after binding it, in its generaged `v_bind_live_symbols`, with the value of its own `g_live_reload_info` global. + // + // This is not necessary on macos and linux, but it is best to have the same code across systems anyway. + // eprintln('>>>>> before &g_live_reload_info: ${voidptr(&g_live_reload_info)} | g_live_reload_info: ${voidptr(g_live_reload_info)}') + g_live_reload_info = p + // eprintln('>>>>> after &g_live_reload_info: ${voidptr(&g_live_reload_info)} | g_live_reload_info: ${voidptr(g_live_reload_info)}') }