18#ifndef INCLUDED_SCRIPTINTERFACE_OBJECT
19#define INCLUDED_SCRIPTINTERFACE_OBJECT
36template<
typename PropType>
41 JS::RootedObject object(rq.
cx, &obj.toObject());
42 if constexpr (std::is_same_v<int, PropType>)
44 JS::RootedId id(rq.
cx, INT_TO_JSID(name));
45 return JS_GetPropertyById(rq.
cx,
object,
id,
out);
47 else if constexpr (std::is_same_v<const char*, PropType>)
48 return JS_GetProperty(rq.
cx,
object, name,
out);
50 return JS_GetUCProperty(rq.
cx,
object, name, wcslen(name),
out);
53template<
typename T,
typename PropType>
56 JS::RootedValue val(rq.
cx);
57 if (!GetProperty<PropType>(rq, obj, name, &val))
63 JS::RootedValue val(rq.
cx, JS::ObjectValue(*
out.get()));
66 out.set(val.toObjectOrNull());
86 JS::RootedObject object(rq.
cx, &obj.toObject());
89 if (!JS_HasProperty(rq.
cx,
object, name, &found))
97template<
typename PropType>
98inline bool SetProperty(
const ScriptRequest& rq, JS::HandleValue obj, PropType name, JS::HandleValue value,
bool constant =
false,
bool enumerable =
true)
102 attrs |= JSPROP_READONLY | JSPROP_PERMANENT;
104 attrs |= JSPROP_ENUMERATE;
108 JS::RootedObject object(rq.
cx, &obj.toObject());
109 if constexpr (std::is_same_v<int, PropType>)
111 JS::RootedId id(rq.
cx, INT_TO_JSID(name));
112 return JS_DefinePropertyById(rq.
cx,
object,
id, value, attrs);
114 else if constexpr (std::is_same_v<const char*, PropType>)
115 return JS_DefineProperty(rq.
cx,
object, name, value, attrs);
117 return JS_DefineUCProperty(rq.
cx,
object, name, value, attrs);
120template<
typename T,
typename PropType>
121inline bool SetProperty(
const ScriptRequest& rq, JS::HandleValue obj, PropType name,
const T& value,
bool constant =
false,
bool enumerable =
true)
123 JS::RootedValue val(rq.
cx);
125 return SetProperty<PropType>(rq, obj, name, val, constant, enumerable);
131 return SetProperty<T, int>(rq, obj, name, value, constant, enumerable);
137 JS::RootedValue constructor(rq.
cx, JS::ObjectOrNullValue(JS_GetConstructor(rq.
cx, obj)));
147 JS::RootedObject obj(rq.
cx, val.toObjectOrNull());
155 if (!objVal.isObject())
158 JS::RootedObject obj(rq.
cx, &objVal.toObject());
161 return JS_DeepFreezeObject(rq.
cx, obj);
163 return JS_FreezeObject(rq.
cx, obj);
176 if (!objVal.isObjectOrNull())
178 LOGERROR(
"EnumeratePropertyNames expected object type!");
182 JS::RootedObject obj(rq.
cx, &objVal.toObject());
183 JS::RootedIdVector props(rq.
cx);
185 if (!js::GetPropertyKeys(rq.
cx, obj, enumerableOnly? 0 : JSITER_HIDDEN, &props))
188 out.reserve(
out.size() + props.length());
189 for (
size_t i = 0; i < props.length(); ++i)
191 JS::RootedId id(rq.
cx, props[i]);
192 JS::RootedValue val(rq.
cx);
193 if (!JS_IdToValue(rq.
cx,
id, &val))
201 std::string propName;
205 out.emplace_back(std::move(propName));
216 JS::RootedObject obj(rq.
cx, JS_NewPlainObject(rq.
cx));
218 return JS::UndefinedValue();
219 return JS::ObjectValue(*obj.get());
225 return !objectValue.isNullOrUndefined();
233template<
typename T,
typename... Args>
234inline bool CreateObject(
const ScriptRequest& rq, JS::MutableHandleValue objectValue,
const char* propertyName,
const T& propertyValue, Args
const&... args)
236 JS::RootedValue val(rq.
cx);
237 ToJSVal(rq, &val, propertyValue);
246 objectValue.setObjectOrNull(JS::NewArrayObject(rq.
cx, length));
247 return !objectValue.isNullOrUndefined();
#define LOGERROR(...)
Definition: CLogger.h:37
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
JSContext * cx
Definition: ScriptRequest.h:92
Wraps SM APIs for manipulating JS objects.
Definition: JSON.h:35
bool FromJSVal(const ScriptRequest &rq, const JS::HandleValue val, T &ret)
Convert a JS::Value to a C++ type.
bool EnumeratePropertyNames(const ScriptRequest &rq, JS::HandleValue objVal, bool enumerableOnly, std::vector< std::string > &out)
Returns all properties of the object, both own properties and inherited.
Definition: Object.h:174
bool SetProperty(const ScriptRequest &rq, JS::HandleValue obj, PropType name, JS::HandleValue value, bool constant=false, bool enumerable=true)
Set the named property on the given object.
Definition: Object.h:98
bool FreezeObject(const ScriptRequest &rq, JS::HandleValue objVal, bool deep)
Definition: Object.h:153
void ToJSVal(const ScriptRequest &rq, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a JS::Value.
bool GetPropertyInt(const ScriptRequest &rq, JS::HandleValue obj, int name, T &out)
Definition: Object.h:71
bool CreateArray(const ScriptRequest &rq, JS::MutableHandleValue objectValue, size_t length=0)
Sets the given value to a new JS object or Null Value in case of out-of-memory.
Definition: Object.h:244
bool GetObjectClassName(const ScriptRequest &rq, JS::HandleObject obj, T &name)
Definition: Object.h:135
bool SetPropertyInt(const ScriptRequest &rq, JS::HandleValue obj, int name, const T &value, bool constant=false, bool enumerable=true)
Definition: Object.h:129
bool HasProperty(const ScriptRequest &rq, JS::HandleValue obj, const char *name)
Check the named property has been defined on the given object.
Definition: Object.h:82
JS::Value CreateObject(const ScriptRequest &rq)
Create a plain object (i.e.
Definition: Object.h:214
bool GetProperty(const ScriptRequest &rq, JS::HandleValue obj, PropType name, JS::MutableHandleValue out)
Get the named property on the given object.
Definition: Object.h:37
#define T(string_literal)
Definition: secure_crt.cpp:77
unsigned int uint
Definition: types.h:42
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407