This commit is contained in:
Wojtek Figat
2023-01-03 23:04:09 +01:00
parent b4faa128b7
commit 4012434102
4 changed files with 34 additions and 12 deletions

View File

@@ -732,6 +732,23 @@ ScriptingTypeHandle Scripting::FindScriptingType(const StringAnsiView& fullname)
return ScriptingTypeHandle();
}
ScriptingObject* Scripting::NewObject(const ScriptingTypeHandle& type)
{
if (!type)
{
LOG(Error, "Invalid type.");
return nullptr;
}
const ScriptingType& scriptingType = type.GetType();
// Create unmanaged object
const ScriptingObjectSpawnParams params(Guid::New(), type);
ScriptingObject* obj = scriptingType.Script.Spawn(params);
if (obj == nullptr)
LOG(Error, "Failed to spawn object of type \'{0}\'.", scriptingType.ToString());
return obj;
}
ScriptingObject* Scripting::NewObject(const MClass* type)
{
if (type == nullptr)
@@ -762,10 +779,7 @@ ScriptingObject* Scripting::NewObject(const MClass* type)
const ScriptingObjectSpawnParams params(Guid::New(), ScriptingTypeHandle(module, typeIndex));
ScriptingObject* obj = scriptingType.Script.Spawn(params);
if (obj == nullptr)
{
LOG(Error, "Failed to spawn object of type \'{0}.{1}\'.", String(mono_class_get_namespace(typeClass)), String(mono_class_get_name(typeClass)));
return nullptr;
}
LOG(Error, "Failed to spawn object of type \'{0}\'.", scriptingType.ToString());
return obj;
#else
LOG(Error, "Not supported object creation from Managed class.");

View File

@@ -109,6 +109,13 @@ public:
/// <returns>The scripting type or invalid type if missing.</returns>
static ScriptingTypeHandle FindScriptingType(const StringAnsiView& fullname);
/// <summary>
/// Creates a new instance of the given type object (native construction).
/// </summary>
/// <param name="type">The scripting object type class.</param>
/// <returns>The created object or null if failed.</returns>
static ScriptingObject* NewObject(const ScriptingTypeHandle& type);
/// <summary>
/// Creates a new instance of the given class object (native construction).
/// </summary>

View File

@@ -16,7 +16,7 @@ TEST_CASE("Scripting")
// Test native class
ScriptingTypeHandle type = Scripting::FindScriptingType("FlaxEngine.TestClassNative");
CHECK(type == TestClassNative::TypeInitializer);
ScriptingObject* object = Scripting::NewObject(type.GetType().ManagedClass);
ScriptingObject* object = Scripting::NewObject(type);
CHECK(object);
CHECK(object->Is<TestClassNative>());
TestClassNative* testClass = (TestClassNative*)object;
@@ -29,7 +29,7 @@ TEST_CASE("Scripting")
// Test managed class
type = Scripting::FindScriptingType("FlaxEngine.TestClassManaged");
CHECK(type);
object = Scripting::NewObject(type.GetType().ManagedClass);
object = Scripting::NewObject(type);
CHECK(object);
CHECK(object->Is<TestClassNative>());
testClass = (TestClassNative*)object;
@@ -46,7 +46,7 @@ TEST_CASE("Scripting")
{
ScriptingTypeHandle type = Scripting::FindScriptingType("FlaxEngine.TestClassManaged");
CHECK(type);
ScriptingObject* object = Scripting::NewObject(type.GetType().ManagedClass);
ScriptingObject* object = Scripting::NewObject(type);
CHECK(object);
MObject* managed = object->GetOrCreateManagedInstance(); // Ensure to create C# object and run it's ctor
CHECK(managed);
@@ -65,12 +65,13 @@ TEST_CASE("Scripting")
CHECK(arr2[1].Vector == testClass->SimpleStruct.Vector);
CHECK(arr2[1].Object == testClass);
}
SECTION("Test Interface")
{
// Test native interface implementation
ScriptingTypeHandle type = Scripting::FindScriptingType("FlaxEngine.TestClassNative");
CHECK(type);
ScriptingObject* object = Scripting::NewObject(type.GetType().ManagedClass);
ScriptingObject* object = Scripting::NewObject(type);
CHECK(object);
TestClassNative* testClass = (TestClassNative*)object;
int32 methodResult = testClass->TestInterfaceMethod(TEXT("123"));
@@ -86,7 +87,7 @@ TEST_CASE("Scripting")
// Test managed interface override
type = Scripting::FindScriptingType("FlaxEngine.TestClassManaged");
CHECK(type);
object = Scripting::NewObject(type.GetType().ManagedClass);
object = Scripting::NewObject(type);
CHECK(object);
testClass = (TestClassNative*)object;
methodResult = testClass->TestInterfaceMethod(TEXT("123"));
@@ -102,7 +103,7 @@ TEST_CASE("Scripting")
// Test managed interface implementation
type = Scripting::FindScriptingType("FlaxEngine.TestInterfaceManaged");
CHECK(type);
object = Scripting::NewObject(type.GetType().ManagedClass);
object = Scripting::NewObject(type);
CHECK(object);
interface = ScriptingObject::ToInterface<ITestInterface>(object);
CHECK(interface);

View File

@@ -158,13 +158,13 @@ namespace Flax.Build.Bindings
{
var wrapperName = GenerateCppWrapperNativeToVariantMethodName(typeInfo);
CppVariantFromTypes[wrapperName] = typeInfo;
return $"VariantFrom{GenerateCppWrapperNativeToVariantMethodName(typeInfo)}Array({value}, {typeInfo.ArraySize})";
return $"VariantFrom{GenerateCppWrapperNativeToVariantMethodName(typeInfo)}Array((const {typeInfo}*){value}, {typeInfo.ArraySize})";
}
if (typeInfo.Type == "Array" && typeInfo.GenericArgs != null)
{
var wrapperName = GenerateCppWrapperNativeToVariantMethodName(typeInfo.GenericArgs[0]);
CppVariantFromTypes[wrapperName] = typeInfo;
return $"VariantFrom{wrapperName}Array({value}.Get(), {value}.Count())";
return $"VariantFrom{wrapperName}Array((const {typeInfo.GenericArgs[0]}*){value}.Get(), {value}.Count())";
}
if (typeInfo.Type == "Dictionary" && typeInfo.GenericArgs != null)
{