From d4cadb5dc1e7af4aaa576a0741fa7204c8732be1 Mon Sep 17 00:00:00 2001 From: Christoph Strehle Date: Mon, 10 Jun 2024 13:22:19 +0200 Subject: [PATCH] Fix emscripten config Leaving the emscripten macros returning a value empty was leading to syntax errors The pointer returned by `EM_ASM_PTR` must be freed by calling `free` Add emscripten without a syntax check for now as we would need the emscripten tool chain --- cfg/emscripten.cfg | 116 ++++++++++++++++++++++++++++++++++++---- test/CMakeLists.txt | 1 + test/cfg/emscripten.cpp | 91 +++++++++++++++++++++++++++++++ test/cfg/runtests.sh | 11 ++++ tools/donate_cpu_lib.py | 3 +- 5 files changed, 210 insertions(+), 12 deletions(-) create mode 100644 test/cfg/emscripten.cpp diff --git a/cfg/emscripten.cfg b/cfg/emscripten.cfg index 0d6479150bc..7a5c5903cf5 100644 --- a/cfg/emscripten.cfg +++ b/cfg/emscripten.cfg @@ -1,14 +1,108 @@ - - - - - - - - - - - + + + + + + + + + + + false + + + + + + + + + + + + false + + + + + + + + + + + + false + + + + + + + + + + + false + + + + + + + + + + + + false + + + + + + + + + + + + false + + + + + + + + + + + + false + + + emscripten_asm_const_ptr + emscripten_asm_const_ptr_sync_on_main_thread + free + + + + + + + + + + + + + + + + + + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a92ef8ed166..58af2900ab0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -138,6 +138,7 @@ if (BUILD_TESTS) add_cfg(bsd.c) add_cfg(cairo.c) add_cfg(cppunit.cpp) + add_cfg(emscripten.cpp) # TODO: posix needs to specified first or it has a different mmap() config # TODO: get rid of posix dependency add_cfg(gnu.c ADD_LIBRARY posix) diff --git a/test/cfg/emscripten.cpp b/test/cfg/emscripten.cpp new file mode 100644 index 00000000000..9f4bf9a58e6 --- /dev/null +++ b/test/cfg/emscripten.cpp @@ -0,0 +1,91 @@ + +// Test library configuration for emscripten.cfg +// +// Usage: +// $ cppcheck --check-library --library=emscripten --enable=style,information --inconclusive --error-exitcode=1 --disable=missingInclude --inline-suppr test/cfg/emscripten.cpp +// => +// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0 +// + +#include +#include + +void em_asm_test() +{ + // inline some JavaScript + EM_ASM(alert('hello');); + MAIN_THREAD_EM_ASM(alert('hello main thread');); + + // pass parameters to JavaScript + EM_ASM( + { + console.log('I received: ' + [$0, $1]); + }, + 100, 35.5); + + // pass a string to JavaScript + EM_ASM({console.log('hello ' + UTF8ToString($0))}, "world!"); +} + +void em_asm_int_test() +{ + // cppcheck-suppress unreadVariable + const int x = EM_ASM_INT({ + return $0 + 42; + }, 100); + + // cppcheck-suppress unreadVariable + const int y = MAIN_THREAD_EM_ASM_INT({return 2;}); +} + +void em_asm_double_test() +{ + // cppcheck-suppress unreadVariable + const double x = EM_ASM_DOUBLE({ + return $0 + 1.0; + }, 2.0); + + // cppcheck-suppress unreadVariable + const double y = MAIN_THREAD_EM_ASM_DOUBLE({return 1.0;}); +} + +void em_asm_ptr_test() +{ + void* ptr = EM_ASM_PTR({ + return stringToNewUTF8("Hello"); + }); + printf("%s", static_cast(ptr)); + free(ptr); +} + +void em_asm_ptr_memleak_test() +{ + const char *str = static_cast(EM_ASM_PTR({ + return stringToNewUTF8("Hello"); + })); + printf("%s", str); + + // cppcheck-suppress memleak +} + +void main_thread_em_asm_ptr_test() +{ + // cppcheck-suppress leakReturnValNotUsed + MAIN_THREAD_EM_ASM_PTR( + return stringToNewUTF8("Hello"); + ); +} + +EM_JS(void, two_alerts, (), { + alert('hai'); + alert('bai'); + }); +EM_JS(void, take_args, (int x, float y), { + console.log('I received: ' + [x, y]); + }); + +void em_js_test() +{ + two_alerts(); + take_args(100, 35.5); +} \ No newline at end of file diff --git a/test/cfg/runtests.sh b/test/cfg/runtests.sh index 7169886eb70..64a18cd5bd6 100755 --- a/test/cfg/runtests.sh +++ b/test/cfg/runtests.sh @@ -447,6 +447,13 @@ function cppunit_fn { fi } +# emscripten.cpp +function emscripten_fn { + # TODO: Syntax check via g++ does not work because it can not find a valid emscripten.h + # ${CXX} "${CXX_OPT[@]}" ${DIR}emscripten.cpp + true +} + function check_file { f=$(basename "$1") lib="${f%%.*}" @@ -467,6 +474,10 @@ function check_file { cppunit_fn "${CPPCHECK}" "${CPPCHECK_OPT[@]}" --library="$lib" "${DIR}""$f" ;; + emscripten.cpp) + emscripten_fn + "${CPPCHECK}" "${CPPCHECK_OPT[@]}" --library="$lib" "${DIR}""$f" + ;; gnu.c) gnu_fn # TODO: posix needs to specified first or it has a different mmap() config diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index a2a2aaec994..d721914476e 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -16,7 +16,7 @@ # Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/ # Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic # changes) -CLIENT_VERSION = "1.3.59" +CLIENT_VERSION = "1.3.60" # Timeout for analysis with Cppcheck in seconds CPPCHECK_TIMEOUT = 30 * 60 @@ -681,6 +681,7 @@ def __init__(self): 'bsd': ['', '', '','', '', '', ''], 'cairo': [''], 'cppunit': [''], 'icu': [''],