I thought security token has a string type. Since it's a v8::Value
you might store security token inv8pp::context
as a persistent handle, either to get it from v8::context:
Here's possible implementation:
v8::Handle<v8::Object> v8pp::context::global()
{
/* Note the comment from v8.h:
Returns the global proxy object.
*
* Global proxy object is a thin wrapper whose prototype points to actual
* context's global object with the properties like Object, etc. This is done
* that way for security reasons (for more details see
* https://wiki.mozilla.org/Gecko:SplitWindow).
*
* Please note that changes to global proxy object prototype most probably
* would break VM---v8 expects only global object as a prototype of global
* proxy object.
*/
return v8pp::to_local(isolate_, impl_)->Global();
}
v8::Handle<v8::Value> v8pp::context::security_token()
{
return v8pp::to_local(isolate_, impl_)->GetSecurityToken();
}
void v8pp::context::set_security_token(v8::Handle<v8::Value> value)
{
return v8pp::to_local(isolate_, impl_)->SetSecurityToken(value);
}
If you need to have a context clone, maybe you need to use a copy constructor:
v8pp::context::context(context const& src)
: own_isolate_(false)
, isolate_(src)
{
v8::HandleScope scope(isolate_);
v8::Local<v8::ObjectTemplate> src_templ = to_local(isolate_, src.templ_);
v8::Local<v8::Object> src_global = to_local(isolate_, src.impl_)->Global();
v8::Local<v8::Context> impl = v8::Context::New(isolate_, nullptr, src_templ, src_global);
impl->Enter();
impl_.Reset(isolate_, impl);
}
It requires to store an ObjectTemplate used to create v8::Context
as a persistent handle in v8pp::context
instance.
Having copy ctor you may quik create many contexts (but they will be in initial state):
v8pp::context parent; // create, enter to parent
{
v8pp::context child = parent; // copy initial global from parent, enter to child context
{
v8pp::context child2 = child;
...
} // exit from child 2, enter to child
} // exit from child, enter to parent
Copy ctor maybe not so goo idea, because v8pp::context becomes copy constructible with side effect (enterin into the V8 context). I'd rather would have a static function, say static v8pp::context v8pp::context::clone_and_enter(v8pp::context& src)
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