Skip to content

Commit 6f8c7ae

Browse files
committed
[wasm64] Update webidl to support wasm64
The changes to `test/webidl/post.js` make debugging easier since they don't silently swallow errors. I'm not sure why those try/catches where in there since all those calls are expected to succeed.
1 parent c782f8d commit 6f8c7ae

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

test/test_core.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -7627,7 +7627,6 @@ def test_embind_no_rtti_followed_by_rtti(self):
76277627
self.emcc_args += ['-lembind', '-fno-rtti', '-frtti']
76287628
self.do_run(src, '418\ndotest returned: 42\n')
76297629

7630-
@no_wasm64('webidl not compatible with MEMORY64 yet')
76317630
@parameterized({
76327631
'': ('DEFAULT', False),
76337632
'all': ('ALL', False),
@@ -7646,8 +7645,12 @@ def test_webidl(self, mode, allow_memory_growth):
76467645
self.set_setting('WASM_ASYNC_COMPILATION', 0)
76477646

76487647
# Force IDL checks mode
7648+
if self.is_wasm64():
7649+
args = ['--wasm64']
7650+
else:
7651+
args = []
76497652
with env_modify({'IDL_CHECKS': mode}):
7650-
self.run_process([WEBIDL_BINDER, test_file('webidl/test.idl'), 'glue'])
7653+
self.run_process([WEBIDL_BINDER, test_file('webidl/test.idl'), 'glue'] + args)
76517654
self.assertExists('glue.cpp')
76527655
self.assertExists('glue.js')
76537656

@@ -7667,7 +7670,7 @@ def test_webidl(self, mode, allow_memory_growth):
76677670

76687671
# Export things on "TheModule". This matches the typical use pattern of the bound library
76697672
# being used as Box2D.* or Ammo.*, and we cannot rely on "Module" being always present (closure may remove it).
7670-
self.emcc_args += ['-Wall', '--post-js=glue.js', '--extern-post-js=extern-post.js']
7673+
self.emcc_args += ['--post-js=glue.js', '--extern-post-js=extern-post.js']
76717674
if mode == 'ALL':
76727675
self.emcc_args += ['-sASSERTIONS']
76737676
if allow_memory_growth:

tools/emscripten.py

+2
Original file line numberDiff line numberDiff line change
@@ -893,12 +893,14 @@ def create_pointer_conversion_wrappers(metadata):
893893
'stackAlloc': 'pp',
894894
'emscripten_builtin_malloc': 'pp',
895895
'malloc': 'pp',
896+
'webidl_malloc': 'pp',
896897
'memalign': 'ppp',
897898
'memcmp': '_ppp',
898899
'memcpy': 'pppp',
899900
'__getTypeName': 'pp',
900901
'setThrew': '_p',
901902
'free': '_p',
903+
'webidl_free': '_p',
902904
'stackRestore': '_p',
903905
'__cxa_is_pointer_type': '_p',
904906
'stackSave': 'p',

tools/webidl_binder.py

+64-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
https://emscripten.org/docs/porting/connecting_cpp_and_javascript/WebIDL-Binder.html
99
"""
1010

11+
import argparse
1112
import os
1213
import sys
1314
from typing import List
@@ -55,8 +56,15 @@ def getExtendedAttribute(self, _name):
5556
return None
5657

5758

58-
input_file = sys.argv[1]
59-
output_base = sys.argv[2]
59+
parser = argparse.ArgumentParser()
60+
parser.add_argument('--wasm64', action='store_true', default=False,
61+
help='Build for wasm64')
62+
parser.add_argument('infile')
63+
parser.add_argument('outfile')
64+
options = parser.parse_args()
65+
66+
input_file = options.infile
67+
output_base = options.outfile
6068
cpp_output = output_base + '.cpp'
6169
js_output = output_base + '.js'
6270

@@ -415,6 +423,10 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
415423
call_postfix = ''
416424
if return_type != 'Void' and not constructor:
417425
call_prefix = 'return '
426+
427+
if options.wasm64 and (constructor or return_type in interfaces or return_type == 'String'):
428+
call_postfix += ')'
429+
418430
if not constructor:
419431
if return_type in interfaces:
420432
call_prefix += 'wrapPointer('
@@ -426,17 +438,28 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
426438
call_prefix += '!!('
427439
call_postfix += ')'
428440

441+
if options.wasm64 and (constructor or return_type in interfaces or return_type == 'String'):
442+
call_prefix += 'Number('
443+
444+
429445
args = [(all_args[i].identifier.name if isinstance(all_args[i], WebIDL.IDLArgument) else ('arg%d' % i)) for i in range(max_args)]
430446
if not constructor and not is_static:
431447
body = ' var self = this.ptr;\n'
432-
pre_arg = ['self']
448+
if options.wasm64:
449+
pre_arg = ['BigInt(self)']
450+
else:
451+
pre_arg = ['self']
433452
else:
434453
body = ''
435454
pre_arg = []
436455

437456
if any(arg.type.isString() or arg.type.isArray() for arg in all_args):
438457
body += ' ensureCache.prepare();\n'
439458

459+
def is_ptr_arg(i):
460+
t = all_args[i].type
461+
return (t.isArray() or t.isAny() or t.isString() or t.isObject() or t.isInterface())
462+
440463
for i, (js_arg, arg) in enumerate(zip(args, all_args)):
441464
if i >= min_args:
442465
optional = True
@@ -500,9 +523,11 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
500523

501524
if do_default:
502525
if not (arg.type.isArray() and not array_attribute):
503-
body += " if ({0} && typeof {0} === 'object') {0} = {0}.ptr;\n".format(js_arg)
526+
body += f" if ({js_arg} && typeof {js_arg} === 'object') {js_arg} = {js_arg}.ptr;\n"
504527
if arg.type.isString():
505528
body += " else {0} = ensureString({0});\n".format(js_arg)
529+
if options.wasm64 and is_ptr_arg(i):
530+
body += f' if ({args[i]} === null) {args[i]} = 0;\n'
506531
else:
507532
# an array can be received here
508533
arg_type = arg.type.name
@@ -517,18 +542,45 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
517542
elif arg_type == 'Double':
518543
body += " if (typeof {0} == 'object') {{ {0} = ensureFloat64({0}); }}\n".format(js_arg)
519544

545+
call_args = pre_arg
546+
547+
for i, arg in enumerate(args):
548+
if options.wasm64 and is_ptr_arg(i):
549+
arg = f'BigInt({arg})'
550+
call_args.append(arg)
551+
520552
c_names = {}
553+
554+
def make_call_args(i):
555+
if pre_arg:
556+
i += 1
557+
return ', '.join(call_args[:i])
558+
521559
for i in range(min_args, max_args):
522-
c_names[i] = 'emscripten_bind_%s_%d' % (bindings_name, i)
523-
body += ' if (%s === undefined) { %s%s(%s)%s%s }\n' % (args[i], call_prefix, '_' + c_names[i], ', '.join(pre_arg + args[:i]), call_postfix, '' if 'return ' in call_prefix else '; ' + (cache or ' ') + 'return')
524-
c_names[max_args] = 'emscripten_bind_%s_%d' % (bindings_name, max_args)
525-
body += ' %s%s(%s)%s;\n' % (call_prefix, '_' + c_names[max_args], ', '.join(pre_arg + args), call_postfix)
560+
c_names[i] = f'emscripten_bind_{bindings_name}_{i}'
561+
if 'return ' in call_prefix:
562+
after_call = ''
563+
else:
564+
after_call = '; ' + cache + 'return'
565+
args_for_call = make_call_args(i)
566+
body += ' if (%s === undefined) { %s_%s(%s)%s%s }\n' % (args[i], call_prefix, c_names[i],
567+
args_for_call,
568+
call_postfix, after_call)
569+
dbg(call_prefix)
570+
c_names[max_args] = f'emscripten_bind_{bindings_name}_{max_args}'
571+
args_for_call = make_call_args(len(args))
572+
body += ' %s_%s(%s)%s;\n' % (call_prefix, c_names[max_args], args_for_call, call_postfix)
526573
if cache:
527-
body += ' ' + cache + '\n'
574+
body += f' {cache}\n'
575+
576+
if constructor:
577+
declare_name = ' ' + func_name
578+
else:
579+
declare_name = ''
528580
mid_js.append(r'''function%s(%s) {
529581
%s
530582
};
531-
''' % ((' ' + func_name) if constructor else '', ', '.join(args), body[:-1]))
583+
''' % (declare_name, ', '.join(args), body[:-1]))
532584

533585
# C
534586

@@ -538,7 +590,8 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
538590
continue
539591
sig = list(map(full_typename, raw))
540592
if array_attribute:
541-
sig = [x.replace('[]', '') for x in sig] # for arrays, ignore that this is an array - our get/set methods operate on the elements
593+
# for arrays, ignore that this is an array - our get/set methods operate on the elements
594+
sig = [x.replace('[]', '') for x in sig]
542595

543596
c_arg_types = list(map(type_to_c, sig))
544597
c_class_name = type_to_c(class_name, non_pointing=True)

0 commit comments

Comments
 (0)