mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-27 10:52:59 +00:00
Split off Object-related functions from ScriptInterface
Follows 34b1920e7b.
This splits off the object-related functions, such as
[Set/Get/Has]Property, CreateObject, CreateArray, FreezeObject.
It also puts the definitions in the header itself, which might end up
with faster code here & there, though perhaps slower compilation time
(somewhat doubtful since we already included most things anyways).
Differential Revision: https://code.wildfiregames.com/D3956
This was SVN commit r25430.
This commit is contained in:
@@ -126,28 +126,6 @@ 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, converts the arguments to JS::Values and sets them as properties.
|
||||
* This is static so that callers like ToJSVal can use it with the JSContext directly instead of having to obtain the instance using GetScriptInterfaceAndCBData.
|
||||
* Can throw an exception.
|
||||
*/
|
||||
template<typename... Args>
|
||||
static bool CreateObject(const ScriptRequest& rq, JS::MutableHandleValue objectValue, Args const&... args)
|
||||
{
|
||||
JS::RootedObject obj(rq.cx);
|
||||
|
||||
if (!CreateObject_(rq, &obj, args...))
|
||||
return false;
|
||||
|
||||
objectValue.setObject(*obj);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the given value to a new JS object or Null Value in case of out-of-memory.
|
||||
*/
|
||||
static void CreateArray(const ScriptRequest& rq, JS::MutableHandleValue objectValue, size_t length = 0);
|
||||
|
||||
/**
|
||||
* Set the named property on the global object.
|
||||
* Optionally makes it {ReadOnly, DontEnum}. We do not allow to make it DontDelete, so that it can be hotloaded
|
||||
@@ -156,58 +134,6 @@ public:
|
||||
template<typename T>
|
||||
bool SetGlobal(const char* name, const T& value, bool replace = false, bool constant = true, bool enumerate = true);
|
||||
|
||||
/**
|
||||
* Set the named property on the given object.
|
||||
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
||||
*/
|
||||
template<typename T>
|
||||
bool SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant = false, bool enumerate = true) const;
|
||||
|
||||
/**
|
||||
* Set the named property on the given object.
|
||||
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
||||
*/
|
||||
template<typename T>
|
||||
bool SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool constant = false, bool enumerate = true) const;
|
||||
|
||||
/**
|
||||
* Set the integer-named property on the given object.
|
||||
* Optionally makes it {ReadOnly, DontDelete, DontEnum}.
|
||||
*/
|
||||
template<typename T>
|
||||
bool SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool constant = false, bool enumerate = true) const;
|
||||
|
||||
/**
|
||||
* Get the named property on the given object.
|
||||
*/
|
||||
template<typename T>
|
||||
bool GetProperty(JS::HandleValue obj, const char* name, T& out) const;
|
||||
bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const;
|
||||
bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleObject out) const;
|
||||
|
||||
template<typename T>
|
||||
static bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, T& out);
|
||||
static bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, JS::MutableHandleValue out);
|
||||
static bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, JS::MutableHandleObject out);
|
||||
|
||||
/**
|
||||
* Get the integer-named property on the given object.
|
||||
*/
|
||||
template<typename T>
|
||||
bool GetPropertyInt(JS::HandleValue obj, int name, T& out) const;
|
||||
bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out) const;
|
||||
bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleObject out) const;
|
||||
|
||||
template<typename T>
|
||||
static bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, T& out);
|
||||
static bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, JS::MutableHandleValue out);
|
||||
static bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, JS::MutableHandleObject out);
|
||||
|
||||
/**
|
||||
* Check the named property has been defined on the given object.
|
||||
*/
|
||||
bool HasProperty(JS::HandleValue obj, const char* name) const;
|
||||
|
||||
/**
|
||||
* Get an object from the global scope or any lexical scope.
|
||||
* This can return globally accessible objects even if they are not properties
|
||||
@@ -217,20 +143,8 @@ public:
|
||||
*/
|
||||
static bool GetGlobalProperty(const ScriptRequest& rq, const std::string& name, JS::MutableHandleValue out);
|
||||
|
||||
/**
|
||||
* Returns all properties of the object, both own properties and inherited.
|
||||
* This is essentially equivalent to calling Object.getOwnPropertyNames()
|
||||
* and recursing up the prototype chain.
|
||||
* NB: this does not return properties with symbol or numeric keys, as that would
|
||||
* require a variant in the vector, and it's not useful for now.
|
||||
* @param enumerableOnly - only return enumerable properties.
|
||||
*/
|
||||
bool EnumeratePropertyNames(JS::HandleValue objVal, bool enumerableOnly, std::vector<std::string>& out) const;
|
||||
|
||||
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
|
||||
|
||||
bool FreezeObject(JS::HandleValue objVal, bool deep) const;
|
||||
|
||||
/**
|
||||
* Convert an object to a UTF-8 encoded string, either with JSON
|
||||
* (if pretty == true and there is no JSON error) or with toSource().
|
||||
@@ -331,22 +245,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static bool CreateObject_(const ScriptRequest& rq, JS::MutableHandleObject obj);
|
||||
|
||||
template<typename T, typename... Args>
|
||||
static bool CreateObject_(const ScriptRequest& rq, JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args)
|
||||
{
|
||||
JS::RootedValue val(rq.cx);
|
||||
Script::ToJSVal(rq, &val, propertyValue);
|
||||
|
||||
return CreateObject_(rq, obj, args...) && JS_DefineProperty(rq.cx, obj, propertyName, val, JSPROP_ENUMERATE);
|
||||
}
|
||||
|
||||
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace, bool constant, bool enumerate);
|
||||
bool SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool constant, bool enumerate) const;
|
||||
bool SetProperty_(JS::HandleValue obj, const wchar_t* name, JS::HandleValue value, bool constant, bool enumerate) const;
|
||||
bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool constant, bool enumerate) const;
|
||||
|
||||
struct CustomType
|
||||
{
|
||||
@@ -372,66 +271,6 @@ bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace,
|
||||
return SetGlobal_(name, val, replace, constant, enumerate);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant, bool enumerate) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
JS::RootedValue val(rq.cx);
|
||||
Script::ToJSVal(rq, &val, value);
|
||||
return SetProperty_(obj, name, val, constant, enumerate);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool constant, bool enumerate) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
JS::RootedValue val(rq.cx);
|
||||
Script::ToJSVal(rq, &val, value);
|
||||
return SetProperty_(obj, name, val, constant, enumerate);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool constant, bool enumerate) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
JS::RootedValue val(rq.cx);
|
||||
Script::ToJSVal(rq, &val, value);
|
||||
return SetPropertyInt_(obj, name, val, constant, enumerate);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, T& out) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
return GetProperty(rq, obj, name, out);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, T& out)
|
||||
{
|
||||
JS::RootedValue val(rq.cx);
|
||||
if (!GetProperty(rq, obj, name, &val))
|
||||
return false;
|
||||
return Script::FromJSVal(rq, val, out);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, T& out) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
return GetPropertyInt(rq, obj, name, out);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, T& out)
|
||||
{
|
||||
JS::RootedValue val(rq.cx);
|
||||
if (!GetPropertyInt(rq, obj, name, &val))
|
||||
return false;
|
||||
return Script::FromJSVal(rq, val, out);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::Eval(const char* code, T& ret) const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user