Skip to content

Commit 8935432

Browse files
author
runrevali
committed
[[ LCB StdLib ]] Allow undefined elements of arrays and lists
1 parent d52f09f commit 8935432

File tree

5 files changed

+74
-32
lines changed

5 files changed

+74
-32
lines changed

libscript/src/array.mlc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public foreign handler MCArrayEvalElementsOf(in Target as array, out Value as li
2525

2626
public foreign handler MCArrayEvalIsAmongTheKeysOfCaseless(in Needle as string, in IsNot as bool, in Target as array, out Result as bool) as undefined binds to "<builtin>"
2727

28-
public foreign handler MCArrayEvalIsAmongTheElementsOf(in Needle as any, in IsNot as bool, in Target as array, out Result as bool) as undefined binds to "<builtin>"
28+
public foreign handler MCArrayEvalIsAmongTheElementsOf(in Needle as optional any, in IsNot as bool, in Target as array, out Result as bool) as undefined binds to "<builtin>"
2929

3030
public foreign handler MCArrayEvalNumberOfElementsIn(in Target as array, out Count as index) as undefined binds to "<builtin>"
3131

3232
// Case insensitive map access / storage
33-
public foreign handler MCArrayFetchElementOfCaseless(in Target as array, in Key as string, out Value as any) as undefined binds to "<builtin>"
34-
public foreign handler MCArrayStoreElementOfCaseless(in Value as any, inout Target as array, in Key as string) as undefined binds to "<builtin>"
33+
public foreign handler MCArrayFetchElementOfCaseless(in Target as array, in Key as string, out Value as optional any) as undefined binds to "<builtin>"
34+
public foreign handler MCArrayStoreElementOfCaseless(in Value as optional any, inout Target as array, in Key as string) as undefined binds to "<builtin>"
3535

3636
public foreign handler MCArrayEvalEmpty(out Value as array) as undefined binds to "<builtin>"
3737

38-
public foreign handler MCArrayRepeatForEachElement(inout Iterator as optional pointer, out Iterand as any, in Container as array) as bool binds to "<builtin>"
38+
public foreign handler MCArrayRepeatForEachElement(inout Iterator as optional pointer, out Iterand as optional any, in Container as array) as bool binds to "<builtin>"
3939
public foreign handler MCArrayRepeatForEachKey(inout Iterator as optional pointer, out Iterand as string, in Container as array) as bool binds to "<builtin>"
4040

4141
public foreign handler MCArrayDeleteElementOfCaseless(inout Target as array, in Key as string) as undefined binds to "<builtin>"

libscript/src/list.mlc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ This library consists of the operations on lists included in the standard librar
2020

2121
module com.livecode.list
2222

23-
public foreign handler MCListEvalHeadOf(in Target as list, out Value as any) as undefined binds to "<builtin>"
24-
public foreign handler MCListEvalTailOf(in Target as list, out Value as any) as undefined binds to "<builtin>"
23+
public foreign handler MCListEvalHeadOf(in Target as list, out Value as optional any) as undefined binds to "<builtin>"
24+
public foreign handler MCListEvalTailOf(in Target as list, out Value as optional any) as undefined binds to "<builtin>"
2525

26-
public foreign handler MCListExecPushSingleElementOnto(in Value as any, in IsFront as bool, inout Target as list) as undefined binds to "<builtin>"
27-
public foreign handler MCListExecPopElementInto(in IsFront as bool, inout Source as list, out Target as any) as undefined binds to "<builtin>"
26+
public foreign handler MCListExecPushSingleElementOnto(in Value as optional any, in IsFront as bool, inout Target as list) as undefined binds to "<builtin>"
27+
public foreign handler MCListExecPopElementInto(in IsFront as bool, inout Source as list, out Target as optional any) as undefined binds to "<builtin>"
2828

2929
public foreign handler MCListEvalNumberOfElementsIn(in Target as list, out Count as index) as undefined binds to "<builtin>"
3030

31-
public foreign handler MCListEvalIsAmongTheElementsOf(in Needle as any, in Target as list, out Result as bool) as undefined binds to "<builtin>"
31+
public foreign handler MCListEvalIsAmongTheElementsOf(in Needle as optional any, in Target as list, out Result as bool) as undefined binds to "<builtin>"
3232
public foreign handler MCListEvalContainsElements(in Target as list, in Needle as list, out Result as bool) as undefined binds to "<builtin>"
3333

3434
public foreign handler MCListEvalBeginsWith(in Source as list, in Prefix as list, out Result as bool) as undefined binds to "<builtin>"
@@ -40,13 +40,13 @@ public foreign handler MCListEvalIsNotEqualTo(in Left as list, in Right as list,
4040
public foreign handler MCListFetchElementOf(in Index as int, in Target as list, out Value as any) as undefined binds to "<builtin>"
4141
public foreign handler MCListStoreElementOf(in Value as any, in Index as int, inout Target as list) as undefined binds to "<builtin>"
4242
public foreign handler MCListFetchElementRangeOf(in Start as int, in Finish as int, in Target as list, out Value as list) as undefined binds to "<builtin>"
43-
public foreign handler MCListStoreElementRangeOf(in Value as any, in Start as int, in Finish as int, inout Target as list) as undefined binds to "<builtin>"
43+
public foreign handler MCListStoreElementRangeOf(in Value as optional any, in Start as int, in Finish as int, inout Target as list) as undefined binds to "<builtin>"
4444

45-
public foreign handler MCListFetchIndexOf(in Target as list, in Index as int, out Value as any) as undefined binds to "<builtin>"
46-
public foreign handler MCListStoreIndexOf(in Value as any, inout Target as list, in Index as int) as undefined binds to "<builtin>"
45+
public foreign handler MCListFetchIndexOf(in Target as list, in Index as int, out Value as optional any) as undefined binds to "<builtin>"
46+
public foreign handler MCListStoreIndexOf(in Value as optional any, inout Target as list, in Index as int) as undefined binds to "<builtin>"
4747

48-
public foreign handler MCListStoreBeforeElementOf(in Value as any, in Index as int, inout Target as list) as undefined binds to "<builtin>"
49-
public foreign handler MCListStoreAfterElementOf(in Value as any, in Index as int, inout Target as list) as undefined binds to "<builtin>"
48+
public foreign handler MCListStoreBeforeElementOf(in Value as optional any, in Index as int, inout Target as list) as undefined binds to "<builtin>"
49+
public foreign handler MCListStoreAfterElementOf(in Value as optional any, in Index as int, inout Target as list) as undefined binds to "<builtin>"
5050

5151
public foreign handler MCListSpliceIntoElementRangeOf(in Source as list, in Start as int, in Finish as int, inout Target as list) as undefined binds to "<builtin>"
5252
public foreign handler MCListSpliceIntoElementOf(in Source as list, in Index as int, inout Target as list) as undefined binds to "<builtin>"
@@ -58,13 +58,13 @@ public foreign handler MCListSpliceAfter(in Source as list, inout Target as list
5858

5959
public foreign handler MCListEvalEmpty(out Value as list) as undefined binds to "<builtin>"
6060

61-
public foreign handler MCListRepeatForEachElement(inout Iterator as optional pointer, out Iterand as any, in Container as list) as bool binds to "<builtin>"
61+
public foreign handler MCListRepeatForEachElement(inout Iterator as optional pointer, out Iterand as optional any, in Container as list) as bool binds to "<builtin>"
6262

63-
public foreign handler MCListFetchFirstElementOf(in Target as list, out Value as any) as undefined binds to "<builtin>"
64-
public foreign handler MCListStoreFirstElementOf(in Value as any, inout Target as list) as undefined binds to "<builtin>"
63+
public foreign handler MCListFetchFirstElementOf(in Target as list, out Value as optional any) as undefined binds to "<builtin>"
64+
public foreign handler MCListStoreFirstElementOf(in Value as optional any, inout Target as list) as undefined binds to "<builtin>"
6565

66-
public foreign handler MCListFetchLastElementOf(in Target as list, out Value as any) as undefined binds to "<builtin>"
67-
public foreign handler MCListStoreLastElementOf(in Value as any, inout Target as list) as undefined binds to "<builtin>"
66+
public foreign handler MCListFetchLastElementOf(in Target as list, out Value as optional any) as undefined binds to "<builtin>"
67+
public foreign handler MCListStoreLastElementOf(in Value as optional any, inout Target as list) as undefined binds to "<builtin>"
6868

6969
public foreign handler MCListExecDeleteElementOf(in Index as index, inout Target as list) as undefined binds to "<builtin>"
7070
public foreign handler MCListExecDeleteElementRangeOf(in Start as index, in Finish as index, inout Target as list) as undefined binds to "<builtin>"

libscript/src/module-array.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ extern "C" MC_DLLEXPORT void MCArrayEvalNumberOfElementsIn(MCArrayRef p_target,
8383

8484
extern "C" MC_DLLEXPORT void MCArrayEvalIsAmongTheElementsOf(MCValueRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output)
8585
{
86-
r_output = !MCArrayApply(p_target, is_not_among_the_elements_of, p_needle);
86+
MCValueRef t_value;
87+
t_value = p_needle != nil ? p_needle : kMCNull;
88+
r_output = !MCArrayApply(p_target, is_not_among_the_elements_of, t_value);
8789
}
8890

8991
extern "C" MC_DLLEXPORT void MCArrayEvalIsAmongTheKeysOfCaseless(MCStringRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output)
@@ -125,8 +127,11 @@ extern "C" MC_DLLEXPORT void MCArrayStoreElementOfCaseless(MCValueRef p_value, M
125127
MCAutoArrayRef t_array;
126128
MCArrayMutableCopy(x_target, &t_array);
127129

130+
MCValueRef t_value;
131+
t_value = p_value != nil ? p_value : kMCNull;
132+
128133
if (!create_key_for_array(p_key, x_target, &t_key) ||
129-
!MCArrayStoreValue(*t_array, MCArrayIsCaseSensitive(*t_array), *t_key, p_value))
134+
!MCArrayStoreValue(*t_array, MCArrayIsCaseSensitive(*t_array), *t_key, t_value))
130135
return;
131136

132137
MCAutoArrayRef t_new_array;

libscript/src/module-list.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ extern "C" MC_DLLEXPORT void MCListExecPushSingleElementOnto(MCValueRef p_value,
3333
MCAutoProperListRef t_mutable_list;
3434
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
3535
return;
36+
37+
MCValueRef t_value;
38+
t_value = p_value != nil ? p_value : kMCNull;
3639

3740
if (p_is_front)
3841
{
39-
if (!MCProperListPushElementOntoFront(*t_mutable_list, p_value))
42+
if (!MCProperListPushElementOntoFront(*t_mutable_list, t_value))
4043
return;
4144
}
4245
else
4346
{
44-
if (!MCProperListPushElementOntoBack(*t_mutable_list, p_value))
47+
if (!MCProperListPushElementOntoBack(*t_mutable_list, t_value))
4548
return;
4649
}
4750

@@ -83,8 +86,11 @@ extern "C" MC_DLLEXPORT void MCListEvalNumberOfElementsIn(MCProperListRef p_targ
8386

8487
extern "C" MC_DLLEXPORT void MCListEvalIsAmongTheElementsOf(MCValueRef p_needle, MCProperListRef p_target, bool& r_output)
8588
{
89+
MCValueRef t_value;
90+
t_value = p_needle != nil ? p_needle : kMCNull;
91+
8692
uindex_t t_dummy;
87-
r_output = MCProperListFirstIndexOfElement(p_target, p_needle, 0, t_dummy);
93+
r_output = MCProperListFirstIndexOfElement(p_target, t_value, 0, t_dummy);
8894
}
8995

9096
extern "C" MC_DLLEXPORT void MCListEvalContainsElements(MCProperListRef p_target, MCProperListRef p_needle, bool& r_output)
@@ -122,8 +128,11 @@ extern "C" MC_DLLEXPORT void MCListStoreElementOf(MCValueRef p_value, index_t p_
122128
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
123129
return;
124130

131+
MCValueRef t_value;
132+
t_value = p_value != nil ? p_value : kMCNull;
133+
125134
MCProperListRemoveElements(*t_mutable_list, t_start, t_count);
126-
MCProperListInsertElement(*t_mutable_list, p_value, t_start);
135+
MCProperListInsertElement(*t_mutable_list, t_value, t_start);
127136

128137
MCAutoProperListRef t_immutable;
129138
if (!MCProperListCopy(*t_mutable_list, &t_immutable))
@@ -161,8 +170,11 @@ extern "C" MC_DLLEXPORT void MCListStoreElementRangeOf(MCValueRef p_value, index
161170
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
162171
return;
163172

173+
MCValueRef t_value;
174+
t_value = p_value != nil ? p_value : kMCNull;
175+
164176
MCProperListRemoveElements(*t_mutable_list, t_start, t_count);
165-
MCProperListInsertElement(*t_mutable_list, p_value, t_start);
177+
MCProperListInsertElement(*t_mutable_list, t_value, t_start);
166178

167179
MCAutoProperListRef t_immutable;
168180
if (!MCProperListCopy(*t_mutable_list, &t_immutable))
@@ -178,7 +190,9 @@ extern "C" MC_DLLEXPORT void MCListFetchIndexOf(MCProperListRef p_target, index_
178190

179191
extern "C" MC_DLLEXPORT void MCListStoreIndexOf(MCValueRef p_value, MCProperListRef& x_target, index_t p_index)
180192
{
181-
MCListStoreElementOf(p_value, p_index, x_target);
193+
MCValueRef t_value;
194+
t_value = p_value != nil ? p_value : kMCNull;
195+
MCListStoreElementOf(t_value, p_index, x_target);
182196
}
183197

184198
extern "C" MC_DLLEXPORT void MCListStoreAfterElementOf(MCValueRef p_value, index_t p_index, MCProperListRef& x_target)
@@ -198,7 +212,10 @@ extern "C" MC_DLLEXPORT void MCListStoreAfterElementOf(MCValueRef p_value, index
198212
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
199213
return;
200214

201-
MCProperListInsertElement(*t_mutable_list, p_value, t_start);
215+
MCValueRef t_value;
216+
t_value = p_value != nil ? p_value : kMCNull;
217+
218+
MCProperListInsertElement(*t_mutable_list, t_value, t_start);
202219

203220
MCAutoProperListRef t_immutable;
204221
if (!MCProperListCopy(*t_mutable_list, &t_immutable))
@@ -222,7 +239,10 @@ extern "C" MC_DLLEXPORT void MCListStoreBeforeElementOf(MCValueRef p_value, inde
222239
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
223240
return;
224241

225-
MCProperListInsertElement(*t_mutable_list, p_value, t_start);
242+
MCValueRef t_value;
243+
t_value = p_value != nil ? p_value : kMCNull;
244+
245+
MCProperListInsertElement(*t_mutable_list, t_value, t_start);
226246

227247
MCAutoProperListRef t_immutable;
228248
if (!MCProperListCopy(*t_mutable_list, &t_immutable))
@@ -238,7 +258,9 @@ extern "C" MC_DLLEXPORT void MCListFetchFirstElementOf(MCProperListRef p_target,
238258

239259
extern "C" MC_DLLEXPORT void MCListStoreFirstElementOf(MCValueRef p_value, MCProperListRef& x_target)
240260
{
241-
MCListStoreElementOf(p_value, 1, x_target);
261+
MCValueRef t_value;
262+
t_value = p_value != nil ? p_value : kMCNull;
263+
MCListStoreElementOf(t_value, 1, x_target);
242264
}
243265

244266
extern "C" MC_DLLEXPORT void MCListFetchLastElementOf(MCProperListRef p_target, MCValueRef& r_output)
@@ -248,7 +270,9 @@ extern "C" MC_DLLEXPORT void MCListFetchLastElementOf(MCProperListRef p_target,
248270

249271
extern "C" MC_DLLEXPORT void MCListStoreLastElementOf(MCValueRef p_value, MCProperListRef& x_target)
250272
{
251-
MCListStoreElementOf(p_value, -1, x_target);
273+
MCValueRef t_value;
274+
t_value = p_value != nil ? p_value : kMCNull;
275+
MCListStoreElementOf(t_value, -1, x_target);
252276
}
253277

254278
////////////////////////////////////////////////////////////////////////////////////////////////////

toolchain/lc-compile/test.mlc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,13 @@ public handler testArray(inout xResults as list)
453453
delete tTest["key"]
454454

455455
testLog("Array", "DeleteElement", the number of elements in tTest is 0, xResults)
456-
456+
457+
variable tArrayWithUndefined as array
458+
put the empty array into tArrayWithUndefined
459+
put undefined into tArrayWithUndefined["a"]
460+
variable tUndefined
461+
put tArrayWithUndefined["a"] into tUndefined
462+
testLog("Array", "UndefinedElement", tUndefined is not defined, xResults)
457463
end handler
458464

459465
public handler testBitwise(inout xResults as list)
@@ -853,6 +859,13 @@ public handler testList(inout xResults as list)
853859
delete the first element of tTest
854860
testLog("List", "DeleteFirstElement", tTest[1] is 2, xResults)
855861

862+
variable tListWithUndefined as list
863+
put the empty list into tListWithUndefined
864+
push undefined onto tListWithUndefined
865+
variable tUndefined
866+
put the head of tListWithUndefined into tUndefined
867+
testLog("List", "UndefinedElement", tUndefined is not defined, xResults)
868+
856869
end handler
857870

858871
public handler testSort(inout xResults as list)

0 commit comments

Comments
 (0)