File: Object.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (252 lines) | stat: -rw-r--r-- 8,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Copyright (C) 2021 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_SCRIPTINTERFACE_OBJECT
#define INCLUDED_SCRIPTINTERFACE_OBJECT

#include "ScriptConversions.h"
#include "ScriptRequest.h"
#include "ScriptTypes.h"

#include "ps/CLogger.h"

/**
 * Wraps SM APIs for manipulating JS objects.
 */

namespace Script
{
/**
 * Get the named property on the given object.
 */
template<typename PropType>
inline bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, PropType name, JS::MutableHandleValue out)
{
	if (!obj.isObject())
		return false;
	JS::RootedObject object(rq.cx, &obj.toObject());
	if constexpr (std::is_same_v<int, PropType>)
	{
		JS::RootedId id(rq.cx, INT_TO_JSID(name));
		return JS_GetPropertyById(rq.cx, object, id, out);
	}
	else if constexpr (std::is_same_v<const char*, PropType>)
		return JS_GetProperty(rq.cx, object, name, out);
	else
		return JS_GetUCProperty(rq.cx, object, name, wcslen(name), out);
}

template<typename T, typename PropType>
inline bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, PropType name, T& out)
{
	JS::RootedValue val(rq.cx);
	if (!GetProperty<PropType>(rq, obj, name, &val))
		return false;
	return FromJSVal(rq, val, out);
}
inline bool GetProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name, JS::MutableHandleObject out)
{
	JS::RootedValue val(rq.cx, JS::ObjectValue(*out.get()));
	if (!GetProperty(rq, obj, name, &val))
		return false;
	out.set(val.toObjectOrNull());
	return true;
}

template<typename T>
inline bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, T& out)
{
	return GetProperty(rq, obj, name, out);
}
inline bool GetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, JS::MutableHandleValue out)
{
	return GetProperty(rq, obj, name, out);
}
/**
 * Check the named property has been defined on the given object.
 */
inline bool HasProperty(const ScriptRequest& rq, JS::HandleValue obj, const char* name)
{
	if (!obj.isObject())
		return false;
	JS::RootedObject object(rq.cx, &obj.toObject());

	bool found;
	if (!JS_HasProperty(rq.cx, object, name, &found))
		return false;
	return found;
}

/**
 * Set the named property on the given object.
 */
template<typename PropType>
inline bool SetProperty(const ScriptRequest& rq, JS::HandleValue obj, PropType name, JS::HandleValue value, bool constant = false, bool enumerable = true)
{
	uint attrs = 0;
	if (constant)
		attrs |= JSPROP_READONLY | JSPROP_PERMANENT;
	if (enumerable)
		attrs |= JSPROP_ENUMERATE;

	if (!obj.isObject())
		return false;
	JS::RootedObject object(rq.cx, &obj.toObject());
	if constexpr (std::is_same_v<int, PropType>)
	{
		JS::RootedId id(rq.cx, INT_TO_JSID(name));
		return JS_DefinePropertyById(rq.cx, object, id, value, attrs);
	}
	else if constexpr (std::is_same_v<const char*, PropType>)
		return JS_DefineProperty(rq.cx, object, name, value, attrs);
	else
		return JS_DefineUCProperty(rq.cx, object, name, value, attrs);
}

template<typename T, typename PropType>
inline bool SetProperty(const ScriptRequest& rq, JS::HandleValue obj, PropType name, const T& value, bool constant = false, bool enumerable = true)
{
	JS::RootedValue val(rq.cx);
	Script::ToJSVal(rq, &val, value);
	return SetProperty<PropType>(rq, obj, name, val, constant, enumerable);
}

template<typename T>
inline bool SetPropertyInt(const ScriptRequest& rq, JS::HandleValue obj, int name, const T& value, bool constant = false, bool enumerable = true)
{
	return SetProperty<T, int>(rq, obj, name, value, constant, enumerable);
}

template<typename T>
inline bool GetObjectClassName(const ScriptRequest& rq, JS::HandleObject obj, T& name)
{
	JS::RootedValue constructor(rq.cx, JS::ObjectOrNullValue(JS_GetConstructor(rq.cx, obj)));
	return constructor.isObject() && Script::HasProperty(rq, constructor, "name") && Script::GetProperty(rq, constructor, "name", name);
}

/**
 * Get the name of the object's class. Note that inheritance may lead to unexpected results.
 */
template<typename T>
inline bool GetObjectClassName(const ScriptRequest& rq, JS::HandleValue val, T& name)
{
	JS::RootedObject obj(rq.cx, val.toObjectOrNull());
	if (!obj)
		return false;
	return GetObjectClassName(rq, obj, name);
}

inline bool FreezeObject(const ScriptRequest& rq, JS::HandleValue objVal, bool deep)
{
	if (!objVal.isObject())
		return false;

	JS::RootedObject obj(rq.cx, &objVal.toObject());

	if (deep)
		return JS_DeepFreezeObject(rq.cx, obj);
	else
		return JS_FreezeObject(rq.cx, obj);
}

/**
 * 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.
 */
inline bool EnumeratePropertyNames(const ScriptRequest& rq, JS::HandleValue objVal, bool enumerableOnly, std::vector<std::string>& out)
{
	if (!objVal.isObjectOrNull())
	{
		LOGERROR("EnumeratePropertyNames expected object type!");
		return false;
	}

	JS::RootedObject obj(rq.cx, &objVal.toObject());
	JS::RootedIdVector props(rq.cx);
	// This recurses up the prototype chain on its own.
	if (!js::GetPropertyKeys(rq.cx, obj, enumerableOnly? 0 : JSITER_HIDDEN, &props))
		return false;

	out.reserve(out.size() + props.length());
	for (size_t i = 0; i < props.length(); ++i)
	{
		JS::RootedId id(rq.cx, props[i]);
		JS::RootedValue val(rq.cx);
		if (!JS_IdToValue(rq.cx, id, &val))
			return false;

		// Ignore integer properties for now.
		// TODO: is this actually a thing in ECMAScript 6?
		if (!val.isString())
			continue;

		std::string propName;
		if (!FromJSVal(rq, val, propName))
			return false;

		out.emplace_back(std::move(propName));
	}

	return true;
}

/**
 * Create a plain object (i.e. {}). If it fails, returns undefined.
 */
inline JS::Value CreateObject(const ScriptRequest& rq)
{
	JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx));
	if (!obj)
		return JS::UndefinedValue();
	return JS::ObjectValue(*obj.get());
}

inline bool CreateObject(const ScriptRequest& rq, JS::MutableHandleValue objectValue)
{
	objectValue.set(CreateObject(rq));
	return !objectValue.isNullOrUndefined();
}

/**
 * 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 T, typename... Args>
inline bool CreateObject(const ScriptRequest& rq, JS::MutableHandleValue objectValue, const char* propertyName, const T& propertyValue, Args const&... args)
{
	JS::RootedValue val(rq.cx);
	ToJSVal(rq, &val, propertyValue);
	return CreateObject(rq, objectValue, args...) && SetProperty(rq, objectValue, propertyName, val, false, true);
}

/**
 * Sets the given value to a new JS object or Null Value in case of out-of-memory.
 */
inline bool CreateArray(const ScriptRequest& rq, JS::MutableHandleValue objectValue, size_t length = 0)
{
	objectValue.setObjectOrNull(JS::NewArrayObject(rq.cx, length));
	return !objectValue.isNullOrUndefined();
}

} // namespace Script

#endif // INCLUDED_SCRIPTINTERFACE_Object