Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions core/object/method_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**************************************************************************/
/* method_info.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "method_info.h"
#include "core/variant/typed_array.h"

MethodInfo::operator Dictionary() const {
Dictionary d;
d["name"] = name;
d["args"] = convert_property_list(arguments);
Array da;
for (int i = 0; i < default_arguments.size(); i++) {
da.push_back(default_arguments[i]);
}
d["default_args"] = da;
d["flags"] = flags;
d["id"] = id;
Dictionary r = return_val;
d["return"] = r;
return d;
}

MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
MethodInfo mi;

if (p_dict.has("name")) {
mi.name = p_dict["name"];
}
Array args;
if (p_dict.has("args")) {
args = p_dict["args"];
}

for (const Variant &arg : args) {
Dictionary d = arg;
mi.arguments.push_back(PropertyInfo::from_dict(d));
}
Array defargs;
if (p_dict.has("default_args")) {
defargs = p_dict["default_args"];
}
for (const Variant &defarg : defargs) {
mi.default_arguments.push_back(defarg);
}

if (p_dict.has("return")) {
mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
}

if (p_dict.has("flags")) {
mi.flags = p_dict["flags"];
}

return mi;
}

uint32_t MethodInfo::get_compatibility_hash() const {
bool has_return = (return_val.type != Variant::NIL) || (return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);

uint32_t hash = hash_murmur3_one_32(has_return);
hash = hash_murmur3_one_32(arguments.size(), hash);

if (has_return) {
hash = hash_murmur3_one_32(return_val.type, hash);
if (return_val.class_name != StringName()) {
hash = hash_murmur3_one_32(return_val.class_name.hash(), hash);
}
}

for (const PropertyInfo &arg : arguments) {
hash = hash_murmur3_one_32(arg.type, hash);
if (arg.class_name != StringName()) {
hash = hash_murmur3_one_32(arg.class_name.hash(), hash);
}
}

hash = hash_murmur3_one_32(default_arguments.size(), hash);
for (const Variant &v : default_arguments) {
hash = hash_murmur3_one_32(v.hash(), hash);
}

hash = hash_murmur3_one_32(flags & METHOD_FLAG_CONST ? 1 : 0, hash);
hash = hash_murmur3_one_32(flags & METHOD_FLAG_VARARG ? 1 : 0, hash);

return hash_fmix32(hash);
}
123 changes: 123 additions & 0 deletions core/object/method_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**************************************************************************/
/* method_info.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "core/variant/variant.h"
#include "property_info.h"

enum MethodFlags {
METHOD_FLAG_NORMAL = 1,
METHOD_FLAG_EDITOR = 2,
METHOD_FLAG_CONST = 4,
METHOD_FLAG_VIRTUAL = 8,
METHOD_FLAG_VARARG = 16,
METHOD_FLAG_STATIC = 32,
METHOD_FLAG_OBJECT_CORE = 64,
METHOD_FLAG_VIRTUAL_REQUIRED = 128,
METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
};

struct MethodInfo {
String name;
PropertyInfo return_val;
uint32_t flags = METHOD_FLAGS_DEFAULT;
int id = 0;
Vector<PropertyInfo> arguments;
Vector<Variant> default_arguments;
int return_val_metadata = 0;
Vector<int> arguments_metadata;

int get_argument_meta(int p_arg) const {
ERR_FAIL_COND_V(p_arg < -1 || p_arg > arguments.size(), 0);
if (p_arg == -1) {
return return_val_metadata;
}
return arguments_metadata.size() > p_arg ? arguments_metadata[p_arg] : 0;
}

inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id && name == p_method.name; }
inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }

operator Dictionary() const;

static MethodInfo from_dict(const Dictionary &p_dict);

uint32_t get_compatibility_hash() const;

MethodInfo() {}

explicit MethodInfo(const GDExtensionMethodInfo &pinfo) :
name(*reinterpret_cast<StringName *>(pinfo.name)),
return_val(PropertyInfo(pinfo.return_value)),
flags(pinfo.flags),
id(pinfo.id) {
for (uint32_t i = 0; i < pinfo.argument_count; i++) {
arguments.push_back(PropertyInfo(pinfo.arguments[i]));
}
const Variant *def_values = (const Variant *)pinfo.default_arguments;
for (uint32_t j = 0; j < pinfo.default_argument_count; j++) {
default_arguments.push_back(def_values[j]);
}
}

MethodInfo(const String &p_name) { name = p_name; }

template <typename... VarArgs>
MethodInfo(const String &p_name, VarArgs... p_params) {
name = p_name;
arguments = Vector<PropertyInfo>{ p_params... };
}

MethodInfo(Variant::Type ret) { return_val.type = ret; }
MethodInfo(Variant::Type ret, const String &p_name) {
return_val.type = ret;
name = p_name;
}

template <typename... VarArgs>
MethodInfo(Variant::Type ret, const String &p_name, VarArgs... p_params) {
name = p_name;
return_val.type = ret;
arguments = Vector<PropertyInfo>{ p_params... };
}

MethodInfo(const PropertyInfo &p_ret, const String &p_name) {
return_val = p_ret;
name = p_name;
}

template <typename... VarArgs>
MethodInfo(const PropertyInfo &p_ret, const String &p_name, VarArgs... p_params) {
return_val = p_ret;
name = p_name;
arguments = Vector<PropertyInfo>{ p_params... };
}
};
140 changes: 0 additions & 140 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,146 +82,6 @@ struct _ObjectSignalLock {

#define OBJ_SIGNAL_LOCK _ObjectSignalLock _signal_lock(this);

PropertyInfo::operator Dictionary() const {
Dictionary d;
d["name"] = name;
d["class_name"] = class_name;
d["type"] = type;
d["hint"] = hint;
d["hint_string"] = hint_string;
d["usage"] = usage;
return d;
}

PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) {
PropertyInfo pi;

if (p_dict.has("type")) {
pi.type = Variant::Type(int(p_dict["type"]));
}

if (p_dict.has("name")) {
pi.name = p_dict["name"];
}

if (p_dict.has("class_name")) {
pi.class_name = p_dict["class_name"];
}

if (p_dict.has("hint")) {
pi.hint = PropertyHint(int(p_dict["hint"]));
}

if (p_dict.has("hint_string")) {
pi.hint_string = p_dict["hint_string"];
}

if (p_dict.has("usage")) {
pi.usage = p_dict["usage"];
}

return pi;
}

TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list) {
TypedArray<Dictionary> va;
for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
va.push_back(Dictionary(E->get()));
}

return va;
}

TypedArray<Dictionary> convert_property_list(const Vector<PropertyInfo> &p_vector) {
TypedArray<Dictionary> va;
for (const PropertyInfo &E : p_vector) {
va.push_back(Dictionary(E));
}

return va;
}

MethodInfo::operator Dictionary() const {
Dictionary d;
d["name"] = name;
d["args"] = convert_property_list(arguments);
Array da;
for (int i = 0; i < default_arguments.size(); i++) {
da.push_back(default_arguments[i]);
}
d["default_args"] = da;
d["flags"] = flags;
d["id"] = id;
Dictionary r = return_val;
d["return"] = r;
return d;
}

MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
MethodInfo mi;

if (p_dict.has("name")) {
mi.name = p_dict["name"];
}
Array args;
if (p_dict.has("args")) {
args = p_dict["args"];
}

for (const Variant &arg : args) {
Dictionary d = arg;
mi.arguments.push_back(PropertyInfo::from_dict(d));
}
Array defargs;
if (p_dict.has("default_args")) {
defargs = p_dict["default_args"];
}
for (const Variant &defarg : defargs) {
mi.default_arguments.push_back(defarg);
}

if (p_dict.has("return")) {
mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
}

if (p_dict.has("flags")) {
mi.flags = p_dict["flags"];
}

return mi;
}

uint32_t MethodInfo::get_compatibility_hash() const {
bool has_return = (return_val.type != Variant::NIL) || (return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);

uint32_t hash = hash_murmur3_one_32(has_return);
hash = hash_murmur3_one_32(arguments.size(), hash);

if (has_return) {
hash = hash_murmur3_one_32(return_val.type, hash);
if (return_val.class_name != StringName()) {
hash = hash_murmur3_one_32(return_val.class_name.hash(), hash);
}
}

for (const PropertyInfo &arg : arguments) {
hash = hash_murmur3_one_32(arg.type, hash);
if (arg.class_name != StringName()) {
hash = hash_murmur3_one_32(arg.class_name.hash(), hash);
}
}

hash = hash_murmur3_one_32(default_arguments.size(), hash);
for (const Variant &v : default_arguments) {
hash = hash_murmur3_one_32(v.hash(), hash);
}

hash = hash_murmur3_one_32(flags & METHOD_FLAG_CONST ? 1 : 0, hash);
hash = hash_murmur3_one_32(flags & METHOD_FLAG_VARARG ? 1 : 0, hash);

return hash_fmix32(hash);
}

Object::Connection::operator Variant() const {
Dictionary d;
d["signal"] = signal;
Expand Down
Loading