Empirical
JSWrap.h File Reference

Wrap a C++ function and convert it to an integer that can be called from Javascript. More...

#include <array>
#include <functional>
#include <tuple>
#include "../meta/meta.h"
#include "../base/assert.h"
#include "../base/vector.h"
#include "../tools/functions.h"
#include "../tools/mem_track.h"
#include "../tools/tuple_struct.h"
#include "../tools/tuple_utils.h"
#include "init.h"
#include "js_utils.h"

Go to the source code of this file.

Detailed Description

Wrap a C++ function and convert it to an integer that can be called from Javascript.

Note
This file is part of Empirical, https://github.com/devosoft/Empirical
Date
2015-2018

To wrap a function, call:

uint32_t fun_id = emp::JSWrap(FunctionToBeWrapped, "JS_Function_Name");`

To manually callback a function from Javascript, first set emp_i.cb_args to an array of function arguments, then call empCppCallback( fun_id ); This all happens automatically if you use the emp.Callback(fun_id, args...) function from Javascript.

The JS_Function_Name string is optional, but if you use it, the appropriate function will be automatically generated in Javascript by JSWrap, in the emp class.

For example, if you have:

int AddPair(int x, int y) { return x + y; }`

You can wrap it with:

size_t fun_id = emp::JSWrap(AddPair, "AddPair");

And then in Javascript, you can simply call it as:

emp.AddPair(4, 5); // will return 9.

Todo:

Add a JSWrap that takes an object and method and does the bind automatically.

Build a non-enscripten version; it should still be callable from the C++ side, but mostly to be able to test programs without Emscripten.