From ba1ee57df43fe93c8b650478324ddd6cc1a3c7bc Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 11 Jul 2005 22:40:32 +0000 Subject: r8333: merged with latest upstream ejs sources (This used to be commit b0f9ddafe95d4e8d846bc72a39e94d22da271348) --- source4/lib/ejs/config.mk | 2 +- source4/lib/ejs/ejs.c | 1060 ---------------------------------------- source4/lib/ejs/ejs.h | 16 +- source4/lib/ejs/ejsInternal.h | 5 +- source4/lib/ejs/ejsLex.c | 5 +- source4/lib/ejs/ejsLib.c | 1061 +++++++++++++++++++++++++++++++++++++++++ source4/lib/ejs/ejsParser.c | 16 +- source4/lib/ejs/ejsProcs.c | 2 +- source4/lib/ejs/var.c | 50 +- source4/lib/ejs/var.h | 25 +- 10 files changed, 1134 insertions(+), 1108 deletions(-) delete mode 100644 source4/lib/ejs/ejs.c create mode 100644 source4/lib/ejs/ejsLib.c (limited to 'source4/lib/ejs') diff --git a/source4/lib/ejs/config.mk b/source4/lib/ejs/config.mk index 2d436cbe0b..f2c0e62f1e 100644 --- a/source4/lib/ejs/config.mk +++ b/source4/lib/ejs/config.mk @@ -2,7 +2,7 @@ # Start SUBSYSTEM EJS [SUBSYSTEM::EJS] ADD_OBJ_FILES = \ - lib/ejs/ejs.o \ + lib/ejs/ejsLib.o \ lib/ejs/ejsLex.o \ lib/ejs/ejsParser.o \ lib/ejs/ejsProcs.o \ diff --git a/source4/lib/ejs/ejs.c b/source4/lib/ejs/ejs.c deleted file mode 100644 index 41af795370..0000000000 --- a/source4/lib/ejs/ejs.c +++ /dev/null @@ -1,1060 +0,0 @@ -/* - * @file ejs.c - * @brief Embedded JavaScript (EJS) - * @overview Main module interface logic. - */ -/********************************* Copyright **********************************/ -/* - * @copy default.g - * - * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved. - * Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved. - * - * This software is distributed under commercial and open source licenses. - * You may use the GPL open source license described below or you may acquire - * a commercial license from Mbedthis Software. You agree to be fully bound - * by the terms of either license. Consult the LICENSE.TXT distributed with - * this software for full details. - * - * This software is open source; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. See the GNU General Public License for more - * details at: http://www.mbedthis.com/downloads/gplLicense.html - * - * This program is distributed WITHOUT ANY WARRANTY; without even the - * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * This GPL license does NOT permit incorporating this software into - * proprietary programs. If you are unable to comply with the GPL, you must - * acquire a commercial license to use this software. Commercial licenses - * for this software and support services are available from Mbedthis - * Software at http://www.mbedthis.com - * - * @end - */ -/********************************** Includes **********************************/ - -#include "lib/ejs/ejsInternal.h" - -#if BLD_FEATURE_EJS - -/********************************** Local Data ********************************/ - -/* - * These fields must be locked before any access when multithreaded - */ -static MprVar master; /* Master object */ -static MprArray *ejsList; /* List of ej handles */ - -#if BLD_FEATURE_MULTITHREAD -static EjsLock lock; -static EjsUnlock unlock; -static void *lockData; -#define ejsLock() if (lock) { (lock)(lockData); } else -#define ejsUnlock() if (unlock) { (unlock)(lockData); } else -#else -#define ejsLock() -#define ejsUnlock() -#endif - -/****************************** Forward Declarations **************************/ - -static char *getNextVarToken(char **next, char *tokBuf, int tokBufLen); - -/************************************* Code ***********************************/ -/* - * Initialize the EJ subsystem - */ - -int ejsOpen(EjsLock lockFn, EjsUnlock unlockFn, void *data) -{ - MprVar *np; - -#if BLD_FEATURE_MULTITHREAD - if (lockFn) { - lock = lockFn; - unlock = unlockFn; - lockData = data; - } -#endif - ejsLock(); - - /* - * Master is the top level object (above global). It is used to clone its - * contents into the global scope for each. This is never visible to the - * user, so don't use ejsCreateObj(). - */ - master = mprCreateObjVar("master", EJS_SMALL_OBJ_HASH_SIZE); - if (master.type == MPR_TYPE_UNDEFINED) { - ejsUnlock(); - return MPR_ERR_CANT_ALLOCATE; - } - - ejsList = mprCreateArray(); - ejsDefineStandardProperties(&master); - - /* - * Make these objects immutable - */ - np = mprGetFirstProperty(&master, MPR_ENUM_FUNCTIONS | MPR_ENUM_DATA); - while (np) { - mprSetVarReadonly(np, 1); - np = mprGetNextProperty(&master, np, MPR_ENUM_FUNCTIONS | - MPR_ENUM_DATA); - } - ejsUnlock(); - return 0; -} - -/******************************************************************************/ - -void ejsClose() -{ - ejsLock(); - mprDestroyArray(ejsList); - mprDestroyVar(&master); - ejsUnlock(); -} - -/******************************************************************************/ -/* - * Create and initialize an EJS engine - */ - -EjsId ejsOpenEngine(EjsHandle primaryHandle, EjsHandle altHandle) -{ - MprVar *np; - Ejs *ep; - - ep = mprMalloc(sizeof(Ejs)); - if (ep == 0) { - return (EjsId) -1; - } - memset(ep, 0, sizeof(Ejs)); - - ejsLock(); - ep->eid = (EjsId) mprAddToArray(ejsList, ep); - ejsUnlock(); - - /* - * Create array of local variable frames - */ - ep->frames = mprCreateArray(); - if (ep->frames == 0) { - ejsCloseEngine(ep->eid); - return (EjsId) -1; - } - ep->primaryHandle = primaryHandle; - ep->altHandle = altHandle; - - /* - * Create first frame: global variables - */ - ep->global = (MprVar*) mprMalloc(sizeof(MprVar)); - *ep->global = ejsCreateObj("global", EJS_OBJ_HASH_SIZE); - if (ep->global->type == MPR_TYPE_UNDEFINED) { - ejsCloseEngine(ep->eid); - return (EjsId) -1; - } - mprAddToArray(ep->frames, ep->global); - - /* - * Create first local variable frame - */ - ep->local = (MprVar*) mprMalloc(sizeof(MprVar)); - *ep->local = ejsCreateObj("local", EJS_OBJ_HASH_SIZE); - if (ep->local->type == MPR_TYPE_UNDEFINED) { - ejsCloseEngine(ep->eid); - return (EjsId) -1; - } - mprAddToArray(ep->frames, ep->local); - - /* - * Clone all master variables into the global frame. This does a - * reference copy. - * - * ejsDefineStandardProperties(ep->global); - */ - np = mprGetFirstProperty(&master, MPR_ENUM_FUNCTIONS | MPR_ENUM_DATA); - while (np) { - mprCreateProperty(ep->global, np->name, np); - np = mprGetNextProperty(&master, np, MPR_ENUM_FUNCTIONS | - MPR_ENUM_DATA); - } - - mprCreateProperty(ep->global, "global", ep->global); - mprCreateProperty(ep->global, "this", ep->global); - mprCreateProperty(ep->local, "local", ep->local); - - return ep->eid; -} - -/******************************************************************************/ -/* - * Close an EJS instance - */ - -void ejsCloseEngine(EjsId eid) -{ - Ejs *ep; - MprVar *vp; - void **handles; - int i; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return; - } - - mprFree(ep->error); - mprDestroyVar(&ep->result); - mprDestroyVar(&ep->tokenNumber); - - mprDeleteProperty(ep->local, "local"); - mprDeleteProperty(ep->global, "this"); - mprDeleteProperty(ep->global, "global"); - - handles = ep->frames->handles; - for (i = 0; i < ep->frames->max; i++) { - vp = handles[i]; - if (vp) { -#if BLD_DEBUG - if (vp->type == MPR_TYPE_OBJECT && vp->properties->refCount > 1) { - mprLog(7, "ejsCloseEngine: %s has ref count %d\n", - vp->name, vp->properties->refCount); - } -#endif - mprDestroyVar(vp); - mprFree(vp); - mprRemoveFromArray(ep->frames, i); - } - } - mprDestroyArray(ep->frames); - - ejsLock(); - mprRemoveFromArray(ejsList, (int) ep->eid); - ejsUnlock(); - - mprFree(ep); -} - -/******************************************************************************/ -/* - * Evaluate an EJS script file - */ - -int ejsEvalFile(EjsId eid, char *path, MprVar *result, char **emsg) -{ - struct stat sbuf; - Ejs *ep; - char *script; - int rc, fd; - - mprAssert(path && *path); - - if (emsg) { - *emsg = NULL; - } - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - goto error; - } - - if ((fd = open(path, O_RDONLY | O_BINARY, 0666)) < 0) { - ejsError(ep, "Can't open %s\n", path); - goto error; - } - - if (stat(path, &sbuf) < 0) { - close(fd); - ejsError(ep, "Cant stat %s", path); - goto error; - } - - if ((script = (char*) mprMalloc(sbuf.st_size + 1)) == NULL) { - close(fd); - ejsError(ep, "Cant malloc %d", (int)sbuf.st_size); - goto error; - } - - if (read(fd, script, sbuf.st_size) != (int)sbuf.st_size) { - close(fd); - mprFree(script); - ejsError(ep, "Error reading %s", path); - goto error; - } - - script[sbuf.st_size] = '\0'; - close(fd); - - rc = ejsEvalBlock(eid, script, result, emsg); - mprFree(script); - - return rc; - -/* - * Error return - */ -error: - *emsg = mprStrdup(ep->error); - return -1; -} - -/******************************************************************************/ -/* - * Create a new variable scope block. This pushes the old local frame down - * the stack and creates a new local variables frame. - */ - -int ejsOpenBlock(EjsId eid) -{ - Ejs *ep; - - if((ep = ejsPtr(eid)) == NULL) { - return -1; - } - - ep->local = (MprVar*) mprMalloc(sizeof(MprVar)); - *ep->local = ejsCreateObj("localBlock", EJS_OBJ_HASH_SIZE); - - mprCreateProperty(ep->local, "local", ep->local); - - return mprAddToArray(ep->frames, ep->local); -} - -/******************************************************************************/ -/* - * Close a variable scope block opened via ejsOpenBlock. Pop back the old - * local variables frame. - */ - -int ejsCloseBlock(EjsId eid, int fid) -{ - Ejs *ep; - - if((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return -1; - } - - /* - * Must remove self-references before destroying "local" - */ - mprDeleteProperty(ep->local, "local"); - - mprDestroyVar(ep->local); - mprFree(ep->local); - - mprRemoveFromArray(ep->frames, fid); - ep->local = (MprVar*) ep->frames->handles[ep->frames->used - 1]; - - return 0; -} - -/******************************************************************************/ -/* - * Create a new variable scope block and evaluate a script. All frames - * created during this context will be automatically deleted when complete. - * vp and emsg are optional. i.e. created local variables will be discarded - * when this routine returns. - */ - -int ejsEvalBlock(EjsId eid, char *script, MprVar *vp, char **emsg) -{ - int rc, fid; - - mprAssert(script); - - fid = ejsOpenBlock(eid); - rc = ejsEvalScript(eid, script, vp, emsg); - ejsCloseBlock(eid, fid); - - return rc; -} - -/******************************************************************************/ -/* - * Parse and evaluate a EJS. Return the result in *vp. The result is "owned" - * by EJ and the caller must not free it. Returns -1 on errors and zero - * for success. On errors, emsg will be set to the reason. The caller must - * free emsg. - */ - -int ejsEvalScript(EjsId eid, char *script, MprVar *vp, char **emsg) -{ - Ejs *ep; - EjsInput *oldBlock; - int state; - void *endlessLoopTest; - int loopCounter; - - if (emsg) { - *emsg = NULL; - } - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return -1; - } - - mprDestroyVar(&ep->result); - - if (script == 0) { - return 0; - } - - /* - * Allocate a new evaluation block, and save the old one - */ - oldBlock = ep->input; - ejsLexOpenScript(ep, script); - - /* - * Do the actual parsing and evaluation - */ - loopCounter = 0; - endlessLoopTest = NULL; - ep->exitStatus = 0; - - do { - state = ejsParse(ep, EJS_STATE_BEGIN, EJS_FLAGS_EXE); - - if (state == EJS_STATE_RET) { - state = EJS_STATE_EOF; - } - /* - * Stuck parser and endless recursion protection. - */ - if (endlessLoopTest == ep->input->scriptServp) { - if (loopCounter++ > 10) { - state = EJS_STATE_ERR; - ejsError(ep, "Syntax error"); - } - } else { - endlessLoopTest = ep->input->scriptServp; - loopCounter = 0; - } - } while (state != EJS_STATE_EOF && state != EJS_STATE_ERR); - - ejsLexCloseScript(ep); - - /* - * Return any error string to the user - */ - if (state == EJS_STATE_ERR && emsg) { - *emsg = mprStrdup(ep->error); - } - - /* - * Restore the old evaluation block - */ - ep->input = oldBlock; - - if (state == EJS_STATE_ERR) { - return -1; - } - - if (vp) { - *vp = ep->result; - } - - return ep->exitStatus; -} - -/******************************************************************************/ -/* - * Core error handling - */ - -static void ejsErrorCore(Ejs* ep, const char *fmt, va_list args) PRINTF_ATTRIBUTE(2, 0); - -static void ejsErrorCore(Ejs* ep, const char *fmt, va_list args) -{ - EjsInput *ip; - char *errbuf, *msgbuf; - - mprAssert(ep); - mprAssert(args); - - msgbuf = NULL; - mprAllocVsprintf(&msgbuf, MPR_MAX_STRING, fmt, args); - - if (ep) { - ip = ep->input; - if (ip) { - mprAllocSprintf(&errbuf, MPR_MAX_STRING, - "%s\nError on line %d. Offending line: %s\n\n", - msgbuf, ip->lineNumber, ip->line); - } else { - mprAllocSprintf(&errbuf, MPR_MAX_STRING, "%s\n", msgbuf); - } - mprFree(ep->error); - ep->error = errbuf; - } - mprFree(msgbuf); -} - -/******************************************************************************/ -/* - * Internal use function to set the error message - */ - -void ejsError(Ejs* ep, const char* fmt, ...) -{ - va_list args; - - va_start(args, fmt); - ejsErrorCore(ep, fmt, args); - va_end(args); -} - -/******************************************************************************/ -/* - * Public routine to set the error message - */ - -void ejsSetErrorMsg(EjsId eid, const char* fmt, ...) -{ - va_list args; - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return; - } - va_start(args, fmt); - ejsErrorCore(ep, fmt, args); - va_end(args); -} - -/******************************************************************************/ -/* - * Get the current line number - */ - -int ejsGetLineNumber(EjsId eid) -{ - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return -1; - } - return ep->input->lineNumber; -} - -/******************************************************************************/ -/* - * Return the local object - */ - -MprVar *ejsGetLocalObject(EjsId eid) -{ - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return 0; - } - return ep->local; -} - -/******************************************************************************/ -/* - * Return the global object - */ - -MprVar *ejsGetGlobalObject(EjsId eid) -{ - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return 0; - } - return ep->global; -} - -/******************************************************************************/ -/* - * Copy the value of an object property. Return value is in "value". - * If deepCopy is true, copy all object/strings. Otherwise, object reference - * counts are incremented. Callers must always call mprDestroyVar on the - * return value to prevent leaks. - * - * Returns: -1 on errors or if the variable is not found. - */ - -int ejsCopyVar(EjsId eid, const char *var, MprVar *value, bool deepCopy) -{ - Ejs *ep; - MprVar *vp; - - mprAssert(var && *var); - mprAssert(value); - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return -1; - } - - if (ejsGetVarCore(ep, var, 0, &vp, 0) < 0) { - return -1; - } - - return mprCopyProperty(value, vp, deepCopy); -} - -/******************************************************************************/ -/* - * Return the value of an object property. Return value is in "value". - * Objects and strings are not copied and reference counts are not modified. - * Callers should NOT call mprDestroyVar. Returns: -1 on errors or if the - * variable is not found. - */ - -int ejsReadVar(EjsId eid, const char *var, MprVar *value) -{ - Ejs *ep; - MprVar *vp; - - mprAssert(var && *var); - mprAssert(value); - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return -1; - } - - if (ejsGetVarCore(ep, var, 0, &vp, 0) < 0) { - return -1; - } - - return mprReadProperty(vp, value); -} - -/******************************************************************************/ -/* - * Set a variable that may be an arbitrarily complex object or array reference. - * Will always define in the top most variable frame. - */ - -int ejsWriteVar(EjsId eid, const char *var, MprVar *value) -{ - Ejs *ep; - MprVar *vp; - - mprAssert(var && *var); - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return -1; - } - - if (ejsGetVarCore(ep, var, 0, &vp, EJS_FLAGS_CREATE) < 0) { - return -1; - } - mprAssert(vp); - - /* - * Only copy the value. Don't overwrite the object's name - */ - mprWriteProperty(vp, value); - - return 0; -} - -/******************************************************************************/ -/* - * Set a variable that may be an arbitrarily complex object or array reference. - * Will always define in the top most variable frame. - */ - -int ejsWriteVarValue(EjsId eid, const char *var, MprVar value) -{ - return ejsWriteVar(eid, var, &value); -} - -/******************************************************************************/ -/* - * Delete a variable - */ - -int ejsDeleteVar(EjsId eid, const char *var) -{ - Ejs *ep; - MprVar *vp; - MprVar *obj; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return -1; - } - if (ejsGetVarCore(ep, var, &obj, &vp, 0) < 0) { - return -1; - } - mprDeleteProperty(obj, vp->name); - return 0; -} - -/******************************************************************************/ -/* - * Set the expression return value - */ - -void ejsSetReturnValue(EjsId eid, MprVar value) -{ - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return; - } - mprCopyVar(&ep->result, &value, MPR_SHALLOW_COPY); -} - -/******************************************************************************/ -/* - * Set the expression return value to a string value - */ - -void ejsSetReturnString(EjsId eid, const char *str) -{ - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return; - } - mprCopyVarValue(&ep->result, mprCreateStringVar(str, 0), MPR_SHALLOW_COPY); -} - -/******************************************************************************/ -/* - * Get the expression return value - */ - -MprVar *ejsGetReturnValue(EjsId eid) -{ - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return 0; - } - return &ep->result; -} - -/******************************************************************************/ -/* - * Define a C function. If eid < 0, then update the master object with this - * function. NOTE: in this case, functionName must be simple without any "." or - * "[]" elements. If eid >= 0, add to the specified script engine. In this - * case, functionName can be an arbitrary object reference and can contain "." - * or "[]". - */ - -void ejsDefineCFunction(EjsId eid, const char *functionName, MprCFunction fn, - void *thisPtr, int flags) -{ - if (eid < 0) { - ejsLock(); - mprCreatePropertyValue(&master, functionName, - mprCreateCFunctionVar(fn, thisPtr, flags)); - ejsUnlock(); - } else { - ejsWriteVarValue(eid, functionName, - mprCreateCFunctionVar(fn, thisPtr, flags)); - } -} - -/******************************************************************************/ -/* - * Define a C function with String arguments - */ - -void ejsDefineStringCFunction(EjsId eid, const char *functionName, - MprStringCFunction fn, void *thisPtr, int flags) -{ - if (eid < 0) { - ejsLock(); - mprCreatePropertyValue(&master, functionName, - mprCreateStringCFunctionVar(fn, thisPtr, flags)); - ejsUnlock(); - } else { - ejsWriteVarValue(eid, functionName, - mprCreateStringCFunctionVar(fn, thisPtr, flags)); - } -} - -/******************************************************************************/ -/* - * Define a JavaScript function. Args should be comma separated. - * Body should not contain braces. - */ - -void ejsDefineFunction(EjsId eid, const char *functionName, char *args, char *body) -{ - MprVar v; - - v = mprCreateFunctionVar(args, body, 0); - if (eid < 0) { - ejsLock(); - mprCreateProperty(&master, functionName, &v); - ejsUnlock(); - } else { - ejsWriteVar(eid, functionName, &v); - } - mprDestroyVar(&v); -} - -/******************************************************************************/ - -void *ejsGetThisPtr(EjsId eid) -{ - Ejs *ep; - - if ((ep = ejsPtr(eid)) == NULL) { - mprAssert(ep); - return 0; - } - return ep->thisPtr; -} - -/******************************************************************************/ -/* - * Find a variable given a variable name and return the parent object and - * the variable itself, the variable . This routine supports variable names - * that may be objects or arrays but may NOT have expressions in the array - * indicies. Returns -1 on errors or if the variable is not found. - */ - -int ejsGetVarCore(Ejs *ep, const char *varName_c, MprVar **obj, MprVar **varValue, - int flags) -{ - MprVar *currentObj; - MprVar *currentVar; - char tokBuf[EJS_MAX_ID]; - char *propertyName, *token, *next, *cp, *varName; - - if (obj) { - *obj = 0; - } - if (varValue) { - *varValue = 0; - } - currentObj = ejsFindObj(ep, 0, varName_c, flags); - currentVar = 0; - propertyName = 0; - - varName = mprStrdup(varName_c); - next = varName; - - token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); - - while (currentObj != 0 && token != 0 && *token) { - - if (*token == '[') { - token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); - - propertyName = token; - if (*propertyName == '\"') { - propertyName++; - if ((cp = strchr(propertyName, '\"')) != 0) { - *cp = '\0'; - } - } else if (*propertyName == '\'') { - propertyName++; - if ((cp = strchr(propertyName, '\'')) != 0) { - *cp = '\0'; - } - } - - currentObj = currentVar; - currentVar = ejsFindProperty(ep, 0, currentObj, propertyName, 0); - - token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); - if (*token != ']') { - mprFree(varName); - return -1; - } - - } else if (*token == '.') { - token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); - if (!isalpha((int) token[0]) && - token[0] != '_' && token[0] != '$') { - mprFree(varName); - return -1; - } - - propertyName = token; - currentObj = currentVar; - currentVar = ejsFindProperty(ep, 0, currentObj, token, 0); - - } else { - currentVar = ejsFindProperty(ep, 0, currentObj, token, 0); - } - token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); - } - mprFree(varName); - - if (currentVar == 0 && currentObj >= 0 && flags & EJS_FLAGS_CREATE) { - currentVar = mprCreatePropertyValue(currentObj, propertyName, - mprCreateUndefinedVar()); - } - if (obj) { - *obj = currentObj; - } - - /* - * Don't use mprCopyVar as it will copy the data - */ - if (varValue) { - *varValue = currentVar; - } - return currentVar ? 0 : -1; -} - -/******************************************************************************/ -/* - * Get the next token as part of a variable specification. This will return - * a pointer to the next token and will return a pointer to the next token - * (after this one) in "next". The tokBuf holds the parsed token. - */ -static char *getNextVarToken(char **next, char *tokBuf, int tokBufLen) -{ - char *start, *cp; - int len; - - start = *next; - while (isspace((int) *start) || *start == '\n' || *start == '\r') { - start++; - } - cp = start; - - if (*cp == '.' || *cp == '[' || *cp == ']') { - cp++; - } else { - while (*cp && *cp != '.' && *cp != '[' && *cp != ']' && - !isspace((int) *cp) && *cp != '\n' && *cp != '\r') { - cp++; - } - } - len = mprMemcpy(tokBuf, tokBufLen - 1, start, cp - start); - tokBuf[len] = '\0'; - - *next = cp; - return tokBuf; -} - -/******************************************************************************/ -/* - * Get the EJS structure pointer - */ - -Ejs *ejsPtr(EjsId eid) -{ - Ejs *handle; - int intId; - - intId = (int) eid; - - ejsLock(); - mprAssert(0 <= intId && intId < ejsList->max); - - if (intId < 0 || intId >= ejsList->max || ejsList->handles[intId] == NULL) { - mprAssert(0); - ejsUnlock(); - return NULL; - } - handle = ejsList->handles[intId]; - ejsUnlock(); - return handle; -} - -/******************************************************************************/ -/* - * Utility routine to crack JavaScript arguments. Return the number of args - * seen. This routine only supports %s and %d type args. - * - * Typical usage: - * - * if (ejsParseArgs(argc, argv, "%s %d", &name, &age) < 2) { - * mprError("Insufficient args\n"); - * return -1; - * } - */ - -int ejsParseArgs(int argc, char **argv, char *fmt, ...) -{ - va_list vargs; - bool *bp; - char *cp, **sp, *s; - int *ip, argn; - - va_start(vargs, fmt); - - if (argv == 0) { - return 0; - } - - for (argn = 0, cp = fmt; cp && *cp && argn < argc && argv[argn]; ) { - if (*cp++ != '%') { - continue; - } - - s = argv[argn]; - switch (*cp) { - case 'b': - bp = va_arg(vargs, bool*); - if (bp) { - if (strcmp(s, "true") == 0 || s[0] == '1') { - *bp = 1; - } else { - *bp = 0; - } - } else { - *bp = 0; - } - break; - - case 'd': - ip = va_arg(vargs, int*); - *ip = atoi(s); - break; - - case 's': - sp = va_arg(vargs, char**); - *sp = s; - break; - - default: - mprAssert(0); - } - argn++; - } - - va_end(vargs); - return argn; -} - -/******************************************************************************/ - -#else -void ejsDummy() {} - -/******************************************************************************/ -#endif /* BLD_FEATURE_EJS */ - -/******************************************************************************/ -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim:tw=78 - * vim600: sw=4 ts=4 fdm=marker - * vim<600: sw=4 ts=4 - */ diff --git a/source4/lib/ejs/ejs.h b/source4/lib/ejs/ejs.h index 79216d4712..f1d2bb4c6e 100644 --- a/source4/lib/ejs/ejs.h +++ b/source4/lib/ejs/ejs.h @@ -44,8 +44,8 @@ #ifndef _h_EJS #define _h_EJS 1 -#include "lib/ejs/miniMpr.h" -#include "lib/ejs/var.h" +#include "miniMpr.h" +#include "var.h" #ifdef __cplusplus extern "C" { @@ -75,7 +75,7 @@ extern void ejsCloseEngine(EjsId eid); * Evaluation functions */ extern int ejsEvalFile(EjsId eid, char *path, MprVar *result, char **emsg); -extern int ejsEvalScript(EjsId eid, char *script, MprVar *result, +extern int ejsEvalScript(EjsId eid, char *script, MprVar *result, char **emsg); extern int ejsRunFunction(int eid, MprVar *obj, const char *functionName, MprArray *args); @@ -87,7 +87,8 @@ extern int ejsRunFunction(int eid, MprVar *obj, const char *functionName, extern MprVar ejsCreateObj(const char *name, int hashSize); extern MprVar ejsCreateArray(const char *name, int hashSize); extern bool ejsDestroyVar(MprVar *obj); -extern int ejsCopyVar(EjsId eid, const char *var, MprVar *value, bool copyRef); +extern int ejsCopyVar(EjsId eid, const char *var, MprVar *value, + bool copyRef); extern int ejsReadVar(EjsId eid, const char *var, MprVar *value); extern int ejsWriteVar(EjsId eid, const char *var, MprVar *value); extern int ejsWriteVarValue(EjsId eid, const char *var, MprVar value); @@ -99,8 +100,8 @@ extern MprVar *ejsGetGlobalObject(EjsId eid); /* * Function routines */ -extern void ejsDefineFunction(EjsId eid, const char *functionName, char *args, - char *body); +extern void ejsDefineFunction(EjsId eid, const char *functionName, + char *args, char *body); extern void ejsDefineCFunction(EjsId eid, const char *functionName, MprCFunction fn, void *thisPtr, int flags); extern void ejsDefineStringCFunction(EjsId eid, const char *functionName, @@ -109,7 +110,8 @@ extern void *ejsGetThisPtr(EjsId eid); extern MprVar *ejsGetReturnValue(EjsId eid); extern int ejsGetLineNumber(EjsId eid); extern int ejsParseArgs(int argc, char **argv, char *fmt, ...); -extern void ejsSetErrorMsg(EjsId eid, const char* fmt, ...) PRINTF_ATTRIBUTE(2,3); +extern void ejsSetErrorMsg(EjsId eid, const char* fmt, ...) + PRINTF_ATTRIBUTE(2,3); extern void ejsSetReturnValue(EjsId eid, MprVar value); extern void ejsSetReturnString(EjsId eid, const char *str); diff --git a/source4/lib/ejs/ejsInternal.h b/source4/lib/ejs/ejsInternal.h index 4d54c4e8c6..3bf99d88b9 100644 --- a/source4/lib/ejs/ejsInternal.h +++ b/source4/lib/ejs/ejsInternal.h @@ -39,7 +39,7 @@ #ifndef _h_EJS_INTERNAL #define _h_EJS_INTERNAL 1 -#include "lib/ejs/ejs.h" +#include "ejs.h" /********************************** Defines ***********************************/ @@ -253,7 +253,8 @@ extern void ejsLexPutbackToken(Ejs* ep, int tid, char *string); /* * Parsing */ -extern MprVar *ejsFindObj(Ejs *ep, int state, const char *property, int flags); +extern MprVar *ejsFindObj(Ejs *ep, int state, const char *property, + int flags); extern MprVar *ejsFindProperty(Ejs *ep, int state, MprVar *obj, char *property, int flags); extern int ejsGetVarCore(Ejs *ep, const char *var, MprVar **obj, diff --git a/source4/lib/ejs/ejsLex.c b/source4/lib/ejs/ejsLex.c index b0d6483c2a..6dcc1c7b78 100644 --- a/source4/lib/ejs/ejsLex.c +++ b/source4/lib/ejs/ejsLex.c @@ -36,7 +36,7 @@ */ /********************************** Includes **********************************/ -#include "lib/ejs/ejsInternal.h" +#include "ejsInternal.h" #if BLD_FEATURE_EJS @@ -439,6 +439,9 @@ static int getLexicalToken(Ejs *ep, int state) */ while (1) { if ((c = inputGetc(ep)) < 0) { + if (style == '/') { + return EJS_TOK_EOF; + } ejsError(ep, "Syntax Error"); return EJS_TOK_ERR; } diff --git a/source4/lib/ejs/ejsLib.c b/source4/lib/ejs/ejsLib.c new file mode 100644 index 0000000000..caae5b6495 --- /dev/null +++ b/source4/lib/ejs/ejsLib.c @@ -0,0 +1,1061 @@ +/* + * @file ejs.c + * @brief Embedded JavaScript (EJS) + * @overview Main module interface logic. + */ +/********************************* Copyright **********************************/ +/* + * @copy default.g + * + * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved. + * Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved. + * + * This software is distributed under commercial and open source licenses. + * You may use the GPL open source license described below or you may acquire + * a commercial license from Mbedthis Software. You agree to be fully bound + * by the terms of either license. Consult the LICENSE.TXT distributed with + * this software for full details. + * + * This software is open source; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See the GNU General Public License for more + * details at: http://www.mbedthis.com/downloads/gplLicense.html + * + * This program is distributed WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * This GPL license does NOT permit incorporating this software into + * proprietary programs. If you are unable to comply with the GPL, you must + * acquire a commercial license to use this software. Commercial licenses + * for this software and support services are available from Mbedthis + * Software at http://www.mbedthis.com + * + * @end + */ +/********************************** Includes **********************************/ + +#include "ejsInternal.h" + +#if BLD_FEATURE_EJS + +/********************************** Local Data ********************************/ + +/* + * These fields must be locked before any access when multithreaded + */ +static MprVar master; /* Master object */ +static MprArray *ejsList; /* List of ej handles */ + +#if BLD_FEATURE_MULTITHREAD +static EjsLock lock; +static EjsUnlock unlock; +static void *lockData; +#define ejsLock() if (lock) { (lock)(lockData); } else +#define ejsUnlock() if (unlock) { (unlock)(lockData); } else +#else +#define ejsLock() +#define ejsUnlock() +#endif + +/****************************** Forward Declarations **************************/ + +static char *getNextVarToken(char **next, char *tokBuf, int tokBufLen); + +/************************************* Code ***********************************/ +/* + * Initialize the EJ subsystem + */ + +int ejsOpen(EjsLock lockFn, EjsUnlock unlockFn, void *data) +{ + MprVar *np; + +#if BLD_FEATURE_MULTITHREAD + if (lockFn) { + lock = lockFn; + unlock = unlockFn; + lockData = data; + } +#endif + ejsLock(); + + /* + * Master is the top level object (above global). It is used to clone its + * contents into the global scope for each. This is never visible to the + * user, so don't use ejsCreateObj(). + */ + master = mprCreateObjVar("master", EJS_SMALL_OBJ_HASH_SIZE); + if (master.type == MPR_TYPE_UNDEFINED) { + ejsUnlock(); + return MPR_ERR_CANT_ALLOCATE; + } + + ejsList = mprCreateArray(); + ejsDefineStandardProperties(&master); + + /* + * Make these objects immutable + */ + np = mprGetFirstProperty(&master, MPR_ENUM_FUNCTIONS | MPR_ENUM_DATA); + while (np) { + mprSetVarReadonly(np, 1); + np = mprGetNextProperty(&master, np, MPR_ENUM_FUNCTIONS | + MPR_ENUM_DATA); + } + ejsUnlock(); + return 0; +} + +/******************************************************************************/ + +void ejsClose() +{ + ejsLock(); + mprDestroyArray(ejsList); + mprDestroyVar(&master); + ejsUnlock(); +} + +/******************************************************************************/ +/* + * Create and initialize an EJS engine + */ + +EjsId ejsOpenEngine(EjsHandle primaryHandle, EjsHandle altHandle) +{ + MprVar *np; + Ejs *ep; + + ep = mprMalloc(sizeof(Ejs)); + if (ep == 0) { + return (EjsId) -1; + } + memset(ep, 0, sizeof(Ejs)); + + ejsLock(); + ep->eid = (EjsId) mprAddToArray(ejsList, ep); + ejsUnlock(); + + /* + * Create array of local variable frames + */ + ep->frames = mprCreateArray(); + if (ep->frames == 0) { + ejsCloseEngine(ep->eid); + return (EjsId) -1; + } + ep->primaryHandle = primaryHandle; + ep->altHandle = altHandle; + + /* + * Create first frame: global variables + */ + ep->global = (MprVar*) mprMalloc(sizeof(MprVar)); + *ep->global = ejsCreateObj("global", EJS_OBJ_HASH_SIZE); + if (ep->global->type == MPR_TYPE_UNDEFINED) { + ejsCloseEngine(ep->eid); + return (EjsId) -1; + } + mprAddToArray(ep->frames, ep->global); + + /* + * Create first local variable frame + */ + ep->local = (MprVar*) mprMalloc(sizeof(MprVar)); + *ep->local = ejsCreateObj("local", EJS_OBJ_HASH_SIZE); + if (ep->local->type == MPR_TYPE_UNDEFINED) { + ejsCloseEngine(ep->eid); + return (EjsId) -1; + } + mprAddToArray(ep->frames, ep->local); + + /* + * Clone all master variables into the global frame. This does a + * reference copy. + * + * ejsDefineStandardProperties(ep->global); + */ + np = mprGetFirstProperty(&master, MPR_ENUM_FUNCTIONS | MPR_ENUM_DATA); + while (np) { + mprCreateProperty(ep->global, np->name, np); + np = mprGetNextProperty(&master, np, MPR_ENUM_FUNCTIONS | + MPR_ENUM_DATA); + } + + mprCreateProperty(ep->global, "global", ep->global); + mprCreateProperty(ep->global, "this", ep->global); + mprCreateProperty(ep->local, "local", ep->local); + + return ep->eid; +} + +/******************************************************************************/ +/* + * Close an EJS instance + */ + +void ejsCloseEngine(EjsId eid) +{ + Ejs *ep; + MprVar *vp; + void **handles; + int i; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return; + } + + mprFree(ep->error); + mprDestroyVar(&ep->result); + mprDestroyVar(&ep->tokenNumber); + + mprDeleteProperty(ep->local, "local"); + mprDeleteProperty(ep->global, "this"); + mprDeleteProperty(ep->global, "global"); + + handles = ep->frames->handles; + for (i = 0; i < ep->frames->max; i++) { + vp = handles[i]; + if (vp) { +#if BLD_DEBUG + if (vp->type == MPR_TYPE_OBJECT && vp->properties->refCount > 1) { + mprLog(7, "ejsCloseEngine: %s has ref count %d\n", + vp->name, vp->properties->refCount); + } +#endif + mprDestroyVar(vp); + mprFree(vp); + mprRemoveFromArray(ep->frames, i); + } + } + mprDestroyArray(ep->frames); + + ejsLock(); + mprRemoveFromArray(ejsList, (int) ep->eid); + ejsUnlock(); + + mprFree(ep); +} + +/******************************************************************************/ +/* + * Evaluate an EJS script file + */ + +int ejsEvalFile(EjsId eid, char *path, MprVar *result, char **emsg) +{ + struct stat sbuf; + Ejs *ep; + char *script; + int rc, fd; + + mprAssert(path && *path); + + if (emsg) { + *emsg = NULL; + } + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + goto error; + } + + if ((fd = open(path, O_RDONLY | O_BINARY, 0666)) < 0) { + ejsError(ep, "Can't open %s\n", path); + goto error; + } + + if (stat(path, &sbuf) < 0) { + close(fd); + ejsError(ep, "Cant stat %s", path); + goto error; + } + + if ((script = (char*) mprMalloc(sbuf.st_size + 1)) == NULL) { + close(fd); + ejsError(ep, "Cant malloc %d", (int) sbuf.st_size); + goto error; + } + + if (read(fd, script, sbuf.st_size) != (int) sbuf.st_size) { + close(fd); + mprFree(script); + ejsError(ep, "Error reading %s", path); + goto error; + } + + script[sbuf.st_size] = '\0'; + close(fd); + + rc = ejsEvalBlock(eid, script, result, emsg); + mprFree(script); + + return rc; + +/* + * Error return + */ +error: + *emsg = mprStrdup(ep->error); + return -1; +} + +/******************************************************************************/ +/* + * Create a new variable scope block. This pushes the old local frame down + * the stack and creates a new local variables frame. + */ + +int ejsOpenBlock(EjsId eid) +{ + Ejs *ep; + + if((ep = ejsPtr(eid)) == NULL) { + return -1; + } + + ep->local = (MprVar*) mprMalloc(sizeof(MprVar)); + *ep->local = ejsCreateObj("localBlock", EJS_OBJ_HASH_SIZE); + + mprCreateProperty(ep->local, "local", ep->local); + + return mprAddToArray(ep->frames, ep->local); +} + +/******************************************************************************/ +/* + * Close a variable scope block opened via ejsOpenBlock. Pop back the old + * local variables frame. + */ + +int ejsCloseBlock(EjsId eid, int fid) +{ + Ejs *ep; + + if((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return -1; + } + + /* + * Must remove self-references before destroying "local" + */ + mprDeleteProperty(ep->local, "local"); + + mprDestroyVar(ep->local); + mprFree(ep->local); + + mprRemoveFromArray(ep->frames, fid); + ep->local = (MprVar*) ep->frames->handles[ep->frames->used - 1]; + + return 0; +} + +/******************************************************************************/ +/* + * Create a new variable scope block and evaluate a script. All frames + * created during this context will be automatically deleted when complete. + * vp and emsg are optional. i.e. created local variables will be discarded + * when this routine returns. + */ + +int ejsEvalBlock(EjsId eid, char *script, MprVar *vp, char **emsg) +{ + int rc, fid; + + mprAssert(script); + + fid = ejsOpenBlock(eid); + rc = ejsEvalScript(eid, script, vp, emsg); + ejsCloseBlock(eid, fid); + + return rc; +} + +/******************************************************************************/ +/* + * Parse and evaluate a EJS. Return the result in *vp. The result is "owned" + * by EJ and the caller must not free it. Returns -1 on errors and zero + * for success. On errors, emsg will be set to the reason. The caller must + * free emsg. + */ + +int ejsEvalScript(EjsId eid, char *script, MprVar *vp, char **emsg) +{ + Ejs *ep; + EjsInput *oldBlock; + int state; + void *endlessLoopTest; + int loopCounter; + + if (emsg) { + *emsg = NULL; + } + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return -1; + } + + mprDestroyVar(&ep->result); + + if (script == 0) { + return 0; + } + + /* + * Allocate a new evaluation block, and save the old one + */ + oldBlock = ep->input; + ejsLexOpenScript(ep, script); + + /* + * Do the actual parsing and evaluation + */ + loopCounter = 0; + endlessLoopTest = NULL; + ep->exitStatus = 0; + + do { + state = ejsParse(ep, EJS_STATE_BEGIN, EJS_FLAGS_EXE); + + if (state == EJS_STATE_RET) { + state = EJS_STATE_EOF; + } + /* + * Stuck parser and endless recursion protection. + */ + if (endlessLoopTest == ep->input->scriptServp) { + if (loopCounter++ > 10) { + state = EJS_STATE_ERR; + ejsError(ep, "Syntax error"); + } + } else { + endlessLoopTest = ep->input->scriptServp; + loopCounter = 0; + } + } while (state != EJS_STATE_EOF && state != EJS_STATE_ERR); + + ejsLexCloseScript(ep); + + /* + * Return any error string to the user + */ + if (state == EJS_STATE_ERR && emsg) { + *emsg = mprStrdup(ep->error); + } + + /* + * Restore the old evaluation block + */ + ep->input = oldBlock; + + if (state == EJS_STATE_ERR) { + return -1; + } + + if (vp) { + *vp = ep->result; + } + + return ep->exitStatus; +} + +/******************************************************************************/ +/* + * Core error handling + */ + +static void ejsErrorCore(Ejs* ep, const char *fmt, va_list args) + PRINTF_ATTRIBUTE(2, 0); + +static void ejsErrorCore(Ejs* ep, const char *fmt, va_list args) +{ + EjsInput *ip; + char *errbuf, *msgbuf; + + mprAssert(ep); + mprAssert(args); + + msgbuf = NULL; + mprAllocVsprintf(&msgbuf, MPR_MAX_STRING, fmt, args); + + if (ep) { + ip = ep->input; + if (ip) { + mprAllocSprintf(&errbuf, MPR_MAX_STRING, + "%s\nError on line %d. Offending line: %s\n\n", + msgbuf, ip->lineNumber, ip->line); + } else { + mprAllocSprintf(&errbuf, MPR_MAX_STRING, "%s\n", msgbuf); + } + mprFree(ep->error); + ep->error = errbuf; + } + mprFree(msgbuf); +} + +/******************************************************************************/ +/* + * Internal use function to set the error message + */ + +void ejsError(Ejs* ep, const char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + ejsErrorCore(ep, fmt, args); + va_end(args); +} + +/******************************************************************************/ +/* + * Public routine to set the error message + */ + +void ejsSetErrorMsg(EjsId eid, const char* fmt, ...) +{ + va_list args; + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return; + } + va_start(args, fmt); + ejsErrorCore(ep, fmt, args); + va_end(args); +} + +/******************************************************************************/ +/* + * Get the current line number + */ + +int ejsGetLineNumber(EjsId eid) +{ + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return -1; + } + return ep->input->lineNumber; +} + +/******************************************************************************/ +/* + * Return the local object + */ + +MprVar *ejsGetLocalObject(EjsId eid) +{ + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return 0; + } + return ep->local; +} + +/******************************************************************************/ +/* + * Return the global object + */ + +MprVar *ejsGetGlobalObject(EjsId eid) +{ + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return 0; + } + return ep->global; +} + +/******************************************************************************/ +/* + * Copy the value of an object property. Return value is in "value". + * If deepCopy is true, copy all object/strings. Otherwise, object reference + * counts are incremented. Callers must always call mprDestroyVar on the + * return value to prevent leaks. + * + * Returns: -1 on errors or if the variable is not found. + */ + +int ejsCopyVar(EjsId eid, const char *var, MprVar *value, bool deepCopy) +{ + Ejs *ep; + MprVar *vp; + + mprAssert(var && *var); + mprAssert(value); + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return -1; + } + + if (ejsGetVarCore(ep, var, 0, &vp, 0) < 0) { + return -1; + } + + return mprCopyProperty(value, vp, deepCopy); +} + +/******************************************************************************/ +/* + * Return the value of an object property. Return value is in "value". + * Objects and strings are not copied and reference counts are not modified. + * Callers should NOT call mprDestroyVar. Returns: -1 on errors or if the + * variable is not found. + */ + +int ejsReadVar(EjsId eid, const char *var, MprVar *value) +{ + Ejs *ep; + MprVar *vp; + + mprAssert(var && *var); + mprAssert(value); + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return -1; + } + + if (ejsGetVarCore(ep, var, 0, &vp, 0) < 0) { + return -1; + } + + return mprReadProperty(vp, value); +} + +/******************************************************************************/ +/* + * Set a variable that may be an arbitrarily complex object or array reference. + * Will always define in the top most variable frame. + */ + +int ejsWriteVar(EjsId eid, const char *var, MprVar *value) +{ + Ejs *ep; + MprVar *vp; + + mprAssert(var && *var); + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return -1; + } + + if (ejsGetVarCore(ep, var, 0, &vp, EJS_FLAGS_CREATE) < 0) { + return -1; + } + mprAssert(vp); + + /* + * Only copy the value. Don't overwrite the object's name + */ + mprWriteProperty(vp, value); + + return 0; +} + +/******************************************************************************/ +/* + * Set a variable that may be an arbitrarily complex object or array reference. + * Will always define in the top most variable frame. + */ + +int ejsWriteVarValue(EjsId eid, const char *var, MprVar value) +{ + return ejsWriteVar(eid, var, &value); +} + +/******************************************************************************/ +/* + * Delete a variable + */ + +int ejsDeleteVar(EjsId eid, const char *var) +{ + Ejs *ep; + MprVar *vp; + MprVar *obj; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return -1; + } + if (ejsGetVarCore(ep, var, &obj, &vp, 0) < 0) { + return -1; + } + mprDeleteProperty(obj, vp->name); + return 0; +} + +/******************************************************************************/ +/* + * Set the expression return value + */ + +void ejsSetReturnValue(EjsId eid, MprVar value) +{ + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return; + } + mprCopyVar(&ep->result, &value, MPR_SHALLOW_COPY); +} + +/******************************************************************************/ +/* + * Set the expression return value to a string value + */ + +void ejsSetReturnString(EjsId eid, const char *str) +{ + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return; + } + mprCopyVarValue(&ep->result, mprCreateStringVar(str, 0), MPR_SHALLOW_COPY); +} + +/******************************************************************************/ +/* + * Get the expression return value + */ + +MprVar *ejsGetReturnValue(EjsId eid) +{ + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return 0; + } + return &ep->result; +} + +/******************************************************************************/ +/* + * Define a C function. If eid < 0, then update the master object with this + * function. NOTE: in this case, functionName must be simple without any "." or + * "[]" elements. If eid >= 0, add to the specified script engine. In this + * case, functionName can be an arbitrary object reference and can contain "." + * or "[]". + */ + +void ejsDefineCFunction(EjsId eid, const char *functionName, MprCFunction fn, + void *thisPtr, int flags) +{ + if (eid < 0) { + ejsLock(); + mprCreatePropertyValue(&master, functionName, + mprCreateCFunctionVar(fn, thisPtr, flags)); + ejsUnlock(); + } else { + ejsWriteVarValue(eid, functionName, + mprCreateCFunctionVar(fn, thisPtr, flags)); + } +} + +/******************************************************************************/ +/* + * Define a C function with String arguments + */ + +void ejsDefineStringCFunction(EjsId eid, const char *functionName, + MprStringCFunction fn, void *thisPtr, int flags) +{ + if (eid < 0) { + ejsLock(); + mprCreatePropertyValue(&master, functionName, + mprCreateStringCFunctionVar(fn, thisPtr, flags)); + ejsUnlock(); + } else { + ejsWriteVarValue(eid, functionName, + mprCreateStringCFunctionVar(fn, thisPtr, flags)); + } +} + +/******************************************************************************/ +/* + * Define a JavaScript function. Args should be comma separated. + * Body should not contain braces. + */ + +void ejsDefineFunction(EjsId eid, const char *functionName, char *args, + char *body) +{ + MprVar v; + + v = mprCreateFunctionVar(args, body, 0); + if (eid < 0) { + ejsLock(); + mprCreateProperty(&master, functionName, &v); + ejsUnlock(); + } else { + ejsWriteVar(eid, functionName, &v); + } + mprDestroyVar(&v); +} + +/******************************************************************************/ + +void *ejsGetThisPtr(EjsId eid) +{ + Ejs *ep; + + if ((ep = ejsPtr(eid)) == NULL) { + mprAssert(ep); + return 0; + } + return ep->thisPtr; +} + +/******************************************************************************/ +/* + * Find a variable given a variable name and return the parent object and + * the variable itself, the variable . This routine supports variable names + * that may be objects or arrays but may NOT have expressions in the array + * indicies. Returns -1 on errors or if the variable is not found. + */ + +int ejsGetVarCore(Ejs *ep, const char *vname, MprVar **obj, + MprVar **varValue, int flags) +{ + MprVar *currentObj; + MprVar *currentVar; + char tokBuf[EJS_MAX_ID]; + char *propertyName, *token, *next, *cp, *varName; + + if (obj) { + *obj = 0; + } + if (varValue) { + *varValue = 0; + } + currentObj = ejsFindObj(ep, 0, vname, flags); + currentVar = 0; + propertyName = 0; + + next = varName = mprStrdup(vname); + + token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); + + while (currentObj != 0 && token != 0 && *token) { + + if (*token == '[') { + token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); + + propertyName = token; + if (*propertyName == '\"') { + propertyName++; + if ((cp = strchr(propertyName, '\"')) != 0) { + *cp = '\0'; + } + } else if (*propertyName == '\'') { + propertyName++; + if ((cp = strchr(propertyName, '\'')) != 0) { + *cp = '\0'; + } + } + + currentObj = currentVar; + currentVar = ejsFindProperty(ep, 0, currentObj, propertyName, 0); + + token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); + if (*token != ']') { + mprFree(varName); + return -1; + } + + } else if (*token == '.') { + token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); + if (!isalpha((int) token[0]) && + token[0] != '_' && token[0] != '$') { + mprFree(varName); + return -1; + } + + propertyName = token; + currentObj = currentVar; + currentVar = ejsFindProperty(ep, 0, currentObj, token, 0); + + } else { + currentVar = ejsFindProperty(ep, 0, currentObj, token, 0); + } + token = getNextVarToken(&next, tokBuf, sizeof(tokBuf)); + } + mprFree(varName); + + if (currentVar == 0 && currentObj >= 0 && flags & EJS_FLAGS_CREATE) { + currentVar = mprCreatePropertyValue(currentObj, propertyName, + mprCreateUndefinedVar()); + } + if (obj) { + *obj = currentObj; + } + + /* + * Don't use mprCopyVar as it will copy the data + */ + if (varValue) { + *varValue = currentVar; + } + return currentVar ? 0 : -1; +} + +/******************************************************************************/ +/* + * Get the next token as part of a variable specification. This will return + * a pointer to the next token and will return a pointer to the next token + * (after this one) in "next". The tokBuf holds the parsed token. + */ +static char *getNextVarToken(char **next, char *tokBuf, int tokBufLen) +{ + char *start, *cp; + int len; + + start = *next; + while (isspace((int) *start) || *start == '\n' || *start == '\r') { + start++; + } + cp = start; + + if (*cp == '.' || *cp == '[' || *cp == ']') { + cp++; + } else { + while (*cp && *cp != '.' && *cp != '[' && *cp != ']' && + !isspace((int) *cp) && *cp != '\n' && *cp != '\r') { + cp++; + } + } + len = mprMemcpy(tokBuf, tokBufLen - 1, start, cp - start); + tokBuf[len] = '\0'; + + *next = cp; + return tokBuf; +} + +/******************************************************************************/ +/* + * Get the EJS structure pointer + */ + +Ejs *ejsPtr(EjsId eid) +{ + Ejs *handle; + int intId; + + intId = (int) eid; + + ejsLock(); + mprAssert(0 <= intId && intId < ejsList->max); + + if (intId < 0 || intId >= ejsList->max || ejsList->handles[intId] == NULL) { + mprAssert(0); + ejsUnlock(); + return NULL; + } + handle = ejsList->handles[intId]; + ejsUnlock(); + return handle; +} + +/******************************************************************************/ +/* + * Utility routine to crack JavaScript arguments. Return the number of args + * seen. This routine only supports %s and %d type args. + * + * Typical usage: + * + * if (ejsParseArgs(argc, argv, "%s %d", &name, &age) < 2) { + * mprError("Insufficient args\n"); + * return -1; + * } + */ + +int ejsParseArgs(int argc, char **argv, char *fmt, ...) +{ + va_list vargs; + bool *bp; + char *cp, **sp, *s; + int *ip, argn; + + va_start(vargs, fmt); + + if (argv == 0) { + return 0; + } + + for (argn = 0, cp = fmt; cp && *cp && argn < argc && argv[argn]; ) { + if (*cp++ != '%') { + continue; + } + + s = argv[argn]; + switch (*cp) { + case 'b': + bp = va_arg(vargs, bool*); + if (bp) { + if (strcmp(s, "true") == 0 || s[0] == '1') { + *bp = 1; + } else { + *bp = 0; + } + } else { + *bp = 0; + } + break; + + case 'd': + ip = va_arg(vargs, int*); + *ip = atoi(s); + break; + + case 's': + sp = va_arg(vargs, char**); + *sp = s; + break; + + default: + mprAssert(0); + } + argn++; + } + + va_end(vargs); + return argn; +} + +/******************************************************************************/ + +#else +void ejsDummy() {} + +/******************************************************************************/ +#endif /* BLD_FEATURE_EJS */ + +/******************************************************************************/ +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/ejs/ejsParser.c b/source4/lib/ejs/ejsParser.c index 72b0889c55..a5c40e593d 100644 --- a/source4/lib/ejs/ejsParser.c +++ b/source4/lib/ejs/ejsParser.c @@ -35,7 +35,7 @@ /********************************** Includes **********************************/ -#include "lib/ejs/ejsInternal.h" +#include "ejsInternal.h" #if BLD_FEATURE_EJS @@ -1128,9 +1128,11 @@ static int parseFunctionDec(Ejs *ep, int state, int flags) mprDestroyVar(&v); return EJS_STATE_ERR; } - - /* register the function name early to allow for recursive - function calls (see note in ECMA standard, page 71) */ + + /* + * Register the function name early to allow for recursive + * function calls (see note in ECMA standard, page 71) + */ if (!(flags & EJS_FLAGS_ASSIGNMENT)) { currentObj = ejsFindObj(ep, 0, procName, flags); vp = mprSetProperty(currentObj, procName, &v); @@ -2127,7 +2129,8 @@ static int evalFunction(Ejs *ep, MprVar *obj, int flags) * Run a function */ -int ejsRunFunction(int eid, MprVar *obj, const char *functionName, MprArray *args) +int ejsRunFunction(int eid, MprVar *obj, const char *functionName, + MprArray *args) { EjsProc proc, *saveProc; Ejs *ep; @@ -2284,7 +2287,8 @@ static void appendValue(MprVar *dest, MprVar *src) oldLen = strlen(oldBuf); buf = mprRealloc(oldBuf, (len + oldLen + 1) * sizeof(char)); dest->string = buf; - strncpy(&buf[oldLen], value, len+1); + strcpy(&buf[oldLen], value); + } else { *dest = mprCreateStringVar(value, 1); } diff --git a/source4/lib/ejs/ejsProcs.c b/source4/lib/ejs/ejsProcs.c index e9932406ad..c01f411161 100644 --- a/source4/lib/ejs/ejsProcs.c +++ b/source4/lib/ejs/ejsProcs.c @@ -34,7 +34,7 @@ */ /********************************** Includes **********************************/ -#include "lib/ejs/ejsInternal.h" +#include "ejsInternal.h" #if BLD_FEATURE_EJS diff --git a/source4/lib/ejs/var.c b/source4/lib/ejs/var.c index 70a277ae73..2ba56987dd 100644 --- a/source4/lib/ejs/var.c +++ b/source4/lib/ejs/var.c @@ -44,7 +44,7 @@ /********************************** Includes **********************************/ -#include "lib/ejs/var.h" +#include "var.h" /*********************************** Locals ***********************************/ #if VAR_DEBUG @@ -344,7 +344,8 @@ MprType mprGetVarType(MprVar *vp) * already exists in the object, then just write its value. */ -MprVar *mprCreateProperty(MprVar *obj, const char *propertyName, MprVar *newValue) +MprVar *mprCreateProperty(MprVar *obj, const char *propertyName, + MprVar *newValue) { MprVar *prop, *last; int bucketIndex; @@ -376,7 +377,7 @@ MprVar *mprCreateProperty(MprVar *obj, const char *propertyName, MprVar *newValu } if (prop) { - /* FUTURE -- remove. Just for debug. */ + /* FUTURE -- remove. Just for debug. */ mprAssert(prop == 0); mprLog(0, "Attempting to create property %s in object %s\n", propertyName, obj->name); @@ -428,7 +429,8 @@ MprVar *mprCreateProperty(MprVar *obj, const char *propertyName, MprVar *newValu * by pointer. */ -MprVar *mprCreatePropertyValue(MprVar *obj, const char *propertyName, MprVar newValue) +MprVar *mprCreatePropertyValue(MprVar *obj, const char *propertyName, + MprVar newValue) { return mprCreateProperty(obj, propertyName, &newValue); } @@ -513,7 +515,8 @@ MprVar *mprSetProperty(MprVar *obj, const char *propertyName, MprVar *newValue) * new value is passed by value rather than by pointer. */ -MprVar *mprSetPropertyValue(MprVar *obj, const char *propertyName, MprVar newValue) +MprVar *mprSetPropertyValue(MprVar *obj, const char *propertyName, + MprVar newValue) { return mprSetProperty(obj, propertyName, &newValue); } @@ -602,7 +605,7 @@ MprVar *mprGetProperty(MprVar *obj, const char *property, MprVar *value) for (prop = getObjChain(obj->properties, property); prop; prop = prop->forw) { - if (prop->name && + if (prop->name && prop->name[0] == property[0] && strcmp(prop->name, property) == 0) { break; } @@ -1170,7 +1173,8 @@ MprVar mprCreateCFunctionVar(MprCFunction fn, void *thisPtr, int flags) * Initialize a C function. */ -MprVar mprCreateStringCFunctionVar(MprStringCFunction fn, void *thisPtr, int flags) +MprVar mprCreateStringCFunctionVar(MprStringCFunction fn, void *thisPtr, + int flags) { MprVar v; @@ -1183,13 +1187,14 @@ MprVar mprCreateStringCFunctionVar(MprStringCFunction fn, void *thisPtr, int fla return v; } +/******************************************************************************/ /* - * Initialize an opaque pointer. + * Initialize an opaque pointer. */ -MprVar mprCreatePtrVar(void *ptr, const char *name) +MprVar mprCreatePtrVar(void *ptr) { - MprVar v; + MprVar v; memset(&v, 0x0, sizeof(v)); v.type = MPR_TYPE_PTR; @@ -1356,7 +1361,7 @@ MprVar mprCreateStringVar(const char *value, bool allocate) v.string = mprStrdup(value); v.allocatedData = 1; } else { - v.string = value; + v.string = (char*) value; } return v; } @@ -1410,14 +1415,14 @@ static void copyVarCore(MprVar *dest, MprVar *src, int copyDepth) dest->boolean = src->boolean; break; - case MPR_TYPE_STRING_CFUNCTION: - dest->cFunctionWithStrings = src->cFunctionWithStrings; - break; - case MPR_TYPE_PTR: dest->ptr = src->ptr; break; + case MPR_TYPE_STRING_CFUNCTION: + dest->cFunctionWithStrings = src->cFunctionWithStrings; + break; + case MPR_TYPE_CFUNCTION: dest->cFunction = src->cFunction; break; @@ -1617,6 +1622,10 @@ void mprVarToString(char** out, int size, char *fmt, MprVar *obj) *out = mprStrdup("null"); break; + case MPR_TYPE_PTR: + mprAllocSprintf(out, size, "[Opaque Pointer %p]", obj->ptr); + break; + case MPR_TYPE_BOOL: if (obj->boolean) { *out = mprStrdup("true"); @@ -1665,16 +1674,12 @@ void mprVarToString(char** out, int size, char *fmt, MprVar *obj) mprAllocSprintf(out, size, "[C StringFunction]"); break; - case MPR_TYPE_PTR: - mprAllocSprintf(out, size, "[C Pointer: %p]", obj->ptr); - break; - case MPR_TYPE_FUNCTION: mprAllocSprintf(out, size, "[JavaScript Function]"); break; case MPR_TYPE_OBJECT: - /* FUTURE -- really want: [object class: name] */ + /* FUTURE -- really want: [object class: name] */ mprAllocSprintf(out, size, "[object %s]", obj->name); break; @@ -1753,6 +1758,7 @@ MprVar mprParseVar(char *buf, MprType preferredType) case MPR_TYPE_OBJECT: case MPR_TYPE_UNDEFINED: case MPR_TYPE_NULL: + case MPR_TYPE_PTR: default: break; @@ -2157,7 +2163,7 @@ bool mprIsNan(double f) #if WIN return _isnan(f); #elif VXWORKS - /* FUTURE */ + /* FUTURE */ return (0); #else return (f == FP_NAN); @@ -2170,7 +2176,7 @@ bool mprIsInfinite(double f) #if WIN return !_finite(f); #elif VXWORKS - /* FUTURE */ + /* FUTURE */ return (0); #else return (f == FP_INFINITE); diff --git a/source4/lib/ejs/var.h b/source4/lib/ejs/var.h index 4882aacf0f..300a0795d8 100644 --- a/source4/lib/ejs/var.h +++ b/source4/lib/ejs/var.h @@ -56,7 +56,7 @@ /********************************* Includes ***********************************/ -#include "lib/ejs/miniMpr.h" +#include "miniMpr.h" /********************************** Defines ***********************************/ @@ -92,7 +92,7 @@ typedef int MprType; #define MPR_TYPE_FUNCTION 8 /* JavaScript function */ #define MPR_TYPE_STRING 9 /* String (immutable) */ #define MPR_TYPE_STRING_CFUNCTION 10 /* C/C++ function with string args */ -#define MPR_TYPE_PTR 11 /* C pointer */ +#define MPR_TYPE_PTR 11 /* Opaque pointer */ /* * Create a type for the default number type @@ -140,7 +140,7 @@ typedef BLD_FEATURE_NUM_TYPE MprNum; #define mprVarIsFloating(type) \ (type == MPR_TYPE_FLOAT) #define mprVarIsPtr(type) \ - (type == MPR_TYPE_PTR) + (type == MPR_TYPE_PTR) #define mprVarIsUndefined(var) \ ((var)->type == MPR_TYPE_UNDEFINED) #define mprVarIsNull(var) \ @@ -252,9 +252,12 @@ typedef struct MprProperties { /* Collection of properties */ #endif struct MprVar **buckets; /* Hash chains */ int numItems; /* Total count of items */ + /* FUTURE - Better way of doing this */ int numDataItems; /* Enumerable data items */ uint hashSize : 8; /* Size of the hash table */ + /* FUTURE -- increase size of refCount */ uint refCount : 8; /* References to this property*/ + /* FUTURE - make these flags */ uint deleteProtect : 8; /* Don't recursively delete */ uint visited : 8; /* Node has been processed */ } MprProperties; @@ -263,8 +266,11 @@ typedef struct MprProperties { /* Collection of properties */ * Universal Variable Type */ typedef struct MprVar { + /* FUTURE - remove name to outside reference */ MprStr name; /* Property name */ + /* FUTURE - remove */ MprStr fullName; /* Full object name */ + /* FUTURE - make part of the union */ MprProperties *properties; /* Pointer to properties */ /* @@ -318,7 +324,7 @@ typedef struct MprVar { void *thisPtr; } cFunctionWithStrings; MprStr string; /* Allocated string */ - void *ptr; /* C pointer */ + void *ptr; /* Opaque pointer */ #if !BLD_DEBUG && !LINUX && !VXWORKS }; #endif @@ -355,7 +361,6 @@ extern MprVar mprCreateObjVar(const char *name, int hashSize); extern MprVar mprCreateBoolVar(bool value); extern MprVar mprCreateCFunctionVar(MprCFunction fn, void *thisPtr, int flags); -extern MprVar mprCreatePtrVar(void *ptr, const char *name); #if BLD_FEATURE_FLOATING_POINT extern MprVar mprCreateFloatVar(double value); #endif @@ -370,6 +375,7 @@ extern MprVar mprCreateStringCFunctionVar(MprStringCFunction fn, void *thisPtr, int flags); extern MprVar mprCreateStringVar(const char *value, bool allocate); extern MprVar mprCreateUndefinedVar(void); +extern MprVar mprCreatePtrVar(void *ptr); extern bool mprDestroyVar(MprVar *vp); extern bool mprDestroyAllVars(MprVar* vp); extern MprType mprGetVarType(MprVar *vp); @@ -404,9 +410,12 @@ extern int mprDeleteProperty(MprVar *obj, const char *property); /* * Get/Set properties. Set will update/create. */ -extern MprVar *mprGetProperty(MprVar *obj, const char *property, MprVar *value); -extern MprVar *mprSetProperty(MprVar *obj, const char *property, MprVar *value); -extern MprVar *mprSetPropertyValue(MprVar *obj, const char *property, MprVar value); +extern MprVar *mprGetProperty(MprVar *obj, const char *property, + MprVar *value); +extern MprVar *mprSetProperty(MprVar *obj, const char *property, + MprVar *value); +extern MprVar *mprSetPropertyValue(MprVar *obj, const char *property, + MprVar value); /* * Directly read/write property values (the property must already exist) -- cgit