Skip to content

Commit 6ee2428

Browse files
Add support for indexer properties
1 parent 749b2c7 commit 6ee2428

6 files changed

Lines changed: 118 additions & 5 deletions

File tree

Unity/Assets/NativeScript/Bindings.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ delegate void InitDelegate(
307307
IntPtr systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKey,
308308
IntPtr systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValue,
309309
IntPtr systemCollectionsGenericListSystemStringConstructor,
310+
IntPtr systemCollectionsGenericListSystemStringPropertyGetItem,
311+
IntPtr systemCollectionsGenericListSystemStringPropertySetItem,
310312
IntPtr systemCollectionsGenericListSystemStringMethodAddSystemString,
311313
IntPtr systemCollectionsGenericLinkedListNodeSystemStringConstructorSystemString,
312314
IntPtr systemCollectionsGenericLinkedListNodeSystemStringPropertyGetValue,
@@ -468,6 +470,8 @@ static extern void Init(
468470
IntPtr systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKey,
469471
IntPtr systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValue,
470472
IntPtr systemCollectionsGenericListSystemStringConstructor,
473+
IntPtr systemCollectionsGenericListSystemStringPropertyGetItem,
474+
IntPtr systemCollectionsGenericListSystemStringPropertySetItem,
471475
IntPtr systemCollectionsGenericListSystemStringMethodAddSystemString,
472476
IntPtr systemCollectionsGenericLinkedListNodeSystemStringConstructorSystemString,
473477
IntPtr systemCollectionsGenericLinkedListNodeSystemStringPropertyGetValue,
@@ -538,6 +542,8 @@ IntPtr systemExceptionConstructorSystemString
538542
delegate int SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKeyDelegate(int thisHandle);
539543
delegate double SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValueDelegate(int thisHandle);
540544
delegate int SystemCollectionsGenericListSystemStringConstructorDelegate();
545+
delegate int SystemCollectionsGenericListSystemStringPropertyGetItemDelegate(int thisHandle, int index);
546+
delegate void SystemCollectionsGenericListSystemStringPropertySetItemDelegate(int thisHandle, int index, int valueHandle);
541547
delegate void SystemCollectionsGenericListSystemStringMethodAddSystemStringDelegate(int thisHandle, int itemHandle);
542548
delegate int SystemCollectionsGenericLinkedListNodeSystemStringConstructorSystemStringDelegate(int valueHandle);
543549
delegate int SystemCollectionsGenericLinkedListNodeSystemStringPropertyGetValueDelegate(int thisHandle);
@@ -632,6 +638,8 @@ public static void Open(
632638
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKeyDelegate(SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKey)),
633639
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValueDelegate(SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValue)),
634640
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericListSystemStringConstructorDelegate(SystemCollectionsGenericListSystemStringConstructor)),
641+
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericListSystemStringPropertyGetItemDelegate(SystemCollectionsGenericListSystemStringPropertyGetItem)),
642+
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericListSystemStringPropertySetItemDelegate(SystemCollectionsGenericListSystemStringPropertySetItem)),
635643
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericListSystemStringMethodAddSystemStringDelegate(SystemCollectionsGenericListSystemStringMethodAddSystemString)),
636644
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericLinkedListNodeSystemStringConstructorSystemStringDelegate(SystemCollectionsGenericLinkedListNodeSystemStringConstructorSystemString)),
637645
Marshal.GetFunctionPointerForDelegate(new SystemCollectionsGenericLinkedListNodeSystemStringPropertyGetValueDelegate(SystemCollectionsGenericLinkedListNodeSystemStringPropertyGetValue)),
@@ -1359,6 +1367,46 @@ static int SystemCollectionsGenericListSystemStringConstructor()
13591367
}
13601368
}
13611369

1370+
[MonoPInvokeCallback(typeof(SystemCollectionsGenericListSystemStringPropertyGetItemDelegate))]
1371+
static int SystemCollectionsGenericListSystemStringPropertyGetItem(int thisHandle, int index)
1372+
{
1373+
try
1374+
{
1375+
var thiz = (System.Collections.Generic.List<string>)NativeScript.Bindings.ObjectStore.Get(thisHandle);
1376+
var returnValue = thiz[index];
1377+
return NativeScript.Bindings.ObjectStore.GetHandle(returnValue);
1378+
}
1379+
catch (System.NullReferenceException ex)
1380+
{
1381+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
1382+
return default(int);
1383+
}
1384+
catch (System.Exception ex)
1385+
{
1386+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
1387+
return default(int);
1388+
}
1389+
}
1390+
1391+
[MonoPInvokeCallback(typeof(SystemCollectionsGenericListSystemStringPropertySetItemDelegate))]
1392+
static void SystemCollectionsGenericListSystemStringPropertySetItem(int thisHandle, int index, int valueHandle)
1393+
{
1394+
try
1395+
{
1396+
var thiz = (System.Collections.Generic.List<string>)NativeScript.Bindings.ObjectStore.Get(thisHandle);
1397+
var value = (string)NativeScript.Bindings.ObjectStore.Get(valueHandle);
1398+
thiz[index] = value;
1399+
}
1400+
catch (System.NullReferenceException ex)
1401+
{
1402+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
1403+
}
1404+
catch (System.Exception ex)
1405+
{
1406+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
1407+
}
1408+
}
1409+
13621410
[MonoPInvokeCallback(typeof(SystemCollectionsGenericListSystemStringMethodAddSystemStringDelegate))]
13631411
static void SystemCollectionsGenericListSystemStringMethodAddSystemString(int thisHandle, int itemHandle)
13641412
{

Unity/Assets/NativeScript/Editor/GenerateBindings.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,7 @@ static void AppendMethod(
22102210
enclosingType,
22112211
methodIsStatic,
22122212
builders.CsharpFunctions);
2213+
builders.CsharpFunctions.Append('.');
22132214
builders.CsharpFunctions.Append(methodName);
22142215
AppendCSharpTypeParameters(
22152216
methodTypeParams,
@@ -3074,7 +3075,17 @@ static void AppendGetter(
30743075
enclosingType,
30753076
methodIsStatic,
30763077
builders.CsharpFunctions);
3077-
builders.CsharpFunctions.Append(fieldName);
3078+
if (parameters.Length == 1)
3079+
{
3080+
builders.CsharpFunctions.Append('[');
3081+
builders.CsharpFunctions.Append(parameters[0].Name);
3082+
builders.CsharpFunctions.Append("]");
3083+
}
3084+
else
3085+
{
3086+
builders.CsharpFunctions.Append('.');
3087+
builders.CsharpFunctions.Append(fieldName);
3088+
}
30783089
builders.CsharpFunctions.Append(';');
30793090
if (!isReadOnly
30803091
&& enclosingTypeKind == TypeKind.ManagedStruct)
@@ -3246,9 +3257,21 @@ static void AppendSetter(
32463257
enclosingType,
32473258
methodIsStatic,
32483259
builders.CsharpFunctions);
3249-
builders.CsharpFunctions.Append(fieldName);
3250-
builders.CsharpFunctions.Append(" = ");
3251-
builders.CsharpFunctions.Append("value;");
3260+
if (parameters.Length == 2)
3261+
{
3262+
builders.CsharpFunctions.Append('[');
3263+
builders.CsharpFunctions.Append(parameters[0].Name);
3264+
builders.CsharpFunctions.Append("] = ");
3265+
builders.CsharpFunctions.Append(parameters[1].Name);
3266+
}
3267+
else
3268+
{
3269+
builders.CsharpFunctions.Append('.');
3270+
builders.CsharpFunctions.Append(fieldName);
3271+
builders.CsharpFunctions.Append(" = ");
3272+
builders.CsharpFunctions.Append("value");
3273+
}
3274+
builders.CsharpFunctions.Append(';');
32523275
if (!isReadOnly
32533276
&& enclosingTypeKind == TypeKind.ManagedStruct)
32543277
{
@@ -4338,7 +4361,6 @@ static void AppendCsharpFunctionCallSubject(
43384361
{
43394362
output.Append("thiz");
43404363
}
4341-
output.Append('.');
43424364
}
43434365

43444366
static void AppendCsharpFunctionCallParameters(

Unity/Assets/NativeScriptTypes.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@
256256
"ParamTypes": []
257257
}
258258
],
259+
"Properties": [
260+
{
261+
"Name": "Item"
262+
}
263+
],
259264
"Methods": [
260265
{
261266
"Name": "Add",

Unity/CppSource/Game/Game.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ void PluginMain()
3232
strings.Add("two");
3333
strings.Add("three");
3434
Debug::Log(strings);
35+
String first = strings.GetItem(0);
36+
Debug::Log(first);
37+
strings.SetItem(0, "new one");
38+
first = strings.GetItem(0);
39+
Debug::Log(first);
3540

3641
System::Runtime::CompilerServices::StrongBox<System::String> strongbox("secret");
3742
Debug::Log(strongbox.GetValue());

Unity/CppSource/NativeScript/Bindings.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ namespace Plugin
7474
int32_t (*SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKey)(int32_t thisHandle);
7575
double (*SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValue)(int32_t thisHandle);
7676
int32_t (*SystemCollectionsGenericListSystemStringConstructor)();
77+
int32_t (*SystemCollectionsGenericListSystemStringPropertyGetItem)(int32_t thisHandle, int32_t index);
78+
void (*SystemCollectionsGenericListSystemStringPropertySetItem)(int32_t thisHandle, int32_t index, int32_t valueHandle);
7779
void (*SystemCollectionsGenericListSystemStringMethodAddSystemString)(int32_t thisHandle, int32_t itemHandle);
7880
int32_t (*SystemCollectionsGenericLinkedListNodeSystemStringConstructorSystemString)(int32_t valueHandle);
7981
int32_t (*SystemCollectionsGenericLinkedListNodeSystemStringPropertyGetValue)(int32_t thisHandle);
@@ -2022,6 +2024,31 @@ namespace System
20222024
}
20232025
}
20242026

2027+
System::String List<System::String>::GetItem(int32_t index)
2028+
{
2029+
auto returnValue = Plugin::SystemCollectionsGenericListSystemStringPropertyGetItem(Handle, index);
2030+
if (Plugin::unhandledCsharpException)
2031+
{
2032+
System::Exception* ex = Plugin::unhandledCsharpException;
2033+
Plugin::unhandledCsharpException = nullptr;
2034+
ex->ThrowReferenceToThis();
2035+
delete ex;
2036+
}
2037+
return System::String(Plugin::InternalUse::Only, returnValue);
2038+
}
2039+
2040+
void List<System::String>::SetItem(int32_t index, System::String value)
2041+
{
2042+
Plugin::SystemCollectionsGenericListSystemStringPropertySetItem(Handle, index, value.Handle);
2043+
if (Plugin::unhandledCsharpException)
2044+
{
2045+
System::Exception* ex = Plugin::unhandledCsharpException;
2046+
Plugin::unhandledCsharpException = nullptr;
2047+
ex->ThrowReferenceToThis();
2048+
delete ex;
2049+
}
2050+
}
2051+
20252052
void List<System::String>::Add(System::String item)
20262053
{
20272054
Plugin::SystemCollectionsGenericListSystemStringMethodAddSystemString(Handle, item.Handle);
@@ -2957,6 +2984,8 @@ DLLEXPORT void Init(
29572984
int32_t (*systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKey)(int32_t thisHandle),
29582985
double (*systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValue)(int32_t thisHandle),
29592986
int32_t (*systemCollectionsGenericListSystemStringConstructor)(),
2987+
int32_t (*systemCollectionsGenericListSystemStringPropertyGetItem)(int32_t thisHandle, int32_t index),
2988+
void (*systemCollectionsGenericListSystemStringPropertySetItem)(int32_t thisHandle, int32_t index, int32_t valueHandle),
29602989
void (*systemCollectionsGenericListSystemStringMethodAddSystemString)(int32_t thisHandle, int32_t itemHandle),
29612990
int32_t (*systemCollectionsGenericLinkedListNodeSystemStringConstructorSystemString)(int32_t valueHandle),
29622991
int32_t (*systemCollectionsGenericLinkedListNodeSystemStringPropertyGetValue)(int32_t thisHandle),
@@ -3016,6 +3045,8 @@ DLLEXPORT void Init(
30163045
Plugin::SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKey = systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetKey;
30173046
Plugin::SystemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValue = systemCollectionsGenericKeyValuePairSystemString_SystemDoublePropertyGetValue;
30183047
Plugin::SystemCollectionsGenericListSystemStringConstructor = systemCollectionsGenericListSystemStringConstructor;
3048+
Plugin::SystemCollectionsGenericListSystemStringPropertyGetItem = systemCollectionsGenericListSystemStringPropertyGetItem;
3049+
Plugin::SystemCollectionsGenericListSystemStringPropertySetItem = systemCollectionsGenericListSystemStringPropertySetItem;
30193050
Plugin::SystemCollectionsGenericListSystemStringMethodAddSystemString = systemCollectionsGenericListSystemStringMethodAddSystemString;
30203051
Plugin::SystemCollectionsGenericLinkedListNodeSystemStringConstructorSystemString = systemCollectionsGenericLinkedListNodeSystemStringConstructorSystemString;
30213052
Plugin::SystemCollectionsGenericLinkedListNodeSystemStringPropertyGetValue = systemCollectionsGenericLinkedListNodeSystemStringPropertyGetValue;

Unity/CppSource/NativeScript/Bindings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ namespace System
735735
bool operator==(const List<System::String>& other) const;
736736
bool operator!=(const List<System::String>& other) const;
737737
List();
738+
System::String GetItem(int32_t index);
739+
void SetItem(int32_t index, System::String value);
738740
void Add(System::String item);
739741
};
740742
}

0 commit comments

Comments
 (0)