ejslib.c

来自「samba最新软件」· C语言 代码 · 共 1,091 行 · 第 1/2 页

C
1,091
字号
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;}/******************************************************************************/#elsevoid 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 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?