Nativescript 1.1
implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
This commit is contained in:
@@ -21,19 +21,22 @@ def generate_bindings(path):
|
||||
|
||||
impl = generate_class_implementation(icalls, used_classes, c)
|
||||
|
||||
header_file = open("include/" + strip_name(c["name"]) + ".hpp", "w+")
|
||||
header_file = open("include/gen/" + strip_name(c["name"]) + ".hpp", "w+")
|
||||
header_file.write(header)
|
||||
|
||||
source_file = open("src/" + strip_name(c["name"]) + ".cpp", "w+")
|
||||
source_file = open("src/gen/" + strip_name(c["name"]) + ".cpp", "w+")
|
||||
source_file.write(impl)
|
||||
|
||||
|
||||
icall_header_file = open("src/__icalls.hpp", "w+")
|
||||
icall_header_file = open("src/gen/__icalls.hpp", "w+")
|
||||
icall_header_file.write(generate_icall_header(icalls))
|
||||
|
||||
icall_source_file = open("src/__icalls.cpp", "w+")
|
||||
icall_source_file = open("src/gen/__icalls.cpp", "w+")
|
||||
icall_source_file.write(generate_icall_implementation(icalls))
|
||||
|
||||
register_types_file = open("src/gen/__register_types.cpp", "w+")
|
||||
register_types_file.write(generate_type_registry(classes))
|
||||
|
||||
|
||||
def is_reference_type(t):
|
||||
for c in classes:
|
||||
@@ -79,6 +82,8 @@ def generate_class_header(used_classes, c):
|
||||
# so don't include it here because it's not needed
|
||||
if class_name != "Object" and class_name != "Reference":
|
||||
source.append("#include <core/Ref.hpp>")
|
||||
else:
|
||||
source.append("#include <core/TagDB.hpp>")
|
||||
|
||||
|
||||
included = []
|
||||
@@ -100,7 +105,6 @@ def generate_class_header(used_classes, c):
|
||||
if c["base_class"] != "":
|
||||
source.append("#include \"" + strip_name(c["base_class"]) + ".hpp\"")
|
||||
|
||||
|
||||
|
||||
source.append("namespace godot {")
|
||||
source.append("")
|
||||
@@ -118,15 +122,40 @@ def generate_class_header(used_classes, c):
|
||||
vararg_templates = ""
|
||||
|
||||
# generate the class definition here
|
||||
source.append("class " + class_name + ("" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {")
|
||||
|
||||
source.append("class " + class_name + (" : public _Wrapped" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {")
|
||||
|
||||
if c["base_class"] == "":
|
||||
source.append("public: enum { ___CLASS_IS_SCRIPT = 0, };")
|
||||
source.append("private:")
|
||||
source.append("")
|
||||
|
||||
if c["singleton"]:
|
||||
source.append("\tstatic " + class_name + " *_singleton;")
|
||||
source.append("")
|
||||
source.append("\t" + class_name + "();")
|
||||
source.append("")
|
||||
|
||||
|
||||
source.append("public:")
|
||||
source.append("")
|
||||
|
||||
# ___get_class_name
|
||||
source.append("\tstatic inline char *___get_class_name() { return (char *) \"" + strip_name(c["name"]) + "\"; }")
|
||||
|
||||
source.append("\tstatic inline Object *___get_from_variant(Variant a) { return (Object *) a; }")
|
||||
if c["singleton"]:
|
||||
source.append("\tstatic inline " + class_name + " *get_singleton()")
|
||||
source.append("\t{")
|
||||
source.append("\t\tif (!" + class_name + "::_singleton) {")
|
||||
source.append("\t\t\t" + class_name + "::_singleton = new " + class_name + ";")
|
||||
source.append("\t\t}")
|
||||
source.append("\t\treturn " + class_name + "::_singleton;")
|
||||
source.append("\t}")
|
||||
source.append("")
|
||||
|
||||
# godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");"
|
||||
|
||||
# ___get_class_name
|
||||
source.append("\tstatic inline const char *___get_class_name() { return (const char *) \"" + strip_name(c["name"]) + "\"; }")
|
||||
|
||||
source.append("\tstatic inline Object *___get_from_variant(Variant a) { godot_object *o = (godot_object*) a; return (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, o); }")
|
||||
|
||||
enum_values = []
|
||||
|
||||
@@ -146,17 +175,26 @@ def generate_class_header(used_classes, c):
|
||||
|
||||
|
||||
if c["instanciable"]:
|
||||
source.append("\tstatic void *operator new(size_t);")
|
||||
|
||||
source.append("\tstatic void operator delete(void *);")
|
||||
source.append("")
|
||||
source.append("")
|
||||
source.append("\tstatic " + class_name + " *_new();")
|
||||
|
||||
source.append("\n\t// methods")
|
||||
|
||||
|
||||
if class_name == "Object":
|
||||
source.append("#ifndef GODOT_CPP_NO_OBJECT_CAST")
|
||||
source.append("\ttemplate<class T>")
|
||||
source.append("\tstatic T *cast_to(const Object *obj);")
|
||||
source.append("#endif")
|
||||
source.append("")
|
||||
|
||||
for method in c["methods"]:
|
||||
|
||||
method_signature = ""
|
||||
|
||||
method_signature += "static " if c["singleton"] else ""
|
||||
# TODO decide what to do about virtual methods
|
||||
# method_signature += "virtual " if method["is_virtual"] else ""
|
||||
method_signature += make_gdnative_type(method["return_type"])
|
||||
method_name = escape_cpp(method["name"])
|
||||
method_signature += method_name + "("
|
||||
@@ -224,7 +262,7 @@ def generate_class_header(used_classes, c):
|
||||
vararg_templates += "\ttemplate <class... Args> " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "Array::make(args...));\n\t}\n"""
|
||||
method_signature += "const Array& __var_args = Array()"
|
||||
|
||||
method_signature += ")" + (" const" if method["is_const"] and not c["singleton"] else "")
|
||||
method_signature += ")" + (" const" if method["is_const"] else "")
|
||||
|
||||
|
||||
source.append("\t" + method_signature + ";")
|
||||
@@ -234,11 +272,10 @@ def generate_class_header(used_classes, c):
|
||||
source.append("")
|
||||
|
||||
|
||||
|
||||
source.append("}")
|
||||
source.append("")
|
||||
|
||||
|
||||
|
||||
source.append("#endif")
|
||||
|
||||
|
||||
@@ -279,23 +316,20 @@ def generate_class_implementation(icalls, used_classes, c):
|
||||
source.append("namespace godot {")
|
||||
|
||||
|
||||
core_object_name = ("___static_object_" + strip_name(c["name"])) if c["singleton"] else "this"
|
||||
core_object_name = "this"
|
||||
|
||||
|
||||
source.append("")
|
||||
source.append("")
|
||||
|
||||
|
||||
if c["singleton"]:
|
||||
source.append("static godot_object *" + core_object_name + ";")
|
||||
source.append("" + class_name + " *" + class_name + "::_singleton = NULL;")
|
||||
source.append("")
|
||||
source.append("")
|
||||
|
||||
# FIXME Test if inlining has a huge impact on binary size
|
||||
source.append("static inline void ___singleton_init()")
|
||||
source.append("{")
|
||||
source.append("\tif (" + core_object_name + " == nullptr) {")
|
||||
source.append("\t\t" + core_object_name + " = godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");")
|
||||
source.append("\t}")
|
||||
source.append(class_name + "::" + class_name + "() {")
|
||||
source.append("\t_owner = godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");")
|
||||
source.append("}")
|
||||
|
||||
source.append("")
|
||||
@@ -304,18 +338,14 @@ def generate_class_implementation(icalls, used_classes, c):
|
||||
|
||||
|
||||
if c["instanciable"]:
|
||||
source.append("void *" + strip_name(c["name"]) + "::operator new(size_t)")
|
||||
source.append(class_name + " *" + strip_name(c["name"]) + "::_new()")
|
||||
source.append("{")
|
||||
source.append("\treturn godot::api->godot_get_class_constructor((char *)\"" + c["name"] + "\")();")
|
||||
source.append("}")
|
||||
|
||||
source.append("void " + strip_name(c["name"]) + "::operator delete(void *ptr)")
|
||||
source.append("{")
|
||||
source.append("\tgodot::api->godot_object_destroy((godot_object *)ptr);")
|
||||
source.append("\treturn (" + class_name + " *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, godot::api->godot_get_class_constructor((char *)\"" + c["name"] + "\")());")
|
||||
source.append("}")
|
||||
|
||||
for method in c["methods"]:
|
||||
method_signature = ""
|
||||
|
||||
|
||||
method_signature += make_gdnative_type(method["return_type"])
|
||||
method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "("
|
||||
@@ -332,20 +362,23 @@ def generate_class_implementation(icalls, used_classes, c):
|
||||
method_signature += ", "
|
||||
method_signature += "const Array& __var_args"
|
||||
|
||||
method_signature += ")" + (" const" if method["is_const"] and not c["singleton"] else "")
|
||||
method_signature += ")" + (" const" if method["is_const"] else "")
|
||||
|
||||
source.append(method_signature + " {")
|
||||
|
||||
|
||||
if method["name"] == "free":
|
||||
# dirty hack because Object::free is marked virtual but doesn't actually exist...
|
||||
source.append("\tgodot::api->godot_object_destroy(_owner);")
|
||||
source.append("}")
|
||||
source.append("")
|
||||
continue
|
||||
else:
|
||||
|
||||
|
||||
|
||||
if c["singleton"]:
|
||||
source.append("\t___singleton_init();")
|
||||
|
||||
|
||||
source.append("\tstatic godot_method_bind *mb = nullptr;")
|
||||
source.append("\tif (mb == nullptr) {")
|
||||
source.append("\t\tmb = godot::api->godot_method_bind_get_method(\"" + c["name"] +"\", \"" + method["name"] + "\");")
|
||||
source.append("\t}")
|
||||
source.append("\tstatic godot_method_bind *mb = nullptr;")
|
||||
source.append("\tif (mb == nullptr) {")
|
||||
source.append("\t\tmb = godot::api->godot_method_bind_get_method(\"" + c["name"] +"\", \"" + method["name"] + "\");")
|
||||
source.append("\t}")
|
||||
|
||||
return_statement = ""
|
||||
|
||||
@@ -408,10 +441,17 @@ def generate_class_implementation(icalls, used_classes, c):
|
||||
source.append("")
|
||||
|
||||
source.append("\tVariant __result;")
|
||||
source.append("\t*(godot_variant *) &__result = godot::api->godot_method_bind_call(mb, (godot_object *) " + core_object_name + ", (const godot_variant **) __args, " + size + ", nullptr);")
|
||||
source.append("\t*(godot_variant *) &__result = godot::api->godot_method_bind_call(mb, ((const Object *) " + core_object_name + ")->_owner, (const godot_variant **) __args, " + size + ", nullptr);")
|
||||
|
||||
source.append("")
|
||||
|
||||
if is_class_type(method["return_type"]):
|
||||
source.append("\tObject *obj = Object::___get_from_variant(__result);")
|
||||
source.append("\tif (obj->has_method(\"reference\"))")
|
||||
source.append("\t\tobj->callv(\"reference\", Array());")
|
||||
|
||||
source.append("")
|
||||
|
||||
|
||||
for i, argument in enumerate(method["arguments"]):
|
||||
source.append("\tgodot::api->godot_variant_destroy((godot_variant *) &__given_args[" + str(i) + "]);")
|
||||
@@ -424,7 +464,7 @@ def generate_class_implementation(icalls, used_classes, c):
|
||||
if is_reference_type(method["return_type"]):
|
||||
cast += "Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(__result);"
|
||||
else:
|
||||
cast += "(" + strip_name(method["return_type"]) + " *) (Object *) __result;"
|
||||
cast += "(" + strip_name(method["return_type"]) + " *) " + strip_name(method["return_type"] + "::___get_from_variant(") + "__result);"
|
||||
else:
|
||||
cast += "__result;"
|
||||
source.append("\treturn " + cast)
|
||||
@@ -445,7 +485,7 @@ def generate_class_implementation(icalls, used_classes, c):
|
||||
|
||||
icall_name = get_icall_name(icall_sig)
|
||||
|
||||
return_statement += icall_name + "(mb, (godot_object *) " + core_object_name
|
||||
return_statement += icall_name + "(mb, (const Object *) " + core_object_name
|
||||
|
||||
for arg in method["arguments"]:
|
||||
return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if is_reference_type(arg["type"]) else "")
|
||||
@@ -494,7 +534,7 @@ def generate_icall_header(icalls):
|
||||
|
||||
method_signature = ""
|
||||
|
||||
method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, godot_object *inst"
|
||||
method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst"
|
||||
|
||||
for arg in args:
|
||||
method_signature += ", const "
|
||||
@@ -547,7 +587,7 @@ def generate_icall_implementation(icalls):
|
||||
|
||||
method_signature = ""
|
||||
|
||||
method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, godot_object *inst"
|
||||
method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst"
|
||||
|
||||
for i, arg in enumerate(args):
|
||||
method_signature += ", const "
|
||||
@@ -568,7 +608,7 @@ def generate_icall_implementation(icalls):
|
||||
source.append(method_signature + " {")
|
||||
|
||||
if ret_type != "void":
|
||||
source.append("\t" + return_type(ret_type) + "ret;")
|
||||
source.append("\t" + ("godot_object *" if is_class_type(ret_type) else return_type(ret_type)) + "ret;")
|
||||
if is_class_type(ret_type):
|
||||
source.append("\tret = nullptr;")
|
||||
|
||||
@@ -581,7 +621,7 @@ def generate_icall_implementation(icalls):
|
||||
if is_primitive(arg) or is_core_type(arg):
|
||||
wrapped_argument += "(void *) &arg" + str(i)
|
||||
else:
|
||||
wrapped_argument += "(void *) arg" + str(i)
|
||||
wrapped_argument += "(void *) arg" + str(i) + "->_owner"
|
||||
|
||||
wrapped_argument += ","
|
||||
source.append(wrapped_argument)
|
||||
@@ -589,10 +629,17 @@ def generate_icall_implementation(icalls):
|
||||
source.append("\t};")
|
||||
source.append("")
|
||||
|
||||
source.append("\tgodot::api->godot_method_bind_ptrcall(mb, inst, args, " + ("nullptr" if ret_type == "void" else "&ret") + ");")
|
||||
source.append("\tgodot::api->godot_method_bind_ptrcall(mb, inst->_owner, args, " + ("nullptr" if ret_type == "void" else "&ret") + ");")
|
||||
|
||||
if ret_type != "void":
|
||||
source.append("\treturn ret;")
|
||||
if is_class_type(ret_type):
|
||||
source.append("\tif (ret) {")
|
||||
source.append("\t\treturn (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, ret);")
|
||||
source.append("\t}")
|
||||
source.append("")
|
||||
source.append("\treturn (Object *) ret;")
|
||||
else:
|
||||
source.append("\treturn ret;")
|
||||
|
||||
source.append("}")
|
||||
|
||||
@@ -604,8 +651,44 @@ def generate_icall_implementation(icalls):
|
||||
|
||||
|
||||
|
||||
def generate_type_registry(classes):
|
||||
source = []
|
||||
|
||||
source.append("#include \"TagDB.hpp\"")
|
||||
source.append("#include <typeinfo>")
|
||||
source.append("\n")
|
||||
|
||||
for c in classes:
|
||||
source.append("#include <" + strip_name(c["name"]) + ".hpp>")
|
||||
|
||||
source.append("")
|
||||
source.append("")
|
||||
|
||||
source.append("namespace godot {")
|
||||
|
||||
source.append("void ___register_types()")
|
||||
source.append("{")
|
||||
|
||||
for c in classes:
|
||||
class_name = strip_name(c["name"])
|
||||
base_class_name = strip_name(c["base_class"])
|
||||
|
||||
class_type_hash = "typeid(" + class_name + ").hash_code()"
|
||||
|
||||
base_class_type_hash = "typeid(" + base_class_name + ").hash_code()"
|
||||
|
||||
if base_class_name == "":
|
||||
base_class_type_hash = "0"
|
||||
|
||||
source.append("\tgodot::_TagDB::register_global_type(\"" + class_name + "\", " + class_type_hash + ", " + base_class_type_hash + ");")
|
||||
|
||||
source.append("}")
|
||||
|
||||
source.append("")
|
||||
source.append("}")
|
||||
|
||||
|
||||
return "\n".join(source)
|
||||
|
||||
|
||||
|
||||
@@ -664,6 +747,8 @@ def get_used_classes(c):
|
||||
|
||||
|
||||
def strip_name(name):
|
||||
if len(name) == 0:
|
||||
return name
|
||||
if name[0] == '_':
|
||||
return name[1:]
|
||||
return name
|
||||
|
||||
Reference in New Issue
Block a user