Skip to content

Commit 749b2c7

Browse files
Add Plugin::InternalUse type to C++ handle constructors to prevent accidental usage by game code and collision with C# constructors taking just an int
Make generated destructors virtual
1 parent e01df76 commit 749b2c7

3 files changed

Lines changed: 201 additions & 175 deletions

File tree

Unity/Assets/NativeScript/Editor/GenerateBindings.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,7 @@ static void AppendMonoBehaviour(
26202620
AppendCppTypeName(
26212621
type,
26222622
builders.CppMonoBehaviourMessages);
2623-
builders.CppMonoBehaviourMessages.Append(" thiz(thisHandle);\n");
2623+
builders.CppMonoBehaviourMessages.Append(" thiz(Plugin::InternalUse::Only, thisHandle);\n");
26242624
for (int i = 0; i < numParams; ++i)
26252625
{
26262626
ParameterInfo param = parameters[i];
@@ -2633,7 +2633,7 @@ static void AppendMonoBehaviour(
26332633
builders.CppMonoBehaviourMessages);
26342634
builders.CppMonoBehaviourMessages.Append(" param");
26352635
builders.CppMonoBehaviourMessages.Append(i);
2636-
builders.CppMonoBehaviourMessages.Append("(param");
2636+
builders.CppMonoBehaviourMessages.Append("(Plugin::InternalUse::Only, param");
26372637
builders.CppMonoBehaviourMessages.Append(i);
26382638
builders.CppMonoBehaviourMessages.Append("Handle);\n");
26392639
}
@@ -2890,7 +2890,7 @@ static void AppendExceptions(
28902890
AppendCppTypeName(
28912891
exceptionType,
28922892
builders.CppMethodDefinitions);
2893-
builders.CppMethodDefinitions.Append("(handle)\n");
2893+
builders.CppMethodDefinitions.Append("(Plugin::InternalUse::Only, handle)\n");
28942894
AppendIndent(
28952895
throwerIndent + 1,
28962896
builders.CppMethodDefinitions);
@@ -3442,7 +3442,7 @@ StringBuilder output
34423442
AppendCppTypeParameters(
34433443
typeParams,
34443444
output);
3445-
output.Append("(int32_t handle);\n");
3445+
output.Append("(Plugin::InternalUse iu, int32_t handle);\n");
34463446

34473447
// Copy constructor
34483448
AppendIndent(indent + 1, output);
@@ -3480,7 +3480,7 @@ StringBuilder output
34803480

34813481
// Destructor
34823482
AppendIndent(indent + 1, output);
3483-
output.Append('~');
3483+
output.Append("virtual ~");
34843484
AppendWithoutGenericTypeCountSuffix(
34853485
type.Name,
34863486
output);
@@ -3636,13 +3636,13 @@ static int AppendCppMethodDefinitionBegin(
36363636
AppendWithoutGenericTypeCountSuffix(
36373637
enclosingType.Name,
36383638
output);
3639-
output.Append("(int32_t handle)\n");
3639+
output.Append("(Plugin::InternalUse iu, int32_t handle)\n");
36403640
AppendIndent(indent, output);
36413641
output.Append("\t: ");
36423642
AppendCppTypeName(
36433643
baseType,
36443644
output);
3645-
output.Append("(handle)\n");
3645+
output.Append("(iu, handle)\n");
36463646
AppendIndent(indent, output);
36473647
output.Append("{\n");
36483648
AppendIndent(indent + 1, output);
@@ -3689,7 +3689,7 @@ static int AppendCppMethodDefinitionBegin(
36893689
AppendCppTypeName(
36903690
baseType,
36913691
output);
3692-
output.Append("(other.Handle)\n");
3692+
output.Append("(Plugin::InternalUse::Only, other.Handle)\n");
36933693
AppendIndent(indent, output);
36943694
output.Append("{\n");
36953695
AppendIndent(indent + 1, output);
@@ -3736,7 +3736,7 @@ static int AppendCppMethodDefinitionBegin(
37363736
AppendCppTypeName(
37373737
baseType,
37383738
output);
3739-
output.Append("(other.Handle)\n");
3739+
output.Append("(Plugin::InternalUse::Only, other.Handle)\n");
37403740
AppendIndent(indent, output);
37413741
output.Append("{\n");
37423742
AppendIndent(indent + 1, output);
@@ -4725,7 +4725,19 @@ static void AppendCppMethodReturn(
47254725
if (returnType != null && !returnType.Equals(typeof(void)))
47264726
{
47274727
AppendIndent(indent, output);
4728-
output.Append("return returnValue;\n");
4728+
output.Append("return ");
4729+
if (IsFullValueType(returnType))
4730+
{
4731+
output.Append("returnValue");
4732+
}
4733+
else
4734+
{
4735+
AppendCppTypeName(
4736+
returnType,
4737+
output);
4738+
output.Append("(Plugin::InternalUse::Only, returnValue)");
4739+
}
4740+
output.Append(";\n");
47294741
}
47304742
}
47314743

0 commit comments

Comments
 (0)