-
Notifications
You must be signed in to change notification settings - Fork 900
/
error.cpp
1296 lines (1131 loc) · 42.7 KB
/
error.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
AutoHotkey
Copyright 2003-2009 Chris Mallett (support@autohotkey.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "stdafx.h"
#include "script.h"
#include "globaldata.h"
#include "window.h"
#include "TextIO.h"
#include "abi.h"
#include <richedit.h>
Line *Line::PreparseError(LPTSTR aErrorText, LPTSTR aExtraInfo)
// Returns a different type of result for use with the Pre-parsing methods.
{
// Make all preparsing errors critical because the runtime reliability
// of the program relies upon the fact that the aren't any kind of
// problems in the script (otherwise, unexpected behavior may result).
// Update: It's okay to return FAIL in this case. CRITICAL_ERROR should
// be avoided whenever OK and FAIL are sufficient by themselves, because
// otherwise, callers can't use the NOT operator to detect if a function
// failed (since FAIL is value zero, but CRITICAL_ERROR is non-zero):
LineError(aErrorText, FAIL, aExtraInfo);
return NULL; // Always return NULL because the callers use it as their return value.
}
#ifdef CONFIG_DEBUGGER
LPCTSTR Debugger::WhatThrew()
{
// We want 'What' to indicate the function/sub/operation that *threw* the exception.
// For BIFs, throwing is always explicit. For a UDF, 'What' should only name it if
// it explicitly constructed the Exception object. This provides an easy way for
// OnError and Catch to categorise errors. No information is lost because File/Line
// can already be used locate the function/sub that was running.
// So only return a name when a BIF is raising an error:
if (mStack.mTop < mStack.mBottom || mStack.mTop->type != DbgStack::SE_BIF)
return _T("");
return mStack.mTop->func->mName;
}
#endif
IObject *Line::CreateRuntimeException(LPCTSTR aErrorText, LPCTSTR aExtraInfo, Object *aPrototype)
{
// Build the parameters for Object::Create()
ExprTokenType aParams[3]; int aParamCount = 2;
ExprTokenType* aParam[3] { aParams + 0, aParams + 1, aParams + 2 };
aParams[0].SetValue(const_cast<LPTSTR>(aErrorText));
#ifdef CONFIG_DEBUGGER
aParams[1].SetValue(const_cast<LPTSTR>(g_Debugger.WhatThrew()));
#else
// Without the debugger stack, there's no good way to determine what's throwing. It could be:
//g_act[mActionType].Name; // A command implemented as an Action (g_act).
//g->CurrentFunc->mName; // A user-defined function (perhaps when mActionType == ACT_THROW).
//???; // A built-in function implemented as a Func (g_BIF).
aParams[1].SetValue(_T(""), 0);
#endif
if (aExtraInfo && *aExtraInfo)
aParams[aParamCount++].SetValue(const_cast<LPTSTR>(aExtraInfo));
auto obj = Object::Create();
if (!obj)
return nullptr;
if (!aPrototype)
aPrototype = ErrorPrototype::Error;
obj->SetBase(aPrototype);
FuncResult rt;
g_script.mCurrLine = this;
g_script.mNewRuntimeException = obj;
if (!obj->Construct(rt, aParam, aParamCount))
obj = nullptr; // Construct released it.
g_script.mNewRuntimeException = nullptr;
return obj;
}
ResultType Line::ThrowRuntimeException(LPCTSTR aErrorText, LPCTSTR aExtraInfo)
{
return g_script.ThrowRuntimeException(aErrorText, aExtraInfo, this, FAIL);
}
ResultType Script::ThrowRuntimeException(LPCTSTR aErrorText, LPCTSTR aExtraInfo
, Line *aLine, ResultType aErrorType, Object *aPrototype)
{
// ThrownToken should only be non-NULL while control is being passed up the
// stack, which implies no script code can be executing.
ASSERT(!g->ThrownToken);
if (!aLine)
aLine = mCurrLine;
ResultToken *token;
if ( !(token = new ResultToken)
|| !(token->object = aLine->CreateRuntimeException(aErrorText, aExtraInfo, aPrototype)) )
{
// Out of memory. It's likely that we were called for this very reason.
// Since we don't even have enough memory to allocate an exception object,
// just show an error message and exit the thread. Don't call LineError(),
// since that would recurse into this function.
if (token)
delete token;
if (!g->ThrownToken)
{
MsgBox(ERR_OUTOFMEM ERR_ABORT);
return FAIL;
}
//else: Thrown by Error constructor?
}
else
{
token->symbol = SYM_OBJECT;
token->mem_to_free = NULL;
return aLine->SetThrownToken(*g, token, aErrorType);
}
// Returning FAIL causes each caller to also return FAIL, until either the
// thread has fully exited or the recursion layer handling ACT_TRY is reached:
return FAIL;
}
ResultType Script::ThrowRuntimeException(LPCTSTR aErrorText, LPCTSTR aExtraInfo)
{
return ThrowRuntimeException(aErrorText, aExtraInfo, mCurrLine, FAIL);
}
ResultType Line::SetThrownToken(global_struct &g, ResultToken *aToken, ResultType aErrorType)
{
#ifdef CONFIG_DEBUGGER
if (g_Debugger.IsConnected())
if (g_Debugger.IsAtBreak() || g_Debugger.PreThrow(aToken) && !(g.ExcptMode & EXCPTMODE_CATCH))
{
// IsAtBreak() indicates the debugger was already in a break state, likely
// processing a property_get or context_get which caused script execution.
// In that case, silence all error dialogs and don't set g.ThrownToken since
// the debugger is lax about detecting/clearing it. If PreThrow was called:
// The debugger has entered (and left) a break state, so the client has had a
// chance to inspect the exception and report it. There's nothing in the DBGp
// spec about what to do next, probably since PHP would just log the error.
// In our case, it seems more useful to suppress the dialog than to show it.
g_script.FreeExceptionToken(aToken);
return FAIL;
}
#endif
g.ThrownToken = aToken;
if (!(g.ExcptMode & EXCPTMODE_CATCH))
return g_script.UnhandledException(this, aErrorType); // Usually returns FAIL; may return OK if aErrorType == FAIL_OR_OK.
return FAIL;
}
ResultType Script::Win32Error(DWORD aError, ResultType aErrorType)
{
TCHAR number_string[_MAX_ULTOSTR_BASE10_COUNT];
// Convert aError to string to pass it through RuntimeError, but it will ultimately
// be converted to the error number and proper message by OSError.Prototype.__New.
_ultot(aError, number_string, 10);
return RuntimeError(number_string, _T(""), aErrorType, nullptr, ErrorPrototype::OS);
}
void Script::SetErrorStdOut(LPTSTR aParam)
{
mErrorStdOutCP = Line::ConvertFileEncoding(aParam);
// Seems best not to print errors to stderr if the encoding was invalid. Current behaviour
// for an encoding of -1 would be to print only the ASCII characters and drop the rest, but
// if our caller is expecting UTF-16, it won't be readable.
mErrorStdOut = mErrorStdOutCP != -1;
// If invalid, no error is shown here because this function might be called early, before
// Line::sSourceFile[0] is given its value. Instead, errors appearing as dialogs should
// be a sufficient clue that the /ErrorStdOut= value was invalid.
}
void Script::PrintErrorStdOut(LPCTSTR aErrorText, int aLength, LPCTSTR aFile)
{
#ifdef CONFIG_DEBUGGER
if (g_Debugger.OutputStdOut(aErrorText))
return;
#endif
TextFile tf;
tf.Open(aFile, TextStream::APPEND, mErrorStdOutCP);
tf.Write(aErrorText, aLength);
tf.Close();
}
int FormatStdErr(LPTSTR aBuf, int aBufSize, LPCTSTR aErrorText, LPCTSTR aExtraInfo, FileIndexType aFileIndex, LineNumberType aLineNumber, bool aWarn = false)
{
#define STD_ERROR_FORMAT _T("%s (%d) : ==> %s%s\n")
int n = sntprintf(aBuf, aBufSize, STD_ERROR_FORMAT, Line::sSourceFile[aFileIndex], aLineNumber
, aWarn ? _T("Warning: ") : _T(""), aErrorText);
if (*aExtraInfo)
n += sntprintf(aBuf + n, aBufSize - n, _T(" Specifically: %s\n"), aExtraInfo);
return n;
}
// For backward compatibility, this actually prints to stderr, not stdout.
void Script::PrintErrorStdOut(LPCTSTR aErrorText, LPCTSTR aExtraInfo, FileIndexType aFileIndex, LineNumberType aLineNumber)
{
TCHAR buf[LINE_SIZE * 2];
auto n = FormatStdErr(buf, _countof(buf), aErrorText, aExtraInfo, aFileIndex, aLineNumber);
PrintErrorStdOut(buf, n, _T("**"));
}
ResultType Line::LineError(LPCTSTR aErrorText, ResultType aErrorType, LPCTSTR aExtraInfo)
{
ASSERT(aErrorText);
if (!aExtraInfo)
aExtraInfo = _T("");
if (g_script.mIsReadyToExecute)
{
return g_script.RuntimeError(aErrorText, aExtraInfo, aErrorType, this);
}
#ifdef CONFIG_DLL
if (LibNotifyProblem(aErrorText, aExtraInfo, this))
return aErrorType;
#endif
if (g_script.mErrorStdOut && aErrorType != WARN)
{
// JdeB said:
// Just tested it in Textpad, Crimson and Scite. they all recognise the output and jump
// to the Line containing the error when you double click the error line in the output
// window (like it works in C++). Had to change the format of the line to:
// printf("%s (%d) : ==> %s: \n%s \n%s\n",szInclude, nAutScriptLine, szText, szScriptLine, szOutput2 );
// MY: Full filename is required, even if it's the main file, because some editors (EditPlus)
// seem to rely on that to determine which file and line number to jump to when the user double-clicks
// the error message in the output window.
// v1.0.47: Added a space before the colon as originally intended. Toralf said, "With this minor
// change the error lexer of Scite recognizes this line as a Microsoft error message and it can be
// used to jump to that line."
g_script.PrintErrorStdOut(aErrorText, aExtraInfo, mFileIndex, mLineNumber);
return FAIL;
}
return g_script.ShowError(aErrorText, aErrorType, aExtraInfo, this);
}
ResultType Script::RuntimeError(LPCTSTR aErrorText, LPCTSTR aExtraInfo, ResultType aErrorType, Line *aLine, Object *aPrototype)
{
ASSERT(aErrorText);
if (!aExtraInfo)
aExtraInfo = _T("");
#ifdef CONFIG_DEBUGGER
if (g_Debugger.IsAtBreak()) // i.e. the debugger is processing a property_get or context_get.
return FAIL; // Silent abort, no g->ThrownToken.
#endif
if ((g->ExcptMode || mOnError.Count()
#ifdef CONFIG_DEBUGGER
|| g_Debugger.BreakOnExceptionIsEnabled()
#endif
|| aPrototype) && aErrorType != WARN)
return ThrowRuntimeException(aErrorText, aExtraInfo, aLine, aErrorType, aPrototype);
#ifdef CONFIG_DLL
if (LibNotifyProblem(aErrorText, aExtraInfo, aLine))
return aErrorType;
#endif
return ShowError(aErrorText, aErrorType, aExtraInfo, aLine);
}
FResult FError(LPCTSTR aErrorText, LPCTSTR aExtraInfo, Object *aPrototype)
{
return g_script.RuntimeError(aErrorText, aExtraInfo, FAIL_OR_OK, nullptr, aPrototype) ? FR_ABORTED : FR_FAIL;
}
struct ErrorBoxParam
{
LPCTSTR text;
ResultType type;
LPCTSTR info;
Line *line;
IObject *obj;
#ifdef CONFIG_DEBUGGER
int stack_index;
#endif
};
#ifdef CONFIG_DEBUGGER
void InsertCallStack(HWND re, ErrorBoxParam &error)
{
TCHAR buf[SCRIPT_STACK_BUF_SIZE], *stack = _T("");
if (error.obj && error.obj->IsOfType(Object::sPrototype))
{
auto obj = static_cast<Object*>(error.obj);
ExprTokenType stk;
if (obj->GetOwnProp(stk, _T("Stack")) && stk.symbol == SYM_STRING)
stack = stk.marker;
}
else if (error.stack_index >= 0)
{
GetScriptStack(stack = buf, _countof(buf), g_Debugger.mStack.mBottom + error.stack_index);
}
CHARFORMAT cfBold;
cfBold.cbSize = sizeof(cfBold);
cfBold.dwMask = CFM_BOLD | CFM_LINK;
cfBold.dwEffects = CFE_BOLD;
SendMessage(re, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfBold);
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)_T("Call stack:\n"));
cfBold.dwEffects = 0;
SendMessage(re, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfBold);
if (!*stack)
return;
// Prevent insertion of a blank line at the end (a bit pedantic, I know):
auto stack_end = _tcschr(stack, '\0');
if (stack_end[-1] == '\n')
*--stack_end = '\0';
if (stack_end > stack && stack_end[-1] == '\r')
*--stack_end = '\0';
CHARFORMAT cfLink;
cfLink.cbSize = sizeof(cfLink);
cfLink.dwMask = CFM_LINK | CFM_BOLD;
cfLink.dwEffects = CFE_LINK;
//cfLink.crTextColor = 0xbb4d00; // Has no effect on Windows 7 or 11 (even with CFM_COLOR).
CHARRANGE cr;
SendMessage(re, EM_EXGETSEL, 0, (LPARAM)&cr); // This will become the start position of the stack text.
auto start_pos = cr.cpMin;
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)stack);
for (auto cp = stack; ; )
{
if (auto ext = _tcsstr(cp, _T(".ahk (")))
{
// Apply CFE_LINK effect (and possibly colour) to the full path.
cr.cpMax = cr.cpMin + int(ext - cp) + 4;
SendMessage(re, EM_EXSETSEL, 0, (LPARAM)&cr);
SendMessage(re, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfLink);
}
auto cpn = _tcschr(cp, '\n');
if (!cpn)
break;
cr.cpMin += int(cpn - cp);
if (cpn == cp || cpn[-1] != '\r') // Count the \n only if \r wasn't already counted, since it seems RichEdit uses just \r internally.
++cr.cpMin;
cp = cpn + 1;
}
cr.cpMin = cr.cpMax = start_pos - 1; // Remove selection.
SendMessage(re, EM_EXSETSEL, 0, (LPARAM)&cr);
}
#endif
void InitErrorBox(HWND hwnd, ErrorBoxParam &error)
{
TCHAR buf[1024];
SetWindowText(hwnd, g_script.DefaultDialogTitle());
SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)&error);
HWND re = GetDlgItem(hwnd, IDC_ERR_EDIT);
RECT rc, rcOffset {0,0,7,7};
SendMessage(re, EM_GETRECT, 0, (LPARAM)&rc);
MapDialogRect(hwnd, &rcOffset);
rc.left += rcOffset.right;
rc.top += rcOffset.bottom;
rc.right -= rcOffset.right;
rc.bottom -= rcOffset.bottom;
SendMessage(re, EM_SETRECTNP, 0, (LPARAM)&rc);
PARAFORMAT pf;
pf.cbSize = sizeof(pf);
pf.dwMask = PFM_TABSTOPS;
pf.cTabCount = 1;
pf.rgxTabs[0] = 300;
SendMessage(re, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
SETTEXTEX t { ST_SELECTION | ST_UNICODE, CP_UTF16 };
SETTEXTEX t_rtf { ST_SELECTION | ST_DEFAULT, CP_UTF8 };
CHARFORMAT2 cf;
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_SIZE;
cf.yHeight = 9*20;
SendMessage(re, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
cf.dwMask = CFM_SIZE | CFM_COLOR;
cf.yHeight = 10*20;
cf.crTextColor = 0x3399;
cf.dwEffects = 0;
SendMessage(re, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
sntprintf(buf, _countof(buf), _T("%s: %.500s\n\n")
, error.type == CRITICAL_ERROR ? _T("Critical Error")
: error.type == WARN ? _T("Warning") : _T("Error")
, error.text);
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)buf);
if (error.info && *error.info)
{
UINT suffix = _tcslen(error.info) > 80 ? 8230 : 0;
if (error.line)
sntprintf(buf, _countof(buf), _T("Specifically: %.80s%s\n\n"), error.info, &suffix);
else
sntprintf(buf, _countof(buf), _T("Text:\t%.80s%s\n"), error.info, &suffix);
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)buf);
}
if (error.line)
{
#define LINES_ABOVE_AND_BELOW 2
// Determine the range of lines to be shown:
Line *line_start = error.line, *line_end = error.line;
if (g_AllowMainWindow)
{
for (int i = 0
; i < LINES_ABOVE_AND_BELOW && line_start->mPrevLine != NULL
; ++i, line_start = line_start->mPrevLine);
for (int i = 0
; i < LINES_ABOVE_AND_BELOW && line_end->mNextLine != NULL
; ++i, line_end = line_end->mNextLine);
}
//else show only a single line, to conceal the script's source code.
int last_file = 0; // Init to zero so path is omitted if it is the main file.
for (auto line = line_start; ; line = line->mNextLine)
{
if (last_file != line->mFileIndex)
{
last_file = line->mFileIndex;
sntprintf(buf, _countof(buf), _T("\t---- %s\n"), Line::sSourceFile[line->mFileIndex]);
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)buf);
}
int lead = 0;
if (line == error.line)
{
cf.dwMask = CFM_COLOR | CFM_BACKCOLOR;
cf.crTextColor = 0; // Use explicit black to ensure visibility if a high contrast theme is enabled.
cf.crBackColor = 0x60ffff;
SendMessage(re, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
buf[lead++] = 9654; // ▶
}
buf[lead++] = '\t';
buf[lead] = '\0';
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)buf);
line->ToText(buf, _countof(buf), true);
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)buf);
if (line == line_end)
break;
}
}
else
{
sntprintf(buf, _countof(buf), _T("Line:\t%d\nFile:\t"), g_script.CurrentLine());
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)buf);
cf.dwMask = CFM_LINK;
cf.dwEffects = CFE_LINK; // Mark it as a link.
SendMessage(re, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)g_script.CurrentFile());
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)_T("\n\n"));
}
LPCTSTR footer;
switch (error.type)
{
case WARN: footer = ERR_WARNING_FOOTER; break;
case FAIL_OR_OK: footer = nullptr; break;
case CRITICAL_ERROR: footer = UNSTABLE_WILL_EXIT; break;
default: footer = (g->ExcptMode & EXCPTMODE_DELETE) ? ERR_ABORT_DELETE
: g_script.mIsReadyToExecute ? ERR_ABORT_NO_SPACES
: g_script.mIsRestart ? OLD_STILL_IN_EFFECT
: WILL_EXIT;
}
if (footer)
{
if (error.line)
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)_T("\n"));
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)footer);
}
#ifdef CONFIG_DEBUGGER
ExprTokenType tk;
if ( error.stack_index >= 0
|| error.obj && error.obj->IsOfType(Object::sPrototype)
&& static_cast<Object*>(error.obj)->GetOwnProp(tk, _T("Stack"))
&& !TokenIsEmptyString(tk) )
{
// Stack trace appears to be available, so add a link to show it.
CHARRANGE cr;
for (int i = footer ? 2 : 1; i; --i)
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)_T("\n"));
SendMessage(re, EM_EXGETSEL, 0, (LPARAM)&cr);
#define SHOW_CALL_STACK_TEXT _T("Show call stack »")
SendMessage(re, EM_REPLACESEL, FALSE, (LPARAM)SHOW_CALL_STACK_TEXT);
cr.cpMax = -1; // Select to end.
SendMessage(re, EM_EXSETSEL, 0, (LPARAM)&cr);
cf.dwMask = CFM_LINK;
cf.dwEffects = CFE_LINK; // Mark it as a link.
SendMessage(re, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
cr.cpMin = -1; // Deselect (move selection anchor to insertion point).
SendMessage(re, EM_EXSETSEL, 0, (LPARAM)&cr);
}
#endif
SendMessage(re, EM_SETEVENTMASK, 0, ENM_REQUESTRESIZE | ENM_LINK | ENM_KEYEVENTS);
SendMessage(re, EM_REQUESTRESIZE, 0, 0);
#ifndef AUTOHOTKEYSC
if (error.line && error.line->mFileIndex ? *Line::sSourceFile[error.line->mFileIndex] == '*'
: g_script.mKind != Script::ScriptKindFile)
// Source "file" is an embedded resource or stdin, so can't be edited.
EnableWindow(GetDlgItem(hwnd, ID_FILE_EDITSCRIPT), FALSE);
#endif
if (error.type != FAIL_OR_OK)
{
HWND hide = GetDlgItem(hwnd, error.type == WARN ? IDCANCEL : IDCONTINUE);
if (error.type == WARN)
{
// Hide "Abort" since it it's not applicable to warnings except as an alias of ExitApp,
// shift "Continue" to the right for aesthetic purposes and make it the default button
// (otherwise the left-most button would become the default).
RECT rc;
GetClientRect(hide, &rc);
MapWindowPoints(hide, hwnd, (LPPOINT)&rc, 1);
HWND keep = GetDlgItem(hwnd, IDCONTINUE);
MoveWindow(keep, rc.left, rc.top, rc.right, rc.bottom, FALSE);
DefDlgProc(hwnd, DM_SETDEFID, IDCONTINUE, 0);
SetFocus(keep);
}
ShowWindow(hide, SW_HIDE);
}
}
INT_PTR CALLBACK ErrorBoxProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDCONTINUE:
case IDCANCEL:
EndDialog(hwnd, wParam);
return TRUE;
case ID_FILE_EDITSCRIPT:
{
auto &error = *(ErrorBoxParam*)GetWindowLongPtr(hwnd, DWLP_USER);
if (error.line)
{
g_script.Edit(Line::sSourceFile[error.line->mFileIndex]);
return TRUE;
}
}
default:
if (LOWORD(wParam) >= ID_FILE_RELOADSCRIPT)
{
// Call the handler directly since g_hWnd might be NULL if this is a warning dialog.
HandleMenuItem(NULL, LOWORD(wParam), NULL);
if (LOWORD(wParam) == ID_FILE_RELOADSCRIPT)
EndDialog(hwnd, IDCANCEL);
return TRUE;
}
}
break;
case WM_NOTIFY:
if (wParam == IDC_ERR_EDIT)
{
HWND re = ((NMHDR*)lParam)->hwndFrom;
switch (((NMHDR*)lParam)->code)
{
case EN_MSGFILTER:
{
auto mf = (MSGFILTER*)lParam;
if (mf->msg == WM_CHAR)
// Forward it to any of the buttons so the dialog will process it as a mnemonic.
PostMessage(GetDlgItem(hwnd, IDCANCEL), mf->msg, mf->wParam, mf->lParam);
break;
}
case EN_REQUESTRESIZE: // Received when the RichEdit's content grows beyond its capacity to display all at once.
{
RECT &rcNew = ((REQRESIZE*)lParam)->rc;
RECT rcOld, rcInner;
GetWindowRect(re, &rcOld);
SendMessage(re, EM_GETRECT, 0, (LPARAM)&rcInner);
// Stack traces can get quite "tall" if the paths/lines are mostly short,
// so impose a rough limit to ensure the dialog remains usable.
int rough_limit = GetSystemMetrics(SM_CYSCREEN) * 3 / 4;
if (rcNew.bottom > rough_limit)
rcNew.bottom = rough_limit;
int delta = rcNew.bottom - (rcInner.bottom - rcInner.top);
if (rcNew.bottom == rough_limit)
SendMessage(re, EM_SHOWSCROLLBAR, SB_VERT, TRUE);
// Enable horizontal scroll bars if necessary.
if (rcNew.right > (rcInner.right - rcInner.left)
&& !(GetWindowLong(re, GWL_STYLE) & WS_HSCROLL))
{
SendMessage(re, EM_SHOWSCROLLBAR, SB_HORZ, TRUE);
delta += GetSystemMetrics(SM_CYHSCROLL);
}
// Move the buttons (and the RichEdit, temporarily).
ScrollWindow(hwnd, 0, delta, NULL, NULL);
// Resize the RichEdit, while also moving it back to the origin.
MoveWindow(re, 0, 0, rcOld.right - rcOld.left, rcOld.bottom - rcOld.top + delta, TRUE);
// Adjust the dialog's height and vertical position.
GetWindowRect(hwnd, &rcOld);
MoveWindow(hwnd, rcOld.left, rcOld.top - (delta / 2), rcOld.right - rcOld.left, rcOld.bottom - rcOld.top + delta, TRUE);
break;
}
case EN_LINK: // Received when the user clicks or moves the mouse over text with the CFE_LINK effect.
if (((ENLINK*)lParam)->msg == WM_LBUTTONUP)
{
TEXTRANGE tr { ((ENLINK*)lParam)->chrg };
SendMessage(re, EM_EXSETSEL, 0, (LPARAM)&tr.chrg);
tr.lpstrText = (LPTSTR)talloca(tr.chrg.cpMax - tr.chrg.cpMin + 1);
*tr.lpstrText = '\0';
SendMessage(re, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
PostMessage(hwnd, WM_NEXTDLGCTL, TRUE, FALSE); // Make it less edit-like by shifting focus away.
#ifdef CONFIG_DEBUGGER
if (!_tcscmp(tr.lpstrText, SHOW_CALL_STACK_TEXT))
{
auto &error = *(ErrorBoxParam*)GetWindowLongPtr(hwnd, DWLP_USER);
InsertCallStack(re, error);
return TRUE;
}
#endif
g_script.Edit(tr.lpstrText);
return TRUE;
}
break;
}
}
break;
case WM_INITDIALOG:
InitErrorBox(hwnd, *(ErrorBoxParam *)lParam);
return FALSE; // "return FALSE to prevent the system from setting the default keyboard focus"
}
return FALSE;
}
ResultType Script::ShowError(LPCTSTR aErrorText, ResultType aErrorType, LPCTSTR aExtraInfo, Line *aLine, IObject *aException)
{
if (!aErrorText)
aErrorText = _T("");
if (!aExtraInfo)
aExtraInfo = _T("");
if (!aLine)
aLine = mCurrLine;
#ifdef CONFIG_DEBUGGER
if (g_Debugger.HasStdErrHook())
{
TCHAR buf[LINE_SIZE * 2];
FormatStdErr(buf, _countof(buf), aErrorText, aExtraInfo
, aLine ? aLine->mFileIndex : mCurrFileIndex
, aLine ? aLine->mLineNumber : mCombinedLineNumber
, aErrorType == WARN);
g_Debugger.OutputStdErr(buf);
}
#endif
static auto sMod = LoadLibrary(_T("riched20.dll")); // RichEdit20W
//static auto sMod = LoadLibrary(_T("msftedit.dll")); // MSFTEDIT_CLASS (RICHEDIT50W)
ErrorBoxParam error;
error.text = aErrorText;
error.type = aErrorType;
error.info = aExtraInfo;
error.line = aLine;
error.obj = aException;
#ifdef CONFIG_DEBUGGER
error.stack_index = (aException || !g_script.mIsReadyToExecute) ? -1 : int(g_Debugger.mStack.mTop - g_Debugger.mStack.mBottom);
#endif
INT_PTR result = DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_ERRORBOX), NULL, ErrorBoxProc, (LPARAM)&error);
if (result == IDCONTINUE && aErrorType == FAIL_OR_OK)
return OK;
if (result == -1) // May have failed to show the custom dialog box.
MsgBox(aErrorText, MB_TOPMOST); // Keep it simple since it will hopefully never be needed.
if (aErrorType == CRITICAL_ERROR && mIsReadyToExecute)
ExitApp(EXIT_CRITICAL); // Pass EXIT_CRITICAL to ensure the program always exits, regardless of OnExit.
if (aErrorType == WARN && result == IDCANCEL && !mIsReadyToExecute) // Let Escape cancel loading the script.
ExitApp(EXIT_EXIT);
return FAIL; // Some callers rely on a FAIL result to propagate failure.
}
ResultType Script::ScriptError(LPCTSTR aErrorText, LPCTSTR aExtraInfo)
// Even though this is a Script method, including it here since it shares
// a common theme with the other error-displaying functions:
{
if (mCurrLine && g_script.mIsReadyToExecute)
// If a line is available, do RuntimeError instead for exceptions and line context.
return RuntimeError(aErrorText, aExtraInfo, FAIL, mCurrLine);
// Otherwise: The fact that mCurrLine is NULL means that the line currently being loaded
// has not yet been successfully added to the linked list. Such errors will always result
// in the program exiting.
if (!aErrorText)
aErrorText = _T("Unk"); // Placeholder since it shouldn't be NULL.
if (!aExtraInfo) // In case the caller explicitly called it with NULL.
aExtraInfo = _T("");
#ifdef CONFIG_DLL
if (LibNotifyProblem(aErrorText, aExtraInfo, nullptr))
return FAIL;
#endif
if (g_script.mErrorStdOut && !g_script.mIsReadyToExecute) // i.e. runtime errors are always displayed via dialog.
{
// See LineError() for details.
PrintErrorStdOut(aErrorText, aExtraInfo, mCurrFileIndex, mCombinedLineNumber);
}
else
{
ShowError(aErrorText, FAIL, aExtraInfo, nullptr);
}
return FAIL; // See above for why it's better to return FAIL than CRITICAL_ERROR.
}
LPCTSTR VarKindForErrorMessage(Var *aVar)
{
switch (aVar->Type())
{
case VAR_VIRTUAL: return _T("built-in variable");
case VAR_CONSTANT: return aVar->Object()->Type();
default: return Var::DeclarationType(aVar->Scope());
}
}
ResultType Script::ConflictingDeclarationError(LPCTSTR aDeclType, Var *aExisting)
{
TCHAR buf[127];
sntprintf(buf, _countof(buf), _T("This %s declaration conflicts with an existing %s.")
, aDeclType, VarKindForErrorMessage(aExisting));
return ScriptError(buf, aExisting->mName);
}
ResultType Line::ValidateVarUsage(Var *aVar, int aUsage)
{
if ( VARREF_IS_WRITE(aUsage)
&& (aUsage == VARREF_REF
? aVar->Type() != VAR_NORMAL // Aliasing VAR_VIRTUAL is currently unsupported.
: aVar->IsReadOnly()) )
return VarIsReadOnlyError(aVar, aUsage);
return OK;
}
ResultType Script::VarIsReadOnlyError(Var *aVar, int aErrorType)
{
TCHAR buf[127];
sntprintf(buf, _countof(buf), _T("This %s cannot %s.")
, VarKindForErrorMessage(aVar)
, aErrorType == VARREF_LVALUE ? _T("be assigned a value")
: aErrorType == VARREF_REF ? _T("have its reference taken")
: _T("be used as an output variable"));
return ScriptError(buf, aVar->mName);
}
ResultType Line::VarIsReadOnlyError(Var *aVar, int aErrorType)
{
g_script.mCurrLine = this;
return g_script.VarIsReadOnlyError(aVar, aErrorType);
}
ResultType Line::LineUnexpectedError()
{
TCHAR buf[127];
sntprintf(buf, _countof(buf), _T("Unexpected \"%s\""), g_act[mActionType].Name);
return LineError(buf);
}
ResultType Script::CriticalError(LPCTSTR aErrorText, LPCTSTR aExtraInfo)
{
g->ExcptMode = EXCPTMODE_NONE; // Do not throw an exception.
if (mCurrLine)
mCurrLine->LineError(aErrorText, CRITICAL_ERROR, aExtraInfo);
// mCurrLine should always be non-NULL during runtime, and CRITICAL_ERROR should
// cause LineError() to exit even if an OnExit routine is present, so this is here
// mainly for maintainability.
TerminateApp(EXIT_CRITICAL, 0);
return FAIL; // Never executed.
}
__declspec(noinline)
ResultType ResultToken::Error(LPCTSTR aErrorText)
{
// Defining this overload separately rather than making aErrorInfo optional reduces code size
// by not requiring the compiler to 'push' the second parameter's default value at each call site.
return Error(aErrorText, _T(""));
}
__declspec(noinline)
ResultType ResultToken::Error(LPCTSTR aErrorText, LPCTSTR aExtraInfo)
{
return Error(aErrorText, aExtraInfo, nullptr);
}
__declspec(noinline)
ResultType ResultToken::Error(LPCTSTR aErrorText, Object *aPrototype)
{
return Error(aErrorText, nullptr, aPrototype);
}
__declspec(noinline)
ResultType ResultToken::Error(LPCTSTR aErrorText, LPCTSTR aExtraInfo, Object *aPrototype)
{
// These two assertions should always pass, since anything else would imply returning a value,
// not throwing an error. If they don't, the memory/object might not be freed since the caller
// isn't expecting a value, or they might be freed twice (if the callee already freed it).
//ASSERT(!mem_to_free); // At least one caller frees it after calling this function.
ASSERT(symbol != SYM_OBJECT);
return Fail(g_script.RuntimeError(aErrorText, aExtraInfo, FAIL_OR_OK, nullptr, aPrototype));
}
__declspec(noinline)
ResultType ResultToken::Error(LPCTSTR aErrorText, ExprTokenType &aExtraInfo, Object *aPrototype)
{
TCHAR buf[MAX_NUMBER_SIZE];
return Error(aErrorText, TokenToString(aExtraInfo, buf), aPrototype);
}
__declspec(noinline)
ResultType ResultToken::MemoryError()
{
return Error(ERR_OUTOFMEM, nullptr, ErrorPrototype::Memory);
}
ResultType MemoryError()
{
return g_script.RuntimeError(ERR_OUTOFMEM, nullptr, FAIL, nullptr, ErrorPrototype::Memory);
}
void SimpleHeap::CriticalFail()
{
g_script.CriticalError(ERR_OUTOFMEM);
}
__declspec(noinline)
ResultType ResultToken::ValueError(LPCTSTR aErrorText)
{
return Error(aErrorText, nullptr, ErrorPrototype::Value);
}
__declspec(noinline)
ResultType ResultToken::ValueError(LPCTSTR aErrorText, LPCTSTR aExtraInfo)
{
return Error(aErrorText, aExtraInfo, ErrorPrototype::Value);
}
__declspec(noinline)
ResultType ValueError(LPCTSTR aErrorText, LPCTSTR aExtraInfo, ResultType aErrorType)
{
if (!g_script.mIsReadyToExecute)
return g_script.ScriptError(aErrorText, aExtraInfo);
return g_script.RuntimeError(aErrorText, aExtraInfo, aErrorType, nullptr, ErrorPrototype::Value);
}
__declspec(noinline)
FResult FValueError(LPCTSTR aErrorText, LPCTSTR aExtraInfo)
{
return FError(aErrorText, aExtraInfo, ErrorPrototype::Value);
}
__declspec(noinline)
ResultType ResultToken::UnknownMemberError(ExprTokenType &aObject, int aFlags, LPCTSTR aMember)
{
TCHAR msg[512];
if (!aMember)
aMember = (aFlags & IT_CALL) ? _T("Call") : _T("__Item");
sntprintf(msg, _countof(msg), _T("This value of type \"%s\" has no %s named \"%s\".")
, TokenTypeString(aObject), (aFlags & IT_CALL) ? _T("method") : _T("property"), aMember);
return Error(msg, nullptr, (aFlags & IT_CALL) ? ErrorPrototype::Method : ErrorPrototype::Property);
}
__declspec(noinline)
ResultType ResultToken::Win32Error(DWORD aError)
{
if (g_script.Win32Error(aError) == FAIL)
return SetExitResult(FAIL);
SetValue(_T(""), 0);
return FAIL;
}
void TokenTypeAndValue(ExprTokenType &aToken, LPCTSTR &aType, LPCTSTR &aValue, TCHAR *aNBuf)
{
if (aToken.symbol == SYM_VAR && aToken.var->IsUninitializedNormalVar())
aType = _T("unset variable"), aValue = aToken.var->mName;
else if (TokenIsEmptyString(aToken))
aType = _T("empty string"), aValue = _T("");
else
aType = TokenTypeString(aToken), aValue = TokenToString(aToken, aNBuf);
}
__declspec(noinline)
ResultType TypeError(LPCTSTR aExpectedType, ExprTokenType &aActualValue)
{
TCHAR number_buf[MAX_NUMBER_SIZE];
LPCTSTR actual_type, value_as_string;
TokenTypeAndValue(aActualValue, actual_type, value_as_string, number_buf);
return TypeError(aExpectedType, actual_type, value_as_string);
}
ResultType TypeError(LPCTSTR aExpectedType, LPCTSTR aActualType, LPCTSTR aExtraInfo)
{
auto an = [](LPCTSTR thing) {
return _tcschr(_T("aeiou"), ctolower(*thing)) ? _T("n") : _T("");
};
TCHAR msg[512];
sntprintf(msg, _countof(msg), _T("Expected a%s %s but got a%s %s.")
, an(aExpectedType), aExpectedType, an(aActualType), aActualType);
return g_script.RuntimeError(msg, aExtraInfo, FAIL_OR_OK, nullptr, ErrorPrototype::Type);
}
ResultType ResultToken::TypeError(LPCTSTR aExpectedType, ExprTokenType &aActualValue)
{
return Fail(::TypeError(aExpectedType, aActualValue));
}
FResult FTypeError(LPCTSTR aExpectedType, ExprTokenType &aActualValue)
{
return TypeError(aExpectedType, aActualValue) == OK ? FR_ABORTED : FR_FAIL;
}
__declspec(noinline)
ResultType ParamError(int aIndex, ExprTokenType *aParam, LPCTSTR aExpectedType, LPCTSTR aFunction)
{
auto an = [](LPCTSTR thing) {
return _tcschr(_T("aeiou"), ctolower(*thing)) ? _T("n") : _T("");
};
TCHAR msg[512];
TCHAR number_buf[MAX_NUMBER_SIZE];
LPCTSTR actual_type, value_as_string;
#ifdef CONFIG_DEBUGGER
if (!aFunction)
aFunction = g_Debugger.WhatThrew();
#endif
if (!aParam || aParam->symbol == SYM_MISSING)
{
#ifdef CONFIG_DEBUGGER
sntprintf(msg, _countof(msg), _T("Parameter #%i of %s must not be omitted in this case.")
, aIndex + 1, aFunction);
#else
sntprintf(msg, _countof(msg), _T("Parameter #%i must not be omitted in this case.")
, aIndex + 1);
#endif
return g_script.RuntimeError(msg, nullptr, FAIL_OR_OK, nullptr, ErrorPrototype::Value);
}
TokenTypeAndValue(*aParam, actual_type, value_as_string, number_buf);
if (!*value_as_string && !aExpectedType)
value_as_string = actual_type;
#ifdef CONFIG_DEBUGGER
if (aExpectedType)
sntprintf(msg, _countof(msg), _T("Parameter #%i of %s requires a%s %s, but received a%s %s.")
, aIndex + 1, aFunction, an(aExpectedType), aExpectedType, an(actual_type), actual_type);
else
sntprintf(msg, _countof(msg), _T("Parameter #%i of %s is invalid."), aIndex + 1, g_Debugger.WhatThrew());
#else
if (aExpectedType)
sntprintf(msg, _countof(msg), _T("Parameter #%i requires a%s %s, but received a%s %s.")
, aIndex + 1, an(aExpectedType), aExpectedType, an(actual_type), actual_type);
else
sntprintf(msg, _countof(msg), _T("Parameter #%i invalid."), aIndex + 1);
#endif
return g_script.RuntimeError(msg, value_as_string, FAIL_OR_OK, nullptr
, aExpectedType ? ErrorPrototype::Type : ErrorPrototype::Value);