compiler: streamline main function handling

* compiler: streamline C main function generation

* fix most tests

* compiler: fix for 'go update()' in graph.v . More precise parser error messages.

* Fix temporarily examples/hot_reload/message.v by using os inside it (os.clear).

* Make graph.v easier to quickly modify by defining y outside the loop.

* Fix failure of /v/nv/compiler/tests/defer_test.v when run with 'v -g' (#line directive was not on its own line, but right after } ).

* Do not pass the os.args to tests, even if the tests import os (they are more stable when run in a controlled environment).

* fix declared and not used in the js backend.

* fix js main => main__main too.
This commit is contained in:
Delyan Angelov 2019-09-28 20:42:29 +03:00 committed by Alexander Medvednikov
parent 0160c7a89d
commit a4cbe78d97
10 changed files with 141 additions and 92 deletions

View file

@ -1,9 +1,10 @@
module main
import os
import time
fn (v &V) generate_hotcode_reloading_compiler_flags() []string {
mut a := []string
mut a := []string
if v.pref.is_live || v.pref.is_so {
// See 'man dlopen', and test running a GUI program compiled with -live
if (v.os == .linux || os.user_os() == 'linux'){
@ -13,11 +14,11 @@ fn (v &V) generate_hotcode_reloading_compiler_flags() []string {
a << '-flat_namespace'
}
}
return a
return a
}
fn (v &V) generate_hotcode_reloading_declarations() {
mut cgen := v.cgen
mut cgen := v.cgen
if v.os != .windows && v.os != .msvc {
if v.pref.is_so {
cgen.genln('pthread_mutex_t live_fn_mutex;')
@ -35,9 +36,33 @@ fn (v &V) generate_hotcode_reloading_declarations() {
}
}
fn (v &V) generate_hot_reload_code() {
mut cgen := v.cgen
fn (v &V) generate_hotcode_reloading_main_caller() {
if !v.pref.is_live { return }
// We are in live code reload mode, so start the .so loader in the background
mut cgen := v.cgen
cgen.genln('')
file_base := os.filename(v.dir).replace('.v', '')
if !(v.os == .windows || v.os == .msvc) {
// unix:
so_name := file_base + '.so'
cgen.genln(' char *live_library_name = "$so_name";')
cgen.genln(' load_so(live_library_name);')
cgen.genln(' pthread_t _thread_so;')
cgen.genln(' pthread_create(&_thread_so , NULL, &reload_so, live_library_name);')
} else {
// windows:
so_name := file_base + if v.os == .msvc {'.dll'} else {'.so'}
cgen.genln(' char *live_library_name = "$so_name";')
cgen.genln(' live_fn_mutex = CreateMutexA(0, 0, 0);')
cgen.genln(' load_so(live_library_name);')
cgen.genln(' unsigned long _thread_so;')
cgen.genln(' _thread_so = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&reload_so, 0, 0, 0);')
}
}
fn (v &V) generate_hot_reload_code() {
mut cgen := v.cgen
// Hot code reloading
if v.pref.is_live {
mut file := os.realpath(v.dir)
@ -46,24 +71,33 @@ fn (v &V) generate_hot_reload_code() {
// Need to build .so file before building the live application
// The live app needs to load this .so file on initialization.
mut vexe := os.args[0]
if os.user_os() == 'windows' {
vexe = vexe.replace('\\', '\\\\')
file = file.replace('\\', '\\\\')
}
mut msvc := ''
if v.os == .msvc {
msvc = '-os msvc'
}
mut debug := ''
if v.pref.is_debug {
debug = '-debug'
}
os.system('$vexe $msvc $debug -o $file_base -shared $file')
cmd_compile_shared_library := '$vexe $msvc $debug -o $file_base -shared $file'
if v.pref.show_c_cmd {
println(cmd_compile_shared_library)
}
ticks := time.ticks()
os.system(cmd_compile_shared_library)
diff := time.ticks() - ticks
println('compiling shared library took $diff ms')
println('=========\n')
cgen.genln('
void lfnmutex_print(char *s){