godunko / adawebpack

Ada WASM Runtime and Bindings for Web API
BSD 3-Clause "New" or "Revised" License
53 stars 3 forks source link

Is there a way to call an Ada function in WASM from javascript? #12

Closed bracke closed 3 years ago

bracke commented 3 years ago

Hi

In Blazor it's possible to call a C# method from javascript: https://code-maze.com/how-to-call-csharp-methods-from-javascript-in-blazor-webassembly/

Is something similar possible in AdaWebpack?

Note: This is not an event handler, but a more generic way of calling Ada code.

Kind regards Bent Bracke

godunko commented 3 years ago

Hello,

Yes, it is possible while may be not trivial when JS objects and strings need to be passed to Ada.

See https://github.com/godunko/adawebpack/tree/master/examples/call_ada as simple example.

ec-c commented 2 months ago

@godunko

Is there an example of how I can pass u8 elem. arrays to Ada and receive them in JS?

reznikmm commented 2 months ago

To pass it to Ada you can use a function similar to buffer_to_wasm_stream_element_buffer in source/adawebpack.mjs. To get it back, you need to return address and size from Ada side and then call new Uint8Array(instance.exports.memory.buffer, a, l);. We don't have corresponding example right now.