Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can see the exported function in the mymodule.js however the await _jsRuntime.InvokeAsync cant find it #22580

Open
brommadnaz929 opened this issue Sep 17, 2024 · 11 comments

Comments

@brommadnaz929
Copy link

I had a C++ project that I used to build using a makelist.txt file. The project has Array.hh MyLib++.hh MyLib++.cc . All three modules are defined within the same namespace. In blazor webassembly, in the server side c# dlls I want to call the function xpower which is found in MyLib.
I have tried with many options but no luck. The await _jsRuntime.InvokeAsync("_xpower",...) returns runtime error (visible in the console) with reason _xpower undefined. using Typeof Module._xpower returns undefined too.
thanks.

@brommadnaz929
Copy link
Author

when I combined all modules into one, compiled using -s MODULARIZE=1 -s EXPORT_ES6=1 the name of the function _xpower was present in the .js file. but, the webassembly still failing to fund it

@brommadnaz929
Copy link
Author

I add the following in the index.html:

<script type="module"> import createModule from './mymodule.js'; // Adjust the path if necessary createModule().then((Module) => { if (typeof Module._xpower === 'function') { console.log("Function myfunction is accessible!"); Module._xpower(); } else { console.log("Function myfunction is NOT accessible."); } }).catch((error) => { console.error("Error loading module:", error); }); </script>

and the console output is Function is accessible however with the following error after:
Error loading module: RuntimeError: memory access out of bounds
at mymodule.wasm:0x672c
at mymodule.wasm:0x6a8a
at mymodule.wasm:0x3060
at mymodule.wasm:0x1b21
at Object._xpower (VM18 mymodule.js:630:12)
at (index):54:24

when I still call the function then I get undefined:
Unhandled exception rendering component: Could not find '_xpower' ('_xpower' was undefined).
Error: Could not find '_xpower' ('_xpower' was undefined).

@sbc100
Copy link
Collaborator

sbc100 commented Sep 18, 2024

Can you share the full link command you are using? I general, if you want to export a function such as _xpower you would need to do -sEXPORTED_FUNCTIONS=_xpower. Alternatively you could mark the function as EMSCRIPTEN_KEEPALIVE in the source code.

@sbc100
Copy link
Collaborator

sbc100 commented Sep 18, 2024

Oh I see you have managed to call the function, but its crashing. You can get more information from the backtrace if you build with -g (or --profiling-functions). That might help you found out its crashing.

@brommadnaz929
Copy link
Author

brommadnaz929 commented Sep 19, 2024 via email

@sbc100
Copy link
Collaborator

sbc100 commented Sep 19, 2024

Passing arrays and non-trival data type to WebAssembly requires some kind of serialization layer yes.

For more information on the various options here here https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html.

I assume _jsRuntime.InvokeAsync is being called from the C/C++ code? But what does the code look like that is calling the native wasm function? Is it just using ccall? I'm a little confused about why you seem to be calling _xpower via InvokeAsync from the C/C++ when _xpower is already a C/C++ function? Am I correct that _xpower is a C/C++ function that you are trying to call from JS?

I would recommend starting something very simple and building up. For example, just export some simple function that take plain integers and make sure that is all working.

I would also highly recommend --profiling-funcs if -g doesn't work... although if -g is not working for you we would like to know why. That seems rather worrying.

@brommadnaz929
Copy link
Author

brommadnaz929 commented Sep 19, 2024 via email

@brommadnaz929
Copy link
Author

brommadnaz929 commented Sep 20, 2024 via email

@sbc100
Copy link
Collaborator

sbc100 commented Sep 20, 2024

cwrap works like call but it doesn't take the actual arguments, it just takes tree params: 1) Function name, 2) return type, 3) array of param types.

cwrap then returns a function pointr which you pass you arguments too.

also, note that the 'array' type supported by ccall/cwrap only supports byte arrays.. if you want the C code to receive an array of anyting by bytes I don't think you can use that.

@brommadnaz929
Copy link
Author

brommadnaz929 commented Sep 21, 2024 via email

@brommadnaz929
Copy link
Author

brommadnaz929 commented Sep 21, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants