ejsprocs.c

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

C
705
字号
/* *	@file 	ejsProc.c *	@brief 	EJS support functions *//********************************* 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/****************************** Forward Declarations **************************//* *	Object constructors */static int 		objectConsProc(EjsHandle eid, int argc, MprVar **argv);static int 		arrayConsProc(EjsHandle eid, int argc, MprVar **argv);static int 		booleanConsProc(EjsHandle eid, int argc, MprVar **agv);static int 		numberConsProc(EjsHandle eid, int argc, MprVar **argv);static int 		stringConsProc(EjsHandle eid, int argc, MprVar **argv);/* *	Core functions */static int 		toStringProc(EjsHandle eid, int argc, MprVar **argv);static int 		valueOfProc(EjsHandle eid, int argc, MprVar **argv);/* *	Triggers */static MprVarTriggerStatus lengthTrigger(MprVarTriggerOp op, 	MprProperties *parentProperties, MprVar *prop, MprVar *newValue,	int copyRef);/******************************************************************************//* *	Routine to create the base common to all object types */MprVar ejsCreateObj(const char *name, int hashSize){	MprVar	o;	o = mprCreateObjVar(name, hashSize);	if (o.type == MPR_TYPE_UNDEFINED) {		mprAssert(0);		return o;	}	mprCreatePropertyValue(&o, "toString", 		mprCreateCFunctionVar(toStringProc, 0, MPR_VAR_SCRIPT_HANDLE));	mprCreatePropertyValue(&o, "valueOf", 		mprCreateCFunctionVar(valueOfProc, 0, MPR_VAR_SCRIPT_HANDLE));	return o;}/******************************************************************************//* *	Routine to destroy a variable */bool ejsDestroyVar(MprVar *obj){	return mprDestroyVar(obj);}/******************************************************************************//* *	Routine to create the base array type */MprVar ejsCreateArray(const char *name, int size){	MprVar	obj, *lp, undef;	char	idx[16];	int		i;	/*	Sanity limit for size of hash table */	obj = ejsCreateObj(name, max(size, 503));	if (obj.type == MPR_TYPE_UNDEFINED) {		mprAssert(0);		return obj;	}	undef = mprCreateUndefinedVar();	for (i = 0; i < size; i++) {		mprItoa(i, idx, sizeof(idx));		mprCreateProperty(&obj, idx, &undef);	}	lp = mprCreatePropertyValue(&obj, "length", mprCreateIntegerVar(size));	mprAssert(lp);	mprSetVarReadonly(lp, 1);	mprAddVarTrigger(lp, lengthTrigger);	return obj;}/******************************************************************************//******************************** Constructors ********************************//******************************************************************************//* *	Object constructor. Nothing really done here. For future expansion. */static int objectConsProc(EjsHandle eid, int argc, MprVar **argv){#if XX_UNUSED_XX	MprVar	*obj;	Ejs		*ep;	if((ep = ejsPtr(eid)) == NULL) {		return -1;	}	obj = mprGetProperty(ep->local, "this", 0);	mprAssert(obj);#endif	return 0;}/******************************************************************************//* *	Array constructor */static int arrayConsProc(EjsHandle eid, int argc, MprVar **argv){	MprVar	*obj, *lp, undef;	Ejs		*ep;	char	idx[16];	int		i, max;	objectConsProc(eid, argc, argv);	if((ep = ejsPtr(eid)) == NULL) {		return -1;	}	obj = mprGetProperty(ep->local, "this", 0);	mprAssert(obj);	if (argc == 1 && mprVarIsNumber(argv[0]->type)) {		/*		 *	x = new Array(size);		 */		undef = mprCreateUndefinedVar();		max = (int) mprVarToInteger(argv[0]);		for (i = 0; i < max; i++) {			mprItoa(i, idx, sizeof(idx));			mprCreateProperty(obj, idx, &undef);		}	} else {		/*		 *	x = new Array(element0, element1, ..., elementN):		 */		max = argc;		for (i = 0; i < max; i++) {			mprItoa(i, idx, sizeof(idx));			mprCreateProperty(obj, idx, argv[i]);		}	}	lp = mprCreatePropertyValue(obj, "length", mprCreateIntegerVar(max));	mprAssert(lp);	mprSetVarReadonly(lp, 1);	mprAddVarTrigger(lp, lengthTrigger);	return 0;}/******************************************************************************//* *	Boolean constructor */static int booleanConsProc(EjsHandle eid, int argc, MprVar **argv){	objectConsProc(eid, argc, argv);	return 0;}/******************************************************************************/#if FUTURE/* *	Date constructor */static int dateConsProc(EjsHandle eid, int argc, MprVar **argv){	objectConsProc(eid, argc, argv);	return 0;}#endif/******************************************************************************//* *	Number constructor */static int numberConsProc(EjsHandle eid, int argc, MprVar **argv){	objectConsProc(eid, argc, argv);	return 0;}/******************************************************************************//* *	String constructor */static int stringConsProc(EjsHandle eid, int argc, MprVar **argv){	objectConsProc(eid, argc, argv);	return 0;}/******************************************************************************//********************************** Functions *********************************//******************************************************************************/static int toStringProc(EjsHandle eid, int argc, MprVar **argv){	MprVar	*obj;	Ejs		*ep;	char	*buf;	int		radix;	if (argc == 0) {		radix = 10;	} else if (argc == 1) {		radix = (int) mprVarToInteger(argv[0]);	} else {		mprAssert(0);		return -1;	}	if((ep = ejsPtr(eid)) == NULL) {		return -1;	}	obj = mprGetProperty(ep->local, "this", 0);	mprAssert(obj);	mprVarToString(&buf, MPR_MAX_STRING, 0, obj);	mprCopyVarValue(&ep->result, mprCreateStringVar(buf, 0), MPR_SHALLOW_COPY);	mprFree(buf);	return 0;}/******************************************************************************/static int valueOfProc(EjsHandle eid, int argc, MprVar **argv){	MprVar	*obj;	Ejs		*ep;	if (argc != 0) {		mprAssert(0);		return -1;	}	if((ep = ejsPtr(eid)) == NULL) {		return -1;	}	obj = mprGetProperty(ep->local, "this", 0);	mprAssert(obj);	switch (obj->type) {	default:	case MPR_TYPE_UNDEFINED:	case MPR_TYPE_NULL:	case MPR_TYPE_CFUNCTION:	case MPR_TYPE_OBJECT:	case MPR_TYPE_FUNCTION:	case MPR_TYPE_STRING_CFUNCTION:	case MPR_TYPE_PTR:		mprCopyVar(&ep->result, obj, MPR_SHALLOW_COPY);		break;	case MPR_TYPE_STRING:		mprCopyVarValue(&ep->result, mprCreateIntegerVar(atoi(obj->string)), 0);		break;	case MPR_TYPE_BOOL:	case MPR_TYPE_INT:#if BLD_FEATURE_INT64	case MPR_TYPE_INT64:#endif#if BLD_FEATURE_FLOATING_POINT	case MPR_TYPE_FLOAT:#endif		mprCopyVar(&ep->result, obj, 0);		break;	} 	return 0;}/******************************************************************************//* *	Var access trigger on the Array.length property. Return the count of *	enumerable properties (don't count functions). */static MprVarTriggerStatus lengthTrigger(MprVarTriggerOp op, 	MprProperties *parentProperties, MprVar *prop, MprVar *newValue, 	int copyRef){	switch (op) {	case MPR_VAR_READ:		/*		 *	Subtract one for the length property	 	 *	FUTURE -- need an API to access parentProperties		 *	FUTURE -- contradiction to be read-only yet allow USE_NEW_VALUE.		 *		API needs finer control.		 */		*newValue = mprCreateIntegerVar(parentProperties->numDataItems - 1);

⌨️ 快捷键说明

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