I just started using JavaScript V8 and compiled a basic example from the Embedder's Guide. Now I want to bind C++ functions to the JavaScript context.
For now I only have a more or less blank class which should handle binding later on.
class Manager
{
public:
Manager()
{
context = Context::New();
}
void Bind(string Name, function<Handle<Value>(const Arguments&)> Function)
{
// bind the function to the given context
}
private:
Persistent<Context> context;
};
How can I bind a std::function
object to the JavaScript V8 context?
The parameter type InvocationCallback
which is requested by the Set
function is a simple typedef.
typedef Handle<Value> (*InvocationCallback)(Arguments const &);
Therefore I had to convert the std::function
to a raw function pointer.
void Register(string Name, function<Handle<Value>(Arguments const &)> Function)
{
InvocationCallback* function = Function.target<InvocationCallback>();
globals->Set(String::New(Name.c_str()), FunctionTemplate::New(*function));
}