-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcodeInterpreter.cpp
3012 lines (2620 loc) · 92.1 KB
/
codeInterpreter.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
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
// Copyright (C) 2015 Faust Logic, Inc.
//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
#include "console/codeInterpreter.h"
#include "console/compiler.h"
#include "console/simBase.h"
#include "console/telnetDebugger.h"
#include "sim/netStringTable.h"
#include "console/ICallMethod.h"
#include "console/stringStack.h"
#include "util/messaging/message.h"
#include "core/strings/findMatch.h"
#include "core/strings/stringUnit.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "cinterface/cinterface.h"
//#define TORQUE_VALIDATE_STACK
using namespace Compiler;
enum EvalConstants
{
MaxStackSize = 1024,
FieldBufferSizeString = 2048,
FieldBufferSizeNumeric = 128,
MethodOnComponent = -2
};
namespace Con
{
// Current script file name and root, these are registered as
// console variables.
extern StringTableEntry gCurrentFile;
extern StringTableEntry gCurrentRoot;
extern S32 gObjectCopyFailures;
}
// Gets a component of an object's field value or a variable and returns it
// in val.
static void getFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField, char val[])
{
const char* prevVal = NULL;
// Grab value from object.
if (object && field)
prevVal = object->getDataField(field, array);
// Otherwise, grab from the string stack. The value coming in will always
// be a string because that is how multicomponent variables are handled.
else
prevVal = STR.getStringValue();
// Make sure we got a value.
if (prevVal && *prevVal)
{
static const StringTableEntry xyzw[] =
{
StringTable->insert("x"),
StringTable->insert("y"),
StringTable->insert("z"),
StringTable->insert("w")
};
static const StringTableEntry rgba[] =
{
StringTable->insert("r"),
StringTable->insert("g"),
StringTable->insert("b"),
StringTable->insert("a")
};
// Translate xyzw and rgba into the indexed component
// of the variable or field.
if (subField == xyzw[0] || subField == rgba[0])
dStrcpy(val, StringUnit::getUnit(prevVal, 0, " \t\n"), 128);
else if (subField == xyzw[1] || subField == rgba[1])
dStrcpy(val, StringUnit::getUnit(prevVal, 1, " \t\n"), 128);
else if (subField == xyzw[2] || subField == rgba[2])
dStrcpy(val, StringUnit::getUnit(prevVal, 2, " \t\n"), 128);
else if (subField == xyzw[3] || subField == rgba[3])
dStrcpy(val, StringUnit::getUnit(prevVal, 3, " \t\n"), 128);
else
val[0] = 0;
}
else
val[0] = 0;
}
// Sets a component of an object's field value based on the sub field. 'x' will
// set the first field, 'y' the second, and 'z' the third.
static void setFieldComponent(SimObject* object, StringTableEntry field, const char* array, StringTableEntry subField)
{
// Copy the current string value
char strValue[1024];
dStrncpy(strValue, STR.getStringValue(), 1024);
char val[1024] = "";
const char* prevVal = NULL;
// Set the value on an object field.
if (object && field)
prevVal = object->getDataField(field, array);
// Set the value on a variable.
else if (gEvalState.currentVariable)
prevVal = gEvalState.getStringVariable();
// Ensure that the variable has a value
if (!prevVal)
return;
static const StringTableEntry xyzw[] =
{
StringTable->insert("x"),
StringTable->insert("y"),
StringTable->insert("z"),
StringTable->insert("w")
};
static const StringTableEntry rgba[] =
{
StringTable->insert("r"),
StringTable->insert("g"),
StringTable->insert("b"),
StringTable->insert("a")
};
// Insert the value into the specified
// component of the string.
if (subField == xyzw[0] || subField == rgba[0])
dStrcpy(val, StringUnit::setUnit(prevVal, 0, strValue, " \t\n"), 128);
else if (subField == xyzw[1] || subField == rgba[1])
dStrcpy(val, StringUnit::setUnit(prevVal, 1, strValue, " \t\n"), 128);
else if (subField == xyzw[2] || subField == rgba[2])
dStrcpy(val, StringUnit::setUnit(prevVal, 2, strValue, " \t\n"), 128);
else if (subField == xyzw[3] || subField == rgba[3])
dStrcpy(val, StringUnit::setUnit(prevVal, 3, strValue, " \t\n"), 128);
if (val[0] != 0)
{
// Update the field or variable.
if (object && field)
object->setDataField(field, 0, val);
else if (gEvalState.currentVariable)
gEvalState.setStringVariable(val);
}
}
extern ExprEvalState gEvalState;
char sTraceBuffer[1024];
StringStack STR;
ConsoleValueStack CSTK;
U32 _FLT = 0; ///< Stack pointer for floatStack.
U32 _UINT = 0; ///< Stack pointer for intStack.
U32 _ITER = 0; ///< Stack pointer for iterStack.
IterStackRecord iterStack[MaxStackSize];
F64 floatStack[MaxStackSize];
S64 intStack[MaxStackSize];
char curFieldArray[256];
char prevFieldArray[256];
typedef OPCodeReturn(CodeInterpreter::*OpFn)(U32&);
static OpFn gOpCodeArray[MAX_OP_CODELEN];
CodeInterpreter::CodeInterpreter(CodeBlock *cb) :
mCodeBlock(cb),
mIterDepth(0),
mCurFloatTable(nullptr),
mCurStringTable(nullptr),
mThisFunctionName(nullptr),
mPopFrame(false),
mObjectCreationStackIndex(0),
mCurrentNewObject(nullptr),
mFailJump(0),
mPrevField(nullptr),
mCurField(nullptr),
mPrevObject(nullptr),
mCurObject(nullptr),
mSaveObject(nullptr),
mThisObject(nullptr),
mNSEntry(nullptr),
mCurFNDocBlock(nullptr),
mCurNSDocBlock(nullptr),
mCallArgc(0),
mCallArgv(nullptr),
mSaveCodeBlock(nullptr),
mCurrentInstruction(0)
{
}
CodeInterpreter::~CodeInterpreter()
{
}
void CodeInterpreter::init()
{
gOpCodeArray[OP_FUNC_DECL] = &CodeInterpreter::op_func_decl;
gOpCodeArray[OP_CREATE_OBJECT] = &CodeInterpreter::op_create_object;
gOpCodeArray[OP_ADD_OBJECT] = &CodeInterpreter::op_add_object;
gOpCodeArray[OP_END_OBJECT] = &CodeInterpreter::op_end_object;
gOpCodeArray[OP_FINISH_OBJECT] = &CodeInterpreter::op_finish_object;
gOpCodeArray[OP_JMPIFFNOT] = &CodeInterpreter::op_jmpiffnot;
gOpCodeArray[OP_JMPIFNOT] = &CodeInterpreter::op_jmpifnot;
gOpCodeArray[OP_JMPIFF] = &CodeInterpreter::op_jmpiff;
gOpCodeArray[OP_JMPIF] = &CodeInterpreter::op_jmpif;
gOpCodeArray[OP_JMPIFNOT_NP] = &CodeInterpreter::op_jmpifnot_np;
gOpCodeArray[OP_JMPIF_NP] = &CodeInterpreter::op_jmpif_np;
gOpCodeArray[OP_JMP] = &CodeInterpreter::op_jmp;
gOpCodeArray[OP_RETURN] = &CodeInterpreter::op_return;
gOpCodeArray[OP_RETURN_VOID] = &CodeInterpreter::op_return_void;
gOpCodeArray[OP_RETURN_FLT] = &CodeInterpreter::op_return_flt;
gOpCodeArray[OP_RETURN_UINT] = &CodeInterpreter::op_return_uint;
gOpCodeArray[OP_CMPEQ] = &CodeInterpreter::op_cmpeq;
gOpCodeArray[OP_CMPGR] = &CodeInterpreter::op_cmpgr;
gOpCodeArray[OP_CMPGE] = &CodeInterpreter::op_cmpge;
gOpCodeArray[OP_CMPLT] = &CodeInterpreter::op_cmplt;
gOpCodeArray[OP_CMPLE] = &CodeInterpreter::op_cmple;
gOpCodeArray[OP_CMPNE] = &CodeInterpreter::op_cmpne;
gOpCodeArray[OP_XOR] = &CodeInterpreter::op_xor;
gOpCodeArray[OP_MOD] = &CodeInterpreter::op_mod;
gOpCodeArray[OP_BITAND] = &CodeInterpreter::op_bitand;
gOpCodeArray[OP_BITOR] = &CodeInterpreter::op_bitor;
gOpCodeArray[OP_NOT] = &CodeInterpreter::op_not;
gOpCodeArray[OP_NOTF] = &CodeInterpreter::op_notf;
gOpCodeArray[OP_ONESCOMPLEMENT] = &CodeInterpreter::op_onescomplement;
gOpCodeArray[OP_SHR] = &CodeInterpreter::op_shr;
gOpCodeArray[OP_SHL] = &CodeInterpreter::op_shl;
gOpCodeArray[OP_AND] = &CodeInterpreter::op_and;
gOpCodeArray[OP_OR] = &CodeInterpreter::op_or;
gOpCodeArray[OP_ADD] = &CodeInterpreter::op_add;
gOpCodeArray[OP_SUB] = &CodeInterpreter::op_sub;
gOpCodeArray[OP_MUL] = &CodeInterpreter::op_mul;
gOpCodeArray[OP_DIV] = &CodeInterpreter::op_div;
gOpCodeArray[OP_NEG] = &CodeInterpreter::op_neg;
gOpCodeArray[OP_INC] = &CodeInterpreter::op_inc;
gOpCodeArray[OP_DEC] = &CodeInterpreter::op_dec;
gOpCodeArray[OP_SETCURVAR] = &CodeInterpreter::op_setcurvar;
gOpCodeArray[OP_SETCURVAR_CREATE] = &CodeInterpreter::op_setcurvar_create;
gOpCodeArray[OP_SETCURVAR_ARRAY] = &CodeInterpreter::op_setcurvar_array;
gOpCodeArray[OP_SETCURVAR_ARRAY_VARLOOKUP] = &CodeInterpreter::op_setcurvar_array_varlookup;
gOpCodeArray[OP_SETCURVAR_ARRAY_CREATE] = &CodeInterpreter::op_setcurvar_array_create;
gOpCodeArray[OP_SETCURVAR_ARRAY_CREATE_VARLOOKUP] = &CodeInterpreter::op_setcurvar_array_create_varlookup;
gOpCodeArray[OP_LOADVAR_UINT] = &CodeInterpreter::op_loadvar_uint;
gOpCodeArray[OP_LOADVAR_FLT] = &CodeInterpreter::op_loadvar_flt;
gOpCodeArray[OP_LOADVAR_STR] = &CodeInterpreter::op_loadvar_str;
gOpCodeArray[OP_LOADVAR_VAR] = &CodeInterpreter::op_loadvar_var;
gOpCodeArray[OP_SAVEVAR_UINT] = &CodeInterpreter::op_savevar_uint;
gOpCodeArray[OP_SAVEVAR_FLT] = &CodeInterpreter::op_savevar_flt;
gOpCodeArray[OP_SAVEVAR_STR] = &CodeInterpreter::op_savevar_str;
gOpCodeArray[OP_SAVEVAR_VAR] = &CodeInterpreter::op_savevar_var;
gOpCodeArray[OP_SETCUROBJECT] = &CodeInterpreter::op_setcurobject;
gOpCodeArray[OP_SETCUROBJECT_INTERNAL] = &CodeInterpreter::op_setcurobject_internal;
gOpCodeArray[OP_SETCUROBJECT_NEW] = &CodeInterpreter::op_setcurobject_new;
gOpCodeArray[OP_SETCURFIELD] = &CodeInterpreter::op_setcurfield;
gOpCodeArray[OP_SETCURFIELD_ARRAY] = &CodeInterpreter::op_setcurfield_array;
gOpCodeArray[OP_SETCURFIELD_TYPE] = &CodeInterpreter::op_setcurfield_type;
gOpCodeArray[OP_SETCURFIELD_ARRAY_VAR] = &CodeInterpreter::op_setcurfield_array_var;
gOpCodeArray[OP_SETCURFIELD_THIS] = &CodeInterpreter::op_setcurfield_this;
gOpCodeArray[OP_LOADFIELD_UINT] = &CodeInterpreter::op_loadfield_uint;
gOpCodeArray[OP_LOADFIELD_FLT] = &CodeInterpreter::op_loadfield_flt;
gOpCodeArray[OP_LOADFIELD_STR] = &CodeInterpreter::op_loadfield_str;
gOpCodeArray[OP_SAVEFIELD_UINT] = &CodeInterpreter::op_savefield_uint;
gOpCodeArray[OP_SAVEFIELD_FLT] = &CodeInterpreter::op_savefield_flt;
gOpCodeArray[OP_SAVEFIELD_STR] = &CodeInterpreter::op_savefield_str;
gOpCodeArray[OP_STR_TO_UINT] = &CodeInterpreter::op_str_to_uint;
gOpCodeArray[OP_STR_TO_FLT] = &CodeInterpreter::op_str_to_flt;
gOpCodeArray[OP_STR_TO_NONE] = &CodeInterpreter::op_str_to_none;
gOpCodeArray[OP_FLT_TO_UINT] = &CodeInterpreter::op_flt_to_uint;
gOpCodeArray[OP_FLT_TO_STR] = &CodeInterpreter::op_flt_to_str;
gOpCodeArray[OP_FLT_TO_NONE] = &CodeInterpreter::op_flt_to_none;
gOpCodeArray[OP_UINT_TO_FLT] = &CodeInterpreter::op_uint_to_flt;
gOpCodeArray[OP_UINT_TO_STR] = &CodeInterpreter::op_uint_to_str;
gOpCodeArray[OP_UINT_TO_NONE] = &CodeInterpreter::op_uint_to_none;
gOpCodeArray[OP_COPYVAR_TO_NONE] = &CodeInterpreter::op_copyvar_to_none;
gOpCodeArray[OP_LOADIMMED_UINT] = &CodeInterpreter::op_loadimmed_uint;
gOpCodeArray[OP_LOADIMMED_FLT] = &CodeInterpreter::op_loadimmed_flt;
gOpCodeArray[OP_TAG_TO_STR] = &CodeInterpreter::op_tag_to_str;
gOpCodeArray[OP_LOADIMMED_STR] = &CodeInterpreter::op_loadimmed_str;
gOpCodeArray[OP_DOCBLOCK_STR] = &CodeInterpreter::op_docblock_str;
gOpCodeArray[OP_LOADIMMED_IDENT] = &CodeInterpreter::op_loadimmed_ident;
gOpCodeArray[OP_CALLFUNC_RESOLVE] = &CodeInterpreter::op_callfunc_resolve;
gOpCodeArray[OP_CALLFUNC] = &CodeInterpreter::op_callfunc;
gOpCodeArray[OP_CALLFUNC_POINTER] = &CodeInterpreter::op_callfunc_pointer;
gOpCodeArray[OP_CALLFUNC_THIS] = &CodeInterpreter::op_callfunc_this;
gOpCodeArray[OP_ADVANCE_STR] = &CodeInterpreter::op_advance_str;
gOpCodeArray[OP_ADVANCE_STR_APPENDCHAR] = &CodeInterpreter::op_advance_str_appendchar;
gOpCodeArray[OP_ADVANCE_STR_COMMA] = &CodeInterpreter::op_advance_str_comma;
gOpCodeArray[OP_ADVANCE_STR_NUL] = &CodeInterpreter::op_advance_str_nul;
gOpCodeArray[OP_REWIND_STR] = &CodeInterpreter::op_rewind_str;
gOpCodeArray[OP_TERMINATE_REWIND_STR] = &CodeInterpreter::op_terminate_rewind_str;
gOpCodeArray[OP_COMPARE_STR] = &CodeInterpreter::op_compare_str;
gOpCodeArray[OP_PUSH] = &CodeInterpreter::op_push;
gOpCodeArray[OP_PUSH_UINT] = &CodeInterpreter::op_push_uint;
gOpCodeArray[OP_PUSH_FLT] = &CodeInterpreter::op_push_flt;
gOpCodeArray[OP_PUSH_VAR] = &CodeInterpreter::op_push_var;
gOpCodeArray[OP_PUSH_THIS] = &CodeInterpreter::op_push_this;
gOpCodeArray[OP_PUSH_FRAME] = &CodeInterpreter::op_push_frame;
gOpCodeArray[OP_ASSERT] = &CodeInterpreter::op_assert;
gOpCodeArray[OP_BREAK] = &CodeInterpreter::op_break;
gOpCodeArray[OP_ITER_BEGIN_STR] = &CodeInterpreter::op_iter_begin_str;
gOpCodeArray[OP_ITER_BEGIN] = &CodeInterpreter::op_iter_begin;
gOpCodeArray[OP_ITER] = &CodeInterpreter::op_iter;
gOpCodeArray[OP_ITER_END] = &CodeInterpreter::op_iter_end;
gOpCodeArray[OP_INVALID] = &CodeInterpreter::op_invalid;
}
ConsoleValueRef CodeInterpreter::exec(U32 ip,
StringTableEntry functionName,
Namespace *thisNamespace,
U32 argc,
ConsoleValueRef *argv,
bool noCalls,
StringTableEntry packageName,
S32 setFrame)
{
mExec.functionName = functionName;
mExec.thisNamespace = thisNamespace;
mExec.argc = argc;
mExec.argv = argv;
mExec.noCalls = noCalls;
mExec.packageName = packageName;
mExec.setFrame = setFrame;
mCodeBlock->incRefCount();
mPopFrame = false;
#ifdef TORQUE_VALIDATE_STACK
U32 stackStart = STR.mStartStackSize;
#endif
STR.clearFunctionOffset(); // ensures arg buffer offset is back to 0
// Lets load up our function arguments.
parseArgs(ip);
// Grab the state of the telenet debugger here once
// so that the push and pop frames are always balanced.
const bool telDebuggerOn = TelDebugger && TelDebugger->isConnected();
if (telDebuggerOn && setFrame < 0)
TelDebugger->pushStackFrame();
mSaveCodeBlock = CodeBlock::smCurrentCodeBlock;
CodeBlock::smCurrentCodeBlock = mCodeBlock;
if (mCodeBlock->name)
{
Con::gCurrentFile = mCodeBlock->name;
Con::gCurrentRoot = mCodeBlock->modPath;
}
U32 *code = mCodeBlock->code;
while (true)
{
mCurrentInstruction = code[ip++];
mNSEntry = nullptr;
#ifdef TORQUE_VALIDATE_STACK
// OP Code check.
AssertFatal(mCurrentInstruction < MAX_OP_CODELEN, "Invalid OP code in script interpreter");
#endif
breakContinueLabel:
OPCodeReturn ret = (this->*gOpCodeArray[mCurrentInstruction])(ip);
if (ret == OPCodeReturn::exitCode)
break;
else if (ret == OPCodeReturn::breakContinue)
goto breakContinueLabel;
}
if (telDebuggerOn && setFrame < 0)
TelDebugger->popStackFrame();
if (mPopFrame)
gEvalState.popFrame();
if (argv)
{
if (gEvalState.traceOn)
{
sTraceBuffer[0] = 0;
dStrcat(sTraceBuffer, "Leaving ", 1024);
if (packageName)
{
dStrcat(sTraceBuffer, "[", 1024);
dStrcat(sTraceBuffer, packageName, 1024);
dStrcat(sTraceBuffer, "]", 1024);
}
if (thisNamespace && thisNamespace->mName)
{
dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
"%s::%s() - return %s", thisNamespace->mName, mThisFunctionName, STR.getStringValue());
}
else
{
dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
"%s() - return %s", mThisFunctionName, STR.getStringValue());
}
Con::printf("%s", sTraceBuffer);
}
}
CodeBlock::smCurrentCodeBlock = mSaveCodeBlock;
if (mSaveCodeBlock && mSaveCodeBlock->name)
{
Con::gCurrentFile = mSaveCodeBlock->name;
Con::gCurrentRoot = mSaveCodeBlock->modPath;
}
mCodeBlock->decRefCount();
#ifdef TORQUE_VALIDATE_STACK
AssertFatal(!(STR.mStartStackSize > stackStart), "String stack not popped enough in script exec");
AssertFatal(!(STR.mStartStackSize < stackStart), "String stack popped too much in script exec");
#endif
return mReturnValue;
}
void CodeInterpreter::parseArgs(U32 &ip)
{
U32 *code = mCodeBlock->code;
if (mExec.argv)
{
U32 fnArgc = code[ip + 2 + 6];
mThisFunctionName = Compiler::CodeToSTE(code, ip);
S32 wantedArgc = getMin(mExec.argc - 1, fnArgc); // argv[0] is func name
if (gEvalState.traceOn)
{
sTraceBuffer[0] = 0;
dStrcat(sTraceBuffer, "Entering ", 1024);
if (mExec.packageName)
{
dStrcat(sTraceBuffer, "[", 1024);
dStrcat(sTraceBuffer, mExec.packageName, 1024);
dStrcat(sTraceBuffer, "]", 1024);
}
if (mExec.thisNamespace && mExec.thisNamespace->mName)
{
dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
"%s::%s(", mExec.thisNamespace->mName, mThisFunctionName);
}
else
{
dSprintf(sTraceBuffer + dStrlen(sTraceBuffer), (U32)(sizeof(sTraceBuffer) - dStrlen(sTraceBuffer)),
"%s(", mThisFunctionName);
}
for (S32 i = 0; i < wantedArgc; i++)
{
dStrcat(sTraceBuffer, mExec.argv[i + 1], 1024);
if (i != wantedArgc - 1)
dStrcat(sTraceBuffer, ", ", 1024);
}
dStrcat(sTraceBuffer, ")", 1024);
Con::printf("%s", sTraceBuffer);
}
gEvalState.pushFrame(mThisFunctionName, mExec.thisNamespace);
mPopFrame = true;
StringTableEntry thisPointer = StringTable->insert("%this");
for (S32 i = 0; i < wantedArgc; i++)
{
StringTableEntry var = Compiler::CodeToSTE(code, ip + (2 + 6 + 1) + (i * 2));
gEvalState.setCurVarNameCreate(var);
ConsoleValueRef ref = mExec.argv[i + 1];
switch (ref.getType())
{
case ConsoleValue::TypeInternalInt:
gEvalState.setIntVariable(ref);
break;
case ConsoleValue::TypeInternalFloat:
gEvalState.setFloatVariable(ref);
break;
case ConsoleValue::TypeInternalStringStackPtr:
gEvalState.setStringStackPtrVariable(ref.getStringStackPtrValue());
break;
case ConsoleValue::TypeInternalStackString:
case ConsoleValue::TypeInternalString:
default:
gEvalState.setStringVariable(ref);
break;
}
if (var == thisPointer)
{
// %this gets optimized as it is flagged as a constant.
// Since it is guarenteed to be constant, we can then perform optimizations.
gEvalState.currentVariable->mIsConstant = true;
// Store a reference to the this pointer object.
mThisObject = Sim::findObject(gEvalState.getStringVariable());
}
}
ip = ip + (fnArgc * 2) + (2 + 6 + 1);
mCurFloatTable = mCodeBlock->functionFloats;
mCurStringTable = mCodeBlock->functionStrings;
}
else
{
mCurFloatTable = mCodeBlock->globalFloats;
mCurStringTable = mCodeBlock->globalStrings;
// If requested stack frame isn't available, request a new one
// (this prevents assert failures when creating local
// variables without a stack frame)
if (gEvalState.getStackDepth() <= mExec.setFrame)
mExec.setFrame = -1;
// Do we want this code to execute using a new stack frame?
if (mExec.setFrame < 0)
{
gEvalState.pushFrame(NULL, NULL);
mPopFrame = true;
}
else
{
// We want to copy a reference to an existing stack frame
// on to the top of the stack. Any change that occurs to
// the locals during this new frame will also occur in the
// original frame.
S32 stackIndex = gEvalState.getStackDepth() - mExec.setFrame - 1;
gEvalState.pushFrameRef(stackIndex);
mPopFrame = true;
}
}
}
OPCodeReturn CodeInterpreter::op_func_decl(U32 &ip)
{
U32 *code = mCodeBlock->code;
if (!mExec.noCalls)
{
StringTableEntry fnName = CodeToSTE(code, ip);
StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
StringTableEntry fnPackage = CodeToSTE(code, ip + 4);
bool hasBody = (code[ip + 6] & 0x01) != 0;
U32 lineNumber = code[ip + 6] >> 1;
Namespace::unlinkPackages();
Namespace *ns = Namespace::find(fnNamespace, fnPackage);
ns->addFunction(fnName, mCodeBlock, hasBody ? ip : 0, mCurFNDocBlock ? dStrdup(mCurFNDocBlock) : NULL, lineNumber);// if no body, set the IP to 0
if (mCurNSDocBlock)
{
// If we have a docblock before we declare the function in the script file,
// this will attempt to set the doc block to the function.
// See OP_DOCBLOCK_STR
if (fnNamespace == StringTable->lookup(mNSDocBlockClass))
{
char *usageStr = dStrdup(mCurNSDocBlock);
usageStr[dStrlen(usageStr)] = '\0';
ns->mUsage = usageStr;
ns->mCleanUpUsage = true;
mCurNSDocBlock = NULL;
}
}
Namespace::relinkPackages();
// If we had a docblock, it's definitely not valid anymore, so clear it out.
mCurFNDocBlock = NULL;
//Con::printf("Adding function %s::%s (%d)", fnNamespace, fnName, ip);
}
ip = code[ip + 7];
return OPCodeReturn::success;
}
OPCodeReturn CodeInterpreter::op_create_object(U32 &ip)
{
U32 *code = mCodeBlock->code;
// Read some useful info.
StringTableEntry objParent = CodeToSTE(code, ip);
bool isDataBlock = code[ip + 2];
bool isInternal = code[ip + 3];
bool isSingleton = code[ip + 4];
U32 lineNumber = code[ip + 5];
mFailJump = code[ip + 6];
// If we don't allow calls, we certainly don't allow creating objects!
// Moved this to after failJump is set. Engine was crashing when
// noCalls = true and an object was being created at the beginning of
// a file. ADL.
if (mExec.noCalls)
{
ip = mFailJump;
return OPCodeReturn::success;
}
// Push the old info to the stack
//Assert( objectCreationStackIndex < objectCreationStackSize );
mObjectCreationStack[mObjectCreationStackIndex].newObject = mCurrentNewObject;
mObjectCreationStack[mObjectCreationStackIndex++].failJump = mFailJump;
// Get the constructor information off the stack.
CSTK.getArgcArgv(NULL, &mCallArgc, &mCallArgv);
const char *objectName = mCallArgv[2];
// Con::printf("Creating object...");
// objectName = argv[1]...
mCurrentNewObject = NULL;
// Are we creating a datablock? If so, deal with case where we override
// an old one.
if (isDataBlock)
{
// Con::printf(" - is a datablock");
// Find the old one if any.
SimObject *db = Sim::getDataBlockGroup()->findObject(objectName);
// Make sure we're not changing types on ourselves...
if (db && dStricmp(db->getClassName(), mCallArgv[1]))
{
Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare data block %s with a different class.", mCodeBlock->getFileLine(ip), objectName);
ip = mFailJump;
STR.popFrame();
CSTK.popFrame();
return OPCodeReturn::success;
}
// If there was one, set the currentNewObject and move on.
if (db)
mCurrentNewObject = db;
}
else if (!isInternal)
{
// IF we aren't looking at a local/internal object, then check if
// this object already exists in the global space
AbstractClassRep* rep = AbstractClassRep::findClassRep(objectName);
if (rep != NULL) {
Con::errorf(ConsoleLogEntry::General, "%s: Cannot name object [%s] the same name as a script class.",
mCodeBlock->getFileLine(ip), objectName);
ip = mFailJump;
STR.popFrame();
CSTK.popFrame();
return OPCodeReturn::success;
}
SimObject *obj = Sim::findObject((const char*)objectName);
if (obj /*&& !obj->isLocalName()*/)
{
if (isSingleton)
{
// Make sure we're not trying to change types
if (dStricmp(obj->getClassName(), (const char*)mCallArgv[1]) != 0)
{
Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s] with a different class [%s] - was [%s].",
mCodeBlock->getFileLine(ip), objectName, (const char*)mCallArgv[1], obj->getClassName());
ip = mFailJump;
STR.popFrame();
CSTK.popFrame();
return OPCodeReturn::success;
}
// We're creating a singleton, so use the found object
// instead of creating a new object.
mCurrentNewObject = obj;
}
else
{
const char* redefineBehavior = Con::getVariable("$Con::redefineBehavior");
if (dStricmp(redefineBehavior, "replaceExisting") == 0)
{
// Save our constructor args as the argv vector is stored on the
// string stack and may get stomped if deleteObject triggers
// script execution.
ConsoleValueRef savedArgv[StringStack::MaxArgs];
for (int i = 0; i< mCallArgc; i++) {
savedArgv[i] = mCallArgv[i];
}
//dMemcpy( savedArgv, callArgv, sizeof( savedArgv[ 0 ] ) * callArgc );
// Prevent stack value corruption
CSTK.pushFrame();
STR.pushFrame();
// --
obj->deleteObject();
obj = NULL;
// Prevent stack value corruption
CSTK.popFrame();
STR.popFrame();
// --
//dMemcpy( callArgv, savedArgv, sizeof( callArgv[ 0 ] ) * callArgc );
for (int i = 0; i<mCallArgc; i++) {
mCallArgv[i] = savedArgv[i];
}
}
else if (dStricmp(redefineBehavior, "renameNew") == 0)
{
for (U32 i = 1;; ++i)
{
String newName = String::ToString("%s%i", objectName, i);
if (!Sim::findObject(newName))
{
objectName = StringTable->insert(newName);
break;
}
}
}
else if (dStricmp(redefineBehavior, "unnameNew") == 0)
{
objectName = StringTable->insert("");
}
else if (dStricmp(redefineBehavior, "postfixNew") == 0)
{
const char* postfix = Con::getVariable("$Con::redefineBehaviorPostfix");
String newName = String::ToString("%s%s", objectName, postfix);
if (Sim::findObject(newName))
{
Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object with postfix [%s].",
mCodeBlock->getFileLine(ip), newName.c_str());
ip = mFailJump;
STR.popFrame();
CSTK.popFrame();
return OPCodeReturn::success;
}
else
objectName = StringTable->insert(newName);
}
else
{
Con::errorf(ConsoleLogEntry::General, "%s: Cannot re-declare object [%s].",
mCodeBlock->getFileLine(ip), objectName);
ip = mFailJump;
STR.popFrame();
CSTK.popFrame();
return OPCodeReturn::success;
}
}
}
}
STR.popFrame();
CSTK.popFrame();
if (!mCurrentNewObject)
{
// Well, looks like we have to create a new object.
ConsoleObject *object = ConsoleObject::create((const char*)mCallArgv[1]);
// Deal with failure!
if (!object)
{
Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-conobject class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
ip = mFailJump;
return OPCodeReturn::success;
}
// Do special datablock init if appropros
if (isDataBlock)
{
SimDataBlock *dataBlock = dynamic_cast<SimDataBlock *>(object);
if (dataBlock)
{
dataBlock->assignId();
}
else
{
// They tried to make a non-datablock with a datablock keyword!
Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-datablock class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
// Clean up...
delete object;
mCurrentNewObject = NULL;
ip = mFailJump;
return OPCodeReturn::success;
}
}
// Finally, set currentNewObject to point to the new one.
mCurrentNewObject = dynamic_cast<SimObject *>(object);
// Deal with the case of a non-SimObject.
if (!mCurrentNewObject)
{
Con::errorf(ConsoleLogEntry::General, "%s: Unable to instantiate non-SimObject class %s.", mCodeBlock->getFileLine(ip), (const char*)mCallArgv[1]);
delete object;
mCurrentNewObject = NULL;
ip = mFailJump;
return OPCodeReturn::success;
}
// Set the declaration line
mCurrentNewObject->setDeclarationLine(lineNumber);
// Set the file that this object was created in
mCurrentNewObject->setFilename(mCodeBlock->name);
// Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
if (*objParent)
{
// Find it!
SimObject *parent;
if (Sim::findObject(objParent, parent))
{
// Con::printf(" - Parent object found: %s", parent->getClassName());
mCurrentNewObject->setCopySource(parent);
mCurrentNewObject->assignFieldsFrom(parent);
// copy any substitution statements
SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
if (parent_db)
{
SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(mCurrentNewObject);
if (currentNewObject_db)
currentNewObject_db->copySubstitutionsFrom(parent_db);
}
}
else
{
if (Con::gObjectCopyFailures == -1)
Con::errorf(ConsoleLogEntry::General, "%s: Unable to find parent object %s for %s.", mCodeBlock->getFileLine(ip), objParent, (const char*)mCallArgv[1]);
else
++Con::gObjectCopyFailures;
// Fail to create the object.
delete object;
mCurrentNewObject = NULL;
ip = mFailJump;
return OPCodeReturn::success;
}
}
// If a name was passed, assign it.
if (objectName[0])
{
if (!isInternal)
mCurrentNewObject->assignName(objectName);
else
mCurrentNewObject->setInternalName(objectName);
// Set the original name
mCurrentNewObject->setOriginalName(objectName);
}
// Prevent stack value corruption
CSTK.pushFrame();
STR.pushFrame();
// --
// Do the constructor parameters.
if (!mCurrentNewObject->processArguments(mCallArgc - 3, mCallArgv + 3))
{
delete mCurrentNewObject;
mCurrentNewObject = NULL;
ip = mFailJump;
// Prevent stack value corruption
CSTK.popFrame();
STR.popFrame();
// --
return OPCodeReturn::success;
}
// Prevent stack value corruption
CSTK.popFrame();
STR.popFrame();
// --
// If it's not a datablock, allow people to modify bits of it.
if (!isDataBlock)
{
mCurrentNewObject->setModStaticFields(true);
mCurrentNewObject->setModDynamicFields(true);
}
}
else
{
mCurrentNewObject->reloadReset(); // AFX (reload-reset)
// Does it have a parent object? (ie, the copy constructor : syntax, not inheriance)
if (*objParent)
{
// Find it!
SimObject *parent;
if (Sim::findObject(objParent, parent))
{
// Con::printf(" - Parent object found: %s", parent->getClassName());
// temporarily block name change
SimObject::preventNameChanging = true;
mCurrentNewObject->setCopySource(parent);
mCurrentNewObject->assignFieldsFrom(parent);
// restore name changing
SimObject::preventNameChanging = false;
// copy any substitution statements
SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
if (parent_db)
{
SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(mCurrentNewObject);
if (currentNewObject_db)
currentNewObject_db->copySubstitutionsFrom(parent_db);
}
}
else
Con::errorf(ConsoleLogEntry::General, "%d: Unable to find parent object %s for %s.", lineNumber, objParent, (const char*)mCallArgv[1]);
}
}
// Advance the IP past the create info...
ip += 7;
return OPCodeReturn::success;
}
OPCodeReturn CodeInterpreter::op_add_object(U32 &ip)
{
// See OP_SETCURVAR for why we do this.
mCurFNDocBlock = NULL;
mCurNSDocBlock = NULL;
// Do we place this object at the root?
bool placeAtRoot = mCodeBlock->code[ip++];
// Con::printf("Adding object %s", currentNewObject->getName());
// Prevent stack value corruption
CSTK.pushFrame();
STR.pushFrame();
// --
// Make sure it wasn't already added, then add it.
if (mCurrentNewObject->isProperlyAdded() == false)
{
bool ret = false;
Message *msg = dynamic_cast<Message *>(mCurrentNewObject);
if (msg)
{
SimObjectId id = Message::getNextMessageID();
if (id != 0xffffffff)
ret = mCurrentNewObject->registerObject(id);
else
Con::errorf("%s: No more object IDs available for messages", mCodeBlock->getFileLine(ip));
}
else
ret = mCurrentNewObject->registerObject();
if (!ret)
{
// This error is usually caused by failing to call Parent::initPersistFields in the class' initPersistFields().
Con::warnf(ConsoleLogEntry::General, "%s: Register object failed for object %s of class %s.", mCodeBlock->getFileLine(ip), mCurrentNewObject->getName(), mCurrentNewObject->getClassName());
delete mCurrentNewObject;
mCurrentNewObject = NULL;
ip = mFailJump;
// Prevent stack value corruption