Embind

Embind is an additional feature that Emscripten offers for connecting JavaScript and C++. Emscripten's site provides the following description:

"Embind is used to bind C++ functions and classes to JavaScript, so that the compiled code can be used in a natural way by 'normal' JavaScript. Embind also supports calling JavaScript classes from C++."

Embind is a powerful feature that allows for tight integration between JavaScript and C++. You can wrap some C++ code in an EMSCRIPTEN_BINDINGS() block and reference it through the Module object in your browser. Let's look at an example from Emscripten's site. The following file, example.cpp, is compiled with the --bind flag of emcc:

// example.cpp
#include <emscripten/bind.h>

using namespace emscripten;

float lerp(float a, float b, float t) {
return (1 - t) * a + t * b;
}

EMSCRIPTEN_BINDINGS(my_module) {
function("lerp", &lerp);
}

The resultant module is loaded in example.html and the lerp() function is called:

<!-- example.html -->
<!doctype html>
<html>
<script src="example.js"></script>
<script>
// example.js was generated by running this command:
// emcc --bind -o example.js example.cpp
console.log('lerp result: ' + Module.lerp(1, 2, 0.5));
</script>
</html>

The preceding example represents a small portion of Embind's capabilities. You can learn more about Embind at https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset