Merge pull request #433 from Zylann/fix_ref_in_object

Don't use Ref<T> in Object.hpp
pull/436/head
Bastiaan Olij 2020-08-09 15:39:36 +10:00 committed by GitHub
commit cd69b58bb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 16 deletions

View File

@ -46,11 +46,11 @@ def is_reference_type(t):
return True return True
return False return False
def make_gdnative_type(t): def make_gdnative_type(t, ref_allowed):
if is_enum(t): if is_enum(t):
return remove_enum_prefix(t) + " " return remove_enum_prefix(t) + " "
elif is_class_type(t): elif is_class_type(t):
if is_reference_type(t): if is_reference_type(t) and ref_allowed:
return "Ref<" + strip_name(t) + "> " return "Ref<" + strip_name(t) + "> "
else: else:
return strip_name(t) + " *" return strip_name(t) + " *"
@ -83,8 +83,10 @@ def generate_class_header(used_classes, c):
# so don't include it here because it's not needed # so don't include it here because it's not needed
if class_name != "Object" and class_name != "Reference": if class_name != "Object" and class_name != "Reference":
source.append("#include <core/Ref.hpp>") source.append("#include <core/Ref.hpp>")
ref_allowed = True
else: else:
source.append("#include <core/TagDB.hpp>") source.append("#include <core/TagDB.hpp>")
ref_allowed = False
included = [] included = []
@ -206,7 +208,7 @@ def generate_class_header(used_classes, c):
# TODO decide what to do about virtual methods # TODO decide what to do about virtual methods
# method_signature += "virtual " if method["is_virtual"] else "" # method_signature += "virtual " if method["is_virtual"] else ""
method_signature += make_gdnative_type(method["return_type"]) method_signature += make_gdnative_type(method["return_type"], ref_allowed)
method_name = escape_cpp(method["name"]) method_name = escape_cpp(method["name"])
method_signature += method_name + "(" method_signature += method_name + "("
@ -215,7 +217,7 @@ def generate_class_header(used_classes, c):
method_arguments = "" method_arguments = ""
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
method_signature += "const " + make_gdnative_type(argument["type"]) method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
argument_name = escape_cpp(argument["name"]) argument_name = escape_cpp(argument["name"])
method_signature += argument_name method_signature += argument_name
method_arguments += argument_name method_arguments += argument_name
@ -298,6 +300,9 @@ def generate_class_header(used_classes, c):
def generate_class_implementation(icalls, used_classes, c): def generate_class_implementation(icalls, used_classes, c):
class_name = strip_name(c["name"]) class_name = strip_name(c["name"])
ref_allowed = class_name != "Object" and class_name != "Reference"
source = [] source = []
source.append("#include \"" + class_name + ".hpp\"") source.append("#include \"" + class_name + ".hpp\"")
source.append("") source.append("")
@ -367,12 +372,11 @@ def generate_class_implementation(icalls, used_classes, c):
for method in c["methods"]: for method in c["methods"]:
method_signature = "" method_signature = ""
method_signature += make_gdnative_type(method["return_type"], ref_allowed)
method_signature += make_gdnative_type(method["return_type"])
method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "(" method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "("
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
method_signature += "const " + make_gdnative_type(argument["type"]) method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
method_signature += escape_cpp(argument["name"]) method_signature += escape_cpp(argument["name"])
if i != len(method["arguments"]) - 1: if i != len(method["arguments"]) - 1:
@ -396,12 +400,13 @@ def generate_class_implementation(icalls, used_classes, c):
continue continue
return_statement = "" return_statement = ""
return_type_is_ref = is_reference_type(method["return_type"]) and ref_allowed
if method["return_type"] != "void": if method["return_type"] != "void":
if is_class_type(method["return_type"]): if is_class_type(method["return_type"]):
if is_enum(method["return_type"]): if is_enum(method["return_type"]):
return_statement += "return (" + remove_enum_prefix(method["return_type"]) + ") " return_statement += "return (" + remove_enum_prefix(method["return_type"]) + ") "
elif is_reference_type(method["return_type"]): elif return_type_is_ref:
return_statement += "return Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor("; return_statement += "return Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(";
else: else:
return_statement += "return " + ("(" + strip_name(method["return_type"]) + " *) " if is_class_type(method["return_type"]) else "") return_statement += "return " + ("(" + strip_name(method["return_type"]) + " *) " if is_class_type(method["return_type"]) else "")
@ -476,7 +481,7 @@ def generate_class_implementation(icalls, used_classes, c):
if method["return_type"] != "void": if method["return_type"] != "void":
cast = "" cast = ""
if is_class_type(method["return_type"]): if is_class_type(method["return_type"]):
if is_reference_type(method["return_type"]): if return_type_is_ref:
cast += "Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(__result);" cast += "Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(__result);"
else: else:
cast += "(" + strip_name(method["return_type"]) + " *) " + strip_name(method["return_type"] + "::___get_from_variant(") + "__result);" cast += "(" + strip_name(method["return_type"]) + " *) " + strip_name(method["return_type"] + "::___get_from_variant(") + "__result);"
@ -485,7 +490,6 @@ def generate_class_implementation(icalls, used_classes, c):
source.append("\treturn " + cast) source.append("\treturn " + cast)
else: else:
args = [] args = []
@ -503,11 +507,15 @@ def generate_class_implementation(icalls, used_classes, c):
return_statement += icall_name + "(___mb.mb_" + method["name"] + ", (const Object *) " + core_object_name return_statement += icall_name + "(___mb.mb_" + method["name"] + ", (const Object *) " + core_object_name
for arg in method["arguments"]: for arg in method["arguments"]:
return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if is_reference_type(arg["type"]) else "") arg_is_ref = is_reference_type(arg["type"]) and ref_allowed
return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if arg_is_ref else "")
return_statement += ")" return_statement += ")"
source.append("\t" + return_statement + (")" if is_reference_type(method["return_type"]) else "") + ";") if return_type_is_ref:
return_statement += ")"
source.append("\t" + return_statement + ";")
source.append("}") source.append("}")
source.append("") source.append("")
@ -709,7 +717,6 @@ def get_icall_name(sig):
def get_used_classes(c): def get_used_classes(c):
classes = [] classes = []
for method in c["methods"]: for method in c["methods"]:
@ -725,9 +732,6 @@ def get_used_classes(c):
def strip_name(name): def strip_name(name):
if len(name) == 0: if len(name) == 0:
return name return name