@@ -209,27 +209,6 @@ var LibraryEmVal = {
209209 return result ;
210210 } ,
211211
212- _emval_as__deps : [ '$Emval' , '$requireRegisteredType' , '$emval_returnValue' ] ,
213- _emval_as : ( handle , returnType , destructorsRef ) => {
214- handle = Emval . toValue ( handle ) ;
215- returnType = requireRegisteredType ( returnType , 'emval::as' ) ;
216- return emval_returnValue ( returnType , destructorsRef , handle ) ;
217- } ,
218-
219- _emval_as_int64__deps : [ '$Emval' , '$requireRegisteredType' ] ,
220- _emval_as_int64 : ( handle , returnType ) => {
221- handle = Emval . toValue ( handle ) ;
222- returnType = requireRegisteredType ( returnType , 'emval::as' ) ;
223- return returnType [ 'toWireType' ] ( null , handle ) ;
224- } ,
225-
226- _emval_as_uint64__deps : [ '$Emval' , '$requireRegisteredType' ] ,
227- _emval_as_uint64 : ( handle , returnType ) => {
228- handle = Emval . toValue ( handle ) ;
229- returnType = requireRegisteredType ( returnType , 'emval::as' ) ;
230- return returnType [ 'toWireType' ] ( null , handle ) ;
231- } ,
232-
233212 _emval_equals__deps : [ '$Emval' ] ,
234213 _emval_equals : ( first , second ) => {
235214 first = Emval . toValue ( first ) ;
@@ -264,13 +243,6 @@ var LibraryEmVal = {
264243 return ! object ;
265244 } ,
266245
267- _emval_call__deps : [ '$emval_methodCallers' , '$Emval' ] ,
268- _emval_call : ( caller , handle , destructorsRef , args ) => {
269- caller = emval_methodCallers [ caller ] ;
270- handle = Emval . toValue ( handle ) ;
271- return caller ( null , handle , destructorsRef , args ) ;
272- } ,
273-
274246 $emval_lookupTypes__deps : [ '$requireRegisteredType' ] ,
275247 $emval_lookupTypes : ( argCount , argTypes ) => {
276248 var a = new Array ( argCount ) ;
@@ -292,11 +264,12 @@ var LibraryEmVal = {
292264 return id;
293265 },
294266
295- _emval_get_method_caller__deps : [
267+ _emval_create_invoker__deps : [
296268 '$emval_addMethodCaller', '$emval_lookupTypes',
297269 '$createNamedFunction', '$emval_returnValue',
270+ '$Emval', '$getStringOrSymbol',
298271 ],
299- _emval_get_method_caller : (argCount, argTypes, kind) => {
272+ _emval_create_invoker : (argCount, argTypes, kind) => {
300273 var GenericWireTypeSize = {{{ 2 * POINTER_SIZE }}};
301274
302275 var types = emval_lookupTypes(argCount, argTypes);
@@ -305,26 +278,38 @@ var LibraryEmVal = {
305278
306279#if !DYNAMIC_EXECUTION
307280 var argN = new Array(argCount);
308- var invokerFunction = (obj, func , destructorsRef, args) => {
281+ var invokerFunction = (handle, methodName , destructorsRef, args) => {
309282 var offset = 0;
310283 for (var i = 0; i < argCount; ++i) {
311284 argN[i] = types[i]['readValueFromPointer'](args + offset);
312285 offset += GenericWireTypeSize;
313286 }
314- var rv = kind === /* CONSTRUCTOR */ 1 ? Reflect.construct(func, argN) : func.apply(obj, argN);
287+ var rv;
288+ switch (kind) {
289+ case {{{ cDefs['internal::EM_INVOKER_KIND::FUNCTION'] }}}:
290+ rv = Emval.toValue(handle).apply(null, argN);
291+ break;
292+ case {{{ cDefs['internal::EM_INVOKER_KIND::CONSTRUCTOR'] }}}:
293+ rv = Reflect.construct(Emval.toValue(handle), argN);
294+ break;
295+ case {{{ cDefs['internal::EM_INVOKER_KIND::CAST'] }}}:
296+ // no-op, just return the argument
297+ rv = argN[0];
298+ break;
299+ case {{{ cDefs['internal::EM_INVOKER_KIND::METHOD'] }}}:
300+ rv = Emval.toValue(handle)[getStringOrSymbol(methodName)](...argN);
301+ break;
302+ }
315303 return emval_returnValue(retType, destructorsRef, rv);
316304 };
317305#else
318306 var functionBody =
319- ` return function ( obj , func , destructorsRef , args ) { \n`;
307+ ` return function ( handle , methodName , destructorsRef , args ) { \n`;
320308
321309 var offset = 0;
322- var argsList = []; // 'obj?, arg0, arg1, arg2, ... , argN'
323- if (kind === {{{ cDefs['internal::EM_METHOD_CALLER_KIND::FUNCTION'] }}}) {
324- argsList.push('obj');
325- }
326- var params = ['retType'];
327- var args = [retType];
310+ var argsList = []; // 'arg0, arg1, arg2, ... , argN'
311+ var params = ['toValue', 'retType'];
312+ var args = [Emval.toValue, retType];
328313 for (var i = 0; i < argCount; ++i) {
329314 argsList.push(` arg$ { i } `);
330315 params.push(` argType$ { i } `);
@@ -333,7 +318,23 @@ var LibraryEmVal = {
333318 ` var arg$ { i} = argType$ { i} . readValueFromPointer ( args$ { offset ? '+' + offset : '' } ) ; \n`;
334319 offset += GenericWireTypeSize;
335320 }
336- var invoker = kind === {{{ cDefs['internal::EM_METHOD_CALLER_KIND::CONSTRUCTOR'] }}} ? 'new func' : 'func.call';
321+ var invoker;
322+ switch (kind){
323+ case {{{ cDefs['internal::EM_INVOKER_KIND::FUNCTION'] }}}:
324+ invoker = 'toValue(handle)';
325+ break;
326+ case {{{ cDefs['internal::EM_INVOKER_KIND::CONSTRUCTOR'] }}}:
327+ invoker = 'new (toValue(handle))';
328+ break;
329+ case {{{ cDefs['internal::EM_INVOKER_KIND::CAST'] }}}:
330+ invoker = '';
331+ break;
332+ case {{{ cDefs['internal::EM_INVOKER_KIND::METHOD'] }}}:
333+ params.push('getStringOrSymbol');
334+ args.push(getStringOrSymbol);
335+ invoker = 'toValue(handle)[getStringOrSymbol(methodName)]';
336+ break;
337+ }
337338 functionBody +=
338339 ` var rv = $ { invoker} ( $ { argsList . join ( ', ' ) } ) ; \n`;
339340 if (!retType.isVoid) {
@@ -351,14 +352,16 @@ var LibraryEmVal = {
351352 return emval_addMethodCaller(createNamedFunction(functionName, invokerFunction));
352353 },
353354
354- _emval_call_method__deps: [ '$getStringOrSymbol' , '$emval_methodCallers' , '$Emval' ] ,
355- _emval_call_method : ( caller , objHandle , methodName , destructorsRef , args ) = > {
356- caller = emval_methodCallers [ caller ] ;
357- objHandle = Emval . toValue ( objHandle ) ;
358- methodName = getStringOrSymbol ( methodName ) ;
359- return caller ( objHandle , objHandle [ methodName ] , destructorsRef , args ) ;
355+ _emval_invoke__deps: ['$getStringOrSymbol', '$emval_methodCallers', '$Emval'],
356+ _emval_invoke: (caller, handle, methodName, destructorsRef, args) => {
357+ return emval_methodCallers[caller](handle, methodName, destructorsRef, args);
360358 },
361359
360+ // Same as ` _emval_invoke `, just imported into Wasm under a different return type .
361+ // TODO: remove this if/when https://github.com/emscripten-core/emscripten/issues/20478 is fixed.
362+ _emval_invoke_i64__deps : [ '_emval_invoke' ] ,
363+ _emval_invoke_i64 : '=__emval_invoke' ,
364+
362365 _emval_typeof__deps : [ '$Emval' ] ,
363366 _emval_typeof : ( handle ) = > {
364367 handle = Emval . toValue ( handle ) ;
0 commit comments