WebAssembly Part 3 : Different ways to call the exported function in WebAssembly.

How to use ccall, cwrap in emscripten to call the exported function like Javascript functions.

This is Part-3 of WebAssembly Tutorial , You can read Part 1 and Part 2 , which gives you the picture of what is webAssembly.

Let’s write a simple C++ program with some functions

// test.cpp

Compile the above program with the emccc

Once it is compiled load the test.js file generated in your html file.

Now we can call the exported functions in 3 ways

  1. Using Module object
  2. Using ccall emscripten in-built function
  3. Using cwrap emscripten in-built function

Calling the Exported function using Module Object

Module._square(💯);
//10000

Calling the Exported function using ccall & cwrap

ccall & cwrapare in-built function in emscripten, The ccall & cwrap function will be exported ,by-default by the emscripten , if you don’t set any optimization while compiling . If there is any optimation option is set during the compilation , this functions is not exported to our Module object by-default. In those cases, ccall & cwrap functions are not exported, we can manually tell emscripten to export the ccall & cwrap function during compilation by setting the option -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']” .

1.Command to compile the code

Now in your html file inside the script tag you can call the exported function using the ccall.

When you are using we don’t need to add _ in front of the function name.

ccall(func_Name_In_Srting, returnType, [argument types ], [arguments]))

<script>

Don’t forget to add [] to argument types & arguments .

`onRuntimeInitialized is an event that is triggered when the wasm is loaded. By using this method , we can confirm that all C function are executed only after the wasm is loaded.

We can also pass string and return string using ccall.

Consider the c program

// test.cpp

Compile the method as

Call the methods as

Module.ccall('toUpperCase', 'string',['string'],["javascript jeep"])

This will return JAVASCRIPT JEEP as result.

You can also pass ‘array’ with ‘array’ as argument type.But I don’t know 🤔 how to do that , if someone reading knows , post in comment.

Using cwrap to call exported function

It will wrap the c function inside the javascript function and return the javascript function .

cwrap(func_name, return_type,[‘argument_type’])

var square = Module.cwrap('square','number',['number'])

Don’t forgot to add the cwrap in EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']

If you find this helpful surprise 🎁 me here.

Share if you feel happy.

Follow Javascript Jeep🚙 if you feel worthy.

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Javascript Jeep🚙💨

Written by

Articles related to javascript

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co