1/**************************************************************************/
2/* script_instance.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef SCRIPT_INSTANCE_H
32#define SCRIPT_INSTANCE_H
33
34#include "core/object/ref_counted.h"
35
36class Script;
37class ScriptLanguage;
38
39class ScriptInstance {
40public:
41 virtual bool set(const StringName &p_name, const Variant &p_value) = 0;
42 virtual bool get(const StringName &p_name, Variant &r_ret) const = 0;
43 virtual void get_property_list(List<PropertyInfo> *p_properties) const = 0;
44 virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const = 0;
45 virtual void validate_property(PropertyInfo &p_property) const = 0;
46
47 virtual bool property_can_revert(const StringName &p_name) const = 0;
48 virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const = 0;
49
50 virtual Object *get_owner() { return nullptr; }
51 virtual void get_property_state(List<Pair<StringName, Variant>> &state);
52
53 virtual void get_method_list(List<MethodInfo> *p_list) const = 0;
54 virtual bool has_method(const StringName &p_method) const = 0;
55
56 virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) = 0;
57
58 template <typename... VarArgs>
59 Variant call(const StringName &p_method, VarArgs... p_args) {
60 Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
61 const Variant *argptrs[sizeof...(p_args) + 1];
62 for (uint32_t i = 0; i < sizeof...(p_args); i++) {
63 argptrs[i] = &args[i];
64 }
65 Callable::CallError cerr;
66 return callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr);
67 }
68
69 virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error); // implement if language supports const functions
70 virtual void notification(int p_notification, bool p_reversed = false) = 0;
71 virtual String to_string(bool *r_valid) {
72 if (r_valid) {
73 *r_valid = false;
74 }
75 return String();
76 }
77
78 //this is used by script languages that keep a reference counter of their own
79 //you can make make Ref<> not die when it reaches zero, so deleting the reference
80 //depends entirely from the script
81
82 virtual void refcount_incremented() {}
83 virtual bool refcount_decremented() { return true; } //return true if it can die
84
85 virtual Ref<Script> get_script() const = 0;
86
87 virtual bool is_placeholder() const { return false; }
88
89 virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid);
90 virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid);
91
92 virtual const Variant get_rpc_config() const;
93
94 virtual ScriptLanguage *get_language() = 0;
95 virtual ~ScriptInstance();
96};
97
98#endif // SCRIPT_INSTANCE_H
99