v8pp::module
from existing v8::ObjectTemplate
and add into this v8pp::module
C++ functions, objects and v8pp::class_
es. And then use that object template in v8::Context::New
:using namespace v8;
Local<ObjectTemplate> global_template = v8::ObjectTemplate::New(isolate);
v8pp::module global(isolate, global_template);
v8pp::class_<Some> some_class;
...
global.set("aa", 123);
global.set('rand", &rand);
global.set("MyClass", some_class);
Local<Context> context = v8::Context::New(isolate, global_template);
v8pp::context
constructor overload with v8pp::module
to use as the global object template.
hmm, well I ended up just adding some static functions to create the class object for the global object then set it to an internal pointer and unwrapped it from the contexts global object on the call.
I was looking at the v8pp class functions and I was wondering if there would be a way for the global object to be recognized as that class then automatically unwrapped when those class functions were called. I wasn't exactly sure what was being done for an object to be recognized as a v8pp class.
Hi @hia3! All wrapped native functions (both free and class members) are being called from inside of try .. catch
block (see forward_function
).
Such a catched std::exception
(as well all its derived types) would be converted to v8::Exception
and set as the result value for the V8 function callback.
Other C++ exception types are not supported and the program behaviour is undefined, since I don't know how V8 handles exceptions thrown out of a function callback.
Well this try .. catch
block in forward_function
is a simple trap for unhandled exceptions which may be thrown from a wrapped C++ function. I'm not sure how to deal with exceptions correctly in such situations because a C++ exception may occur in the middle of V8 code flow:
some V8 code ...
our wrapped C++ function
... rest of V8 code
The current solution with v8::Exception
return allows to use try catch
block in JavaScript and to resume the JS script execution there.
Maybe I should add catch(...)
block into the forward_function
and return some v8::Exception("uknown")
value from that catch.
But in general, I think exception handling should be in your code, since v8pp library knows nothing about your application. In this situation you can use try catch
block inside of the wrapped C++ function to manage all exceptional cases you need.
Do you know how to set constants on the class_ so that the value can be accessed without first creating a instance of the object
ex. I've created v8pp::class_<c++_node_class> node_class and the class is on the context with context.set("Node", node_class) I want to be able to set a value on that class say .ELEMENT_TYPE = 0, but I want to be able to access that class on the main Object. So that Node.ELEMENT_TYPE would return 0. without first having to have a make a node_class object.
template<typename Value>
class_& set_js_const(char const* name, Value value)
{
v8::HandleScope scope(isolate());
class_singleton_.js_function_template()->Set(v8pp::to_v8(isolate(), name),
to_v8(isolate(), value), v8::PropertyAttribute(v8::ReadOnly | v8::DontDelete));
return *this;
}
recv
it is. there is also handy v8pp::call_v8()
that allows to call a V8::Function
with C++ arguments, converting them to V8::Values:v8::Local<v8::Function> fun;
v8::Local<v8::Object> fun_this;
v8::Local<v8::Value> ret = v8pp::call_v8(isolate, fun, fun_this, 11, "str", you_wrapped_cpp_object);
var enumeratedArray = new Array();
for (var n in object)
enumeratedArray.push(n);
So I want to be able to override "in" so that i can send the values that I want back
so i changed the class function to this
v8::Local<v8::FunctionTemplate> func = v8::FunctionTemplate::New(isolate_,//);
//v8::Local<v8::FunctionTemplate> js_func = v8::FunctionTemplate::New(isolate_,
[](v8::FunctionCallbackInfo<v8::Value> const& args)
{
v8::Isolate* isolate = args.GetIsolate();
try
{
return args.GetReturnValue().Set(instance(isolate).wrap_object(args));
}
catch (std::exception const& ex)
{
args.GetReturnValue().Set(throw_ex(isolate, ex.what()));
}
});
setting the constructor on the class_function_template. then I'm also creating an object template after that