Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit b4ebcb4

Browse files
author
runrevali
committed
[[ StdMlc ]] Throw errors in the standard library
1 parent 5623374 commit b4ebcb4

File tree

9 files changed

+118
-81
lines changed

9 files changed

+118
-81
lines changed

libscript/src/module-array.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ extern "C" MC_DLLEXPORT void MCArrayEvalKeysOf(MCArrayRef p_target, MCProperList
6565
MCArrayApply(p_target, list_array_keys, t_list) &&
6666
MCProperListCopyAndRelease(t_list, r_output))
6767
return;
68-
69-
// ctxt . Throw()
7068
}
7169

7270
extern "C" MC_DLLEXPORT void MCArrayEvalElementsOf(MCArrayRef p_target, MCProperListRef& r_output)
@@ -76,8 +74,6 @@ extern "C" MC_DLLEXPORT void MCArrayEvalElementsOf(MCArrayRef p_target, MCProper
7674
MCArrayApply(p_target, list_array_elements, t_list) &&
7775
MCProperListCopyAndRelease(t_list, r_output))
7876
return;
79-
80-
// ctxt . Throw()
8177
}
8278

8379
extern "C" MC_DLLEXPORT void MCArrayEvalNumberOfElementsIn(MCArrayRef p_target, uindex_t& r_output)
@@ -93,7 +89,8 @@ extern "C" MC_DLLEXPORT void MCArrayEvalIsAmongTheElementsOf(MCValueRef p_needle
9389
extern "C" MC_DLLEXPORT void MCArrayEvalIsAmongTheKeysOf(MCStringRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output)
9490
{
9591
MCNewAutoNameRef t_key;
96-
create_key_for_array(p_needle, p_target, &t_key);
92+
if (!create_key_for_array(p_needle, p_target, &t_key))
93+
return;
9794

9895
MCValueRef t_value;
9996
t_value = nil;
@@ -108,13 +105,18 @@ extern "C" MC_DLLEXPORT void MCArrayFetchElementOfCaseless(MCArrayRef p_target,
108105
{
109106
MCNewAutoNameRef t_key;
110107

111-
if (create_key_for_array(p_key, p_target, &t_key) &&
112-
MCArrayFetchValue(p_target, MCArrayIsCaseSensitive(p_target), *t_key, r_output))
108+
if (!create_key_for_array(p_key, p_target, &t_key))
109+
return;
110+
111+
MCValueRef t_value;
112+
t_value = nil;
113+
if (!MCArrayFetchValue(p_target, MCArrayIsCaseSensitive(p_target), *t_key, t_value))
113114
{
114-
MCValueRetain(r_output);
115+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("array key does not exist"), nil);
115116
return;
116-
117117
}
118+
119+
r_output = MCValueRetain(t_value);
118120
}
119121

120122
extern "C" MC_DLLEXPORT void MCArrayStoreElementOfCaseless(MCValueRef p_value, MCArrayRef& x_target, MCStringRef p_key)

libscript/src/module-binary.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,21 @@ extern "C" MC_DLLEXPORT void MCBinaryEvalConcatenateBytes(MCDataRef p_left, MCDa
3434
extern "C" MC_DLLEXPORT void MCBinaryExecPutBytesBefore(MCDataRef p_source, MCDataRef& x_target)
3535
{
3636
MCAutoDataRef t_data;
37-
MCBinaryEvalConcatenateBytes(p_source, x_target, &t_data);
37+
MCBinaryEvalConcatenateBytes(p_source, x_target == (MCDataRef)kMCNull ? kMCEmptyData : x_target, &t_data);
38+
39+
if (MCErrorIsPending())
40+
return;
3841

3942
MCValueAssign(x_target, *t_data);
4043
}
4144

4245
extern "C" MC_DLLEXPORT void MCBinaryExecPutBytesAfter(MCDataRef p_source, MCDataRef& x_target)
4346
{
4447
MCAutoDataRef t_data;
45-
MCBinaryEvalConcatenateBytes(x_target, p_source, &t_data);
48+
MCBinaryEvalConcatenateBytes(x_target == (MCDataRef)kMCNull ? kMCEmptyData : x_target, p_source, &t_data);
49+
50+
if (MCErrorIsPending())
51+
return;
4652

4753
MCValueAssign(x_target, *t_data);
4854
}

libscript/src/module-byte.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ extern "C" MC_DLLEXPORT void MCByteEvalIsAmongTheBytesOf(MCDataRef p_needle, MCD
2727
{
2828
// Error if there is more than one byte.
2929
if (MCDataGetLength(p_needle) != 1)
30+
{
31+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("needle must be a single byte"), nil);
3032
return;
33+
}
3134

3235
bool t_found = MCDataContains(p_target, p_needle);
3336

@@ -100,11 +103,11 @@ extern "C" MC_DLLEXPORT void MCByteFetchByteRangeOf(index_t p_start, index_t p_f
100103
uindex_t t_start, t_count;
101104
MCChunkGetExtentsOfByteChunkByRange(p_target, p_start, p_finish, t_start, t_count);
102105

103-
if (t_count == 0)
104-
return;
105-
106-
if (t_start + t_count > MCDataGetLength(p_target))
106+
if (t_count == 0 || t_start + t_count > MCDataGetLength(p_target))
107+
{
108+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
107109
return;
110+
}
108111

109112
if (!MCDataCopyRange(p_target, MCRangeMake(t_start, t_count), r_output))
110113
return;
@@ -115,11 +118,11 @@ extern "C" MC_DLLEXPORT void MCByteStoreByteRangeOf(MCDataRef p_value, index_t p
115118
uindex_t t_start, t_count;
116119
MCChunkGetExtentsOfByteChunkByRange(x_target, p_start, p_finish, t_start, t_count);
117120

118-
if (t_count == 0)
119-
return;
120-
121-
if (t_start + t_count > MCDataGetLength(x_target))
121+
if (t_count == 0 || t_start + t_count > MCDataGetLength(x_target))
122+
{
123+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
122124
return;
125+
}
123126

124127
MCAutoDataRef t_data;
125128
if (!MCDataMutableCopy(x_target, &t_data))

libscript/src/module-char.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ extern "C" MC_DLLEXPORT void MCCharEvalIsAmongTheCharsOf(MCStringRef p_needle, M
2727
{
2828
// Error if there is more than one char in needle.
2929
if (MCStringGetLength(p_needle) == 1)
30+
{
31+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("needle must be a single char"), nil);
3032
return;
33+
}
3134

3235
uindex_t t_dummy;
3336
r_output = MCStringFirstIndexOfChar(p_target, MCStringGetCodepointAtIndex(p_needle, 0), 0, kMCStringOptionCompareExact, t_dummy);
@@ -38,11 +41,11 @@ extern "C" MC_DLLEXPORT void MCCharFetchCharRangeOf(index_t p_start, index_t p_f
3841
uindex_t t_start, t_count;
3942
MCChunkGetExtentsOfCodeunitChunkByRange(p_target, p_start, p_finish, t_start, t_count);
4043

41-
if (t_count == 0)
42-
return;
43-
44-
if (t_start + t_count > MCStringGetLength(p_target))
44+
if (t_count == 0 || t_start + t_count > MCStringGetLength(p_target))
45+
{
46+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
4547
return;
48+
}
4649

4750
if (!MCStringCopySubstring(p_target, MCRangeMake(t_start, t_count), r_output))
4851
return;
@@ -53,11 +56,11 @@ extern "C" MC_DLLEXPORT void MCCharStoreCharRangeOf(MCStringRef p_value, index_t
5356
uindex_t t_start, t_count;
5457
MCChunkGetExtentsOfCodeunitChunkByRange(x_target, p_start, p_finish, t_start, t_count);
5558

56-
if (t_count == 0)
57-
return;
58-
59-
if (t_start + t_count > MCStringGetLength(x_target))
59+
if (t_count == 0 || t_start + t_count > MCStringGetLength(x_target))
60+
{
61+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
6062
return;
63+
}
6164

6265
MCAutoStringRef t_string;
6366
if (!MCStringMutableCopy(x_target, &t_string))
@@ -112,12 +115,18 @@ extern "C" MC_DLLEXPORT void MCCharEvalOffsetOfChars(bool p_is_last, MCStringRef
112115

113116
extern "C" MC_DLLEXPORT void MCCharEvalOffsetOfCharsAfter(bool p_is_last, MCStringRef p_needle, uindex_t p_after, MCStringRef p_target, uindex_t& r_output)
114117
{
115-
MCCharEvalOffsetOfCharsInRange(p_is_last, p_needle, p_target, MCRangeMake(p_after, UINDEX_MAX), r_output);
118+
uindex_t t_start, t_count;
119+
MCChunkGetExtentsOfCodeunitChunkByRange(p_target, p_after, p_after, t_start, t_count);
120+
121+
MCCharEvalOffsetOfCharsInRange(p_is_last, p_needle, p_target, MCRangeMake(t_start + t_count, UINDEX_MAX), r_output);
116122
}
117123

118124
extern "C" MC_DLLEXPORT void MCCharEvalOffsetOfCharsBefore(bool p_is_first, MCStringRef p_needle, uindex_t p_before, MCStringRef p_target, uindex_t& r_output)
119125
{
120-
MCCharEvalOffsetOfCharsInRange(!p_is_first, p_needle, p_target, MCRangeMake(0, p_before), r_output);
126+
uindex_t t_start, t_count;
127+
MCChunkGetExtentsOfCodeunitChunkByRange(p_target, p_before, p_before, t_start, t_count);
128+
129+
MCCharEvalOffsetOfCharsInRange(!p_is_first, p_needle, p_target, MCRangeMake(0, t_start), r_output);
121130
}
122131

123132
extern "C" MC_DLLEXPORT void MCCharEvalContains(MCStringRef p_source, MCStringRef p_needle, bool& r_result)

libscript/src/module-list.cpp

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ extern "C" MC_DLLEXPORT void MCListExecPushSingleElementOnto(MCValueRef p_value,
5050
return;
5151

5252
MCValueAssign(x_target, *t_immutable);
53-
54-
// ctxt . Throw();
5553
}
5654

5755
extern "C" MC_DLLEXPORT void MCListExecPopElementInto(bool p_is_front, MCProperListRef& x_source, MCValueRef& r_output)
@@ -76,8 +74,6 @@ extern "C" MC_DLLEXPORT void MCListExecPopElementInto(bool p_is_front, MCProperL
7674
return;
7775

7876
MCValueAssign(x_source, *t_immutable);
79-
80-
// ctxt . Throw();
8177
}
8278

8379
extern "C" MC_DLLEXPORT void MCListEvalNumberOfElementsIn(MCProperListRef p_target, uindex_t& r_output)
@@ -102,11 +98,11 @@ extern "C" MC_DLLEXPORT void MCListFetchElementOf(index_t p_index, MCProperListR
10298
uindex_t t_start, t_count;
10399
MCChunkGetExtentsOfElementChunkByExpression(p_target, p_index, t_start, t_count);
104100

105-
if (t_count == 0)
106-
return;
107-
108-
if (t_start + t_count > MCProperListGetLength(p_target))
101+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(p_target))
102+
{
103+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
109104
return;
105+
}
110106

111107
r_output = MCValueRetain(MCProperListFetchElementAtIndex(p_target, t_start));
112108
}
@@ -116,11 +112,11 @@ extern "C" MC_DLLEXPORT void MCListStoreElementOf(MCValueRef p_value, index_t p_
116112
uindex_t t_start, t_count;
117113
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
118114

119-
if (t_count == 0)
120-
return;
121-
122-
if (t_start + t_count > MCProperListGetLength(x_target))
115+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(x_target))
116+
{
117+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
123118
return;
119+
}
124120

125121
MCAutoProperListRef t_mutable_list;
126122
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
@@ -141,11 +137,11 @@ extern "C" MC_DLLEXPORT void MCListFetchElementRangeOf(index_t p_start, index_t
141137
uindex_t t_start, t_count;
142138
MCChunkGetExtentsOfElementChunkByRange(p_target, p_start, p_finish, t_start, t_count);
143139

144-
if (t_count == 0)
145-
return;
146-
147-
if (t_start + t_count > MCProperListGetLength(p_target))
140+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(p_target))
141+
{
142+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
148143
return;
144+
}
149145

150146
MCProperListCopySublist(p_target, MCRangeMake(t_start, t_count), r_output);
151147
}
@@ -155,11 +151,11 @@ extern "C" MC_DLLEXPORT void MCListStoreElementRangeOf(MCValueRef p_value, index
155151
uindex_t t_start, t_count;
156152
MCChunkGetExtentsOfElementChunkByRange(x_target, p_start, p_finish, t_start, t_count);
157153

158-
if (t_count == 0)
159-
return;
160-
161-
if (t_start + t_count > MCProperListGetLength(x_target))
154+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(x_target))
155+
{
156+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
162157
return;
158+
}
163159

164160
MCAutoProperListRef t_mutable_list;
165161
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
@@ -191,11 +187,11 @@ extern "C" MC_DLLEXPORT void MCListStoreAfterElementOf(MCValueRef p_value, index
191187
t_start += t_count;
192188
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
193189

194-
if (t_count == 0)
195-
return;
196-
197-
if (t_start + t_count > MCProperListGetLength(x_target))
190+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(x_target))
191+
{
192+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
198193
return;
194+
}
199195

200196
MCAutoProperListRef t_mutable_list;
201197
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
@@ -215,11 +211,11 @@ extern "C" MC_DLLEXPORT void MCListStoreBeforeElementOf(MCValueRef p_value, inde
215211
uindex_t t_start, t_count;
216212
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
217213

218-
if (t_count == 0)
219-
return;
220-
221-
if (t_start + t_count > MCProperListGetLength(x_target))
214+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(x_target))
215+
{
216+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
222217
return;
218+
}
223219

224220
MCAutoProperListRef t_mutable_list;
225221
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
@@ -241,11 +237,11 @@ extern "C" MC_DLLEXPORT void MCListSpliceIntoElementRangeOf(MCProperListRef p_li
241237
uindex_t t_start, t_count;
242238
MCChunkGetExtentsOfElementChunkByRange(x_target, p_start, p_finish, t_start, t_count);
243239

244-
if (t_count == 0)
245-
return;
246-
247-
if (t_start + t_count > MCProperListGetLength(x_target))
240+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(x_target))
241+
{
242+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
248243
return;
244+
}
249245

250246
MCAutoProperListRef t_mutable_list;
251247
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
@@ -271,11 +267,11 @@ extern "C" MC_DLLEXPORT void MCListSpliceBeforeElementOf(MCProperListRef p_list,
271267
uindex_t t_start, t_count;
272268
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
273269

274-
if (t_count == 0)
275-
return;
276-
277-
if (t_start + t_count > MCProperListGetLength(x_target))
270+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(x_target))
271+
{
272+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
278273
return;
274+
}
279275

280276
MCAutoProperListRef t_mutable_list;
281277
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
@@ -295,11 +291,11 @@ extern "C" MC_DLLEXPORT void MCListSpliceAfterElementOf(MCProperListRef p_list,
295291
uindex_t t_start, t_count;
296292
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
297293

298-
if (t_count == 0)
299-
return;
300-
301-
if (t_start + t_count > MCProperListGetLength(x_target))
294+
if (t_count == 0 || t_start + t_count > MCProperListGetLength(x_target))
295+
{
296+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
302297
return;
298+
}
303299

304300
t_start += t_count;
305301

libscript/src/module-logic.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,5 @@ extern "C" MC_DLLEXPORT void MCLogicEvalStringParsedAsBoolean(MCStringRef p_oper
4343
else if (MCStringIsEqualTo(p_operand, kMCFalseString, kMCStringOptionCompareCaseless))
4444
r_output = false;
4545
else
46-
// Throw
47-
return;
46+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("string must be either \"true\" or \"false\""), nil);
4847
}

0 commit comments

Comments
 (0)