-
Notifications
You must be signed in to change notification settings - Fork 4
/
execute.c
2128 lines (1828 loc) · 74.3 KB
/
execute.c
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
/*
FILE: execute.c
HEADER: execute.h
--GNU LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
TO_HEADER:
#include "conftree.h"
#include "hookers.h"
#include "thread.h"
// This structure stores the information on loaded modules and also on modules
// that were only tried to be loaded, but were not loaded.
typedef struct _Module {
char *pszModuleName; // the name of the module as supplied by the caller, either
// simple name or full path to the dll
void *ModulePointer; // pointer returned by the system function LoadModule/dlopen
void *ModuleInternalParameters; // the modules own pointer initialized to zero, and the module
// alters it as it likes
int ModuleIsActive; // true if the module is active. It can not be unloaded if it is active.
int ModuleIsStatic; // true if the module is statically linked to the interpreter.
struct _Module *next; // the next module in the list of loaded modules
}Module, *pModule;
// note that the ExecuteObject has a pointer to the SupportTable, but the SupportTable
// also has a pointer back to the ExecuteObject
typedef struct _SupportTable *pSupportTable;
#define PSUPPORTTABLE 1
typedef struct _ExecuteObject {
void *(*memory_allocating_function)(size_t);
void (*memory_releasing_function)(void *);
void *pMemorySegment; //this pointer is passed to the memory allocating functions
pReportFunction report;
void *reportptr; // this pointer is passed to the report function. The caller should set it.
unsigned long fErrorFlags;
ptConfigTree pConfig; // configuration data
char *StringTable; // all the string constants of the program zero terminated each
unsigned long cbStringTable; // all the bytes of StringTable including the zeroes
pcNODE CommandArray;
unsigned long StartNode;
unsigned long CommandArraySize;
long cGlobalVariables;
pFixSizeMemoryObject GlobalVariables;
long cLocalVariables;
pFixSizeMemoryObject LocalVariables; // this variable is always stored and restored when a locality is entered
unsigned long ProgramCounter;
unsigned long NextProgramCounter;
int fNextPC; // command sets it to TRUE when the NextProgramCounter was set (like in a jump)
#define fStopRETURN 1
#define fStopSTOP 2
int fStop;
unsigned long lStepCounter; // counts the program steps within the function
unsigned long lGlobalStepCounter; // counts the program steps in the total program
long lFunctionLevel; // the level in function call deepness
// maximal values or zero if no limit exists to help avoid infinite loop s
long GlobalStepLimit; // the max number of steps allowed for the programs
long LocalStepLimit; // the max number of steps inside a function
long FunctionLevelLimit; // the maximal function call deepness
int fWeAreCallingFuction; // This is true, when we are calling a function
unsigned long ErrorCode;
int fErrorGoto; // what type of value is in the ErrorGoto variable
#define ONERROR_NOTHING 0
#define ONERROR_GOTO 1
#define ONERROR_RESUME 2
#define ONERROR_RESUMENEXT 3
unsigned long ErrorGoto; // where to go when an error occures
unsigned long ErrorResume; // where did the error occures, where to resume
unsigned long LastError; // the code of the the last error that happened
unsigned long OperatorNode; // the node number of the current operator
pFixSizeMemoryObject pOpResult; // result of the operator function
pFixSizeMemoryObject pFunctionResult; // result of the current function
pMortalList pGlobalMortalList; // the actually used mortal list
unsigned long FunctionArgumentsNode; // the node of the expression list forming the argument list
pMemoryObject pMo;
CommandFunctionType *pCommandFunction;
void *CommandParameter[NUM_CMD]; // a NULL initialized pointer for each type of command or function
void (*(Finaliser[NUM_CMD]))(struct _ExecuteObject*);
// a NULL initialized pointer for each type of command or function
// if a function pointer is put into this variable it is called upon
// finishing the execution of the program
void **InstructionParameter; // a NULL initialized pointer for each cNODE
VersionInfo Ver;
void *fpStdinFunction; // pointer to standard input function when embedded
void *fpStdouFunction; // pointer to standard output function when embedded
void *fpEnvirFunction; // pointer to the environment variable retrieval function
char *CmdLineArgument; // pointer to the command line argument
SymbolTable OptionsTable; // the options that the program can set using the statement option
void *pEmbedder; // this can be used by the embedding program
pSupportTable pST; // support table for the external functions
pSupportTable pSTI;// support table inherited from a process global program object needed
// only for multi-thread supporting modules
MUTEX mxModules; // to lock the modules list if this is a process SB object otherwise not used
pModule modules; // list of the the loaded modules
struct _ExecuteObject *pEPo;//the process objects execute structure in case this interpreter runs in MT env
char *pszModuleError; // the error message returned by a module call
pHookFunctions pHookers; // structure containing the hooker function pointers
// char *Argv0; // the name of the script executed
LexNASymbol *pCSYMBOLS; // to help locate the command code based on command id string
int fThreadedCommandTable; // true if the command table points to a copy (owned by the thread)
char **CSymbolList;
//unsigned long maxderef;
}ExecuteObject
#ifndef PEXECUTEOBJECT
, *pExecuteObject
#endif
;
#define GETDOUBLEVALUE(x) execute_GetDoubleValue(pEo,(x))
#define GETLONGVALUE(x) execute_GetLongValue(pEo,(x))
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include "basext.h"
#include "sym.h"
#include "errcodes.h"
#include "report.h"
#include "lexer.h"
#include "expression.h"
#include "builder.h"
#include "memory.h"
#include "syntax.h"
#include "execute.h"
#include "myalloc.h"
#include "modumana.h"
#define REPORT(x) if( pEo->report )pEo->report(pEo->reportptr,"",0,x,REPORT_ERROR,NULL,NULL,&(pEo->fErrorFlags))
#if BCC32
#define pow10 _mypow10
#endif
static double pow10(double a)
{
int j,i;
double pro,k;
for( (i= a<0.0) && (a = -a) , j=(int)a , pro=1.0 , k=10; j ;
j%2 && (pro *=k) , j /= 2 , k *= k )
continue;
i && (pro=1.0/pro);
return pro;
}
/*POD
This module contain the functions that execute the code resuled by the builder.
CUT*/
/*POD
=H execute_GetCommandByName()
The op-code of a command can easily be identified, because T<syntax.h> contains
symbolic constant for it. This function can be used by external modules to
get this opcode based on the name of the function. The argument T<pszCommandName>
should be the name of the command, for example T<"ONERRORRESUMENEXT">. The third
argument is the hint for the function to help to find the value. It should always
be the opcode of the command. The return value is the actual opcode of the command.
For example:
=verbatim
i = execute_GetCommandByName(pEo,"ONERRORRESUMENEXT",CMD_ONERRORRESUMENEXT);
=noverbatim
will return T<CMD_ONERRORRESUMENEXT>.
I<Why is this function all about then?>
The reason is that the external module may not be sure that the code
T<CMD_ONERRORRESUMENEXT> is the same when the external module is compiled
and when it is loaded. External modules negotiate the interface version
information with the calling interpreter, but the opcodes may silently changed
from interpreter version to the next interpreter version and still supporting
the same extension interface version.
When an external module needs to know the opcode of a command of the calling
interpreter it first calls this function telling:
I<I need the code of the command ONERRORRESUMENEXT. I think that the code is
CMD_ONERRORRESUMENEXT, but is it the real code?>
The argument T<lCodeHint> is required only, because it speeds up search.
If there is no function found for the given name the returnvalue is zero.
/*FUNCTION*/
long execute_GetCommandByName(pExecuteObject pEo,
char *pszCommandName,
long lCodeHint
){
/*noverbatim
CUT*/
long DownCounter,UpCounter;
if( lCodeHint < START_CMD )lCodeHint = START_CMD;
if( lCodeHint >= END_EXEC )lCodeHint = END_EXEC-1;
DownCounter = UpCounter = lCodeHint;
while( DownCounter || UpCounter ){
if( DownCounter && !strcmp(pszCommandName,pEo->CSymbolList[DownCounter-START_CMD]) )return DownCounter;
if( UpCounter && !strcmp(pszCommandName,pEo->CSymbolList[UpCounter-START_CMD]) )return UpCounter;
UpCounter ++;
if( UpCounter == END_EXEC )UpCounter = 0;
DownCounter --;
if( DownCounter < START_CMD )DownCounter = 0;
}
return 0;
}
/*POD
=H execute_CopyCommandTable()
The command table is a huge table containing pointers to functions. For example
the T<CMD_LET>-th element of the table points to the function T<COMMAND_LET>
implementing the assignment command.
This table is usually treated as constant and is not moduified during run time.
In case a module wants to reimplement a command it should alter this table.
However the table is shared all concurrently running interpreter threads in
a multi-thread variation of ScriptBasic.
To avoid altering the command table of an independent interpreter threadthe module
wanting altering the command table should call this function. This function allocates
memory for a new copy of the command table and copies the original constant
value to this new place. After the copy is done the T<ExecuteObject> will point to
the copied command table and the extension is free to alter the table.
In case the function is called more than once for the same interpreter thread
only the first time is effective. Later the function returns without creating superfluous
copies of the command table.
/*FUNCTION*/
int execute_CopyCommandTable(pExecuteObject pEo
){
/*noverbatim
CUT*/
CommandFunctionType *p;
/* it is already copied to the thread local place */
if( pEo->fThreadedCommandTable )return COMMAND_ERROR_SUCCESS;
p = ALLOC( sizeof( CommandFunctionType ) * (END_EXEC-START_CMD) );
if( p == NULL )return COMMAND_ERROR_MEMORY_LOW;
memcpy(p, pEo->pCommandFunction, sizeof( CommandFunctionType ) * (END_EXEC-START_CMD) );
pEo->pCommandFunction = p;
pEo->fThreadedCommandTable = 1;
return COMMAND_ERROR_SUCCESS;
}
/*POD
=H execute_InitStructure()
/*FUNCTION*/
int execute_InitStructure(pExecuteObject pEo,
pBuildObject pBo
){
/*noverbatim
CUT*/
long maxmem;
int iError;
build_MagicCode(&(pEo->Ver));
pEo->fpStdinFunction = NULL;
pEo->fpStdouFunction = NULL;
pEo->fpEnvirFunction = NULL;
pEo->CmdLineArgument = NULL;
if( cft_GetEx(pEo->pConfig,"maxstep",NULL,NULL,
&(pEo->GlobalStepLimit),NULL,NULL) )
pEo->GlobalStepLimit = 0;
if( cft_GetEx(pEo->pConfig,"maxlocalstep",NULL,NULL,
&(pEo->LocalStepLimit),NULL,NULL) )
pEo->LocalStepLimit = 0;
if( cft_GetEx(pEo->pConfig,"maxlevel",NULL,NULL,
&(pEo->FunctionLevelLimit),NULL,NULL) )
pEo->FunctionLevelLimit = 0;
pEo->CommandArray = pBo->CommandArray ;
pEo->StartNode = pBo->StartNode;
pEo->CommandArraySize = pBo->NodeCounter;
pEo->StringTable = pBo->StringTable;
pEo->cbStringTable = pBo->cbStringTable;
pEo->cGlobalVariables = pBo->cGlobalVariables;
pEo->lGlobalStepCounter = 0L;
pEo->LastError = 0; /* there was no run time error so
far and this led formerly the
BASIC 'ERROR()' function random
on the start */
pEo->pCommandFunction = CommandFunction;
pEo->CSymbolList = COMMANDSYMBOLS;
pEo->fThreadedCommandTable = 0;
pEo->pFunctionResult = NULL;
pEo->pGlobalMortalList = NULL;
pEo->pST = NULL;
pEo->pSTI = NULL;
pEo->pEPo = NULL;
pEo->modules = NULL;
pEo->pMemorySegment = alloc_InitSegment(pEo->memory_allocating_function,pEo->memory_releasing_function);
if( pEo->pMemorySegment == NULL )return COMMAND_ERROR_MEMORY_LOW;
pEo->pMo = alloc_Alloc(sizeof(MemoryObject),pEo->pMemorySegment);
if( pEo->pMo == NULL )return EXE_ERROR_MEMORY_LOW;
if( cft_GetEx(pEo->pConfig,"maxderef",NULL,NULL,
&(pEo->pMo->maxderef),NULL,NULL) )
pEo->pMo->maxderef = 1000;
pEo->pMo->memory_allocating_function = pEo->memory_allocating_function;
pEo->pMo->memory_releasing_function = pEo->memory_releasing_function;
pEo->cbStringTable = 0L;
pEo->OptionsTable = NULL;
if( iError = memory_InitStructure(pEo->pMo) )return iError;
if( cft_GetEx(pEo->pConfig,"maxmem",NULL,NULL,
&maxmem,NULL,NULL) == CFT_ERROR_SUCCESS )
alloc_SegmentLimit(pEo->pMo->pMemorySegment,maxmem);
memory_RegisterTypes(pEo->pMo);
if( hook_Init(pEo, &(pEo->pHookers)) )return 1;
if( modu_Preload(pEo) )return 1;
pEo->GlobalVariables = NULL;
pEo->InstructionParameter = NULL;
return EXE_ERROR_SUCCESS;
}
/*POD
=H execute_ReInitStructure()
This function should be used if a code is executed repeatedly. The first
initialization call is R<execute_InitStructure()> and consecutive executions
should call this function.
/*FUNCTION*/
int execute_ReInitStructure(pExecuteObject pEo,
pBuildObject pBo
){
/*noverbatim
CUT*/
pEo->lGlobalStepCounter = 0L;
pEo->pFunctionResult = NULL;
pEo->pST = NULL;
pEo->pSTI = NULL;
pEo->pEPo = NULL;
pEo->modules = NULL;
if( modu_Preload(pEo) )return 1;
return 0;
}
/*POD
=H execute_Execute_r()
This function executes a program fragment. The execution starts from the class variable
T<ProgramCounter>. This function is called from the R<execute_Execute()> function which is the
main entry point to the basic main program. This function is also called recursively from
the function R<execute_Evaluate()> when a user defined function is to be executed.
/*FUNCTION*/
void execute_Execute_r(pExecuteObject pEo,
int *piErrorCode
){
/*noverbatim
CUT*/
CommandFunctionType ThisCommandFunction;
unsigned long CommandOpCode;
unsigned long pc,npc;
pEo->fStop = 0;
pEo->lStepCounter = 0L;
pEo->fErrorGoto = ONERROR_NOTHING; /* this is the default behaviour */
while( pEo->ProgramCounter ){
pEo->fNextPC = 0;
if( pEo->ProgramCounter > pEo->CommandArraySize ){
*piErrorCode = EXE_ERROR_INVALID_PC;
return;
}
if( pEo->CommandArray[pEo->ProgramCounter-1].OpCode != eNTYPE_LST ){
*piErrorCode = EXE_ERROR_INVALID_NODE;
return;
}
pc = pEo->CommandArray[pEo->ProgramCounter-1].Parameter.NodeList.actualm;
if( pc > pEo->CommandArraySize ){
*piErrorCode = EXE_ERROR_INVALID_PC1;
return;
}
if( pc ){
CommandOpCode = pEo->CommandArray[pc-1].OpCode;
if( CommandOpCode < START_CMD || CommandOpCode > END_CMD ){
*piErrorCode = EXE_ERROR_INVALID_OPCODE;
return;
}
}
npc = pEo->CommandArray[pEo->ProgramCounter-1].Parameter.NodeList.rest ;
if( pc ){
ThisCommandFunction = pEo->pCommandFunction[CommandOpCode - START_CMD];
if( ThisCommandFunction == NULL ){
*piErrorCode = EXE_ERROR_NOT_IMPLEMENTED;
return;
}
}
pEo->ErrorCode = EXE_ERROR_SUCCESS;
if( pEo->pHookers->HOOK_ExecBefore && (*piErrorCode = pEo->pHookers->HOOK_ExecBefore(pEo)) )return;
if( pc )
ThisCommandFunction(pEo);
if( pEo->pHookers->HOOK_ExecAfter && (*piErrorCode = pEo->pHookers->HOOK_ExecAfter(pEo)) )return;
pEo->fWeAreCallingFuction = 0;
pEo->lStepCounter++;
if( pEo->LocalStepLimit && pEo->lStepCounter > (unsigned)pEo->LocalStepLimit ){
*piErrorCode = EXE_ERROR_TOO_LONG_RUN;
return;
}
pEo->lGlobalStepCounter++;
if( pEo->GlobalStepLimit && pEo->lGlobalStepCounter > (unsigned)pEo->GlobalStepLimit ){
*piErrorCode = EXE_ERROR_TOO_LONG_RUN;
return;
}
/* If the error code was set by the instruction then handle the on error goto statements */
if( pEo->ErrorCode ){
pEo->LastError = pEo->ErrorCode;
if( pEo->fErrorGoto == ONERROR_RESUMENEXT ){
pEo->fErrorGoto = ONERROR_NOTHING;
pEo->ErrorResume = 0; /* we resume the exection, we won't go back to anywhere */
pEo->LastError = 0; /* it is resumed, there is no error */
pEo->ErrorGoto = 0; /* no error goto node */
}else
if( pEo->fErrorGoto == ONERROR_RESUME ){
if( ! pEo->ErrorGoto ){
*piErrorCode = pEo->ErrorCode;
return;
}
pEo->ErrorResume = 0; /* we resume the exection, we won't go back to anywhere */
pEo->LastError = 0; /* it is resumed, there is no error */
pEo->fErrorGoto = ONERROR_NOTHING; /* on error is switched off by default */
pEo->ErrorGoto = 0; /* no error goto node */
pEo->fNextPC = 1;
pEo->NextProgramCounter = pEo->ErrorGoto;
}else
if( pEo->fErrorGoto == ONERROR_GOTO ){
if( ! pEo->ErrorGoto ){
*piErrorCode = pEo->ErrorCode;
return;
}
pEo->ErrorResume = pEo->ProgramCounter;
pEo->fNextPC = 1;
pEo->NextProgramCounter = pEo->ErrorGoto;
pEo->ErrorGoto = 0;
}else{
/* fErrorGoto should be ONERROR_NOTHING if we got here */
*piErrorCode = pEo->ErrorCode;
return;
}
}
if( pEo->fStop ){
if( pEo->fStop == fStopRETURN )pEo->fStop = 0;
*piErrorCode = EXE_ERROR_SUCCESS;
return;
}
if( pEo->fNextPC )
pEo->ProgramCounter = pEo->NextProgramCounter;
else
pEo->ProgramCounter = npc;
}
*piErrorCode = EXE_ERROR_SUCCESS;
return;
}
/*POD
=H execute_InitExecute()
/*FUNCTION*/
void execute_InitExecute(pExecuteObject pEo,
int *piErrorCode
){
/*noverbatim
CUT*/
#ifdef NULL_IS_NOT_ZERO
unsigned long i;
#endif
*piErrorCode = 0;
pEo->ProgramCounter = pEo->StartNode;
pEo->LocalVariables = NULL;
pEo->pszModuleError = NULL;
pEo->cLocalVariables = 0;
pEo->ErrorGoto = 0;
if( pEo->GlobalVariables == NULL ){
pEo->GlobalVariables = memory_NewArray(pEo->pMo,1,pEo->cGlobalVariables);
if( pEo->GlobalVariables == NULL ){
*piErrorCode = EXE_ERROR_MEMORY_LOW;
return;
}
}
pEo->fWeAreCallingFuction = 0;
pEo->lFunctionLevel = 0;
if( pEo->InstructionParameter == NULL ){
pEo->InstructionParameter = alloc_Alloc(pEo->CommandArraySize*sizeof(void *),pEo->pMemorySegment);
if( pEo->InstructionParameter == NULL ){
*piErrorCode = EXE_ERROR_MEMORY_LOW;
return;
}
memset(pEo->InstructionParameter,0,pEo->CommandArraySize*sizeof(void *));
}
memset(pEo->CommandParameter,0,NUM_CMD*sizeof(void *));
memset(pEo->Finaliser,0,NUM_CMD*sizeof(void *));
}
/*POD
=H execute_FinishExecute()
/*FUNCTION*/
void execute_FinishExecute(pExecuteObject pEo,
int *piErrorCode
){
/*noverbatim
CUT*/
unsigned long i;
for( i=0 ; i < NUM_CMD ; i++ ){
if( pEo->Finaliser[i] )pEo->Finaliser[i](pEo);
}
modu_UnloadAllModules(pEo);
}
/*POD
=H execute_Execute()
This function was called from the basic T<main> function. This function performs inititalization
that is needed before each execution of the code and calls R<execute_Execute_r()> to perform the execution.
Note that R<execute_Execute_r()> is recursively calls itself.
This function is obsolete and is not used anymore. This is kept in the source
for the shake of old third party variations that may depend on this function.
Use of this function in new applications is discouraged.
/*FUNCTION*/
void execute_Execute(pExecuteObject pEo,
int *piErrorCode
){
/*noverbatim
CUT*/
execute_InitExecute(pEo,piErrorCode);
if( *piErrorCode )return;
execute_Execute_r(pEo,piErrorCode);
if( *piErrorCode ){
REPORT(*piErrorCode);
}
execute_FinishExecute(pEo,piErrorCode);
}
/*POD
=H execute_ExecuteFunction()
This function is used by the embedding layer (aka T<scriba_> functions) to execute a function.
This function is not directly called by the execution of a ScriptBasic program. It may be
used after the execution of the program by a special embeddign application that keeps the
code and the global variables in memory and calls functions of the program.
The function takes T<pEo> as the execution environment. T<StartNode> should be the node where the
sub or function is defined. T<cArgs> should give the number of arguments. T<pArgs> should point
to the argument array. T<pResult> will point to the result. If T<pResult> is T<NULL> the result is
dropped. Otherwise the result is a mortal variable.
Note that this code does not check the number of arguments you provide. There can be more arguments
passed to the SUB than it has declared, therefore you can initialize the local variables of the sub.
(You should know that arguments are local variables in ScriptBasic just as any other non-argument local
variable.)
The arguments should be normal immortal variables. They are passed to the SUB by reference and in case
they are modified the old variable is going to be released.
T<piErrorCode> returns the error code of the execution which is zero in case of no error.
/*FUNCTION*/
void execute_ExecuteFunction(pExecuteObject pEo,
unsigned long StartNode,
long cArgs,
pFixSizeMemoryObject *pArgs,
pFixSizeMemoryObject *pResult,
int *piErrorCode
){
/*noverbatim
CUT*/
unsigned long nItem,pc;
long i;
long CommandOpCode;
pEo->ProgramCounter = StartNode;
pEo->pFunctionResult = NULL;
pEo->lStepCounter = 0;
pEo->fWeAreCallingFuction = 1;
pEo->ErrorGoto = 0;
pEo->ErrorResume = 0;
pEo->fErrorGoto = ONERROR_NOTHING;
pEo->LocalVariables = NULL;
if( pResult )*pResult = NULL;
if( pEo->CommandArray[pEo->ProgramCounter-1].OpCode != eNTYPE_LST ){
*piErrorCode = EXE_ERROR_INVALID_NODE;
return;
}
pc = pEo->CommandArray[pEo->ProgramCounter-1].Parameter.NodeList.actualm;
if( pc > pEo->CommandArraySize ){
*piErrorCode = EXE_ERROR_INVALID_PC1;
return;
}
if( pc ){
CommandOpCode = pEo->CommandArray[pc-1].OpCode;
if( CommandOpCode < START_CMD || CommandOpCode > END_CMD ){
*piErrorCode = EXE_ERROR_INVALID_OPCODE;
return;
}
}
if( CommandOpCode != CMD_FUNCTION &&
CommandOpCode != CMD_FUNCTIONARG &&
CommandOpCode != CMD_SUB &&
CommandOpCode != CMD_SUBARG
){
*piErrorCode = COMMAND_ERROR_INVALID_CODE;
return;
}
nItem = pEo->CommandArray[pEo->ProgramCounter-1].Parameter.NodeList.actualm ;
pEo->cLocalVariables = pEo->CommandArray[nItem-1].Parameter.CommandArgument.Argument.lLongValue;
if( pEo->cLocalVariables ){
pEo->LocalVariables = memory_NewArray(pEo->pMo,1,pEo->cLocalVariables);
if( pEo->LocalVariables == NULL ){
pEo->fStop = fStopSTOP;
return;
}
}
/* there can not be more arguments than local variables. The rest is ignored. */
if( cArgs > pEo->cLocalVariables )cArgs = pEo->cLocalVariables;
for( i=0 ; i < cArgs ; i++ ){
pEo->LocalVariables->Value.aValue[i] = memory_NewRef(pEo->pMo);
memory_SetRef(pEo->pMo,pEo->LocalVariables->Value.aValue+i,pArgs+i);
}
/* step over the function head */
pEo->ProgramCounter = pEo->CommandArray[pEo->ProgramCounter-1].Parameter.NodeList.rest ;
execute_Execute_r(pEo,piErrorCode);
if( pEo->LocalVariables )/* this is null if the function did not have arguments and no local variables */
memory_ReleaseVariable(pEo->pMo,pEo->LocalVariables);
/* if the result is not needed by the caller we drop it. Otherwise it is the caller's responsibility to drop it. */
if( pResult )
*pResult = pEo->pFunctionResult;
else
memory_ReleaseVariable(pEo->pMo,pEo->pFunctionResult);
return;
}
/*POD
=H execute_Evaluate()
This function evaluates an expression. You should not get confused! This is not syntax analysis, caring
operator precedences and grouping by nested parentheses. That has already been done during syntax analysis.
This code performs the code that was generated from an expression.
The result is usually a mortal memory value which is the final result of the expression. However this piece of
code assumes that the caller is careful enough to handle the result as read only, and sometimes the return
value is not mortal. In this case the return value is a memory object that a variable points to. Whenever the
caller needs this value to perform an operation that does not alter the value it is OK. Duplicating the structure
to create a mortal would be waste of time and memory. On the other hand sometimes operations modify their operands
assuming that they are mortal values. They should be careful.
Operators are actually created in the directory T<commands> and they use the macros defined in T<command.h> (created
by T<headerer.pl> from T<command.c>). They help to avoid pitfalls.
The argument T<iArrayAccepted> tells the function whether an array as a result is accepted or not. If a whole
array is accepted as a result of the expression evaluation the array is returned. If the array is not an
acceptable result, then the first element of the array is retuned in case the result is an array. If the result
is NOT an array this parameter has no effect.
/*FUNCTION*/
pFixSizeMemoryObject execute_Evaluate(pExecuteObject pEo,
unsigned long lExpressionRootNode,
pMortalList pMyMortal,
int *piErrorCode,
int iArrayAccepted
){
/*noverbatim
CUT*/
pFixSizeMemoryObject pVar;
char *s;
unsigned long slen,refcount;
unsigned long SaveProgramCounter,SaveStepCounter;
unsigned long SavefErrorGoto,SaveErrorGoto,SaveErrorResume;
pFixSizeMemoryObject SaveLocalVariablesPointer;
pFixSizeMemoryObject SaveFunctionResultPointer;
pFixSizeMemoryObject ThisFunctionResultPointer;
long OpCode;
CommandFunctionType ThisCommandFunction;
pMortalList pSaveMortalList;
if( ! lExpressionRootNode ){
*piErrorCode = EXE_ERROR_INTERNAL;
return NULL;
}
if( pMyMortal == NULL ){
*piErrorCode = EXE_ERROR_INTERNAL;
return NULL;
}
*piErrorCode = EXE_ERROR_SUCCESS;
#define ASSERT_NON_NULL(x) if( (x) == NULL ){ *piErrorCode = EXE_ERROR_MEMORY_LOW; return NULL; }
switch( OpCode = pEo->CommandArray[lExpressionRootNode-1].OpCode ){
case eNTYPE_ARR: /* array access */
pVar = execute_EvaluateArray(pEo,lExpressionRootNode,pMyMortal,piErrorCode);
while( pVar && ( ((!iArrayAccepted) && pVar->vType == VTYPE_ARRAY) || pVar->vType == VTYPE_REF) ){
/* when an array is referenced as scalar the first element is returned */
while( pVar && (!iArrayAccepted) && pVar->vType == VTYPE_ARRAY )
pVar = pVar->Value.aValue[0];
while( pVar && pVar->vType == VTYPE_REF )
pVar = *(pVar->Value.aValue);
}
return memory_SelfOrRealUndef(pVar);
case eNTYPE_SAR: /* associative array access */
pVar = execute_EvaluateSarray(pEo,lExpressionRootNode,pMyMortal,piErrorCode);
while( pVar && ( ((!iArrayAccepted) && pVar->vType == VTYPE_ARRAY) || pVar->vType == VTYPE_REF) ){
/* when an array is referenced as scalar the first element is returned */
while( pVar && (!iArrayAccepted) && pVar->vType == VTYPE_ARRAY )
pVar = pVar->Value.aValue[0];
while( pVar && pVar->vType == VTYPE_REF )
pVar = *(pVar->Value.aValue);
}
return memory_SelfOrRealUndef(pVar);
case eNTYPE_FUN: /* function */
if( pEo->FunctionLevelLimit && pEo->lFunctionLevel > pEo->FunctionLevelLimit ){
*piErrorCode = EXE_ERROR_TOO_DEEP_CALL;
return NULL;
}
SaveLocalVariablesPointer = pEo->LocalVariables;
SaveProgramCounter = pEo->ProgramCounter;
pEo->ProgramCounter = pEo->CommandArray[lExpressionRootNode-1].Parameter.UserFunction.NodeId;
if( pEo->ProgramCounter == 0 ){
*piErrorCode = EXE_ERROR_USERFUN_UNDEFINED;
return NULL;
}
pEo->FunctionArgumentsNode = pEo->CommandArray[lExpressionRootNode-1].Parameter.UserFunction.Argument;
SaveFunctionResultPointer = pEo->pFunctionResult;
pEo->pFunctionResult = NULL;
SaveStepCounter = pEo->lStepCounter;
pEo->lStepCounter = 0;
pEo->fWeAreCallingFuction = 1;
SaveErrorGoto = pEo->ErrorGoto;
pEo->ErrorGoto = 0;
SaveErrorResume = pEo->ErrorResume;
pEo->ErrorResume = 0;
SavefErrorGoto = pEo->fErrorGoto;
pEo->fErrorGoto = ONERROR_NOTHING;
if( pEo->pHookers->HOOK_ExecCall && (*piErrorCode = pEo->pHookers->HOOK_ExecCall(pEo)) )return NULL;
/* function entering code needs access to the caller local variables, therefore
WE SHOULD NOT NULL pEo->LocalVariables */
execute_Execute_r(pEo,piErrorCode);
if( pEo->pHookers->HOOK_ExecReturn ){
/* if there was already an error then there is no way to handle two different errors
one coming from the execution system and one from the hook function. This way the
hook function generated error (if any) is ignored. */
if( *piErrorCode )
pEo->pHookers->HOOK_ExecReturn(pEo);
else
*piErrorCode = pEo->pHookers->HOOK_ExecReturn(pEo);
}
pEo->lStepCounter = SaveStepCounter;
if( pEo->LocalVariables )/* this is null if the function did not have arguments and no local variables */
memory_ReleaseVariable(pEo->pMo,pEo->LocalVariables);
pEo->ProgramCounter = SaveProgramCounter;
pEo->LocalVariables = SaveLocalVariablesPointer;
ThisFunctionResultPointer = pEo->pFunctionResult;
pEo->pFunctionResult = SaveFunctionResultPointer;
while( ThisFunctionResultPointer &&
(!iArrayAccepted) &&
ThisFunctionResultPointer->vType == VTYPE_ARRAY ){
ThisFunctionResultPointer = ThisFunctionResultPointer->Value.aValue[0];
}
/* Functions return their value as immortal values assigned to the very global
variable pEo->pFunctionResult. Here this variable is restored to point to the
saved value and the value returned should be mortalized. */
if( ThisFunctionResultPointer &&
ThisFunctionResultPointer->vType != VTYPE_ARRAY &&
! IsMortal(ThisFunctionResultPointer) )
memory_Mortalize(ThisFunctionResultPointer,pMyMortal);
pEo->ErrorGoto = SaveErrorGoto;
pEo->fErrorGoto = SavefErrorGoto;
pEo->ErrorResume = SaveErrorResume;
if( *piErrorCode )return NULL;
return memory_SelfOrRealUndef(ThisFunctionResultPointer);
case eNTYPE_LVR: /* local variable */
if( pEo->LocalVariables == NULL ){
*piErrorCode = EXE_ERROR_NO_LOCAL;
return NULL;
}
pVar = pEo->LocalVariables->Value.aValue[pEo->CommandArray[lExpressionRootNode-1].Parameter.Variable.Serial-1];
while( pVar && ( ((!iArrayAccepted) && pVar->vType == VTYPE_ARRAY) || pVar->vType == VTYPE_REF) ){
/* when an array is referenced as scalar the first element is returned */
while( pVar && (!iArrayAccepted) && pVar->vType == VTYPE_ARRAY )
pVar = pVar->Value.aValue[0];
refcount = 0;
while( pVar && pVar->vType == VTYPE_REF ){
pVar = *(pVar->Value.aValue);
if( refcount++ > pEo->pMo->maxderef ){
*piErrorCode = COMMAND_ERROR_CIRCULAR;
return NULL;
}
}
}
return memory_SelfOrRealUndef(pVar);
case eNTYPE_GVR: /* global variable */
pVar = pEo->GlobalVariables->Value.aValue[pEo->CommandArray[lExpressionRootNode-1].Parameter.Variable.Serial-1];
while( pVar && ( ((!iArrayAccepted) && pVar->vType == VTYPE_ARRAY) || pVar->vType == VTYPE_REF) ){
/* when an array is referenced as scalar the first element is returned */
while( pVar && (!iArrayAccepted) && pVar->vType == VTYPE_ARRAY )
pVar = pVar->Value.aValue[0];
refcount = 0;
while( pVar && pVar->vType == VTYPE_REF ){
pVar = *(pVar->Value.aValue);
if( refcount++ > pEo->pMo->maxderef ){
*piErrorCode = COMMAND_ERROR_CIRCULAR;
return NULL;
}
}
}
return memory_SelfOrRealUndef(pVar);
case eNTYPE_DBL: /* constant double */
if( pEo->InstructionParameter[lExpressionRootNode-1] == NULL ){
pVar = pEo->InstructionParameter[lExpressionRootNode-1] = memory_NewDouble(pEo->pMo);
ASSERT_NON_NULL(pVar);
pVar->Value.dValue = pEo->CommandArray[lExpressionRootNode-1].Parameter.Constant.dValue;
}else
pVar = pEo->InstructionParameter[lExpressionRootNode-1];
return memory_SelfOrRealUndef(pVar);
case eNTYPE_LNG: /* constant long */
if( pEo->InstructionParameter[lExpressionRootNode-1] == NULL ){
pVar = pEo->InstructionParameter[lExpressionRootNode-1] = memory_NewLong(pEo->pMo);
ASSERT_NON_NULL(pVar);
pVar->Value.lValue = pEo->CommandArray[lExpressionRootNode-1].Parameter.Constant.lValue;
}else
pVar = pEo->InstructionParameter[lExpressionRootNode-1];
return memory_SelfOrRealUndef(pVar);
case eNTYPE_STR: /* constant string */
s = pEo->StringTable+pEo->CommandArray[lExpressionRootNode-1].Parameter.Constant.sValue;
memcpy(&slen, s-sizeof(long), sizeof(long));
pVar = memory_NewMortalCString(pEo->pMo,slen,pMyMortal);
ASSERT_NON_NULL(pVar);
pVar->Value.pValue = s;
return memory_SelfOrRealUndef(pVar);
case eNTYPE_LST: /* list member (invalid) */
*piErrorCode = EXE_ERROR_INVALID_EXPRESSION_NODE;
return NULL;
case eNTYPE_CRG: /* command arguments (invalid) */
*piErrorCode = EXE_ERROR_INVALID_EXPRESSION_NODE1;
return NULL;
default: /* operators and built in functions */
ThisCommandFunction = pEo->pCommandFunction[OpCode - START_CMD];
if( ThisCommandFunction == NULL ){
*piErrorCode = EXE_ERROR_NOT_IMPLEMENTED;
return NULL;
}
pEo->OperatorNode = lExpressionRootNode;
pEo->pOpResult = NULL;
pSaveMortalList = pEo->pGlobalMortalList;
pEo->pGlobalMortalList = pMyMortal;
ThisCommandFunction(pEo);
pEo->pGlobalMortalList = pSaveMortalList;
*piErrorCode = pEo->ErrorCode;
return memory_SelfOrRealUndef(pEo->pOpResult);
}
*piErrorCode = EXE_ERROR_INTERNAL;
return NULL;
}
/*POD
=H execute_LeftValue()
This function evaluate a left value. A left value is a special expression that value can be assigned, and therefore
they usually stand on the left side of the assignment operator. That is the reason for the name.
When an expression is evaluates a pointer to a memory object is returned. Whenever a left value is evaluated a pointer
to the variable is returned. If any code assignes value to the variable pointed by the return value of this function
it should release the memory object that the left value points currently.
/*FUNCTION*/
pFixSizeMemoryObject *execute_LeftValue(pExecuteObject pEo,
unsigned long lExpressionRootNode,
pMortalList pMyMortal,
int *piErrorCode,
int iArrayAccepted
){
/*noverbatim
CUT*/
pFixSizeMemoryObject *ppVar;
long OpCode;
*piErrorCode = EXE_ERROR_SUCCESS;
switch( OpCode = pEo->CommandArray[lExpressionRootNode-1].OpCode ){
case eNTYPE_ARR: /* array access */
return execute_LeftValueArray(pEo,lExpressionRootNode,pMyMortal,piErrorCode);
case eNTYPE_SAR: /* associative array access */
return execute_LeftValueSarray(pEo,lExpressionRootNode,pMyMortal,piErrorCode);
case eNTYPE_LVR: /* local variable */
if( pEo->LocalVariables == NULL ){
*piErrorCode = EXE_ERROR_NO_LOCAL;
return NULL;
}
ppVar = &(pEo->LocalVariables->Value.aValue[pEo->CommandArray[lExpressionRootNode-1].Parameter.Variable.Serial-1]);
/* when an array is referenced as scalar the first element is returned */
while( (!iArrayAccepted) && *ppVar && (*ppVar)->vType == VTYPE_ARRAY )
ppVar = &((*ppVar)->Value.aValue[0]);
return ppVar;
case eNTYPE_GVR: /* global variable */
ppVar = &(pEo->GlobalVariables->Value.aValue[pEo->CommandArray[lExpressionRootNode-1].Parameter.Variable.Serial-1]);
/* when an array is referenced as scalar the first element is returned */
while( (!iArrayAccepted) && *ppVar && (*ppVar)->vType == VTYPE_ARRAY )
ppVar = &((*ppVar)->Value.aValue[0]);
return ppVar;
case eNTYPE_FUN: /* function */
*piErrorCode = EXE_ERROR_INVALID_LVALNODE0;
return NULL;
case eNTYPE_DBL: /* constant double */
*piErrorCode = EXE_ERROR_INVALID_LVALNODE1;