r8631: give an error on incorrect argument count
[samba.git] / source4 / lib / appweb / ejs / ejsParser.c
1 /*
2  *      @file   ejsParser.c
3  *      @brief  EJS Parser and Execution 
4  */
5 /********************************* Copyright **********************************/
6 /*
7  *      @copy   default.g
8  *      
9  *      Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
10  *      Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
11  *      
12  *      This software is distributed under commercial and open source licenses.
13  *      You may use the GPL open source license described below or you may acquire 
14  *      a commercial license from Mbedthis Software. You agree to be fully bound 
15  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
16  *      this software for full details.
17  *      
18  *      This software is open source; you can redistribute it and/or modify it 
19  *      under the terms of the GNU General Public License as published by the 
20  *      Free Software Foundation; either version 2 of the License, or (at your 
21  *      option) any later version. See the GNU General Public License for more 
22  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
23  *      
24  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
25  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
26  *      
27  *      This GPL license does NOT permit incorporating this software into 
28  *      proprietary programs. If you are unable to comply with the GPL, you must
29  *      acquire a commercial license to use this software. Commercial licenses 
30  *      for this software and support services are available from Mbedthis 
31  *      Software at http://www.mbedthis.com 
32  *      
33  *      @end
34  */
35
36 /********************************** Includes **********************************/
37
38 #include        "ejsInternal.h"
39
40 #if BLD_FEATURE_EJS
41
42 /****************************** Forward Declarations **************************/
43
44 static void     appendValue(MprVar *v1, MprVar *v2);
45 static int              evalCond(Ejs *ep, MprVar *lhs, int rel, MprVar *rhs);
46 static int              evalExpr(Ejs *ep, MprVar *lhs, int rel, MprVar *rhs);
47 #if BLD_FEATURE_FLOATING_POINT
48 static int              evalFloatExpr(Ejs *ep, double l, int rel, double r);
49 #endif 
50 static int              evalBoolExpr(Ejs *ep, bool l, int rel, bool r);
51 static int              evalNumericExpr(Ejs *ep, MprNum l, int rel, MprNum r);
52 static int              evalStringExpr(Ejs *ep, MprVar *lhs, int rel, MprVar *rhs);
53 static int              evalFunction(Ejs *ep, MprVar *obj, int flags);
54 static void             freeProc(EjsProc *proc);
55 static int              parseArgs(Ejs *ep, int state, int flags);
56 static int              parseAssignment(Ejs *ep, int state, int flags, char *id, 
57                                         char *fullName);
58 static int              parseCond(Ejs *ep, int state, int flags);
59 static int              parseDeclaration(Ejs *ep, int state, int flags);
60 static int              parseExpr(Ejs *ep, int state, int flags);
61 static int              parseFor(Ejs *ep, int state, int flags);
62 static int              parseForIn(Ejs *ep, int state, int flags);
63 static int              parseFunctionDec(Ejs *ep, int state, int flags);
64 static int              parseFunction(Ejs *ep, int state, int flags, char *id);
65 static int              parseId(Ejs *ep, int state, int flags, char **id, 
66                                         char **fullName, int *fullNameLen, int *done);
67 static int              parseInc(Ejs *ep, int state, int flags);
68 static int              parseIf(Ejs *ep, int state, int flags, int *done);
69 static int              parseStmt(Ejs *ep, int state, int flags);
70 static void     removeNewlines(Ejs *ep, int state);
71 static void     updateResult(Ejs *ep, int state, int flags, MprVar *vp);
72
73 /************************************* Code ***********************************/
74 /*
75  *      Recursive descent parser for EJS
76  */
77
78 int ejsParse(Ejs *ep, int state, int flags)
79 {
80         mprAssert(ep);
81
82         switch (state) {
83         /*
84          *      Any statement, function arguments or conditional expressions
85          */
86         case EJS_STATE_STMT:
87                 if ((state = parseStmt(ep, state, flags)) != EJS_STATE_STMT_DONE &&
88                         state != EJS_STATE_EOF && state != EJS_STATE_STMT_BLOCK_DONE &&
89                         state != EJS_STATE_RET) {
90                         state = EJS_STATE_ERR;
91                 }
92                 break;
93
94         case EJS_STATE_DEC:
95                 if ((state = parseStmt(ep, state, flags)) != EJS_STATE_DEC_DONE &&
96                         state != EJS_STATE_EOF) {
97                         state = EJS_STATE_ERR;
98                 }
99                 break;
100
101         case EJS_STATE_EXPR:
102                 if ((state = parseStmt(ep, state, flags)) != EJS_STATE_EXPR_DONE &&
103                         state != EJS_STATE_EOF) {
104                         state = EJS_STATE_ERR;
105                 }
106                 break;
107
108         /*
109          *      Variable declaration list
110          */
111         case EJS_STATE_DEC_LIST:
112                 state = parseDeclaration(ep, state, flags);
113                 break;
114
115         /*
116          *      Function argument string
117          */
118         case EJS_STATE_ARG_LIST:
119                 state = parseArgs(ep, state, flags);
120                 break;
121
122         /*
123          *      Logical condition list (relational operations separated by &&, ||)
124          */
125         case EJS_STATE_COND:
126                 state = parseCond(ep, state, flags);
127                 break;
128
129         /*
130          *      Expression list
131          */
132         case EJS_STATE_RELEXP:
133                 state = parseExpr(ep, state, flags);
134                 break;
135         }
136
137         if (state == EJS_STATE_ERR && ep->error == NULL) {
138                 ejsError(ep, "Syntax error");
139         }
140         return state;
141 }
142
143 /******************************************************************************/
144 /*
145  *      Parse any statement including functions and simple relational operations
146  */
147
148 static int parseStmt(Ejs *ep, int state, int flags)
149 {
150         EjsProc         *saveProc;
151         MprVar          *vp, *saveObj;
152         char            *id, *fullName, *initToken;
153         int             done, expectSemi, tid, fullNameLen, rel;
154         int             initId;
155
156         mprAssert(ep);
157
158         expectSemi = 0;
159         saveProc = NULL;
160         id = 0;
161         fullName = 0;
162         fullNameLen = 0;
163
164         ep->currentObj = 0;
165         ep->currentProperty = 0;
166
167         for (done = 0; !done && state != EJS_STATE_ERR; ) {
168                 tid = ejsLexGetToken(ep, state);
169
170                 switch (tid) {
171                 default:
172                         ejsLexPutbackToken(ep, EJS_TOK_EXPR, ep->token);
173                         done++;
174                         break;
175
176                 case EJS_TOK_EXPR:
177                         rel = (int) *ep->token;
178                         if (state == EJS_STATE_EXPR) {
179                                 ejsLexPutbackToken(ep, EJS_TOK_EXPR, ep->token);
180                         }
181                         done++;
182                         break;
183
184                 case EJS_TOK_LOGICAL:
185                         ejsLexPutbackToken(ep, tid, ep->token);
186                         done++;
187                         break;
188
189                 case EJS_TOK_ERR:
190                         state = EJS_STATE_ERR;
191                         done++;
192                         break;
193
194                 case EJS_TOK_EOF:
195                         state = EJS_STATE_EOF;
196                         done++;
197                         break;
198
199                 case EJS_TOK_NEWLINE:
200                         break;
201
202                 case EJS_TOK_SEMI:
203                         /*
204                          *      This case is when we discover no statement and just a lone ';'
205                          */
206                         if (state != EJS_STATE_STMT) {
207                                 ejsLexPutbackToken(ep, tid, ep->token);
208                         }
209                         done++;
210                         break;
211
212                 case EJS_TOK_PERIOD:
213                         if (flags & EJS_FLAGS_EXE) {
214                                 if (ep->currentProperty == 0) {
215                                         ejsError(ep, "Undefined object \"%s\"\n", id);
216                                         goto error;
217                                 }
218                         }
219                         ep->currentObj = ep->currentProperty;
220
221                         if ((tid = ejsLexGetToken(ep, state)) != EJS_TOK_ID) {
222                                 ejsError(ep, "Bad property after '.': %s\n", ep->token);
223                                 goto error;
224                         }
225                         mprFree(id);
226                         id = mprStrdup(ep->token);
227
228                         vp = ejsFindProperty(ep, state, ep->currentObj, id, flags);
229                         updateResult(ep, state, flags, vp);
230
231 #if BLD_DEBUG
232                         fullNameLen = mprReallocStrcat(&fullName, MPR_MAX_VAR, fullNameLen,
233                                 0, ".", 0);
234 #endif
235
236                         ep->currentProperty = vp;
237                         ejsLexPutbackToken(ep, tid, ep->token);
238                         break;
239
240                 case EJS_TOK_LBRACKET:
241                         ep->currentObj = ep->currentProperty;
242                         saveObj = ep->currentObj;
243                         if (ejsParse(ep, EJS_STATE_RELEXP, flags) != EJS_STATE_RELEXP_DONE){
244                                 goto error;
245                         }
246                         ep->currentObj = saveObj;
247
248                         mprFree(id);
249                         mprVarToString(&id, MPR_MAX_STRING, 0, &ep->result);
250
251                         if (id[0] == '\0') {
252                                 if (flags & EJS_FLAGS_EXE) {
253                                         ejsError(ep, 
254                                                 "[] expression evaluates to the empty string\n");
255                                         goto error;
256                                 }
257                         } else {
258                                 vp = ejsFindProperty(ep, state, ep->currentObj, id, flags);
259                                 ep->currentProperty = vp;
260                                 updateResult(ep, state, flags, vp);
261                         }
262
263 #if BLD_DEBUG
264                         if (id[0] && strlen(id) < (MPR_MAX_VAR / 2)) {
265                                 /*
266                                  *      If not executing yet, id may not be known
267                                  */
268                                 fullNameLen = mprReallocStrcat(&fullName, MPR_MAX_VAR, 
269                                         fullNameLen, 0, "[", id, "]", 0);
270                         }
271 #endif
272
273                         if ((tid = ejsLexGetToken(ep, state)) != EJS_TOK_RBRACKET) {
274                                 ejsError(ep, "Missing ']'\n");
275                                 goto error;
276                         }
277                         break;
278
279                 case EJS_TOK_ID:
280                         state = parseId(ep, state, flags, &id, &fullName, &fullNameLen, 
281                                 &done);
282                         if (done && state == EJS_STATE_STMT) {
283                                 expectSemi++;
284                         }
285                         break;
286
287                 case EJS_TOK_ASSIGNMENT:
288                         state = parseAssignment(ep, state, flags, id, fullName);
289                         if (state == EJS_STATE_STMT) {
290                                 expectSemi++;
291                                 done++;
292                         }
293                         break;
294
295                 case EJS_TOK_INC_DEC:
296                         state = parseInc(ep, state, flags);
297                         if (state == EJS_STATE_STMT) {
298                                 expectSemi++;
299                         }
300                         break;
301
302                 case EJS_TOK_NEW:
303                         if (ejsParse(ep, EJS_STATE_EXPR, flags | EJS_FLAGS_NEW) 
304                                         != EJS_STATE_EXPR_DONE) {
305                                 goto error;
306                         }
307                         break;
308
309                 case EJS_TOK_DELETE:
310                         if (ejsParse(ep, EJS_STATE_EXPR, 
311                                         flags | EJS_FLAGS_DELETE) != EJS_STATE_EXPR_DONE) {
312                                 goto error;
313                         }
314                         mprDeleteProperty(ep->currentObj, ep->currentProperty->name);
315                         done++;
316                         break;
317
318                 case EJS_TOK_FUNCTION:
319                         state = parseFunctionDec(ep, state, flags);
320                         done++;
321                         break;
322
323                 case EJS_TOK_LITERAL:
324                         /*
325                          *      Set the result to the string literal 
326                          */
327                         mprCopyVarValue(&ep->result, mprCreateStringVar(ep->token, 0), 
328                                 MPR_SHALLOW_COPY);
329                         if (state == EJS_STATE_STMT) {
330                                 expectSemi++;
331                         }
332                         done++;
333                         break;
334
335                 case EJS_TOK_NUMBER:
336                         /*
337                          *      Set the result to the parsed number
338                          */
339                         mprCopyVar(&ep->result, &ep->tokenNumber, 0);
340                         if (state == EJS_STATE_STMT) {
341                                 expectSemi++;
342                         }
343                         done++;
344                         break;
345
346                 case EJS_TOK_FUNCTION_NAME:
347                         state = parseFunction(ep, state, flags, id);
348                         if (state == EJS_STATE_STMT) {
349                                 expectSemi++;
350                         }
351                         if (ep->flags & EJS_FLAGS_EXIT) {
352                                 state = EJS_STATE_RET;
353                         }
354                         done++;
355                         break;
356
357                 case EJS_TOK_IF:
358                         state = parseIf(ep, state, flags, &done);
359                         if (state == EJS_STATE_RET) {
360                                 goto doneParse;
361                         }
362                         break;
363
364                 case EJS_TOK_FOR:
365                         if (state != EJS_STATE_STMT) {
366                                 goto error;
367                         }
368                         if (ejsLexGetToken(ep, state) != EJS_TOK_LPAREN) {
369                                 goto error;
370                         }
371                         /*
372                          *      Need to peek 2-3 tokens ahead and see if this is a 
373                          *              for ([var] x in set) 
374                          *      or
375                          *              for (init ; whileCond; incr)
376                          */
377                         initId = ejsLexGetToken(ep, EJS_STATE_EXPR);
378                         if (initId == EJS_TOK_ID && strcmp(ep->token, "var") == 0) {
379                                 /*      Simply eat var tokens */
380                                 initId = ejsLexGetToken(ep, EJS_STATE_EXPR);
381                         }
382                         initToken = mprStrdup(ep->token);
383
384                         tid = ejsLexGetToken(ep, EJS_STATE_EXPR);
385
386                         ejsLexPutbackToken(ep, tid, ep->token);
387                         ejsLexPutbackToken(ep, initId, initToken);
388                         mprFree(initToken);
389
390                         if (tid == EJS_TOK_IN) {
391                                 if ((state = parseForIn(ep, state, flags)) < 0) {
392                                         goto error;
393                                 }
394                         } else {
395                                 if ((state = parseFor(ep, state, flags)) < 0) {
396                                         goto error;
397                                 }
398                         }
399                         done++;
400                         break;
401
402                 case EJS_TOK_VAR:
403                         if (ejsParse(ep, EJS_STATE_DEC_LIST, flags) 
404                                         != EJS_STATE_DEC_LIST_DONE) {
405                                 goto error;
406                         }
407                         done++;
408                         break;
409
410                 case EJS_TOK_COMMA:
411                         ejsLexPutbackToken(ep, tid, ep->token);
412                         done++;
413                         break;
414
415                 case EJS_TOK_LPAREN:
416                         if (state == EJS_STATE_EXPR) {
417                                 if (ejsParse(ep, EJS_STATE_RELEXP, flags) 
418                                                 != EJS_STATE_RELEXP_DONE) {
419                                         goto error;
420                                 }
421                                 if (ejsLexGetToken(ep, state) != EJS_TOK_RPAREN) {
422                                         goto error;
423                                 }
424                         }
425                         done++;
426                         break;
427
428                 case EJS_TOK_RPAREN:
429                         ejsLexPutbackToken(ep, tid, ep->token);
430                         done++;
431                         break;
432
433                 case EJS_TOK_LBRACE:
434                         /*
435                          *      This handles any code in braces except "if () {} else {}"
436                          */
437                         if (state != EJS_STATE_STMT) {
438                                 goto error;
439                         }
440
441                         /*
442                          *      Parse will return EJS_STATE_STMT_BLOCK_DONE when the RBRACE 
443                          *      is seen.
444                          */
445                         do {
446                                 state = ejsParse(ep, EJS_STATE_STMT, flags);
447                         } while (state == EJS_STATE_STMT_DONE);
448
449                         if (state != EJS_STATE_RET) {
450                                 if (ejsLexGetToken(ep, state) != EJS_TOK_RBRACE) {
451                                         goto error;
452                                 }
453                                 state = EJS_STATE_STMT_DONE;
454                         }
455                         done++;
456                         break;
457
458                 case EJS_TOK_RBRACE:
459                         if (state == EJS_STATE_STMT) {
460                                 ejsLexPutbackToken(ep, tid, ep->token);
461                                 state = EJS_STATE_STMT_BLOCK_DONE;
462                                 done++;
463                                 break;
464                         }
465                         goto error;
466
467                 case EJS_TOK_RETURN:
468                         if (ejsParse(ep, EJS_STATE_RELEXP, flags) 
469                                         != EJS_STATE_RELEXP_DONE) {
470                                 goto error;
471                         }
472                         if (flags & EJS_FLAGS_EXE) {
473                                 while (ejsLexGetToken(ep, state) != EJS_TOK_EOF) {
474                                         ;
475                                 }
476                                 state = EJS_STATE_RET;
477                                 done++;
478                         }
479                         break;
480                 }
481         }
482
483         if (expectSemi) {
484                 tid = ejsLexGetToken(ep, state);
485                 if (tid != EJS_TOK_SEMI && tid != EJS_TOK_NEWLINE && 
486                                 tid != EJS_TOK_EOF) {
487                         goto error;
488                 }
489
490                 /*
491                  *      Skip newline after semi-colon
492                  */
493                 removeNewlines(ep, state);
494         }
495
496 /*
497  *      Free resources and return the correct status
498  */
499 doneParse:
500         mprFree(id);
501         mprFree(fullName);
502
503         /*
504          *      Advance the state
505          */
506         switch (state) {
507         case EJS_STATE_STMT:
508                 return EJS_STATE_STMT_DONE;
509
510         case EJS_STATE_DEC:
511                 return EJS_STATE_DEC_DONE;
512
513         case EJS_STATE_EXPR:
514                 return EJS_STATE_EXPR_DONE;
515
516         case EJS_STATE_STMT_DONE:
517         case EJS_STATE_STMT_BLOCK_DONE:
518         case EJS_STATE_EOF:
519         case EJS_STATE_RET:
520                 return state;
521
522         default:
523                 return EJS_STATE_ERR;
524         }
525
526 /*
527  *      Common error exit
528  */
529 error:
530         state = EJS_STATE_ERR;
531         goto doneParse;
532 }
533
534 /******************************************************************************/
535 /*
536  *      Parse function arguments
537  */
538
539 static int parseArgs(Ejs *ep, int state, int flags)
540 {
541         int             tid;
542
543         mprAssert(ep);
544
545         do {
546                 /*
547                  *      Peek and see if there are no args
548                  */
549                 tid = ejsLexGetToken(ep, state);
550                 ejsLexPutbackToken(ep, tid, ep->token);
551                 if (tid == EJS_TOK_RPAREN) {
552                         break;
553                 }
554
555                 state = ejsParse(ep, EJS_STATE_RELEXP, flags);
556                 if (state == EJS_STATE_EOF || state == EJS_STATE_ERR) {
557                         return state;
558                 }
559                 if (state == EJS_STATE_RELEXP_DONE) {
560                         if (flags & EJS_FLAGS_EXE) {
561                                 mprAssert(ep->proc->args);
562                                 mprAddToArray(ep->proc->args, 
563                                         mprDupVar(&ep->result, MPR_SHALLOW_COPY));
564                         }
565                 }
566                 /*
567                  *      Peek at the next token, continue if more args (ie. comma seen)
568                  */
569                 tid = ejsLexGetToken(ep, state);
570                 if (tid != EJS_TOK_COMMA) {
571                         ejsLexPutbackToken(ep, tid, ep->token);
572                 }
573         } while (tid == EJS_TOK_COMMA);
574
575         if (tid != EJS_TOK_RPAREN && state != EJS_STATE_RELEXP_DONE) {
576                 return EJS_STATE_ERR;
577         }
578         return EJS_STATE_ARG_LIST_DONE;
579 }
580
581 /******************************************************************************/
582 /*
583  *      Parse an assignment statement
584  */
585
586 static int parseAssignment(Ejs *ep, int state, int flags, char *id, 
587         char *fullName)
588 {
589         MprVar          *vp, *saveProperty, *saveObj;
590
591         if (id == 0) {
592                 return -1;
593         }
594
595         saveObj = ep->currentObj;
596         saveProperty = ep->currentProperty;
597         if (ejsParse(ep, EJS_STATE_RELEXP, flags | EJS_FLAGS_ASSIGNMENT) 
598                         != EJS_STATE_RELEXP_DONE) {
599                 return -1;
600         }
601         ep->currentObj = saveObj;
602         ep->currentProperty = saveProperty;
603
604         if (! (flags & EJS_FLAGS_EXE)) {
605                 return state;
606         }
607
608         if (ep->currentProperty) {
609                 /*
610                  *      Update the variable. Update the property name if not
611                  *      yet defined.
612                  */
613                 if (ep->currentProperty->name == 0 || 
614                                 ep->currentProperty->name[0] == '\0') {
615                         mprSetVarName(ep->currentProperty, id);
616                 }
617                 if (mprWriteProperty(ep->currentProperty, &ep->result) < 0){
618                         ejsError(ep, "Can't write to variable\n");
619                         return -1;
620                 }
621
622         } else {
623                 /*
624                  *      Create the variable
625                  */
626                 if (ep->currentObj) {
627                         if (ep->currentObj->type != MPR_TYPE_OBJECT) {
628                                 if (strcmp(ep->currentObj->name, "session") == 0) {
629                                         ejsError(ep, "Variable \"%s\" is not an array or object." 
630                                                 "If using ESP, you need useSession(); in your page.",
631                                                 ep->currentObj->name);
632                                 } else {
633                                         ejsError(ep, "Variable \"%s\" is not an array or object", 
634                                                 ep->currentObj->name);
635                                 }
636                                 return -1;
637                         }
638                         vp = mprCreateProperty(ep->currentObj, id, &ep->result);
639
640                 } else {
641                         /*
642                          *      Standard says: "var x" means declare locally.
643                          *      "x = 2" means declare globally if x is undefined.
644                          */
645                         if (state == EJS_STATE_DEC) {
646                                 vp = mprCreateProperty(ep->local, id, &ep->result);
647                         } else {
648                                 vp = mprCreateProperty(ep->global, id, &ep->result);
649                         }
650                 }
651 #if BLD_DEBUG
652                 mprSetVarFullName(vp, fullName);
653 #endif
654         }
655         return state;
656 }
657
658 /******************************************************************************/
659 /*
660  *      Parse conditional expression (relational ops separated by ||, &&)
661  */
662
663 static int parseCond(Ejs *ep, int state, int flags)
664 {
665         MprVar          lhs, rhs;
666         int                     tid, operator;
667
668         mprAssert(ep);
669
670         mprDestroyVar(&ep->result);
671         rhs = lhs = mprCreateUndefinedVar();
672         operator = 0;
673
674         do {
675                 /*
676                  *      Recurse to handle one side of a conditional. Accumulate the
677                  *      left hand side and the final result in ep->result.
678                  */
679                 state = ejsParse(ep, EJS_STATE_RELEXP, flags);
680                 if (state != EJS_STATE_RELEXP_DONE) {
681                         state = EJS_STATE_ERR;
682                         break;
683                 }
684
685                 if (operator > 0) {
686                         mprCopyVar(&rhs, &ep->result, MPR_SHALLOW_COPY);
687                         if (evalCond(ep, &lhs, operator, &rhs) < 0) {
688                                 state = EJS_STATE_ERR;
689                                 break;
690                         }
691                 }
692                 mprCopyVar(&lhs, &ep->result, MPR_SHALLOW_COPY);
693
694                 tid = ejsLexGetToken(ep, state);
695                 if (tid == EJS_TOK_LOGICAL) {
696                         operator = (int) *ep->token;
697
698                 } else if (tid == EJS_TOK_RPAREN || tid == EJS_TOK_SEMI) {
699                         ejsLexPutbackToken(ep, tid, ep->token);
700                         state = EJS_STATE_COND_DONE;
701                         break;
702
703                 } else {
704                         ejsLexPutbackToken(ep, tid, ep->token);
705                 }
706                 tid = (state == EJS_STATE_RELEXP_DONE);
707
708         } while (state == EJS_STATE_RELEXP_DONE);
709
710         mprDestroyVar(&lhs);
711         mprDestroyVar(&rhs);
712         return state;
713 }
714
715 /******************************************************************************/
716 /*
717  *      Parse variable declaration list. Declarations can be of the following forms:
718  *              var x;
719  *              var x, y, z;
720  *              var x = 1 + 2 / 3, y = 2 + 4;
721  *
722  *      We set the variable to NULL if there is no associated assignment.
723  */
724
725 static int parseDeclaration(Ejs *ep, int state, int flags)
726 {
727         int             tid;
728
729         mprAssert(ep);
730
731         do {
732                 if ((tid = ejsLexGetToken(ep, state)) != EJS_TOK_ID) {
733                         return EJS_STATE_ERR;
734                 }
735                 ejsLexPutbackToken(ep, tid, ep->token);
736
737                 /*
738                  *      Parse the entire assignment or simple identifier declaration
739                  */
740                 if (ejsParse(ep, EJS_STATE_DEC, flags) != EJS_STATE_DEC_DONE) {
741                         return EJS_STATE_ERR;
742                 }
743
744                 /*
745                  *      Peek at the next token, continue if comma seen
746                  */
747                 tid = ejsLexGetToken(ep, state);
748                 if (tid == EJS_TOK_SEMI) {
749                         return EJS_STATE_DEC_LIST_DONE;
750                 } else if (tid != EJS_TOK_COMMA) {
751                         return EJS_STATE_ERR;
752                 }
753         } while (tid == EJS_TOK_COMMA);
754
755         if (tid != EJS_TOK_SEMI) {
756                 return EJS_STATE_ERR;
757         }
758         return EJS_STATE_DEC_LIST_DONE;
759 }
760
761 /******************************************************************************/
762 /*
763  *      Parse expression (leftHandSide operator rightHandSide)
764  */
765
766 static int parseExpr(Ejs *ep, int state, int flags)
767 {
768         MprVar          lhs, rhs;
769         int                     rel, tid;
770
771         mprAssert(ep);
772
773         mprDestroyVar(&ep->result);
774         rhs = lhs = mprCreateUndefinedVar();
775         rel = 0;
776         tid = 0;
777
778         do {
779                 /*
780                  *      This loop will handle an entire expression list. We call parse
781                  *      to evalutate each term which returns the result in ep->result.
782                  */
783                 if (tid == EJS_TOK_LOGICAL) {
784                         state = ejsParse(ep, EJS_STATE_RELEXP, flags);
785                         if (state != EJS_STATE_RELEXP_DONE) {
786                                 state = EJS_STATE_ERR;
787                                 break;
788                         }
789                 } else {
790                         tid = ejsLexGetToken(ep, state);
791                         if (tid == EJS_TOK_EXPR && (int) *ep->token == EJS_EXPR_MINUS) {
792                                 lhs = mprCreateIntegerVar(0);
793                                 rel = (int) *ep->token;
794                         } else {
795                                 ejsLexPutbackToken(ep, tid, ep->token);
796                         }
797
798                         state = ejsParse(ep, EJS_STATE_EXPR, flags);
799                         if (state != EJS_STATE_EXPR_DONE) {
800                                 state = EJS_STATE_ERR;
801                                 break;
802                         }
803                 }
804
805                 if (rel > 0) {
806                         mprCopyVar(&rhs, &ep->result, MPR_SHALLOW_COPY);
807                         if (tid == EJS_TOK_LOGICAL) {
808                                 if (evalCond(ep, &lhs, rel, &rhs) < 0) {
809                                         state = EJS_STATE_ERR;
810                                         break;
811                                 }
812                         } else {
813                                 if (evalExpr(ep, &lhs, rel, &rhs) < 0) {
814                                         state = EJS_STATE_ERR;
815                                         break;
816                                 }
817                         }
818                 }
819                 mprCopyVar(&lhs, &ep->result, MPR_SHALLOW_COPY);
820
821                 if ((tid = ejsLexGetToken(ep, state)) == EJS_TOK_EXPR ||
822                          tid == EJS_TOK_INC_DEC || tid == EJS_TOK_LOGICAL) {
823                         rel = (int) *ep->token;
824
825                 } else {
826                         ejsLexPutbackToken(ep, tid, ep->token);
827                         state = EJS_STATE_RELEXP_DONE;
828                 }
829
830         } while (state == EJS_STATE_EXPR_DONE);
831
832         mprDestroyVar(&lhs);
833         mprDestroyVar(&rhs);
834
835         return state;
836 }
837
838 /******************************************************************************/
839 /*
840  *      Parse the "for ... in" statement. Format for the statement is:
841  *
842  *              for (var in expr) {
843  *                      body;
844  *              }
845  */
846
847 static int parseForIn(Ejs *ep, int state, int flags)
848 {
849         EjsInput        endScript, bodyScript;
850         MprVar          *iteratorVar, *setVar, *vp, v;
851         int                     forFlags, tid;
852
853         mprAssert(ep);
854
855         tid = ejsLexGetToken(ep, state);
856         if (tid != EJS_TOK_ID) {
857                 return -1;
858         }
859         ejsLexPutbackToken(ep, tid, ep->token);
860
861         if (ejsParse(ep, EJS_STATE_EXPR, EJS_FLAGS_FOREACH | EJS_FLAGS_EXE)
862                         != EJS_STATE_EXPR_DONE) {
863                 return -1;
864         }
865         if (ep->currentProperty == 0) {
866                 return -1;
867         }
868         iteratorVar = ep->currentProperty;
869         
870         if (ejsLexGetToken(ep, state) != EJS_TOK_IN) {
871                 return -1;
872         }
873
874         /*
875          *      Get the set
876          */
877         tid = ejsLexGetToken(ep, state);
878         if (tid != EJS_TOK_ID) {
879                 return -1;
880         }
881         ejsLexPutbackToken(ep, tid, ep->token);
882
883         if (ejsParse(ep, EJS_STATE_EXPR, flags) != EJS_STATE_EXPR_DONE) {
884                 return -1;
885         }
886         if (ep->currentProperty == 0 && flags & EJS_FLAGS_EXE) {
887                 return -1;
888         }
889         setVar = ep->currentProperty;
890         
891         if (ejsLexGetToken(ep, state) != EJS_TOK_RPAREN) {
892                 return -1;
893         }
894
895         /*
896          *      Parse the body and remember the end of the body script
897          */
898         forFlags = flags & ~EJS_FLAGS_EXE;
899         ejsLexSaveInputState(ep, &bodyScript);
900         if (ejsParse(ep, EJS_STATE_STMT, forFlags) != EJS_STATE_STMT_DONE) {
901                 ejsLexFreeInputState(ep, &bodyScript);
902                 return -1;
903         }
904         ejsInitInputState(&endScript);
905         ejsLexSaveInputState(ep, &endScript);
906
907         /*
908          *      Now actually do the for loop.
909          */
910         if (flags & EJS_FLAGS_EXE) {
911                 if (setVar->type == MPR_TYPE_OBJECT) {
912                         vp = mprGetFirstProperty(setVar, MPR_ENUM_DATA);
913                         while (vp) {
914                                 if (strcmp(vp->name, "length") != 0) {
915                                         v = mprCreateStringVar(vp->name, 0);
916                                         if (mprWriteProperty(iteratorVar, &v) < 0) {
917                                                 ejsError(ep, "Can't write to variable\n");
918                                                 ejsLexFreeInputState(ep, &bodyScript);
919                                                 ejsLexFreeInputState(ep, &endScript);
920                                                 return -1;
921                                         }
922
923                                         ejsLexRestoreInputState(ep, &bodyScript);
924                                         switch (ejsParse(ep, EJS_STATE_STMT, flags)) {
925                                         case EJS_STATE_RET:
926                                                 return EJS_STATE_RET;
927                                         case EJS_STATE_STMT_DONE:
928                                                 break;
929                                         default:
930                                                 ejsLexFreeInputState(ep, &endScript);
931                                                 ejsLexFreeInputState(ep, &bodyScript);
932                                                 return -1;
933                                         }
934                                 }
935                                 vp = mprGetNextProperty(setVar, vp, MPR_ENUM_DATA);
936                         }
937                 } else {
938                         ejsError(ep, "Variable \"%s\" is not an array or object", 
939                                 setVar->name);
940                         ejsLexFreeInputState(ep, &endScript);
941                         ejsLexFreeInputState(ep, &bodyScript);
942                         return -1;
943                 }
944         }
945         ejsLexRestoreInputState(ep, &endScript);
946
947         ejsLexFreeInputState(ep, &endScript);
948         ejsLexFreeInputState(ep, &bodyScript);
949
950         return state;
951 }
952
953 /******************************************************************************/
954 /*
955  *      Parse the for statement. Format for the expression is:
956  *
957  *              for (initial; condition; incr) {
958  *                      body;
959  *              }
960  */
961
962 static int parseFor(Ejs *ep, int state, int flags)
963 {
964         EjsInput        condScript, endScript, bodyScript, incrScript;
965         int                     forFlags, cond;
966
967         ejsInitInputState(&endScript);
968         ejsInitInputState(&bodyScript);
969         ejsInitInputState(&incrScript);
970         ejsInitInputState(&condScript);
971
972         mprAssert(ep);
973
974         /*
975          *      Evaluate the for loop initialization statement
976          */
977         if (ejsParse(ep, EJS_STATE_EXPR, flags) != EJS_STATE_EXPR_DONE) {
978                 return -1;
979         }
980         if (ejsLexGetToken(ep, state) != EJS_TOK_SEMI) {
981                 return -1;
982         }
983
984         /*
985          *      The first time through, we save the current input context just prior
986          *      to each step: prior to the conditional, the loop increment and 
987          *      the loop body.
988          */
989         ejsLexSaveInputState(ep, &condScript);
990         if (ejsParse(ep, EJS_STATE_COND, flags) != EJS_STATE_COND_DONE) {
991                 goto error;
992         }
993         cond = (ep->result.boolean != 0);
994
995         if (ejsLexGetToken(ep, state) != EJS_TOK_SEMI) {
996                 goto error;
997         }
998
999         /*
1000          *      Don't execute the loop increment statement or the body 
1001          *      first time.
1002          */
1003         forFlags = flags & ~EJS_FLAGS_EXE;
1004         ejsLexSaveInputState(ep, &incrScript);
1005         if (ejsParse(ep, EJS_STATE_EXPR, forFlags) != EJS_STATE_EXPR_DONE) {
1006                 goto error;
1007         }
1008         if (ejsLexGetToken(ep, state) != EJS_TOK_RPAREN) {
1009                 goto error;
1010         }
1011
1012         /*
1013          *      Parse the body and remember the end of the body script
1014          */
1015         ejsLexSaveInputState(ep, &bodyScript);
1016         if (ejsParse(ep, EJS_STATE_STMT, forFlags) != EJS_STATE_STMT_DONE) {
1017                 goto error;
1018         }
1019         ejsLexSaveInputState(ep, &endScript);
1020
1021         /*
1022          *      Now actually do the for loop. Note loop has been rotated
1023          */
1024         while (cond && (flags & EJS_FLAGS_EXE)) {
1025                 /*
1026                  *      Evaluate the body
1027                  */
1028                 ejsLexRestoreInputState(ep, &bodyScript);
1029
1030                 switch (ejsParse(ep, EJS_STATE_STMT, flags)) {
1031                 case EJS_STATE_RET:
1032                         return EJS_STATE_RET;
1033                 case EJS_STATE_STMT_DONE:
1034                         break;
1035                 default:
1036                         goto error;
1037                 }
1038                 /*
1039                  *      Evaluate the increment script
1040                  */
1041                 ejsLexRestoreInputState(ep, &incrScript);
1042                 if (ejsParse(ep, EJS_STATE_EXPR, flags) != EJS_STATE_EXPR_DONE){
1043                         goto error;
1044                 }
1045                 /*
1046                  *      Evaluate the condition
1047                  */
1048                 ejsLexRestoreInputState(ep, &condScript);
1049                 if (ejsParse(ep, EJS_STATE_COND, flags) != EJS_STATE_COND_DONE) {
1050                         goto error;
1051                 }
1052                 mprAssert(ep->result.type == MPR_TYPE_BOOL);
1053                 cond = (ep->result.boolean != 0);
1054         }
1055
1056         ejsLexRestoreInputState(ep, &endScript);
1057
1058 done:
1059         ejsLexFreeInputState(ep, &condScript);
1060         ejsLexFreeInputState(ep, &incrScript);
1061         ejsLexFreeInputState(ep, &endScript);
1062         ejsLexFreeInputState(ep, &bodyScript);
1063         return state;
1064
1065 error:
1066         state = EJS_STATE_ERR;
1067         goto done;
1068 }
1069
1070 /******************************************************************************/
1071 /*
1072  *      Parse a function declaration
1073  */
1074
1075 static int parseFunctionDec(Ejs *ep, int state, int flags)
1076 {
1077         EjsInput        endScript, bodyScript;
1078         MprVar          v, *currentObj, *vp;
1079         char            *procName;
1080         int                     len, tid, bodyFlags;
1081
1082         mprAssert(ep);
1083         mprAssert(ejsPtr(ep->eid));
1084
1085         /*      
1086          *      function <name>(arg, arg, arg) { body };
1087          *      function name(arg, arg, arg) { body };
1088          */
1089
1090         tid = ejsLexGetToken(ep, state);
1091         if (tid == EJS_TOK_ID) {
1092                 procName = mprStrdup(ep->token);
1093                 tid = ejsLexGetToken(ep, state);
1094         }  else {
1095                 procName = 0;
1096         }
1097         if (tid != EJS_TOK_LPAREN) {
1098                 mprFree(procName);
1099                 return EJS_STATE_ERR;
1100         }
1101
1102         /*
1103          *      Hand craft the function value structure.
1104          */
1105         v = mprCreateFunctionVar(0, 0, 0);
1106         tid = ejsLexGetToken(ep, state);
1107         while (tid == EJS_TOK_ID) {
1108                 mprAddToArray(v.function.args, mprStrdup(ep->token));
1109                 tid = ejsLexGetToken(ep, state);
1110                 if (tid == EJS_TOK_RPAREN || tid != EJS_TOK_COMMA) {
1111                         break;
1112                 }
1113                 tid = ejsLexGetToken(ep, state);
1114         }
1115         if (tid != EJS_TOK_RPAREN) {
1116                 mprFree(procName);
1117                 mprDestroyVar(&v);
1118                 return EJS_STATE_ERR;
1119         }
1120
1121         /* Allow new lines before opening brace */
1122         do {
1123                 tid = ejsLexGetToken(ep, state);
1124         } while (tid == EJS_TOK_NEWLINE);
1125
1126         if (tid != EJS_TOK_LBRACE) {
1127                 mprFree(procName);
1128                 mprDestroyVar(&v);
1129                 return EJS_STATE_ERR;
1130         }
1131         
1132         /* 
1133          *      Register the function name early to allow for recursive
1134          *      function calls (see note in ECMA standard, page 71) 
1135          */
1136         if (!(flags & EJS_FLAGS_ASSIGNMENT)) {
1137                 currentObj = ejsFindObj(ep, 0, procName, flags);
1138                 vp = mprSetProperty(currentObj, procName, &v);
1139         }
1140         
1141         /*
1142          *      Parse the function body. Turn execute off.
1143          */
1144         bodyFlags = flags & ~EJS_FLAGS_EXE;
1145         ejsLexSaveInputState(ep, &bodyScript);
1146
1147         do {
1148                 state = ejsParse(ep, EJS_STATE_STMT, bodyFlags);
1149         } while (state == EJS_STATE_STMT_DONE);
1150
1151         tid = ejsLexGetToken(ep, state);
1152         if (state != EJS_STATE_STMT_BLOCK_DONE || tid != EJS_TOK_RBRACE) {
1153                 mprFree(procName);
1154                 mprDestroyVar(&v);
1155                 ejsLexFreeInputState(ep, &bodyScript);
1156                 return EJS_STATE_ERR;
1157         }
1158         ejsLexSaveInputState(ep, &endScript);
1159
1160         /*
1161          *      Save the function body between the starting and ending parse positions.
1162          *      Overwrite the trailing '}' with a null.
1163          */
1164         len = endScript.scriptServp - bodyScript.scriptServp;
1165         v.function.body = mprMalloc(len + 1);
1166         memcpy(v.function.body, bodyScript.scriptServp, len);
1167
1168         if (len <= 0) {
1169                 v.function.body[0] = '\0';
1170         } else {
1171                 v.function.body[len - 1] = '\0';
1172         }
1173         ejsLexFreeInputState(ep, &bodyScript);
1174         ejsLexFreeInputState(ep, &endScript);
1175
1176         /*
1177          *      If we are in an assignment, don't register the function name, rather
1178          *      return the function structure in the parser result.
1179          */
1180         if (flags & EJS_FLAGS_ASSIGNMENT) {
1181                 mprCopyVar(&ep->result, &v, MPR_SHALLOW_COPY);
1182         } else {
1183                 currentObj = ejsFindObj(ep, 0, procName, flags);
1184                 vp = mprSetProperty(currentObj, procName, &v);
1185         }
1186
1187         mprFree(procName);
1188         mprDestroyVar(&v);
1189
1190         return EJS_STATE_STMT;
1191 }
1192
1193 /******************************************************************************/
1194 /*
1195  *      Parse a function name and invoke the function
1196  */
1197
1198 static int parseFunction(Ejs *ep, int state, int flags, char *id)
1199 {
1200         EjsProc         proc, *saveProc;
1201         MprVar          *saveObj;
1202
1203         /*
1204          *      Must save any current ep->proc value for the current stack frame
1205          *      to allow for recursive function calls.
1206          */
1207         saveProc = (ep->proc) ? ep->proc: 0;
1208
1209         memset(&proc, 0, sizeof(EjsProc));
1210         proc.procName = mprStrdup(id);
1211         proc.fn = ep->currentProperty;
1212         proc.args = mprCreateArray();
1213         ep->proc = &proc;
1214
1215         mprDestroyVar(&ep->result);
1216
1217         saveObj = ep->currentObj;
1218         if (ejsParse(ep, EJS_STATE_ARG_LIST, flags) != EJS_STATE_ARG_LIST_DONE) {
1219                 freeProc(&proc);
1220                 ep->proc = saveProc;
1221                 return -1;
1222         }
1223         ep->currentObj = saveObj;
1224
1225         /*
1226          *      Evaluate the function if required
1227          */
1228         if (flags & EJS_FLAGS_EXE) {
1229                 if (evalFunction(ep, ep->currentObj, flags) < 0) {
1230                         freeProc(&proc);
1231                         ep->proc = saveProc;
1232                         return -1;
1233                 }
1234         }
1235
1236         freeProc(&proc);
1237         ep->proc = saveProc;
1238
1239         if (ejsLexGetToken(ep, state) != EJS_TOK_RPAREN) {
1240                 return -1;
1241         }
1242         return state;
1243 }
1244
1245 /******************************************************************************/
1246 /*
1247  *      Parse an identifier. This is a segment of a fully qualified variable.
1248  *      May come here for an initial identifier or for property names
1249  *      after a "." or "[...]".
1250  */
1251
1252 static int parseId(Ejs *ep, int state, int flags, char **id, char **fullName, 
1253         int *fullNameLen, int *done)
1254 {
1255         int             tid;
1256
1257         mprFree(*id);
1258         *id = mprStrdup(ep->token);
1259 #if BLD_DEBUG
1260         *fullNameLen = mprReallocStrcat(fullName, MPR_MAX_VAR, *fullNameLen,
1261                 0, *id, 0);
1262 #endif
1263         if (ep->currentObj == 0) {
1264                 ep->currentObj = ejsFindObj(ep, state, *id, flags);
1265         }
1266
1267         /*
1268          *      Find the referenced variable and store it in currentProperty.
1269           */
1270         ep->currentProperty = ejsFindProperty(ep, state, ep->currentObj, 
1271                 *id, flags);
1272         updateResult(ep, state, flags, ep->currentProperty);
1273
1274 #if BLD_DEBUG
1275         if (ep->currentProperty && (ep->currentProperty->name == 0 || 
1276                         ep->currentProperty->name[0] == '\0')) {
1277                 mprSetVarName(ep->currentProperty, *id);
1278         }
1279 #endif
1280
1281         tid = ejsLexGetToken(ep, state);
1282         if (tid == EJS_TOK_LPAREN) {
1283                 if (ep->currentProperty == 0 && (flags & EJS_FLAGS_EXE)) {
1284                         ejsError(ep, "Function name not defined \"%s\"\n", *id);
1285                         return -1;
1286                 }
1287                 ejsLexPutbackToken(ep, EJS_TOK_FUNCTION_NAME, ep->token);
1288                 return state;
1289         }
1290
1291         if (tid == EJS_TOK_PERIOD || tid == EJS_TOK_LBRACKET || 
1292                         tid == EJS_TOK_ASSIGNMENT || tid == EJS_TOK_INC_DEC) {
1293                 ejsLexPutbackToken(ep, tid, ep->token);
1294                 return state;
1295         }
1296
1297         /*
1298          *      Only come here for variable access and declarations.
1299          *      Assignment handled elsewhere.
1300          */
1301         if (flags & EJS_FLAGS_EXE) {
1302                 if (state == EJS_STATE_DEC) {
1303                         /*
1304                          *      Declare a variable. Standard allows: var x ; var x ;
1305                          */
1306 #if DISABLED
1307                         if (ep->currentProperty != 0) {
1308                                 ejsError(ep, "Variable already defined \"%s\"\n", *id);
1309                                 return -1;
1310                         }
1311 #endif
1312                         /*
1313                          *      Create or overwrite if it already exists
1314                          */
1315                         mprSetPropertyValue(ep->currentObj, *id, 
1316                                 mprCreateUndefinedVar());
1317                         ep->currentProperty = 0;
1318                         mprDestroyVar(&ep->result);
1319
1320                 } else if (flags & EJS_FLAGS_FOREACH) {
1321                         if (ep->currentProperty == 0) {
1322                                 ep->currentProperty = 
1323                                         mprCreatePropertyValue(ep->currentObj, *id, 
1324                                                 mprCreateUndefinedVar());
1325                         }
1326
1327                 } else {
1328                         if (ep->currentProperty == 0) {
1329                                 if (ep->currentObj == ep->global || 
1330                                                 ep->currentObj == ep->local) {
1331                                         ejsError(ep, "Undefined variable \"%s\"\n", *id);
1332                                         return -1;
1333                                 }
1334                                 ep->currentProperty = mprCreatePropertyValue(ep->currentObj, 
1335                                         *id, mprCreateUndefinedVar());
1336                         }
1337                 }
1338         }
1339         ejsLexPutbackToken(ep, tid, ep->token);
1340         if (tid == EJS_TOK_RBRACKET || tid == EJS_TOK_COMMA || 
1341                         tid == EJS_TOK_IN) {
1342                 *done = 1;
1343         }
1344         return state;
1345 }
1346
1347 /******************************************************************************/
1348 /*
1349  *      Parse an "if" statement
1350  */
1351
1352 static int parseIf(Ejs *ep, int state, int flags, int *done)
1353 {
1354         bool    ifResult;
1355         int             thenFlags, elseFlags, tid;
1356
1357         if (state != EJS_STATE_STMT) {
1358                 return -1;
1359         }
1360         if (ejsLexGetToken(ep, state) != EJS_TOK_LPAREN) {
1361                 return -1;
1362         }
1363
1364         /*
1365          *      Evaluate the entire condition list "(condition)"
1366          */
1367         if (ejsParse(ep, EJS_STATE_COND, flags) != EJS_STATE_COND_DONE) {
1368                 return -1;
1369         }
1370         if (ejsLexGetToken(ep, state) != EJS_TOK_RPAREN) {
1371                 return -1;
1372         }
1373
1374         /*
1375          *      This is the "then" case. We need to always parse both cases and
1376          *      execute only the relevant case.
1377          */
1378         ifResult = mprVarToBool(&ep->result);
1379         if (ifResult) {
1380                 thenFlags = flags;
1381                 elseFlags = flags & ~EJS_FLAGS_EXE;
1382         } else {
1383                 thenFlags = flags & ~EJS_FLAGS_EXE;
1384                 elseFlags = flags;
1385         }
1386
1387         /*
1388          *      Process the "then" case.
1389          */
1390         switch (ejsParse(ep, EJS_STATE_STMT, thenFlags)) {
1391         case EJS_STATE_RET:
1392                 state = EJS_STATE_RET;
1393                 return state;
1394         case EJS_STATE_STMT_DONE:
1395                 break;
1396         default:
1397                 return -1;
1398         }
1399
1400         /*
1401          *      Check to see if there is an "else" case
1402          */
1403         removeNewlines(ep, state);
1404         tid = ejsLexGetToken(ep, state);
1405         if (tid != EJS_TOK_ELSE) {
1406                 ejsLexPutbackToken(ep, tid, ep->token);
1407                 *done = 1;
1408                 return state;
1409         }
1410
1411         /*
1412          *      Process the "else" case.
1413          */
1414         switch (ejsParse(ep, EJS_STATE_STMT, elseFlags)) {
1415         case EJS_STATE_RET:
1416                 state = EJS_STATE_RET;
1417                 return state;
1418         case EJS_STATE_STMT_DONE:
1419                 break;
1420         default:
1421                 return -1;
1422         }
1423         *done = 1;
1424         return state;
1425 }
1426
1427 /******************************************************************************/
1428 /*
1429  *      Parse an "++" or "--" statement
1430  */
1431
1432 static int parseInc(Ejs *ep, int state, int flags)
1433 {
1434         MprVar  one;
1435
1436         if (! (flags & EJS_FLAGS_EXE)) {
1437                 return state;
1438         }
1439
1440         if (ep->currentProperty == 0) {
1441                 ejsError(ep, "Undefined variable \"%s\"\n", ep->token);
1442                 return -1;
1443         }
1444         one = mprCreateIntegerVar(1);
1445         if (evalExpr(ep, ep->currentProperty, (int) *ep->token, 
1446                         &one) < 0) {
1447                 return -1;
1448         }
1449         if (mprWriteProperty(ep->currentProperty, &ep->result) < 0) {
1450                 ejsError(ep, "Can't write to variable\n");
1451                 return -1;
1452         }
1453         return state;
1454 }
1455
1456 /******************************************************************************/
1457 /*
1458  *      Evaluate a condition. Implements &&, ||, !. Returns with a boolean result
1459  *      in ep->result. Returns -1 on errors, zero if successful.
1460  */
1461
1462 static int evalCond(Ejs *ep, MprVar *lhs, int rel, MprVar *rhs)
1463 {
1464         bool    l, r, lval;
1465
1466         mprAssert(rel > 0);
1467
1468         l = mprVarToBool(lhs);
1469         r = mprVarToBool(rhs);
1470
1471         switch (rel) {
1472         case EJS_COND_AND:
1473                 lval = l && r;
1474                 break;
1475         case EJS_COND_OR:
1476                 lval = l || r;
1477                 break;
1478         default:
1479                 ejsError(ep, "Bad operator %d", rel);
1480                 return -1;
1481         }
1482
1483         mprCopyVarValue(&ep->result, mprCreateBoolVar(lval), 0);
1484         return 0;
1485 }
1486
1487 /******************************************************************************/
1488 /*
1489  *      Evaluate an operation. Returns with the result in ep->result. Returns -1
1490  *      on errors, otherwise zero is returned.
1491  */
1492
1493 static int evalExpr(Ejs *ep, MprVar *lhs, int rel, MprVar *rhs)
1494 {
1495         char    *str;
1496         MprNum  lval, num;
1497         int             rc;
1498
1499         mprAssert(rel > 0);
1500         str = 0;
1501         lval = 0;
1502
1503         /*
1504          *      Type conversion. This is tricky and must be according to the standard.
1505          *      Only numbers (including floats) and strings can be compared. All other
1506          *      types are first converted to numbers by preference and if that fails,
1507          *      to strings.
1508          *
1509          *      First convert objects to comparable types. The "===" operator will
1510          *      test the sameness of object references. Here, we coerce to comparable
1511          *      types first.
1512          */
1513         if (lhs->type == MPR_TYPE_OBJECT) {
1514                 if (ejsRunFunction(ep->eid, lhs, "toValue", 0) == 0) {
1515                         mprCopyVar(lhs, &ep->result, MPR_SHALLOW_COPY);
1516                 } else {
1517                         if (ejsRunFunction(ep->eid, lhs, "toString", 0) == 0) {
1518                                 mprCopyVar(lhs, &ep->result, MPR_SHALLOW_COPY);
1519                         }
1520                 }
1521                 /* Nothing more can be done */
1522         }
1523
1524         if (rhs->type == MPR_TYPE_OBJECT) {
1525                 if (ejsRunFunction(ep->eid, rhs, "toValue", 0) == 0) {
1526                         mprCopyVar(rhs, &ep->result, MPR_SHALLOW_COPY);
1527                 } else {
1528                         if (ejsRunFunction(ep->eid, rhs, "toString", 0) == 0) {
1529                                 mprCopyVar(rhs, &ep->result, MPR_SHALLOW_COPY);
1530                         }
1531                 }
1532                 /* Nothing more can be done */
1533         }
1534
1535         /*
1536          *      From here on, lhs and rhs may contain allocated data (strings), so 
1537          *      we must always destroy before overwriting.
1538          */
1539         
1540         /*
1541          *      Only allow a few bool operations. Otherwise convert to number.
1542          */
1543         if (lhs->type == MPR_TYPE_BOOL && rhs->type == MPR_TYPE_BOOL &&
1544                         (rel != EJS_EXPR_EQ && rel != EJS_EXPR_NOTEQ &&
1545                         rel != EJS_EXPR_BOOL_COMP)) {
1546                 num = mprVarToNumber(lhs);
1547                 mprDestroyVar(lhs);
1548                 *lhs = mprCreateNumberVar(num);
1549         }
1550
1551         /*
1552          *      Types do not match, so try to coerce the right operand to match the left
1553          *      But first, try to convert a left operand that is a numeric stored as a
1554          *      string, into a numeric.
1555          */
1556         if (lhs->type != rhs->type) {
1557                 if (lhs->type == MPR_TYPE_STRING) {
1558                         if (isdigit((int) lhs->string[0])) {
1559                                 num = mprVarToNumber(lhs);
1560                                 lhs->allocatedVar = 0;
1561                                 mprDestroyVar(lhs);
1562                                 *lhs = mprCreateNumberVar(num);
1563                                 /* Examine further below */
1564
1565                         } else {
1566                                 /*
1567                                  *      Convert the RHS to a string
1568                                  */
1569                                 mprVarToString(&str, MPR_MAX_STRING, 0, rhs);
1570                                 rhs->allocatedVar = 0;
1571                                 mprDestroyVar(rhs);
1572                                 *rhs = mprCreateStringVar(str, 1);
1573                                 mprFree(str);
1574                         }
1575
1576 #if BLD_FEATURE_FLOATING_POINT
1577                 } else if (lhs->type == MPR_TYPE_FLOAT) {
1578                         /*
1579                          *      Convert rhs to floating
1580                          */
1581                         double f = mprVarToFloat(rhs);
1582                         mprDestroyVar(rhs);
1583                         *rhs = mprCreateFloatVar(f);
1584
1585 #endif
1586 #if BLD_FEATURE_INT64
1587                 } else if (lhs->type == MPR_TYPE_INT64) {
1588                         /*
1589                          *      Convert the rhs to 64 bit
1590                          */
1591                         int64 n = mprVarToInteger64(rhs);
1592                         mprDestroyVar(rhs);
1593                         *rhs = mprCreateInteger64Var(n);
1594 #endif
1595                 } else if (lhs->type == MPR_TYPE_BOOL || lhs->type == MPR_TYPE_INT) {
1596
1597                         if (rhs->type == MPR_TYPE_STRING) {
1598                                 /*
1599                                  *      Convert to lhs to a string
1600                                  */
1601                                 mprVarToString(&str, MPR_MAX_STRING, 0, lhs);
1602                                 mprDestroyVar(lhs);
1603                                 *lhs = mprCreateStringVar(str, 1);
1604                                 mprFree(str);
1605
1606 #if BLD_FEATURE_FLOATING_POINT
1607                         } else if (rhs->type == MPR_TYPE_FLOAT) {
1608                                 /*
1609                                  *      Convert lhs to floating
1610                                  */
1611                                 double f = mprVarToFloat(lhs);
1612                                 mprDestroyVar(lhs);
1613                                 *lhs = mprCreateFloatVar(f);
1614 #endif
1615
1616                         } else {
1617                                 /*
1618                                  *      Convert both operands to numbers
1619                                  */
1620                                 num = mprVarToNumber(lhs);
1621                                 mprDestroyVar(lhs);
1622                                 *lhs = mprCreateNumberVar(num);
1623
1624                                 num = mprVarToNumber(rhs);
1625                                 mprDestroyVar(rhs);
1626                                 *rhs = mprCreateNumberVar(num);
1627                         }
1628                 }
1629         }
1630
1631         /*
1632          *      We have failed to coerce the types to be the same. Special case here
1633          *      for undefined and null. We need to allow comparisions against these
1634          *      special values.
1635          */
1636         if (lhs->type == MPR_TYPE_UNDEFINED || lhs->type == MPR_TYPE_NULL) {
1637                 switch (rel) {
1638                 case EJS_EXPR_EQ:
1639                         lval = lhs->type == rhs->type;
1640                         break;
1641                 case EJS_EXPR_NOTEQ:
1642                         lval = lhs->type != rhs->type;
1643                         break;
1644                 default:
1645                         lval = 0;
1646                 }
1647                 mprCopyVarValue(&ep->result, mprCreateBoolVar((bool) lval), 0);
1648                 return 0;
1649         }
1650
1651         /*
1652          *      Types are the same here
1653          */
1654         switch (lhs->type) {
1655         default:
1656         case MPR_TYPE_UNDEFINED:
1657         case MPR_TYPE_NULL:
1658                 /* Should be handled above */
1659                 mprAssert(0);
1660                 return 0;
1661
1662         case MPR_TYPE_STRING_CFUNCTION:
1663         case MPR_TYPE_CFUNCTION:
1664         case MPR_TYPE_FUNCTION:
1665         case MPR_TYPE_OBJECT:
1666                 mprCopyVarValue(&ep->result, mprCreateBoolVar(0), 0);
1667                 return 0;
1668
1669         case MPR_TYPE_PTR:
1670                 mprCopyVarValue(&ep->result, mprCreateBoolVar(lhs->ptr == rhs->ptr), 0);
1671                 return 0;
1672
1673         case MPR_TYPE_BOOL:
1674                 rc = evalBoolExpr(ep, lhs->boolean, rel, rhs->boolean);
1675                 break;
1676
1677 #if BLD_FEATURE_FLOATING_POINT
1678         case MPR_TYPE_FLOAT:
1679                 rc = evalFloatExpr(ep, lhs->floating, rel, rhs->floating);
1680                 break;
1681 #endif
1682
1683         case MPR_TYPE_INT:
1684                 rc = evalNumericExpr(ep, (MprNum) lhs->integer, rel, 
1685                         (MprNum) rhs->integer);
1686                 break;
1687
1688 #if BLD_FEATURE_INT64
1689         case MPR_TYPE_INT64:
1690                 rc = evalNumericExpr(ep, (MprNum) lhs->integer64, rel, 
1691                         (MprNum) rhs->integer64);
1692                 break;
1693 #endif
1694
1695         case MPR_TYPE_STRING:
1696                 rc = evalStringExpr(ep, lhs, rel, rhs);
1697         }
1698         return rc;
1699 }
1700
1701 /******************************************************************************/
1702 #if BLD_FEATURE_FLOATING_POINT
1703 /*
1704  *      Expressions with floating operands
1705  */
1706
1707 static int evalFloatExpr(Ejs *ep, double l, int rel, double r) 
1708 {
1709         double  lval;
1710         bool    logical;
1711
1712         lval = 0;
1713         logical = 0;
1714
1715         switch (rel) {
1716         case EJS_EXPR_PLUS:
1717                 lval = l + r;
1718                 break;
1719         case EJS_EXPR_INC:
1720                 lval = l + 1;
1721                 break;
1722         case EJS_EXPR_MINUS:
1723                 lval = l - r;
1724                 break;
1725         case EJS_EXPR_DEC:
1726                 lval = l - 1;
1727                 break;
1728         case EJS_EXPR_MUL:
1729                 lval = l * r;
1730                 break;
1731         case EJS_EXPR_DIV:
1732                 lval = l / r;
1733                 break;
1734         default:
1735                 logical++;
1736                 break;
1737         }
1738
1739         /*
1740          *      Logical operators
1741          */
1742         if (logical) {
1743
1744                 switch (rel) {
1745                 case EJS_EXPR_EQ:
1746                         lval = l == r;
1747                         break;
1748                 case EJS_EXPR_NOTEQ:
1749                         lval = l != r;
1750                         break;
1751                 case EJS_EXPR_LESS:
1752                         lval = (l < r) ? 1 : 0;
1753                         break;
1754                 case EJS_EXPR_LESSEQ:
1755                         lval = (l <= r) ? 1 : 0;
1756                         break;
1757                 case EJS_EXPR_GREATER:
1758                         lval = (l > r) ? 1 : 0;
1759                         break;
1760                 case EJS_EXPR_GREATEREQ:
1761                         lval = (l >= r) ? 1 : 0;
1762                         break;
1763                 case EJS_EXPR_BOOL_COMP:
1764                         lval = (r == 0) ? 1 : 0;
1765                         break;
1766                 default:
1767                         ejsError(ep, "Bad operator %d", rel);
1768                         return -1;
1769                 }
1770                 mprCopyVarValue(&ep->result, mprCreateBoolVar(lval != 0), 0);
1771
1772         } else {
1773                 mprCopyVarValue(&ep->result, mprCreateFloatVar(lval), 0);
1774         }
1775         return 0;
1776 }
1777
1778 #endif /* BLD_FEATURE_FLOATING_POINT */
1779 /******************************************************************************/
1780 /*
1781  *      Expressions with boolean operands
1782  */
1783
1784 static int evalBoolExpr(Ejs *ep, bool l, int rel, bool r) 
1785 {
1786         bool    lval;
1787
1788         switch (rel) {
1789         case EJS_EXPR_EQ:
1790                 lval = l == r;
1791                 break;
1792         case EJS_EXPR_NOTEQ:
1793                 lval = l != r;
1794                 break;
1795         case EJS_EXPR_BOOL_COMP:
1796                 lval = (r == 0) ? 1 : 0;
1797                 break;
1798         default:
1799                 ejsError(ep, "Bad operator %d", rel);
1800                 return -1;
1801         }
1802         mprCopyVarValue(&ep->result, mprCreateBoolVar(lval), 0);
1803         return 0;
1804 }
1805
1806 /******************************************************************************/
1807 /*
1808  *      Expressions with numeric operands
1809  */
1810
1811 static int evalNumericExpr(Ejs *ep, MprNum l, int rel, MprNum r) 
1812 {
1813         MprNum  lval;
1814         bool    logical;
1815
1816         lval = 0;
1817         logical = 0;
1818
1819         switch (rel) {
1820         case EJS_EXPR_PLUS:
1821                 lval = l + r;
1822                 break;
1823         case EJS_EXPR_INC:
1824                 lval = l + 1;
1825                 break;
1826         case EJS_EXPR_MINUS:
1827                 lval = l - r;
1828                 break;
1829         case EJS_EXPR_DEC:
1830                 lval = l - 1;
1831                 break;
1832         case EJS_EXPR_MUL:
1833                 lval = l * r;
1834                 break;
1835         case EJS_EXPR_DIV:
1836                 if (r != 0) {
1837                         lval = l / r;
1838                 } else {
1839                         ejsError(ep, "Divide by zero");
1840                         return -1;
1841                 }
1842                 break;
1843         case EJS_EXPR_MOD:
1844                 if (r != 0) {
1845                         lval = l % r;
1846                 } else {
1847                         ejsError(ep, "Modulo zero");
1848                         return -1;
1849                 }
1850                 break;
1851         case EJS_EXPR_LSHIFT:
1852                 lval = l << r;
1853                 break;
1854         case EJS_EXPR_RSHIFT:
1855                 lval = l >> r;
1856                 break;
1857
1858         default:
1859                 logical++;
1860                 break;
1861         }
1862
1863         /*
1864          *      Logical operators
1865          */
1866         if (logical) {
1867
1868                 switch (rel) {
1869                 case EJS_EXPR_EQ:
1870                         lval = l == r;
1871                         break;
1872                 case EJS_EXPR_NOTEQ:
1873                         lval = l != r;
1874                         break;
1875                 case EJS_EXPR_LESS:
1876                         lval = (l < r) ? 1 : 0;
1877                         break;
1878                 case EJS_EXPR_LESSEQ:
1879                         lval = (l <= r) ? 1 : 0;
1880                         break;
1881                 case EJS_EXPR_GREATER:
1882                         lval = (l > r) ? 1 : 0;
1883                         break;
1884                 case EJS_EXPR_GREATEREQ:
1885                         lval = (l >= r) ? 1 : 0;
1886                         break;
1887                 case EJS_EXPR_BOOL_COMP:
1888                         lval = (r == 0) ? 1 : 0;
1889                         break;
1890                 default:
1891                         ejsError(ep, "Bad operator %d", rel);
1892                         return -1;
1893                 }
1894                 mprCopyVarValue(&ep->result, mprCreateBoolVar(lval != 0), 0);
1895
1896         } else {
1897                 mprCopyVarValue(&ep->result, mprCreateNumberVar(lval), 0);
1898         }
1899         return 0;
1900 }
1901
1902 /******************************************************************************/
1903 /*
1904  *      Expressions with string operands
1905  */
1906
1907 static int evalStringExpr(Ejs *ep, MprVar *lhs, int rel, MprVar *rhs)
1908 {
1909         int             lval;
1910
1911         mprAssert(ep);
1912         mprAssert(lhs);
1913         mprAssert(rhs);
1914
1915         switch (rel) {
1916         case EJS_EXPR_LESS:
1917                 lval = strcmp(lhs->string, rhs->string) < 0;
1918                 break;
1919         case EJS_EXPR_LESSEQ:
1920                 lval = strcmp(lhs->string, rhs->string) <= 0;
1921                 break;
1922         case EJS_EXPR_GREATER:
1923                 lval = strcmp(lhs->string, rhs->string) > 0;
1924                 break;
1925         case EJS_EXPR_GREATEREQ:
1926                 lval = strcmp(lhs->string, rhs->string) >= 0;
1927                 break;
1928         case EJS_EXPR_EQ:
1929                 lval = strcmp(lhs->string, rhs->string) == 0;
1930                 break;
1931         case EJS_EXPR_NOTEQ:
1932                 lval = strcmp(lhs->string, rhs->string) != 0;
1933                 break;
1934         case EJS_EXPR_PLUS:
1935                 /*
1936                  *      This differs from all the above operations. We append rhs to lhs.
1937                  */
1938                 mprDestroyVar(&ep->result);
1939                 appendValue(&ep->result, lhs);
1940                 appendValue(&ep->result, rhs);
1941                 return 0;
1942
1943         case EJS_EXPR_INC:
1944         case EJS_EXPR_DEC:
1945         case EJS_EXPR_MINUS:
1946         case EJS_EXPR_DIV:
1947         case EJS_EXPR_MOD:
1948         case EJS_EXPR_LSHIFT:
1949         case EJS_EXPR_RSHIFT:
1950         default:
1951                 ejsError(ep, "Bad operator");
1952                 return -1;
1953         }
1954
1955         mprCopyVarValue(&ep->result, mprCreateBoolVar(lval), 0);
1956         return 0;
1957 }
1958
1959 /******************************************************************************/
1960 /*
1961  *      Evaluate a function. obj is set to the current object if a function is being
1962  *      run.
1963  */
1964
1965 static int evalFunction(Ejs *ep, MprVar *obj, int flags)
1966 {
1967         EjsProc         *proc;
1968         MprVar          arguments, callee, thisObject, *prototype, **argValues;
1969         MprArray        *formalArgs, *actualArgs;
1970         char            buf[16], **argNames, **argBuf;
1971         int                     i, rc, fid;
1972
1973         mprAssert(ep); 
1974         mprAssert(ejsPtr(ep->eid));
1975
1976         rc = -1;
1977         proc = ep->proc;
1978         prototype = proc->fn;
1979         actualArgs = proc->args;
1980         argValues = (MprVar**) actualArgs->handles;
1981
1982         /*
1983          *      Create a new variable stack frame. ie. new local variables.
1984          */
1985         fid = ejsOpenBlock(ep->eid);
1986
1987         if (flags & EJS_FLAGS_NEW) {
1988                 /*
1989                  *      Create a new bare object and pass it into the constructor as the 
1990                  *      "this" local variable. 
1991                  */
1992                 thisObject = ejsCreateObj("this", EJS_OBJ_HASH_SIZE);
1993                 mprCreatePropertyValue(ep->local, "this", thisObject);
1994
1995         } else if (obj) {
1996                 mprCreateProperty(ep->local, "this", obj);
1997         }
1998
1999         switch (prototype->type) {
2000         default:
2001                 mprAssert(0);
2002                 break;
2003
2004         case MPR_TYPE_STRING_CFUNCTION:
2005                 if (actualArgs->used > 0) {
2006                         argBuf = mprMalloc((1+actualArgs->used) * sizeof(char*));
2007                         for (i = 0; i < actualArgs->used; i++) {
2008                                 mprVarToString(&argBuf[i], MPR_MAX_STRING, 0, argValues[i]);
2009                         }
2010                         argBuf[i] = NULL;
2011                 } else {
2012                         argBuf = 0;
2013                 }
2014
2015                 /*
2016                  *      Call the function depending on the various handle flags
2017                  */
2018                 ep->thisPtr = prototype->cFunctionWithStrings.thisPtr;
2019                 if (prototype->flags & MPR_VAR_ALT_HANDLE) {
2020                         rc = ((EjsAltStringCFunction) prototype->cFunctionWithStrings.fn)
2021                                 (ep->eid, ep->altHandle, actualArgs->used, argBuf);
2022                 } else if (prototype->flags & MPR_VAR_SCRIPT_HANDLE) {
2023                         rc = (prototype->cFunctionWithStrings.fn)(ep->eid, 
2024                                 actualArgs->used, argBuf);
2025                 } else {
2026                         rc = (prototype->cFunctionWithStrings.fn)(ep->primaryHandle, 
2027                                 actualArgs->used, argBuf);
2028                 }
2029
2030                 if (actualArgs->used > 0) {
2031                         for (i = 0; i < actualArgs->used; i++) {
2032                                 mprFree(argBuf[i]);
2033                         }
2034                         mprFree(argBuf);
2035                 }
2036                 ep->thisPtr = 0;
2037                 break;
2038
2039         case MPR_TYPE_CFUNCTION:
2040                 /*
2041                  *      Call the function depending on the various handle flags
2042                  */
2043                 ep->thisPtr = prototype->cFunction.thisPtr;
2044                 if (prototype->flags & MPR_VAR_ALT_HANDLE) {
2045                         rc = ((EjsAltCFunction) prototype->cFunction.fn)
2046                                 (ep->eid, ep->altHandle, actualArgs->used, argValues);
2047                 } else if (prototype->flags & MPR_VAR_SCRIPT_HANDLE) {
2048                         rc = (prototype->cFunction.fn)(ep->eid, actualArgs->used, 
2049                                 argValues);
2050                 } else {
2051                         rc = (prototype->cFunction.fn)(ep->primaryHandle, 
2052                                 actualArgs->used, argValues);
2053                 }
2054                 ep->thisPtr = 0;
2055                 break;
2056
2057         case MPR_TYPE_FUNCTION:
2058
2059                 formalArgs = prototype->function.args;
2060                 argNames = (char**) formalArgs->handles;
2061
2062                 if (formalArgs->used > actualArgs->used) {
2063                         ejsError(ep, "Bad number of args. Should be %d", 
2064                                          formalArgs->used);
2065                         return -1;
2066                 }
2067
2068                 /*
2069                  *      Create the arguments and callee variables
2070                  */
2071                 arguments = ejsCreateObj("arguments", EJS_SMALL_OBJ_HASH_SIZE);
2072                 callee = ejsCreateObj("callee", EJS_SMALL_OBJ_HASH_SIZE);
2073
2074                 /*
2075                  *      Overwrite the length property
2076                  */
2077                 mprCreatePropertyValue(&arguments, "length", 
2078                         mprCreateIntegerVar(actualArgs->used));
2079                 mprCreatePropertyValue(&callee, "length", 
2080                         mprCreateIntegerVar(formalArgs->used));
2081
2082                 /*
2083                  *      Define all the agruments to be set to the actual parameters
2084                  */
2085                 for (i = 0; i < formalArgs->used; i++) {
2086                         mprCreateProperty(ep->local, argNames[i], argValues[i]);
2087                 }
2088                 for (i = 0; i < actualArgs->used; i++) {
2089                         mprItoa(i, buf, sizeof(buf));
2090                         mprCreateProperty(&arguments, buf, argValues[i]);
2091                 }
2092
2093                 mprCreateProperty(&arguments, "callee", &callee);
2094                 mprCreateProperty(ep->local, "arguments", &arguments);
2095
2096                 /*
2097                  *      Can destroy our variables here as they are now referenced via
2098                  *      "local"
2099                  */
2100                 mprDestroyVar(&callee);
2101                 mprDestroyVar(&arguments);
2102
2103                 /*
2104                  *      Actually run the function
2105                  */
2106                 rc = ejsEvalScript(ep->eid, prototype->function.body, 0, 0);
2107                 break;
2108         }
2109
2110         ejsCloseBlock(ep->eid, fid);
2111
2112         /*
2113          *      New statements return the newly created object as the result of the
2114          *      command
2115          */
2116         if (flags & EJS_FLAGS_NEW) {
2117                 mprDestroyVar(&ep->result);
2118                 /*
2119                  *      Don't copy, we want to assign the actual object into result.
2120                  *      (mprCopyVar would inc the refCount to 2).
2121                  */
2122                 ep->result = thisObject;
2123         }
2124         return rc;
2125 }
2126
2127 /******************************************************************************/
2128 /*
2129  *      Run a function
2130  */
2131
2132 int ejsRunFunction(int eid, MprVar *obj, const char *functionName, 
2133         MprArray *args)
2134 {
2135         EjsProc         proc, *saveProc;
2136         Ejs                     *ep;
2137         int                     rc;
2138
2139         mprAssert(obj);
2140         mprAssert(functionName && *functionName);
2141
2142         if ((ep = ejsPtr(eid)) == NULL) {
2143                 mprAssert(ep);
2144                 return MPR_ERR_NOT_FOUND;
2145         }
2146         saveProc = ep->proc;
2147         ep->proc = &proc;
2148
2149         memset(&proc, 0, sizeof(EjsProc));
2150         mprDestroyVar(&ep->result);
2151
2152         proc.fn = mprGetProperty(obj, functionName, 0);
2153         if (proc.fn == 0 || proc.fn->type == MPR_TYPE_UNDEFINED) {
2154                 ep->proc = saveProc;
2155                 return MPR_ERR_NOT_FOUND;
2156         }
2157         proc.procName = mprStrdup(functionName);
2158         if (args == 0) {
2159                 proc.args = mprCreateArray();
2160                 rc = evalFunction(ep, obj, 0);
2161         } else {
2162                 proc.args = args;
2163                 rc = evalFunction(ep, obj, 0);
2164                 proc.args = 0;
2165         }
2166
2167         freeProc(&proc);
2168         ep->proc = saveProc;
2169
2170         return rc;
2171 }
2172
2173 /******************************************************************************/
2174 /*
2175  *      Find which object contains the property given the current context.
2176  *      Only used for top level properties. 
2177  */
2178
2179 MprVar *ejsFindObj(Ejs *ep, int state, const char *property, int flags)
2180 {
2181         MprVar          *obj;
2182
2183         mprAssert(ep);
2184         mprAssert(property && *property);
2185
2186         if (flags & EJS_FLAGS_GLOBAL) {
2187                 obj = ep->global;
2188
2189         } else if (state == EJS_STATE_DEC || flags & EJS_FLAGS_LOCAL) {
2190                 obj = ep->local;
2191
2192         } else {
2193                 /* First look local, then look global */
2194                 if (mprGetProperty(ep->local, property, 0)) {
2195                         obj = ep->local;
2196                 } else {
2197                         obj = ep->global;
2198                 }
2199         }
2200         return obj;
2201 }
2202
2203 /******************************************************************************/
2204 /*
2205  *      Find an object property given a object and a property name. We
2206  *      intelligently look in the local and global namespaces depending on
2207  *      our state. If not found in local or global, try base classes for function
2208  *      names only. Returns the property or NULL.
2209  */
2210
2211 MprVar *ejsFindProperty(Ejs *ep, int state, MprVar *obj, char *property, 
2212         int flags)
2213 {
2214         MprVar  *vp;
2215
2216         mprAssert(ep);
2217         if (flags & EJS_FLAGS_EXE) {
2218                 mprAssert(property && *property);
2219         }
2220
2221         if (obj != 0) {
2222 #if FUTURE && MB
2223                 op = obj;
2224                 do {
2225                         vp = mprGetProperty(op, property, 0);
2226                         if (vp != 0) {
2227                                 if (op != obj && mprVarIsFunction(vp->type)) {
2228                                 }
2229                                 break;
2230                         }
2231                         op = op->baseObj;
2232                 } while (op);
2233 #endif
2234                 vp = mprGetProperty(obj, property, 0);
2235
2236         } else {
2237                 if (state == EJS_STATE_DEC) {
2238                         vp = mprGetProperty(ep->local, property, 0);
2239
2240                 } else {
2241                         /* Look local first, then global */
2242                         vp = mprGetProperty(ep->local, property, 0);
2243                         if (vp == NULL) {
2244                                 vp = mprGetProperty(ep->global, property, 0);
2245                         }
2246                 }
2247         }
2248         return vp;
2249 }
2250
2251 /******************************************************************************/
2252 /*
2253  *      Update result
2254  */
2255
2256 static void updateResult(Ejs *ep, int state, int flags, MprVar *vp)
2257 {
2258         if (flags & EJS_FLAGS_EXE && state != EJS_STATE_DEC) {
2259                 mprDestroyVar(&ep->result);
2260                 if (vp) {
2261                         mprCopyProperty(&ep->result, vp, MPR_SHALLOW_COPY);
2262                 }
2263         }
2264 }
2265
2266 /******************************************************************************/
2267 /*
2268  *      Append to the pointer value
2269  */
2270
2271 static void appendValue(MprVar *dest, MprVar *src)
2272 {
2273         char    *value, *oldBuf, *buf;
2274         int             len, oldLen;
2275
2276         mprAssert(dest);
2277
2278         mprVarToString(&value, MPR_MAX_STRING, 0, src);
2279
2280         if (mprVarIsValid(dest)) {
2281                 len = strlen(value);
2282                 oldBuf = dest->string;
2283                 oldLen = strlen(oldBuf);
2284                 buf = mprRealloc(oldBuf, (len + oldLen + 1) * sizeof(char));
2285                 dest->string = buf;
2286                 strcpy(&buf[oldLen], value);
2287
2288         } else {
2289                 *dest = mprCreateStringVar(value, 1);
2290         }
2291         mprFree(value);
2292 }
2293
2294 /******************************************************************************/
2295 /*
2296  *      Exit with status
2297  */
2298
2299 void ejsSetExitStatus(int eid, int status)
2300 {
2301         Ejs             *ep;
2302
2303         if ((ep = ejsPtr(eid)) == NULL) {
2304                 mprAssert(ep);
2305                 return;
2306         }
2307         ep->exitStatus = status;
2308         ep->flags |= EJS_FLAGS_EXIT;
2309 }
2310
2311 /******************************************************************************/
2312 /*
2313  *      Free an argument list
2314  */
2315
2316 static void freeProc(EjsProc *proc)
2317 {
2318         MprVar  **argValues;
2319         int             i;
2320
2321         if (proc->args) {
2322                 argValues = (MprVar**) proc->args->handles;
2323
2324                 for (i = 0; i < proc->args->max; i++) {
2325                         if (argValues[i]) {
2326                                 mprDestroyVar(argValues[i]);
2327                                 mprFree(argValues[i]);
2328                                 mprRemoveFromArray(proc->args, i);
2329                         }
2330                 }
2331
2332                 mprDestroyArray(proc->args);
2333         }
2334
2335         if (proc->procName) {
2336                 mprFree(proc->procName);
2337                 proc->procName = NULL;
2338         }
2339 }
2340
2341 /******************************************************************************/
2342 /*
2343  *      This function removes any new lines.  Used for else     cases, etc.
2344  */
2345
2346 static void removeNewlines(Ejs *ep, int state)
2347 {
2348         int tid;
2349
2350         do {
2351                 tid = ejsLexGetToken(ep, state);
2352         } while (tid == EJS_TOK_NEWLINE);
2353
2354         ejsLexPutbackToken(ep, tid, ep->token);
2355 }
2356
2357 /******************************************************************************/
2358
2359 #else
2360 void ejsParserDummy() {}
2361
2362 /******************************************************************************/
2363 #endif /* BLD_FEATURE_EJS */
2364
2365 /*
2366  * Local variables:
2367  * tab-width: 4
2368  * c-basic-offset: 4
2369  * End:
2370  * vim:tw=78
2371  * vim600: sw=4 ts=4 fdm=marker
2372  * vim<600: sw=4 ts=4
2373  */