Improve performance of ScriptInterface::CreateObject from b4626359f5 / D2080 by creating the JSAutoRequest struct only once instead of once per property.

Differential Revision: https://code.wildfiregames.com/D2127
Comments By: Vladislav
This was SVN commit r22680.
This commit is contained in:
elexis
2019-08-17 03:30:07 +00:00
parent ba56191dc2
commit 742f361b2d
2 changed files with 31 additions and 18 deletions
+26 -13
View File
@@ -131,25 +131,22 @@ public:
JSObject* CreateCustomObject(const std::string & typeName) const;
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
/**
* Sets the given value to a new plain JS::Object. Can throw an exception in case of running out of memory.
*/
bool CreateObject(JS::MutableHandleValue objectValue) const;
/**
* Sets the given value to a new plain JS::Object, converts the arguments to JS::Values and sets them as properties.
* Can throw an exception.
*/
template<typename T, typename... Args>
bool CreateObject(JS::MutableHandleValue objectValue, const char* propertyName, const T& propertyValue, Args const&... args) const
template<typename... Args>
bool CreateObject(JS::MutableHandleValue objectValue, Args const&... args) const
{
return CreateObject(objectValue, args...) && SetProperty(objectValue, propertyName, propertyValue);
}
JSContext* cx = GetContext();
JSAutoRequest rq(cx);
JS::RootedObject obj(cx);
template<typename T, typename... Args>
bool CreateObject(JS::MutableHandleValue objectValue, const wchar_t* propertyName, const T& propertyValue, Args const&... args) const
{
return CreateObject(objectValue, args...) && SetProperty(objectValue, propertyName, propertyValue);
if (!CreateObject_(&obj, args...))
return false;
objectValue.setObject(*obj);
return true;
}
/**
@@ -403,6 +400,22 @@ public:
private:
/**
* Careful, the CreateObject_ helpers avoid creation of the JSAutoRequest!
*/
bool CreateObject_(JS::MutableHandleObject obj) const;
template<typename T, typename... Args>
bool CreateObject_(JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args) const
{
// JSAutoRequest is the responsibility of the caller
JSContext* cx = GetContext();
JS::RootedValue val(cx);
AssignOrToJSVal(cx, &val, propertyValue);
return CreateObject_(obj, args...) && JS_DefineProperty(cx, obj, propertyName, val, JSPROP_ENUMERATE);
}
bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const;
bool Eval_(const char* code, JS::MutableHandleValue ret) const;
bool Eval_(const wchar_t* code, JS::MutableHandleValue ret) const;