-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathjsb_script_instance.cpp
More file actions
345 lines (290 loc) · 11.2 KB
/
Copy pathjsb_script_instance.cpp
File metadata and controls
345 lines (290 loc) · 11.2 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "jsb_script_instance.h"
#include "jsb_script.h"
#include "jsb_script_language.h"
#include "modules/gdscript/gdscript_rpc_callable.h"
GodotJSScriptInstanceBase::ScriptCallProfilingScope::ScriptCallProfilingScope(const ScriptProfilingInfo& p_info, const StringName& p_method)
: info_(p_info), method_(p_method)
{
start_time_ = OS::get_singleton()->get_ticks_usec();
}
GodotJSScriptInstanceBase::ScriptCallProfilingScope::~ScriptCallProfilingScope()
{
GodotJSScriptLanguage::get_singleton()->add_script_call_profile_info(
info_.path_,
info_.class_,
method_,
OS::get_singleton()->get_ticks_usec() - start_time_);
}
GodotJSScriptInstanceBase::~GodotJSScriptInstanceBase()
{
jsb_check(owner_);
// When script binding fails due to an invalid script, we delete the invalid script instance.
if (script_.is_valid())
{
jsb_check(script_->get_language());
const GodotJSScriptLanguage* lang = (GodotJSScriptLanguage*) script_->get_language();
MutexLock lock(lang->mutex_);
script_->instances_.erase(owner_);
}
}
jsb::ScriptClassInfoPtr GodotJSScriptInstance::get_script_class() const
{
return env_->get_script_class(class_id_);
}
void GodotJSScriptInstance::postbind()
{
// Store initial value for cached props
for (auto& it : this->script_->script_class_info_.properties)
{
if (it.value.cache)
{
Variant value;
env_->get_script_property_value(object_id_, it.value, value);
property_cache_.insert(it.key, value);
}
}
}
void GodotJSScriptInstance::cache_property(const StringName& name, const Variant& value)
{
property_cache_.insert(name, value);
}
bool GodotJSScriptInstance::set(const StringName& p_name, const Variant& p_value)
{
GodotJSScript* sptr = script_.ptr();
while (sptr)
{
if (const auto& it = sptr->script_class_info_.properties.find(p_name); it)
{
return env_->set_script_property_value(object_id_, it->value, p_value);
}
// TODO: Static variable?
if (const auto& it = sptr->script_class_info_.methods.find(jsb_string_name(_set)); it)
{
Variant name = p_name;
const Variant *args[2] = { &name, &p_value };
Callable::CallError err;
Variant ret = env_->call_script_method(class_id_, object_id_, jsb_string_name(_set), (const Variant **)args, 2, err);
if (err.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::BOOL && ret.operator bool()) {
return true;
}
}
sptr = sptr->base.ptr();
}
return false;
}
bool GodotJSScriptInstance::get(const StringName& p_name, Variant& r_ret) const
{
const Variant* cached_value = property_cache_.getptr(p_name);
if (cached_value)
{
r_ret = *cached_value;
return true;
}
GodotJSScript* sptr = script_.ptr();
while (sptr)
{
if (const auto& it = sptr->script_class_info_.properties.find(p_name); it)
{
return env_->get_script_property_value(object_id_, it->value, r_ret);
}
// TODO: constant?
// TODO: static variable?
// TODO: Inner class?
if (const auto& it = sptr->script_class_info_.signals.find(p_name); it)
{
r_ret = Signal(owner_, p_name);
return true;
}
if (const auto& it = sptr->script_class_info_.methods.find(p_name); it)
{
if (sptr->script_class_info_.rpc_config.has(p_name)) {
r_ret = Callable(memnew(GDScriptRPCCallable(owner_, p_name)));
return true;
} else {
if (!it->value.is_static())
{
r_ret = Callable(owner_, p_name);
return true;
} else {
// TODO: Warp static method to Callable
}
}
}
if (const auto& it = sptr->script_class_info_.methods.find(jsb_string_name(_get)); it)
{
Variant name = p_name;
const Variant *args[1] = { &name };
Callable::CallError err;
Variant ret = env_->call_script_method(class_id_, object_id_, jsb_string_name(_get), (const Variant **)args, 1, err);
if (err.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL)
{
r_ret = ret;
return true;
}
}
sptr = sptr->base.ptr();
}
return false;
}
void GodotJSScriptInstance::get_property_list(List<PropertyInfo>* p_properties) const
{
GodotJSScript* sptr = script_.ptr();
HashSet<String> properties;
while (sptr)
{
if (const auto& it = sptr->script_class_info_.methods.find(jsb_string_name(_get_property_list)); it)
{
Callable::CallError err;
Variant ret = env_->call_script_method(class_id_, object_id_, jsb_string_name(_get_property_list), nullptr, 0, err);
if (err.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL)
{
ERR_FAIL_COND_MSG(ret.get_type() != Variant::ARRAY, "Wrong type for _get_property_list, must be an array of dictionaries.");
Array arr = ret;
for (int i = 0; i < arr.size(); i++)
{
Dictionary d = arr[i];
ERR_CONTINUE(!d.has("name"));
ERR_CONTINUE(!d.has("type"));
PropertyInfo pinfo = PropertyInfo::from_dict(d);
ERR_CONTINUE(pinfo.name.is_empty() && (pinfo.usage & PROPERTY_USAGE_STORAGE));
ERR_CONTINUE(pinfo.type < 0 || pinfo.type >= Variant::VARIANT_MAX);
ERR_CONTINUE_MSG(properties.has(pinfo.name), vformat("Duplicate property \"%s\" in script: %s", pinfo.name, script_->get_path()));
p_properties->push_back(pinfo);
properties.insert(pinfo.name);
}
}
}
#ifdef TOOLS_ENABLED
p_properties->push_back(sptr->get_class_category());
#endif
for (const auto& it : sptr->script_class_info_.properties)
{
ERR_CONTINUE_MSG(properties.has(it.value.name), vformat("Duplicate property \"%s\" in script: %s", it.value.name, script_->get_path()));
p_properties->push_back((PropertyInfo) it.value);
}
sptr = sptr->base.ptr();
}
for (PropertyInfo &prop : *p_properties) {
validate_property(prop);
}
}
const Variant GodotJSScriptInstance::get_rpc_config() const
{
return get_script_class()->rpc_config;
}
Variant::Type GodotJSScriptInstance::get_property_type(const StringName& p_name, bool* r_is_valid) const
{
GodotJSScript* sptr = script_.ptr();
while (sptr) {
if (const HashMap<StringName, jsb::ScriptPropertyInfo>::ConstIterator it = sptr->script_class_info_.properties.find(p_name))
{
if (r_is_valid) *r_is_valid = true;
return it->value.type;
}
sptr = sptr->base.ptr();
}
if (r_is_valid) *r_is_valid = false;
return Variant::NIL;
}
void GodotJSScriptInstance::validate_property(PropertyInfo& p_property) const
{
GodotJSScript* sptr = script_.ptr();
while(sptr)
{
if (const auto& it = sptr->script_class_info_.methods.find(jsb_string_name(_validate_property)); it)
{
Variant property = (Dictionary)p_property;
const Variant *args[1] = { &property };
Callable::CallError err;
Variant ret = env_->call_script_method(class_id_, object_id_, jsb_string_name(_validate_property), (const Variant **)args, 1, err);
if (err.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL)
{
p_property = PropertyInfo::from_dict(property);
}
}
sptr = sptr->base.ptr();
}
}
bool GodotJSScriptInstance::property_can_revert(const StringName& p_name) const
{
GodotJSScript* sptr = script_.ptr();
while(sptr)
{
if (const auto& it = sptr->script_class_info_.methods.find(jsb_string_name(_property_can_revert)); it)
{
Variant name = p_name;
const Variant *args[1] = { &name };
Callable::CallError err;
Variant ret = env_->call_script_method(class_id_, object_id_, jsb_string_name(_property_can_revert), args, 1, err);
if (err.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::BOOL && ret.operator bool()) {
return true;
}
}
sptr = sptr->base.ptr();
}
return false;
}
bool GodotJSScriptInstance::property_get_revert(const StringName& p_name, Variant& r_ret) const
{
GodotJSScript* sptr = script_.ptr();
while(sptr)
{
if (const auto& it = sptr->script_class_info_.methods.find(jsb_string_name(_property_get_revert)); it)
{
Variant name = p_name;
const Variant *args[1] = { &name };
Callable::CallError err;
Variant ret = env_->call_script_method(class_id_, object_id_, jsb_string_name(_property_get_revert), args, 1, err);
if (err.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::BOOL && ret.operator bool()) {
r_ret = ret;
return true;
}
}
sptr = sptr->base.ptr();
}
return false;
}
void GodotJSScriptInstance::get_method_list(List<MethodInfo>* p_list) const
{
script_->get_script_method_list(p_list);
}
bool GodotJSScriptInstance::has_method(const StringName& p_method) const
{
return script_->has_method(p_method);
}
Variant GodotJSScriptInstance::callp(const StringName& p_method, const Variant** p_args, int p_argcount, Callable::CallError& r_error)
{
#if JSB_DEBUG
if (profiling_info_.path_.is_empty())
{
profiling_info_.path_ = script_->get_path();
profiling_info_.class_ = get_script_class()->js_class_name;
}
ScriptCallProfilingScope profiling_scope(profiling_info_, p_method);
#endif
return env_->call_script_method(class_id_, object_id_, p_method, p_args, p_argcount, r_error);
}
void GodotJSScriptInstance::notification(int p_notification, bool p_reversed)
{
if (p_reversed &&
(p_notification == Object::NOTIFICATION_PREDELETE
|| p_notification == Object::NOTIFICATION_PREDELETE_CLEANUP))
{
// the JS counterpart is garbage collected (which finally caused Godot Object deleting)
// so, some of the reversed notifications can not be handled by script instances
return;
}
// Check if environment is being disposed to avoid calling into a destroyed JS context
if (!env_ || env_->is_disposing())
{
return;
}
// since `NOTIFICATION_READY` is not reversed, `notification` will be posted after `callp`.
// so, we can't `call_prelude` here with `NOTIFICATION_READY`
//TODO find the method named `_notification`, cal it with `p_notification` as `argv`
//TODO call it at all type levels? @seealso `GDScriptInstance::notification`
Variant value = p_notification;
const Variant* argv[] = {&value};
Callable::CallError error;
callp(jsb_string_name(_notification), argv, 1, error);
}