3 * @brief MPR Universal Variable Type
6 * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
7 * Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
9 * This software is distributed under commercial and open source licenses.
10 * You may use the GPL open source license described below or you may acquire
11 * a commercial license from Mbedthis Software. You agree to be fully bound
12 * by the terms of either license. Consult the LICENSE.TXT distributed with
13 * this software for full details.
15 * This software is open source; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version. See the GNU General Public License for more
19 * details at: http://www.mbedthis.com/downloads/gplLicense.html
21 * This program is distributed WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * This GPL license does NOT permit incorporating this software into
25 * proprietary programs. If you are unable to comply with the GPL, you must
26 * acquire a commercial license to use this software. Commercial licenses
27 * for this software and support services are available from Mbedthis
28 * Software at http://www.mbedthis.com
33 /******************************* Documentation ********************************/
35 * Variables can efficiently store primitive types and can hold references to
36 * objects. Objects can store properties which are themselves variables.
37 * Properties can be primitive data types, other objects or functions.
38 * Properties are indexed by a character name. A variable may store one of
39 * the following types:
41 * string, integer, integer-64bit, C function, C function with string args,
42 * Javascript function, Floating point number, boolean value, Undefined
43 * value and the Null value.
45 * Variables have names while objects may be referenced by multiple variables.
46 * Objects use reference counting for garbage collection.
48 * This module is not thread safe for performance and compactness. It relies
49 * on upper modules to provide thread synchronization as required. The API
50 * provides primitives to get variable/object references or to get copies of
51 * variables which will help minimize required lock times.
57 /********************************* Includes ***********************************/
59 #include "web_server/ejs/miniMpr.h"
61 /********************************** Defines ***********************************/
64 * Define VAR_DEBUG if you want to track objects. However, this code is not
65 * thread safe and you need to run the server single threaded.
74 * Forward declare types
80 * Possible variable types. Don't use enum because we need to be able to
81 * do compile time conditional compilation on BLD_FEATURE_NUM_TYPE_ID.
84 #define MPR_TYPE_UNDEFINED 0 ///< Undefined. No value has been set.
85 #define MPR_TYPE_NULL 1 ///< Value defined to be null.
86 #define MPR_TYPE_BOOL 2 ///< Boolean type.
87 #define MPR_TYPE_CFUNCTION 3 ///< C function or C++ method
88 #define MPR_TYPE_FLOAT 4 ///< Floating point number
89 #define MPR_TYPE_INT 5 ///< Integer number
90 #define MPR_TYPE_INT64 6 ///< 64-bit Integer number
91 #define MPR_TYPE_OBJECT 7 ///< Object reference
92 #define MPR_TYPE_FUNCTION 8 ///< JavaScript function
93 #define MPR_TYPE_STRING 9 ///< String (immutable)
94 #define MPR_TYPE_STRING_CFUNCTION 10 ///< C/C++ function with string args
97 * Create a type for the default number type
98 * Config.h will define the default number type. For example:
100 * BLD_FEATURE_NUM_TYPE=int
101 * BLD_FEATURE_NUM_TYPE_ID=MPR_TYPE_INT
105 * Set to the type used for MPR numeric variables. Will equate to int, int64
108 typedef BLD_FEATURE_NUM_TYPE MprNum;
111 * Set to the MPR_TYPE used for MPR numeric variables. Will equate to
112 * MPR_TYPE_INT, MPR_TYPE_INT64 or MPR_TYPE_FLOAT.
114 #define MPR_NUM_VAR BLD_FEATURE_NUM_TYPE_ID
115 #define MPR_TYPE_NUM BLD_FEATURE_NUM_TYPE_ID
118 * Return TRUE if a variable is a function type
120 #define mprVarIsFunction(type) \
121 (type == MPR_TYPE_FUNCTION || type == MPR_TYPE_STRING_CFUNCTION || \
122 type == MPR_TYPE_CFUNCTION)
125 * Return TRUE if a variable is a numeric type
127 #define mprVarIsNumber(type) \
128 (type == MPR_TYPE_INT || type == MPR_TYPE_INT64 || type == MPR_TYPE_FLOAT)
131 * Return TRUE if a variable is a boolean
133 #define mprVarIsBoolean(type) \
134 (type == MPR_TYPE_BOOL)
135 #define mprVarIsString(type) \
136 (type == MPR_TYPE_STRING)
137 #define mprVarIsObject(type) \
138 (type == MPR_TYPE_OBJECT)
139 #define mprVarIsFloating(type) \
140 (type == MPR_TYPE_FLOAT)
141 #define mprVarIsUndefined(var) \
142 ((var)->type == MPR_TYPE_UNDEFINED)
143 #define mprVarIsNull(var) \
144 ((var)->type == MPR_TYPE_NULL)
145 #define mprVarIsValid(var) \
146 (((var)->type != MPR_TYPE_NULL) && ((var)->type != MPR_TYPE_UNDEFINED))
148 #define MPR_VAR_MAX_RECURSE 5 /* Max object loops */
150 #if BLD_FEATURE_SQUEEZE
151 #define MPR_MAX_VAR 64 /* Max var full name */
153 #define MPR_MAX_VAR 512
158 #endif /* _NO_PACK */
161 * Function signatures
163 typedef int MprVarHandle;
164 typedef int (*MprCFunction)(MprVarHandle userHandle, int argc,
165 struct MprVar **argv);
166 typedef int (*MprStringCFunction)(MprVarHandle userHandle, int argc,
173 MPR_VAR_WRITE, /* This property is being updated */
174 MPR_VAR_READ, /* This property is being read */
175 MPR_VAR_CREATE_PROPERTY, /* A property is being created */
176 MPR_VAR_DELETE_PROPERTY, /* A property is being deleted */
177 MPR_VAR_DELETE /* This object is being deleted */
181 * Trigger function return codes.
184 MPR_TRIGGER_ABORT, /* Abort the current operation */
185 MPR_TRIGGER_USE_NEW_VALUE, /* Proceed and use the newValue */
186 MPR_TRIGGER_PROCEED /* Proceed with the operation */
187 } MprVarTriggerStatus;
190 * The MprVarTrigger arguments have the following meaning:
192 * op The operation being performed. See MprVarTriggerOp.
193 * parentProperties Pointer to the MprProperties structure.
194 * vp Pointer to the property that registered the trigger.
195 * newValue New value (see below for more details).
196 * copyDepth Specify what data items to copy.
198 * For VAR_READ, newVar is set to a temporary variable that the trigger
199 * function may assign a value to be returned instead of the actual
201 * For VAR_WRITE, newValue holds the new value. The old existing value may be
203 * For DELETE_PROPERTY, vp is the property being deleted. newValue is null.
204 * For ADD_PROPERTY, vp is set to the property being added and newValue holds
207 typedef MprVarTriggerStatus (*MprVarTrigger)(MprVarTriggerOp op,
208 struct MprProperties *parentProperties, struct MprVar *vp,
209 struct MprVar *newValue, int copyDepth);
212 * mprCreateFunctionVar flags
214 /** Use the alternate handle on function callbacks */
215 #define MPR_VAR_ALT_HANDLE 0x1
217 /** Use the script handle on function callbacks */
218 #define MPR_VAR_SCRIPT_HANDLE 0x2
221 * Useful define for the copyDepth argument
223 /** Don't copy any data. Copy only the variable name */
224 #define MPR_NO_COPY 0
226 /** Copy strings. Increment object reference counts. */
227 #define MPR_SHALLOW_COPY 1
229 /** Copy strings and do complete object copies. */
230 #define MPR_DEEP_COPY 2
233 * GetFirst / GetNext flags
235 /** Step into data properties. */
236 #define MPR_ENUM_DATA 0x1
238 /** Step into functions properties. */
239 #define MPR_ENUM_FUNCTIONS 0x2
242 * Collection type to hold properties in an object
244 typedef struct MprProperties { /* Collection of properties */
246 struct MprProperties *next; /* Linked list */
247 struct MprProperties *prev; /* Linked list */
248 char name[32]; /* Debug name */
250 struct MprVar **buckets; /* Hash chains */
251 int numItems; /* Total count of items */
252 int numDataItems; /* Enumerable data items */
253 uint hashSize : 8; /* Size of the hash table */
254 uint refCount : 8; /* References to this property*/
255 uint deleteProtect : 8; /* Don't recursively delete */
256 uint visited : 8; /* Node has been processed */
260 * Universal Variable Type
262 typedef struct MprVar {
263 MprStr name; /* Property name */
264 MprStr fullName; /* Full object name */
265 MprProperties *properties; /* Pointer to properties */
270 MprType type : 8; /* Selector into union */
271 uint bucketIndex : 8; /* Copy of bucket index */
273 uint flags : 5; /* Type specific flags */
274 uint allocatedData : 1; /* Data needs freeing */
275 uint readonly : 1; /* Unmodifiable */
276 uint deleteProtect : 1; /* Don't recursively delete */
278 uint visited : 1; /* Node has been processed */
279 uint allocatedVar : 1; /* Var needs freeing */
280 uint spare : 6; /* Unused */
282 struct MprVar *forw; /* Hash table linkage */
283 MprVarTrigger trigger; /* Trigger function */
286 struct MprVar *baseClass; /* Pointer to class object */
288 MprProperties *parentProperties; /* Pointer to parent object */
291 * Union of primitive types. When debugging on Linux, don't use unions
292 * as the gdb debugger can't display them.
294 #if !BLD_DEBUG && !LINUX && !VXWORKS
297 int boolean; /* Use int for speed */
298 #if BLD_FEATURE_FLOATING_POINT
302 #if BLD_FEATURE_INT64
305 struct { /* Javascript functions */
306 MprArray *args; /* Null terminated */
309 struct { /* Function with MprVar args */
313 struct { /* Function with string args */
314 MprStringCFunction fn;
316 } cFunctionWithStrings;
317 MprStr string; /* Allocated string */
318 #if !BLD_DEBUG && !LINUX && !VXWORKS
324 * Define a field macro so code an use numbers in a "generic" fashion.
326 #if MPR_NUM_VAR == MPR_TYPE_INT || DOXYGEN
327 //* Default numeric type */
328 #define mprNumber integer
330 #if MPR_NUM_VAR == MPR_TYPE_INT64
331 //* Default numeric type */
332 #define mprNumber integer64
334 #if MPR_NUM_VAR == MPR_TYPE_FLOAT
335 //* Default numeric type */
336 #define mprNumber floating
339 typedef BLD_FEATURE_NUM_TYPE MprNumber;
344 #endif /* __NO_PACK */
346 /********************************* Prototypes *********************************/
348 * Variable constructors and destructors
350 extern MprVar mprCreateObjVar(const char *name, int hashSize);
351 extern MprVar mprCreateBoolVar(bool value);
352 extern MprVar mprCreateCFunctionVar(MprCFunction fn, void *thisPtr,
354 #if BLD_FEATURE_FLOATING_POINT
355 extern MprVar mprCreateFloatVar(double value);
357 extern MprVar mprCreateIntegerVar(int value);
358 #if BLD_FEATURE_INT64
359 extern MprVar mprCreateInteger64Var(int64 value);
361 extern MprVar mprCreateFunctionVar(char *args, char *body, int flags);
362 extern MprVar mprCreateNullVar(void);
363 extern MprVar mprCreateNumberVar(MprNumber value);
364 extern MprVar mprCreateStringCFunctionVar(MprStringCFunction fn,
365 void *thisPtr, int flags);
366 extern MprVar mprCreateStringVar(const char *value, bool allocate);
367 extern MprVar mprCreateUndefinedVar(void);
368 extern bool mprDestroyVar(MprVar *vp);
369 extern bool mprDestroyAllVars(MprVar* vp);
370 extern MprType mprGetVarType(MprVar *vp);
375 extern void mprCopyVar(MprVar *dest, MprVar *src, int copyDepth);
376 extern void mprCopyVarValue(MprVar *dest, MprVar src, int copyDepth);
377 extern MprVar *mprDupVar(MprVar *src, int copyDepth);
383 mprAddVarTrigger(MprVar *vp, MprVarTrigger fn);
384 extern int mprGetVarRefCount(MprVar *vp);
385 extern void mprSetVarDeleteProtect(MprVar *vp, int deleteProtect);
386 extern void mprSetVarFullName(MprVar *vp, char *name);
387 extern void mprSetVarReadonly(MprVar *vp, int readonly);
388 extern void mprSetVarName(MprVar *vp, char *name);
391 * Create properties and return a reference to the property.
393 extern MprVar *mprCreateProperty(MprVar *obj, const char *property,
395 extern MprVar *mprCreatePropertyValue(MprVar *obj, const char *property,
397 extern int mprDeleteProperty(MprVar *obj, const char *property);
400 * Get/Set properties. Set will update/create.
402 extern MprVar *mprGetProperty(MprVar *obj, const char *property, MprVar *value);
403 extern MprVar *mprSetProperty(MprVar *obj, const char *property, MprVar *value);
404 extern MprVar *mprSetPropertyValue(MprVar *obj, const char *property, MprVar value);
407 * Directly read/write property values (the property must already exist)
408 * For mprCopyProperty, mprDestroyVar must always called on the var.
410 extern int mprReadProperty(MprVar *prop, MprVar *value);
411 extern int mprWriteProperty(MprVar *prop, MprVar *newValue);
412 extern int mprWritePropertyValue(MprVar *prop, MprVar newValue);
415 * Copy a property. NOTE: reverse of most other args: (dest, src)
417 extern int mprCopyProperty(MprVar *dest, MprVar *prop, int copyDepth);
420 * Enumerate properties
422 extern MprVar *mprGetFirstProperty(MprVar *obj, int includeFlags);
423 extern MprVar *mprGetNextProperty(MprVar *obj, MprVar *currentProperty,
427 * Query properties characteristics
429 extern int mprGetPropertyCount(MprVar *obj, int includeFlags);
432 * Conversion routines
434 extern MprVar mprParseVar(char *str, MprType prefType);
435 extern MprNum mprVarToNumber(MprVar *vp);
436 extern int mprVarToInteger(MprVar *vp);
437 #if BLD_FEATURE_INT64
438 extern int64 mprVarToInteger64(MprVar *vp);
440 extern bool mprVarToBool(MprVar *vp);
441 #if BLD_FEATURE_FLOATING_POINT
442 extern double mprVarToFloat(MprVar *vp);
444 extern void mprVarToString(char** buf, int size, char *fmt, MprVar *vp);
447 * Parsing and utility routines
449 extern MprNum mprParseNumber(char *str);
450 extern int mprParseInteger(char *str);
452 #if BLD_FEATURE_INT64
453 extern int64 mprParseInteger64(char *str);
456 #if BLD_FEATURE_FLOATING_POINT
457 extern double mprParseFloat(char *str);
458 extern bool mprIsInfinite(double f);
459 extern bool mprIsNan(double f);
463 extern void mprPrintObjects(char *msg);
464 extern void mprPrintObjRefCount(MprVar *vp);
471 /*****************************************************************************/
472 #endif /* _h_MPR_VAR */
480 * vim600: sw=4 ts=4 fdm=marker