WebAssembly needs correct function pointer types

3 views
Skip to first unread message

John Dallman

unread,
Nov 26, 2025, 6:23:55 AM (7 days ago) Nov 26
to emscripte...@googlegroups.com
I have a lot of code that stores pointers to functions as void* and then casts them back to the "correct" type. This is part of a LISP interpreter that is embedded in my product's test harness, and used to write test scripts. 

This works in conventional C, but I'm having trouble with it in WebAssembly. I've found that functions with and without argument lists don't match, which is quite reasonable, but I'm having trouble getting a fix to work.

Just how identical do the function signatures have to be?
  • Character-for-character identical?
  • Identical types with different names are OK? 
  • Static and non-static functions with otherwise identical signatures are OK? 
  • Something else?    
I'm currently using Emscripten v3.1.71 - would an update be likely to help? 

Thanks in advance,

John Dallman

Sam Clegg

unread,
Nov 26, 2025, 4:06:08 PM (7 days ago) Nov 26
to emscripte...@googlegroups.com
On Wed, Nov 26, 2025 at 3:23 AM John Dallman <jgdats...@gmail.com> wrote:
I have a lot of code that stores pointers to functions as void* and then casts them back to the "correct" type. This is part of a LISP interpreter that is embedded in my product's test harness, and used to write test scripts. 

This works in conventional C, but I'm having trouble with it in WebAssembly. I've found that functions with and without argument lists don't match, which is quite reasonable, but I'm having trouble getting a fix to work.

Just how identical do the function signatures have to be?
  • Character-for-character identical?
  • Identical types with different names are OK? 
  • Static and non-static functions with otherwise identical signatures are OK? 
  • Something else?    

Function signatures need to be an exact match at the Wasm level.   At the wasm level signatures are made of only the core i32, i64, f32, f64 types, so its likely the many different C signatures have the same Wasm signature.

As long as you are casting the void* pointer back to the correct function type before calling them you should have no issues.

If you are trying to call a function with too many, or too few arguments that does not work.  

We do have a setting that can help if you really need to do this: EMULATE_FUNCTION_POINTER_CASTS.  See https://emscripten.org/docs/tools_reference/settings_reference.html#emulate-function-pointer-casts for more info on this.  This setting is used by some projects (notably Python and Glib) which depend on being able to call via mismatched signatures.   You should be aware that this setting does come with a runtime cost though.

cheer,
sam

John Dallman

unread,
Nov 27, 2025, 11:59:25 AM (6 days ago) Nov 27
to emscripte...@googlegroups.com
On Wed, Nov 26, 2025 at 9:06 PM 'Sam Clegg' via emscripten-discuss <emscripte...@googlegroups.com> wrote:

> Function signatures need to be an exact match at the Wasm level.  At the wasm level 
> signatures are made of only the core i32, i64, f32, f64 types, so its likely the many different 
> C signatures have the same Wasm signature.

OK, I presume pointers are i32 or i64, according to if you're building for 32-bit or 64-bit Wasm?  

> As long as you are casting the void* pointer back to the correct function type before calling them 
> you should have no issues.

I've found my mistake now. I have a lot of changes to make, though. 

Thanks, 

John
--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/CAL_va28ocNLCKKvbomaW2Ci5Asf70uyqis0vKADYBW1XDe0x1A%40mail.gmail.com.

Sam Clegg

unread,
Nov 29, 2025, 2:51:16 PM (4 days ago) Nov 29
to emscripte...@googlegroups.com
On Thu, Nov 27, 2025 at 8:59 AM John Dallman <jgdats...@gmail.com> wrote:
On Wed, Nov 26, 2025 at 9:06 PM 'Sam Clegg' via emscripten-discuss <emscripte...@googlegroups.com> wrote:

> Function signatures need to be an exact match at the Wasm level.  At the wasm level 
> signatures are made of only the core i32, i64, f32, f64 types, so its likely the many different 
> C signatures have the same Wasm signature.

OK, I presume pointers are i32 or i64, according to if you're building for 32-bit or 64-bit Wasm?  

Yes, exactly.
 
Reply all
Reply to author
Forward
0 new messages