Skip to content

Commit

Permalink
[wasm64] Update webidl to support wasm64
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sbc100 committed Jan 24, 2024
1 parent 50a7531 commit 969b661
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 31 deletions.
9 changes: 6 additions & 3 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7627,7 +7627,6 @@ def test_embind_no_rtti_followed_by_rtti(self):
self.emcc_args += ['-lembind', '-fno-rtti', '-frtti']
self.do_run(src, '418\ndotest returned: 42\n')

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

# Force IDL checks mode
if self.is_wasm64():
args = ['--wasm64']
else:
args = []
with env_modify({'IDL_CHECKS': mode}):
self.run_process([WEBIDL_BINDER, test_file('webidl/test.idl'), 'glue'])
self.run_process([WEBIDL_BINDER, test_file('webidl/test.idl'), 'glue'] + args)
self.assertExists('glue.cpp')
self.assertExists('glue.js')

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

# Export things on "TheModule". This matches the typical use pattern of the bound library
# being used as Box2D.* or Ammo.*, and we cannot rely on "Module" being always present (closure may remove it).
self.emcc_args += ['-Wall', '--post-js=glue.js', '--extern-post-js=extern-post.js']
self.emcc_args += ['--post-js=glue.js', '--extern-post-js=extern-post.js']
if mode == 'ALL':
self.emcc_args += ['-sASSERTIONS']
if allow_memory_growth:
Expand Down
6 changes: 3 additions & 3 deletions test/webidl/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ console.log('int_array[0] == ' + arrayClass.get_int_array(0));
console.log('int_array[7] == ' + arrayClass.get_int_array(7));

try {
arrayClass.set_int_array(-1, struct);
arrayClass.set_int_array(-1, 42);
} catch (e) {
console.log('idx -1: ' + e);
}

try {
arrayClass.set_int_array(8, struct);
arrayClass.set_int_array(8, 42);
} catch (e) {
console.log('idx 8: ' + e);
}
Expand Down Expand Up @@ -270,7 +270,7 @@ if (isMemoryGrowthAllowed) {
intArray = intArray.concat(intArray);
storeArray.setArray(intArray);
}

// Make sure the array was copied to the newly allocated HEAP
var numCopiedEntries = 0;
for (var i = 0; i < intArray.length; i++) {
Expand Down
2 changes: 2 additions & 0 deletions tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,12 +893,14 @@ def create_pointer_conversion_wrappers(metadata):
'stackAlloc': 'pp',
'emscripten_builtin_malloc': 'pp',
'malloc': 'pp',
'webidl_malloc': 'pp',
'memalign': 'ppp',
'memcmp': '_ppp',
'memcpy': 'pppp',
'__getTypeName': 'pp',
'setThrew': '_p',
'free': '_p',
'webidl_free': '_p',
'stackRestore': '_p',
'__cxa_is_pointer_type': '_p',
'stackSave': 'p',
Expand Down
107 changes: 82 additions & 25 deletions tools/webidl_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
https://emscripten.org/docs/porting/connecting_cpp_and_javascript/WebIDL-Binder.html
"""

import argparse
import os
import sys
from typing import List
Expand All @@ -32,24 +33,36 @@
# DEBUG=1 will print debug info in render_function
DEBUG = os.environ.get('IDL_VERBOSE') == '1'

if DEBUG:
print(f'Debug print ON, CHECKS=${CHECKS}')
def dbg(*args):
if DEBUG:
print(*args, file=sys.stderr)

dbg(f'Debug print ON, CHECKS=${CHECKS}')

# We need to avoid some closure errors on the constructors we define here.
CONSTRUCTOR_CLOSURE_SUPPRESSIONS = '/** @suppress {undefinedVars, duplicate} @this{Object} */'


class Dummy:
def __init__(self, init):
for k, v in init.items():
self.__dict__[k] = v
def __init__(self, type):
self.type = type

def __repr__(self):
return f'<Dummy type:{self.type}>'

def getExtendedAttribute(self, _name):
return None


input_file = sys.argv[1]
output_base = sys.argv[2]
parser = argparse.ArgumentParser()
parser.add_argument('--wasm64', action='store_true', default=False,
help='Build for wasm64')
parser.add_argument('infile')
parser.add_argument('outfile')
options = parser.parse_args()

input_file = options.infile
output_base = options.outfile
cpp_output = output_base + '.cpp'
js_output = output_base + '.js'

Expand Down Expand Up @@ -392,13 +405,12 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
all_args = sigs.get(max_args)

if DEBUG:
print('renderfunc', class_name, func_name, list(sigs.keys()), return_type, constructor)
for i in range(max_args):
a = all_args[i]
dbg('renderfunc', class_name, func_name, list(sigs.keys()), return_type, constructor)
for i, a in enumerate(all_args):
if isinstance(a, WebIDL.IDLArgument):
print(' ', a.identifier.name, a.identifier, a.type, a.optional)
dbg(' ', a.identifier.name, a.identifier, a.type, a.optional)
else:
print(' arg%d' % i)
dbg(' arg%d (%s)' % (i, a))

# JS

Expand All @@ -409,6 +421,10 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
call_postfix = ''
if return_type != 'Void' and not constructor:
call_prefix = 'return '

if options.wasm64 and (constructor or return_type in interfaces or return_type == 'String'):
call_postfix += ')'

if not constructor:
if return_type in interfaces:
call_prefix += 'wrapPointer('
Expand All @@ -420,17 +436,28 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
call_prefix += '!!('
call_postfix += ')'

if options.wasm64 and (constructor or return_type in interfaces or return_type == 'String'):
call_prefix += 'Number('


args = [(all_args[i].identifier.name if isinstance(all_args[i], WebIDL.IDLArgument) else ('arg%d' % i)) for i in range(max_args)]
if not constructor and not is_static:
body = ' var self = this.ptr;\n'
pre_arg = ['self']
if options.wasm64:
pre_arg = ['BigInt(self)']
else:
pre_arg = ['self']
else:
body = ''
pre_arg = []

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

def is_ptr_arg(i):
t = all_args[i].type
return (t.isArray() or t.isAny() or t.isString() or t.isObject() or t.isInterface())

for i, (js_arg, arg) in enumerate(zip(args, all_args)):
if i >= min_args:
optional = True
Expand Down Expand Up @@ -494,9 +521,11 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,

if do_default:
if not (arg.type.isArray() and not array_attribute):
body += " if ({0} && typeof {0} === 'object') {0} = {0}.ptr;\n".format(js_arg)
body += f" if ({js_arg} && typeof {js_arg} === 'object') {js_arg} = {js_arg}.ptr;\n"
if arg.type.isString():
body += " else {0} = ensureString({0});\n".format(js_arg)
if options.wasm64 and is_ptr_arg(i):
body += f' if ({args[i]} === null) {args[i]} = 0;\n'
else:
# an array can be received here
arg_type = arg.type.name
Expand All @@ -511,18 +540,45 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
elif arg_type == 'Double':
body += " if (typeof {0} == 'object') {{ {0} = ensureFloat64({0}); }}\n".format(js_arg)

call_args = pre_arg

for i, arg in enumerate(args):
if options.wasm64 and is_ptr_arg(i):
arg = f'BigInt({arg})'
call_args.append(arg)

c_names = {}

def make_call_args(i):
if pre_arg:
i += 1
return ', '.join(call_args[:i])

for i in range(min_args, max_args):
c_names[i] = 'emscripten_bind_%s_%d' % (bindings_name, i)
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')
c_names[max_args] = 'emscripten_bind_%s_%d' % (bindings_name, max_args)
body += ' %s%s(%s)%s;\n' % (call_prefix, '_' + c_names[max_args], ', '.join(pre_arg + args), call_postfix)
c_names[i] = f'emscripten_bind_{bindings_name}_{i}'
if 'return ' in call_prefix:
after_call = ''
else:
after_call = '; ' + cache + 'return'
args_for_call = make_call_args(i)
body += ' if (%s === undefined) { %s_%s(%s)%s%s }\n' % (args[i], call_prefix, c_names[i],
args_for_call,
call_postfix, after_call)
dbg(call_prefix)
c_names[max_args] = f'emscripten_bind_{bindings_name}_{max_args}'
args_for_call = make_call_args(len(args))
body += ' %s_%s(%s)%s;\n' % (call_prefix, c_names[max_args], args_for_call, call_postfix)
if cache:
body += ' ' + cache + '\n'
body += f' {cache}\n'

if constructor:
declare_name = ' ' + func_name
else:
declare_name = ''
mid_js.append(r'''function%s(%s) {
%s
};
''' % ((' ' + func_name) if constructor else '', ', '.join(args), body[:-1]))
''' % (declare_name, ', '.join(args), body[:-1]))

# C

Expand All @@ -532,7 +588,8 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer,
continue
sig = list(map(full_typename, raw))
if array_attribute:
sig = [x.replace('[]', '') for x in sig] # for arrays, ignore that this is an array - our get/set methods operate on the elements
# for arrays, ignore that this is an array - our get/set methods operate on the elements
sig = [x.replace('[]', '') for x in sig]

c_arg_types = list(map(type_to_c, sig))
c_class_name = type_to_c(class_name, non_pointing=True)
Expand Down Expand Up @@ -726,9 +783,9 @@ def add_bounds_check_impl():
attr = m.identifier.name

if m.type.isArray():
get_sigs = {1: [Dummy({'type': WebIDL.BuiltinTypes[WebIDL.IDLBuiltinType.Types.long]})]}
set_sigs = {2: [Dummy({'type': WebIDL.BuiltinTypes[WebIDL.IDLBuiltinType.Types.long]}),
Dummy({'type': m.type})]}
get_sigs = {1: [Dummy(type=WebIDL.BuiltinTypes[WebIDL.IDLBuiltinType.Types.long])]}
set_sigs = {2: [Dummy(type=WebIDL.BuiltinTypes[WebIDL.IDLBuiltinType.Types.long]),
Dummy(type=m.type.inner)]}
get_call_content = take_addr_if_nonpointer(m) + 'self->' + attr + '[arg0]'
set_call_content = 'self->' + attr + '[arg0] = ' + deref_if_nonpointer(m) + 'arg1'
if m.getExtendedAttribute('BoundsChecked'):
Expand All @@ -740,7 +797,7 @@ def add_bounds_check_impl():
set_call_content = "(%s, %s)" % (bounds_check, set_call_content)
else:
get_sigs = {0: []}
set_sigs = {1: [Dummy({'type': m.type})]}
set_sigs = {1: [Dummy(type=m.type)]}
get_call_content = take_addr_if_nonpointer(m) + 'self->' + attr
set_call_content = 'self->' + attr + ' = ' + deref_if_nonpointer(m) + 'arg0'

Expand Down

0 comments on commit 969b661

Please sign in to comment.