Skip to content

Commit fb2ea4d

Browse files
authored
Change definition of true and false macros to be js friendly (#24255)
1 parent edd1561 commit fb2ea4d

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

system/include/emscripten/em_asm.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,20 @@ const char __em_asm_sig_builder<__em_asm_type_tuple<Args...> >::buffer[] = { __e
285285
#define EM_ASM_INT_V(code) EM_ASM_INT(code)
286286
#define EM_ASM_DOUBLE_V(code) EM_ASM_DOUBLE(code)
287287

288+
289+
// Normally macros like `true` and `false` are not expanded inside
290+
// of `EM_JS` or `EM_ASM` blocks. However, in the case then an
291+
// additional macro later is added these will be expanded and we want
292+
// to make sure the resulting expansion doesn't break the expectations
293+
// of JS code
294+
#if defined(true) && defined(false)
295+
#undef true
296+
#undef false
297+
// These work for both C and javascript.
298+
// In C !!0 ==> 0 and in javascript !!0 ==> false
299+
// In C !!1 ==> 1 and in javascript !!1 ==> true
300+
#define true (!!1)
301+
#define false (!!0)
302+
#endif
303+
288304
#endif // !defined(__cplusplus) && defined(__STRICT_ANSI__)

system/include/emscripten/em_js.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,19 @@
7272

7373
#define EM_ASYNC_JS(ret, name, params, ...) _EM_JS(ret, name, __asyncjs__##name, params, \
7474
"{ return Asyncify.handleAsync(async () => " #__VA_ARGS__ "); }")
75+
76+
77+
// Normally macros like `true` and `false` are not expanded inside
78+
// of `EM_JS` or `EM_ASM` blocks. However, in the case then an
79+
// additional macro later is added these will be expanded and we want
80+
// to make sure the resulting expansion doesn't break the expectations
81+
// of JS code
82+
#if defined(true) && defined(false)
83+
#undef true
84+
#undef false
85+
// These work for both C and javascript.
86+
// In C !!0 ==> 0 and in javascript !!0 ==> false
87+
// In C !!1 ==> 1 and in javascript !!1 ==> true
88+
#define true (!!1)
89+
#define false (!!0)
90+
#endif

test/test_other.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16062,3 +16062,28 @@ def test_getentropy_d8(self):
1606216062
self.do_runf('main.c', msg, assert_returncode=1)
1606316063
self.v8_args += ['--enable-os-system']
1606416064
self.do_runf('main.c')
16065+
16066+
def test_em_js_bool_macro_expansion(self):
16067+
# Normally macros like `true` and `false` are not expanded inside
16068+
# of `EM_JS` or `EM_ASM` blocks. However, in the case then an
16069+
# additional macro later is added these will be expanded and we want
16070+
# to make sure the resulting expansion doesn't break the expectations
16071+
# of JS code.
16072+
create_file('main.c', '''
16073+
#include <emscripten.h>
16074+
16075+
#define EM_JS_MACROS(ret, func_name, args, body...) \
16076+
EM_JS(ret, func_name, args, body)
16077+
16078+
EM_JS_MACROS(void, check_bool_type, (void), {
16079+
if (typeof true !== "boolean") {
16080+
throw new Error("typeof true is " + typeof true + " not boolean");
16081+
}
16082+
})
16083+
16084+
int main() {
16085+
check_bool_type();
16086+
return 0;
16087+
}
16088+
''')
16089+
self.do_runf('main.c')

0 commit comments

Comments
 (0)