Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions system/include/emscripten/em_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,20 @@ const char __em_asm_sig_builder<__em_asm_type_tuple<Args...> >::buffer[] = { __e
#define EM_ASM_INT_V(code) EM_ASM_INT(code)
#define EM_ASM_DOUBLE_V(code) EM_ASM_DOUBLE(code)


// Normally macros like `true` and `false` are not expanded inside
// of `EM_JS` or `EM_ASM` blocks. However, in the case then an
// additional macro later is added these will be expanded and we want
// to make sure the resulting expansion doesn't break the expectations
// of JS code
#if defined(true) && defined(false)
#undef true
#undef false
// These work for both C and javascript.
// In C !!0 ==> 0 and in javascript !!0 ==> false
// In C !!1 ==> 1 and in javascript !!1 ==> true
#define true (!!1)
#define false (!!0)
#endif

#endif // !defined(__cplusplus) && defined(__STRICT_ANSI__)
16 changes: 16 additions & 0 deletions system/include/emscripten/em_js.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@

#define EM_ASYNC_JS(ret, name, params, ...) _EM_JS(ret, name, __asyncjs__##name, params, \
"{ return Asyncify.handleAsync(async () => " #__VA_ARGS__ "); }")


// Normally macros like `true` and `false` are not expanded inside
// of `EM_JS` or `EM_ASM` blocks. However, in the case then an
// additional macro later is added these will be expanded and we want
// to make sure the resulting expansion doesn't break the expectations
// of JS code
#if defined(true) && defined(false)
#undef true
#undef false
// These work for both C and javascript.
// In C !!0 ==> 0 and in javascript !!0 ==> false
// In C !!1 ==> 1 and in javascript !!1 ==> true
#define true (!!1)
#define false (!!0)
#endif
25 changes: 25 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -16060,3 +16060,28 @@ def test_getentropy_d8(self):
self.do_runf('main.c', msg, assert_returncode=1)
self.v8_args += ['--enable-os-system']
self.do_runf('main.c')

def test_em_js_bool_macro_expansion(self):
# Normally macros like `true` and `false` are not expanded inside
# of `EM_JS` or `EM_ASM` blocks. However, in the case then an
# additional macro later is added these will be expanded and we want
# to make sure the resulting expansion doesn't break the expectations
# of JS code.
create_file('main.c', '''
#include <emscripten.h>

#define EM_JS_MACROS(ret, func_name, args, body...) \
EM_JS(ret, func_name, args, body)

EM_JS_MACROS(void, check_bool_type, (void), {
if (typeof true !== "boolean") {
throw new Error("typeof true is " + typeof true + " not boolean");
}
})

int main() {
check_bool_type();
return 0;
}
''')
self.do_runf('main.c')