fix apply errors
[nedit-bw.git] / global-RestartData.patch
blob1a8d9f81b68cf32da1455b7e5020d460049de168
1 ---
3 source/interpret.c | 117 ++++++++++++++++++++---------------------------------
4 source/interpret.h | 3 -
5 2 files changed, 47 insertions(+), 73 deletions(-)
7 diff --quilt old/source/interpret.c new/source/interpret.c
8 --- old/source/interpret.c
9 +++ new/source/interpret.c
10 @@ -81,8 +81,7 @@ static const char CVSID[] = "$Id: interp
11 enum opStatusCodes {STAT_OK=2, STAT_DONE, STAT_ERROR, STAT_PREEMPT};
13 static int addLoopAddr(Inst *addr, char **msg);
14 -static void saveContext(RestartData *context);
15 -static void restoreContext(RestartData *context);
16 +static RestartData *setContext(RestartData *context);
18 #define OP(name, fn) static int fn(void);
19 #include "ops.h"
20 @@ -175,16 +174,14 @@ static Inst **LoopStackPtr = LoopStack;
21 static const char *ProgramName = "";
23 /* Global data for the interpreter */
24 -static DataValue *TheStack; /* the stack */
25 -static DataValue *StackP; /* next free spot on stack */
26 -static DataValue *FrameP; /* frame pointer (start of local variables
27 - for the current subroutine invocation) */
28 -static Inst *PC; /* program counter during execution */
29 -static char *ErrMsg; /* global for returning error messages
30 - from executing functions */
31 -static WindowInfo
32 - *InitiatingWindow = NULL; /* window from which macro was run */
33 -static WindowInfo *FocusWindow; /* window on which macro commands operate */
34 +static RestartData *Interpreter;
35 +#define StackP (Interpreter->stackP)
36 +#define TheStack (Interpreter->stack)
37 +#define FrameP (Interpreter->frameP)
38 +#define PC (Interpreter->pc)
39 +#define InitiatingWindow (Interpreter->runWindow)
40 +#define FocusWindow (Interpreter->focusWindow)
41 +#define ErrMsg (Interpreter->errMsg)
42 static int PreemptRequest; /* passes preemption requests from called
43 routines back up to the interpreter */
45 @@ -533,7 +530,7 @@ static int setupFrame(RestartData *conte
48 /* !OK_TO_PUSH(totalPushs) */
49 - if (!((context->stackP + totalPushs) <= &context->stack[STACK_SIZE])) {
50 + if (!((context->stackP + totalPushs) <= (context->stack + STACK_SIZE))) {
51 return execError(StackOverflowMsg, "");
54 @@ -648,8 +645,7 @@ int ExecuteMacro(WindowInfo *window, Pro
55 /* Create an execution context (a stack, a stack pointer, a frame pointer,
56 and a program counter) which will retain the program state across
57 preemption and resumption of execution */
58 - context = (RestartData *)XtMalloc(sizeof(RestartData));
59 - context->stack = (DataValue *)XtMalloc(sizeof(DataValue) * STACK_SIZE);
60 + context = XtNew(RestartData);
61 *continuation = context;
62 context->stackP = context->stack;
63 context->runWindow = window;
64 @@ -680,18 +676,17 @@ int ContinueMacro(RestartData *continuat
66 register int status, instCount = 0;
67 register Inst *inst;
68 - RestartData oldContext;
70 - /* To allow macros to be invoked arbitrarily (such as those automatically
71 - triggered within smart-indent) within executing macros, this call is
72 - reentrant. */
73 - saveContext(&oldContext);
74 + RestartData *oldContext;
77 + ** To allow macros to be invoked arbitrarily (such as those automatically
78 + ** triggered within smart-indent) within executing macros, this call is
79 + ** reentrant.
80 + **
81 ** Execution Loop: Call the succesive routine addresses in the program
82 ** until one returns something other than STAT_OK, then take action
84 - restoreContext(continuation);
85 + oldContext = setContext(continuation);
86 ErrMsg = NULL;
87 for (;;) {
89 @@ -716,21 +711,18 @@ int ContinueMacro(RestartData *continuat
90 /* If error return was not STAT_OK, return to caller */
91 if (status != STAT_OK) {
92 if (status == STAT_PREEMPT) {
93 - saveContext(continuation);
94 - restoreContext(&oldContext);
95 + setContext(oldContext);
96 return MACRO_PREEMPT;
97 } else if (status == STAT_ERROR) {
98 *msg = ErrMsg;
99 - saveContext(continuation);
100 + setContext(oldContext);
101 FreeRestartData(continuation);
102 - restoreContext(&oldContext);
103 return MACRO_ERROR;
104 } else if (status == STAT_DONE) {
105 *msg = "";
106 *result = *--StackP;
107 - saveContext(continuation);
108 + setContext(oldContext);
109 FreeRestartData(continuation);
110 - restoreContext(&oldContext);
111 return MACRO_DONE;
114 @@ -740,8 +732,7 @@ int ContinueMacro(RestartData *continuat
115 X, other macros, and other shell scripts a chance to execute */
116 instCount++;
117 if (instCount >= INSTRUCTION_LIMIT) {
118 - saveContext(continuation);
119 - restoreContext(&oldContext);
120 + setContext(oldContext);
121 return MACRO_TIME_LIMIT;
124 @@ -777,7 +768,6 @@ int RunMacroAsSubrCall(RestartData *cont
125 void FreeRestartData(RestartData *context)
127 rewindStack(context);
128 - XtFree((char *)context->stack);
129 XtFree((char *)context);
132 @@ -798,8 +788,8 @@ void PreemptMacro(void)
134 void ModifyReturnedValue(RestartData *context, DataValue dv)
136 - if ((context->pc-1)->val.op == OP_FETCH_RET_VAL)
137 - *(context->stackP-1) = dv;
138 + if ((context->pc - 1)->val.op == OP_FETCH_RET_VAL)
139 + *(context->stackP - 1) = dv;
143 @@ -808,7 +798,7 @@ void ModifyReturnedValue(RestartData *co
145 WindowInfo *MacroRunWindow(void)
147 - return InitiatingWindow;
148 + return Interpreter ? InitiatingWindow : NULL;
152 @@ -818,7 +808,7 @@ WindowInfo *MacroRunWindow(void)
154 WindowInfo *MacroFocusWindow(void)
156 - return FocusWindow;
157 + return Interpreter ? FocusWindow : NULL;
161 @@ -827,7 +817,12 @@ WindowInfo *MacroFocusWindow(void)
163 void SetMacroFocusWindow(WindowInfo *window)
165 - FocusWindow = window;
166 + if (Interpreter) {
167 + FocusWindow = window;
169 + else {
170 + fprintf(stderr, "NEdit: setting FocusWindow while not running a macro\n");
175 @@ -1317,24 +1312,11 @@ void GarbageCollectStrings(void)
177 ** Save and restore execution context to data structure "context"
179 -static void saveContext(RestartData *context)
180 +static RestartData *setContext(RestartData *new)
182 - context->stack = TheStack;
183 - context->stackP = StackP;
184 - context->frameP = FrameP;
185 - context->pc = PC;
186 - context->runWindow = InitiatingWindow;
187 - context->focusWindow = FocusWindow;
190 -static void restoreContext(RestartData *context)
192 - TheStack = context->stack;
193 - StackP = context->stackP;
194 - FrameP = context->frameP;
195 - PC = context->pc;
196 - InitiatingWindow = context->runWindow;
197 - FocusWindow = context->focusWindow;
198 + RestartData *old = Interpreter;
199 + Interpreter = new;
200 + return old;
203 static void freeSymbolList(Symbol *symList)
204 @@ -1435,7 +1417,7 @@ static void addToGlobalSymTab(Symbol *sy
206 /* true, if you can push n values */
207 #define OK_TO_PUSH(n) \
208 - (StackP + (n) <= &TheStack[STACK_SIZE])
209 + (StackP + (n) <= (TheStack + STACK_SIZE))
211 #define PUSH_CHECK(n) \
212 do { \
213 @@ -1604,9 +1586,8 @@ static int pushSymVal(void)
214 symVal = FP_GET_ARG_N(FrameP, argNum);
216 } else if (s->type == PROC_VALUE_SYM) {
217 - char *errMsg;
218 - if (!(s->value.val.subr)(FocusWindow, NULL, 0,
219 - &symVal, &errMsg)) {
220 + char *errMsg;
221 + if (!s->value.val.subr(FocusWindow, NULL, 0, &symVal, &errMsg)) {
222 EXEC_ERROR(errMsg, s->name);
224 } else
225 @@ -2371,8 +2352,8 @@ static int eq(void)
226 /* negated eq() call */
227 static int ne(void)
229 - eq();
230 - return not();
231 + int status = eq();
232 + return (status == STAT_OK) ? not() : status;
236 @@ -2702,7 +2683,6 @@ static int callSubroutineFromSymbol(Symb
237 static DataValue noValue = {NO_TAG, {0}};
238 DataValue argArray = noValue;
239 Program *prog;
240 - char *errMsg;
241 int haveNamedArgs = (nArgs < 0);
243 if (haveNamedArgs) {
244 @@ -2715,6 +2695,7 @@ static int callSubroutineFromSymbol(Symb
246 if (sym->type == C_FUNCTION_SYM) {
247 DataValue result;
248 + char *errMsg;
250 PUSH(argArray); /* push dummy named arg array */
252 @@ -2723,9 +2704,9 @@ static int callSubroutineFromSymbol(Symb
254 /* Call the function and check for preemption */
255 PreemptRequest = False;
256 - if (!sym->value.val.subr(FocusWindow, StackP,
257 - nArgs, &result, &errMsg))
258 + if (!sym->value.val.subr(FocusWindow, StackP, nArgs, &result, &errMsg)) {
259 EXEC_ERROR(errMsg, sym->name);
261 PUSH_RET_VAL(result);
262 return PreemptRequest ? STAT_PREEMPT : STAT_OK;
264 @@ -2738,15 +2719,10 @@ static int callSubroutineFromSymbol(Symb
265 ** values which are already there.
267 if (sym->type == MACRO_FUNCTION_SYM) {
268 - RestartData context;
269 - int status;
270 prog = sym->value.val.prog;
271 prog->refcount++;
272 /* -nArgs means 'arguments are on stack' */
273 - saveContext(&context);
274 - status = setupFrame(&context, prog, -nArgs, NULL, argArray, sym->name);
275 - restoreContext(&context);
276 - return status;
277 + return setupFrame(Interpreter, prog, -nArgs, NULL, argArray, sym->name);
281 @@ -2999,7 +2975,6 @@ static int returnValOrNone(int valOnStac
283 DataValue retVal;
284 static DataValue noValue = {NO_TAG, {0}};
285 - RestartData context;
287 DISASM_RT();
288 STACKDUMP(StackP - FrameP + FP_GET_ARG_COUNT(FrameP) + FP_TO_ARGS_DIST, 3);
289 @@ -3012,9 +2987,7 @@ static int returnValOrNone(int valOnStac
290 retVal = noValue;
293 - saveContext(&context);
294 - rewindFrame(&context);
295 - restoreContext(&context);
296 + rewindFrame(Interpreter);
298 /* push returned value, if requsted */
299 PUSH_RET_VAL(retVal);
300 diff --quilt old/source/interpret.h new/source/interpret.h
301 --- old/source/interpret.h
302 +++ new/source/interpret.h
303 @@ -120,12 +120,13 @@ typedef struct ProgramTag {
305 /* Information needed to re-start a preempted macro */
306 typedef struct {
307 - DataValue *stack;
308 + DataValue stack[STACK_SIZE];
309 DataValue *stackP;
310 DataValue *frameP;
311 Inst *pc;
312 WindowInfo *runWindow;
313 WindowInfo *focusWindow;
314 + char *errMsg;
315 } RestartData;
317 /* state of the accumulator (aka compiler) */