pmed / v8pp

Bind C++ functions and classes into V8 JavaScript engine
http://pmed.github.io/v8pp/
Other
886 stars 118 forks source link

Add support for custom global object in v8pp::context #167

Closed YarikTH closed 2 years ago

YarikTH commented 2 years ago

The feature allows to set up the global object at the context creation stage. module already allows to export functions, modules, classes etc, so we can use modules to setup global object as soon we expose their impl.

pmed commented 2 years ago

Thanks for the idea!

I've changed the context::options in order to use a simple v8::ObjectTemplate for a custom global object, instead of a callback-style global_factory_function.

This feature was also cherry-picked to c++17 branch, and context::options documentation was also updated.

See commits d62a0d581fd4ab05beb8a675216a12c72a5077c8 and 73bf8eda9b14a92d1a3d4e6dea6aa080e2d927ff

YarikTH commented 2 years ago

The reason behind factory function is the fact, that if I want v8pp::context to create Isolate for me, then there is no other chance to create v8::ObjectTemplate between creation if the Isolate by isolate = v8::Isolate::New(create_params); and creation of context here v8::Local<v8::Context> impl = v8::Context::New(isolate_, nullptr, global);.

Factory functor solves the issue, while passing ObjectTemplate doesn't