Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.1.1
-
None
Description
QScriptEngine provides a convenient way of adding functionality to C++ objects mapped to JS using setDefaultPrototype(). I'd like to see this functionality in QJSEngine as well.
There are a number of use cases where this feature is needed. For example, it is currently not safe to call C++ functions that can throw exceptions from JS side. Another need is to create functions with a variable number of parameters, because moc doesn't support varargs. (This feature would require QJSEngine::newFunction() and some other additions as well.)
In short, QJSEngine::setDefaultPrototype() would make it possible to design C++ classes for C++ use and selectively add features that are needed on the JS side only.
Currently, QML objects on the JS side have an empty prototype object. Thus, adding this feature wouldn't break anything that relies on prototypes. Furthermore, I believe the implementation would be quite easy. QQmlPropertyCache::newQObject() already creates a v8 constructor object using FunctionTemplate, and adding the prototype there is trivial. There may be other affected places, if everything doesn't go through the cache, but you should know better than me.