Add proper typeInfo generation for non-scripting API classes

This commit is contained in:
Wojtek Figat
2020-12-23 12:55:53 +01:00
parent 07fb8e034f
commit ce6c360d29

View File

@@ -1046,7 +1046,7 @@ namespace Flax.Build.Bindings
var classTypeNameInternal = classInfo.NativeName; var classTypeNameInternal = classInfo.NativeName;
if (classInfo.Parent != null && !(classInfo.Parent is FileInfo)) if (classInfo.Parent != null && !(classInfo.Parent is FileInfo))
classTypeNameInternal = classInfo.Parent.FullNameNative + '_' + classTypeNameInternal; classTypeNameInternal = classInfo.Parent.FullNameNative + '_' + classTypeNameInternal;
var useUnmanaged = classInfo.IsStatic || classInfo.IsScriptingObject; var useScripting = classInfo.IsStatic || classInfo.IsScriptingObject;
if (classInfo.IsAutoSerialization) if (classInfo.IsAutoSerialization)
GenerateCppAutoSerialization(buildData, contents, moduleInfo, classInfo, classTypeNameNative); GenerateCppAutoSerialization(buildData, contents, moduleInfo, classInfo, classTypeNameNative);
@@ -1059,7 +1059,7 @@ namespace Flax.Build.Bindings
// Events // Events
foreach (var eventInfo in classInfo.Events) foreach (var eventInfo in classInfo.Events)
{ {
if (!useUnmanaged) if (!useScripting)
continue; continue;
CppIncludeFiles.Add("Engine/Scripting/ManagedCLR/MEvent.h"); CppIncludeFiles.Add("Engine/Scripting/ManagedCLR/MEvent.h");
@@ -1131,7 +1131,7 @@ namespace Flax.Build.Bindings
// Fields // Fields
foreach (var fieldInfo in classInfo.Fields) foreach (var fieldInfo in classInfo.Fields)
{ {
if (!useUnmanaged) if (!useScripting)
continue; continue;
if (fieldInfo.Getter != null) if (fieldInfo.Getter != null)
GenerateCppWrapperFunction(buildData, contents, classInfo, fieldInfo.Getter, "{0}"); GenerateCppWrapperFunction(buildData, contents, classInfo, fieldInfo.Getter, "{0}");
@@ -1142,7 +1142,7 @@ namespace Flax.Build.Bindings
// Properties // Properties
foreach (var propertyInfo in classInfo.Properties) foreach (var propertyInfo in classInfo.Properties)
{ {
if (!useUnmanaged) if (!useScripting)
continue; continue;
if (propertyInfo.Getter != null) if (propertyInfo.Getter != null)
GenerateCppWrapperFunction(buildData, contents, classInfo, propertyInfo.Getter); GenerateCppWrapperFunction(buildData, contents, classInfo, propertyInfo.Getter);
@@ -1153,7 +1153,7 @@ namespace Flax.Build.Bindings
// Functions // Functions
foreach (var functionInfo in classInfo.Functions) foreach (var functionInfo in classInfo.Functions)
{ {
if (!useUnmanaged) if (!useScripting)
throw new Exception($"Not supported function {functionInfo.Name} inside non-static and non-scripting class type {classInfo.Name}."); throw new Exception($"Not supported function {functionInfo.Name} inside non-static and non-scripting class type {classInfo.Name}.");
GenerateCppWrapperFunction(buildData, contents, classInfo, functionInfo); GenerateCppWrapperFunction(buildData, contents, classInfo, functionInfo);
} }
@@ -1264,7 +1264,7 @@ namespace Flax.Build.Bindings
// Runtime initialization (internal methods binding) // Runtime initialization (internal methods binding)
contents.AppendLine(" static void InitRuntime()"); contents.AppendLine(" static void InitRuntime()");
contents.AppendLine(" {"); contents.AppendLine(" {");
if (useUnmanaged) if (useScripting)
{ {
foreach (var eventInfo in classInfo.Events) foreach (var eventInfo in classInfo.Events)
{ {
@@ -1291,10 +1291,24 @@ namespace Flax.Build.Bindings
} }
GenerateCppClassInitRuntime?.Invoke(buildData, classInfo, contents); GenerateCppClassInitRuntime?.Invoke(buildData, classInfo, contents);
contents.AppendLine(" }"); contents.AppendLine(" }").AppendLine();
contents.Append('}');
contents.Append(';'); if (!useScripting)
contents.AppendLine(); {
// Constructor
contents.AppendLine(" static void Ctor(void* ptr)");
contents.AppendLine(" {");
contents.AppendLine($" new(ptr){classTypeNameNative}();");
contents.AppendLine(" }").AppendLine();
// Destructor
contents.AppendLine(" static void Dtor(void* ptr)");
contents.AppendLine(" {");
contents.AppendLine($" (({classTypeNameNative}*)ptr)->~{classInfo.NativeName}();");
contents.AppendLine(" }").AppendLine();
}
contents.Append('}').Append(';').AppendLine();
contents.AppendLine(); contents.AppendLine();
// Type initializer // Type initializer
@@ -1302,15 +1316,22 @@ namespace Flax.Build.Bindings
contents.Append($"StringAnsiView(\"{classTypeNameManaged}\", {classTypeNameManaged.Length}), "); contents.Append($"StringAnsiView(\"{classTypeNameManaged}\", {classTypeNameManaged.Length}), ");
contents.Append($"sizeof({classTypeNameNative}), "); contents.Append($"sizeof({classTypeNameNative}), ");
contents.Append($"&{classTypeNameInternal}Internal::InitRuntime, "); contents.Append($"&{classTypeNameInternal}Internal::InitRuntime, ");
if (!classInfo.IsStatic && !classInfo.NoSpawn && useUnmanaged) if (useScripting)
contents.Append($"(ScriptingType::SpawnHandler)&{classTypeNameNative}::Spawn, "); {
else if (classInfo.IsStatic || classInfo.NoSpawn)
contents.Append("&ScriptingType::DefaultSpawn, "); contents.Append("&ScriptingType::DefaultSpawn, ");
if (classInfo.BaseType != null && useUnmanaged) else
contents.Append($"(ScriptingType::SpawnHandler)&{classTypeNameNative}::Spawn, ");
if (classInfo.BaseType != null && useScripting)
contents.Append($"&{classInfo.BaseType}::TypeInitializer, "); contents.Append($"&{classInfo.BaseType}::TypeInitializer, ");
else else
contents.Append("nullptr, "); contents.Append("nullptr, ");
contents.Append(setupScriptVTable); contents.Append(setupScriptVTable);
}
else
{
contents.Append($"&{classTypeNameInternal}Internal::Ctor, &{classTypeNameInternal}Internal::Dtor");
}
contents.Append(");"); contents.Append(");");
contents.AppendLine(); contents.AppendLine();