summaryrefslogtreecommitdiff
path: root/source4/lib/ejs/var.c
blob: 843d2ff738333a403ced93d865c8f5b6895ba524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
/*
 *	@file 	var.c
 *	@brief 	MPR Universal Variable Type
 *	@overview
 *
 *	@copy	default.m
 *	
 *	Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
 *	Copyright (c) Michael O'Brien, 1994-1995. 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
 */

/******************************* Documentation ********************************/

/*
 *	This module is NOT multithreaded. 
 *
 *	Properties are variables that are stored in an object type variable.
 *	Properties can be primitive data types, other objects or functions.
 *	Properties are indexed by a character name.
 */

/********************************** Includes **********************************/

#include	"var.h"

/*********************************** Locals ***********************************/
#if VAR_DEBUG

static MprProperties	objectList;			/* Dummy head of objects list */
static int				objectCount = -1;	/* Count of objects */

#endif
/***************************** Forward Declarations ***************************/

static int 		adjustRefCount(MprProperties *pp, int adj);
static int		adjustVarRefCount(MprVar *vp, int adj);
static MprVar 	*allocProperty(const char *propertyName);
static void 	copyVarCore(MprVar *dest, MprVar *src, int copyDepth);
static MprProperties 
				*createProperties(const char *name, int hashSize);
static bool 	freeVar(MprVar *vp, int force);
static bool 	freeVarStorage(MprVar *vp, int force);
static MprVar	*getObjChain(MprProperties *pp, const char *property);
static int		hash(MprProperties *pp, const char *property);
static bool		releaseProperties(MprProperties *pp, int force);

/*********************************** Code *************************************/
/*
 *	Destroy a variable and all referenced variables. Release any referenced 
 *	object regardless of whether other users still have references. Be VERY
 *	careful using this routine. 
 *
 *	Return TRUE if the underlying data is freed. Objects may not be freed if 
 *	there are other users of the object.
 */

bool mprDestroyAllVars(MprVar *vp)
{
	mprAssert(vp);

	if (vp->trigger) {
		if ((vp->trigger)(MPR_VAR_DELETE, vp->parentProperties, vp, 0, 0) 
				== MPR_TRIGGER_ABORT) {
			return 0;
		}
	}

	/*
	 *	Free the actual value. If this var refers to an object, we will 
	 *	recurse through all the properties freeing all vars.
	 */
	return freeVar(vp, 1);
}

/******************************************************************************/
/*
 *	Destroy a variable. Release any referenced object (destroy if no other
 *	users are referencing).
 *
 *	Return TRUE if the underlying data is freed. Objects may not be freed if 
 *	there are other users of the object.
 */

bool mprDestroyVar(MprVar *vp)
{
	mprAssert(vp);

	if (vp->trigger) {
		if ((vp->trigger)(MPR_VAR_DELETE, vp->parentProperties, vp, 0, 0) 
				== MPR_TRIGGER_ABORT) {
			return 0;
		}
	}

	/*
	 *	Free the actual value. If this var refers to an object, we will 
	 *	recurse through all the properties freeing all that have no other
	 *	references.
	 */
	return freeVar(vp, 0);
}

/******************************************************************************/
/*
 *	Free the value in a variable for primitive types. Release objects.
 *
 *	Return TRUE if the underlying data is freed. Objects may not be freed if 
 *	there are other users of the object.
 */

static bool freeVar(MprVar *vp, int force)
{
	bool	freed;

	mprAssert(vp);

	freed = freeVarStorage(vp, force);

	mprFree(vp->name);
	mprFree(vp->fullName);

	if (vp->allocatedVar) {
		mprFree(vp);
	} else {
		vp->name = 0;
		vp->fullName = 0;
		vp->type = MPR_TYPE_UNDEFINED;
	}
	return freed;
}

/******************************************************************************/
/*
 *	Free the value in a variable for primitive types. Release objects.
 *
 *	Return TRUE if the underlying data is freed. Objects may not be freed if 
 *	there are other users of the object.
 */

static bool freeVarStorage(MprVar *vp, int force)
{
	MprArray	*argList;
	bool		freed;
	int			i;

	freed = 1;
	mprAssert(vp);

	switch (vp->type) {
	default:
		break;

	case MPR_TYPE_STRING:
		if (vp->allocatedData && vp->string != 0) {
			mprFree(vp->string);
			vp->string = 0;
			vp->allocatedData = 0;
		}
		break;

	case MPR_TYPE_OBJECT:
#if VAR_DEBUG
		/*
		 *	Recurse through all properties and release / delete. Release the 
		 *	properties hash table.
		 */
		if (vp->properties->refCount > 1) {
			mprLog(7, "freeVar: ACT \"%s\", 0x%x, ref %d, force %d\n", 
				vp->name, vp->properties, vp->properties->refCount, force);
		} else {
			mprLog(7, "freeVar: DEL \"%s\", 0x%x, ref %d, force %d\n", 
				vp->name, vp->properties, vp->properties->refCount, force);
		}
#endif
		if (vp->allocatedData) {
			freed = releaseProperties(vp->properties, force);
		}
		vp->properties = 0;
		break;

	case MPR_TYPE_FUNCTION:
		if (vp->allocatedData) {
			argList = vp->function.args;
			for (i = 0; i < argList->max; i++) {
				if (argList->handles[i] != 0) {
					mprFree(argList->handles[i]);
				}
			}
			mprDestroyArray(argList);
			vp->function.args = 0;
			mprFree(vp->function.body);
			vp->function.body = 0;
		}
		break;
	}

	vp->type = MPR_TYPE_UNDEFINED;
	return freed;
}

/******************************************************************************/
/*
 *	Adjust the object reference count and return the currrent count of 
 *	users.
 */

static int adjustVarRefCount(MprVar *vp, int adj)
{
	mprAssert(vp);

	if (vp->type != MPR_TYPE_OBJECT) {
		mprAssert(vp->type == MPR_TYPE_OBJECT);
		return 0;
	}
	return adjustRefCount(vp->properties, adj);
}

/******************************************************************************/
/*
 *	Get the object reference count 
 */

int mprGetVarRefCount(MprVar *vp)
{
	mprAssert(vp);

	if (vp->type != MPR_TYPE_OBJECT) {
		mprAssert(vp->type == MPR_TYPE_OBJECT);
		return 0;
	}
	return adjustRefCount(vp->properties, 0);
}

/******************************************************************************/
/*
 *	Update the variable's name
 */

void mprSetVarName(MprVar *vp, char *name)
{
	mprAssert(vp);

	mprFree(vp->name);
	vp->name = mprStrdup(name);
}

/******************************************************************************/
/*
 *	Append to the variable's full name
 */

void mprSetVarFullName(MprVar *vp, char *name)
{
#if VAR_DEBUG
	mprAssert(vp);

	mprFree(vp->fullName);
	vp->fullName = mprStrdup(name);
	if (vp->type == MPR_TYPE_OBJECT) {
		if (strcmp(vp->properties->name, "this") == 0) {
			mprStrcpy(vp->properties->name, sizeof(vp->properties->name), name);
		}
	}
#endif
}

/******************************************************************************/
/*
 *	Make a var impervious to recursive forced deletes. 
 */

void mprSetVarDeleteProtect(MprVar *vp, int deleteProtect)
{
	mprAssert(vp);

	if (vp->type == MPR_TYPE_OBJECT && vp->properties) {
		vp->properties->deleteProtect = deleteProtect;
	}
}

/******************************************************************************/
/*
 *	Make a variable readonly. Can still be deleted.
 */

void mprSetVarReadonly(MprVar *vp, int readonly)
{
	mprAssert(vp);

	vp->readonly = readonly;
}

/******************************************************************************/

MprVarTrigger mprAddVarTrigger(MprVar *vp, MprVarTrigger fn)
{
	MprVarTrigger oldTrigger;

	mprAssert(vp);
	mprAssert(fn);

	oldTrigger = vp->trigger;
	vp->trigger = fn;
	return oldTrigger;
}

/******************************************************************************/

MprType mprGetVarType(MprVar *vp)
{
	mprAssert(vp);

	return vp->type;
}

/******************************************************************************/
/********************************** Properties ********************************/
/******************************************************************************/
/*
 *	Create a property in an object with a defined value. If the property 
 *	already exists in the object, then just write its value.
 */

MprVar *mprCreateProperty(MprVar *obj, const char *propertyName, 
	MprVar *newValue)
{
	MprVar	*prop, *last;
	int		bucketIndex;

	mprAssert(obj);
	mprAssert(propertyName && *propertyName);

	if (obj->type != MPR_TYPE_OBJECT) {
		mprAssert(obj->type == MPR_TYPE_OBJECT);
		return 0;
	}

	/*
	 *	See if property already exists and locate the bucket to hold the
	 *	property reference.
	 */
	last = 0;
	bucketIndex = hash(obj->properties, propertyName);
	prop = obj->properties->buckets[bucketIndex];

	/*
	 *	Find the property in the hash chain if it exists
 	 */
	for (last = 0; prop; last = prop, prop = prop->forw) {
		if (prop->name[0] == propertyName[0] && 
				strcmp(prop->name, propertyName) == 0) {
			break;
		}
	}

	if (prop) {
		/*	FUTURE -- remove. Just for debug. */
		mprAssert(prop == 0);
		mprLog(0, "Attempting to create property %s in object %s\n",
			propertyName, obj->name);
		return 0;
	}

	if (obj->trigger) {
		if ((obj->trigger)(MPR_VAR_CREATE_PROPERTY, obj->properties, prop, 
				newValue, 0) == MPR_TRIGGER_ABORT) {
			return 0;
		}
	}

	/*
 	 *	Create a new property
	 */
	prop = allocProperty(propertyName);
	if (prop == 0) {
		mprAssert(prop);
		return 0;
	}

	copyVarCore(prop, newValue, MPR_SHALLOW_COPY);

	prop->bucketIndex = bucketIndex;
	if (last) {
		last->forw = prop;
	} else {
		obj->properties->buckets[bucketIndex] = prop;
	}
	prop->parentProperties = obj->properties;

	/*
 	 *	Update the item counts 
	 */
	obj->properties->numItems++;
	if (! mprVarIsFunction(prop->type)) {
		obj->properties->numDataItems++;
	}

	return prop;
}

/******************************************************************************/
/*
 *	Create a property in an object with a defined value. If the property 
 *	already exists in the object, then just write its value. Same as 
 *	mprCreateProperty except that the new value is passed by value rather than
 *	by pointer.
 */

MprVar *mprCreatePropertyValue(MprVar *obj, const char *propertyName, 
	MprVar newValue)
{
	return mprCreateProperty(obj, propertyName, &newValue);
}

/******************************************************************************/
/*
 *	Create a new property
 */

static MprVar *allocProperty(const char *propertyName)
{
	MprVar		*prop;

	prop = (MprVar*) mprMalloc(sizeof(MprVar));
	if (prop == 0) {
		mprAssert(prop);
		return 0;
	}
	memset(prop, 0, sizeof(MprVar));
	prop->allocatedVar = 1;
	prop->name = mprStrdup(propertyName);
	prop->forw = (MprVar*) 0;

	return prop;
}

/******************************************************************************/
/*
 *	Update a property in an object with a defined value. Create the property
 *	if it doesn not already exist.
 */

MprVar *mprSetProperty(MprVar *obj, const char *propertyName, MprVar *newValue)
{
	MprVar	*prop, triggerValue;
	int		rc;

	mprAssert(obj);
	mprAssert(propertyName && *propertyName);
	mprAssert(obj->type == MPR_TYPE_OBJECT);

	if (obj->type != MPR_TYPE_OBJECT) {
		mprAssert(0);
		return 0;
	}

	prop = mprGetProperty(obj, propertyName, 0);
	if (prop == 0) {
		return mprCreateProperty(obj, propertyName, newValue);
	}

	if (obj->trigger) {
		/* 
		 *	Call the trigger before the update and pass it the new value.
		 */
		triggerValue = *newValue;
		triggerValue.allocatedVar = 0;
		triggerValue.allocatedData = 0;
		rc = (obj->trigger)(MPR_VAR_WRITE, obj->properties, obj, 
				&triggerValue, 0);
		if (rc == MPR_TRIGGER_ABORT) {
			return 0;

		} else if (rc == MPR_TRIGGER_USE_NEW_VALUE) {
			/*
			 *	Trigger must copy to triggerValue a variable that is not
			 *	a structure copy of the existing data.
			 */
			copyVarCore(prop, &triggerValue, MPR_SHALLOW_COPY);
			mprDestroyVar(&triggerValue);
			return prop;
		}
	}
	copyVarCore(prop, newValue, MPR_SHALLOW_COPY);
	return prop;
}

/******************************************************************************/
/*
 *	Update a property in an object with a defined value. Create the property
 *	if it does not already exist. Same as mprSetProperty except that the 
 *	new value is passed by value rather than by pointer.
 */

MprVar *mprSetPropertyValue(MprVar *obj, const char *propertyName, 
	MprVar newValue)
{
	return mprSetProperty(obj, propertyName, &newValue);
}

/******************************************************************************/
/*
 *	Delete a property from this object
 */

int mprDeleteProperty(MprVar *obj, const char *property)
{
	MprVar		*prop, *last;
	char		*cp;
	int			bucketIndex;

	mprAssert(obj);
	mprAssert(property && *property);
	mprAssert(obj->type == MPR_TYPE_OBJECT);

	if (obj->type != MPR_TYPE_OBJECT) {
		mprAssert(obj->type == MPR_TYPE_OBJECT);
		return 0;
	}

	last = 0;
	bucketIndex = hash(obj->properties, property);
	if ((prop = obj->properties->buckets[bucketIndex]) != 0) {
		for ( ; prop; prop = prop->forw) {
			cp = prop->name;
			if (cp[0] == property[0] && strcmp(cp, property) == 0) {
				break;
			}
			last = prop;
		}
	}
	if (prop == (MprVar*) 0) {
		mprAssert(prop);
		return MPR_ERR_NOT_FOUND;
	}
 	if (prop->readonly) {
		mprAssert(! prop->readonly);
		return MPR_ERR_READ_ONLY;
	}

	if (obj->trigger) {
		if ((obj->trigger)(MPR_VAR_DELETE_PROPERTY, obj->properties, prop, 0, 0)
				== MPR_TRIGGER_ABORT) {
			return MPR_ERR_ABORTED;
		}
	}

	if (last) {
		last->forw = prop->forw;
	} else {
		obj->properties->buckets[bucketIndex] = prop->forw;
	}

	obj->properties->numItems--;
	if (! mprVarIsFunction(prop->type)) {
		obj->properties->numDataItems--;
	}

	mprDestroyVar(prop);

	return 0;
}

/******************************************************************************/
/*
 *	Find a property in an object and return a pointer to it. If a value arg
 *	is supplied, then copy the data into the var. 
 */

MprVar *mprGetProperty(MprVar *obj, const char *property, MprVar *value)
{
	MprVar	*prop, triggerValue;
	int		rc;

	if (obj == 0 || obj->type != MPR_TYPE_OBJECT || property == 0 || 
			*property == '\0') {
		if (value) {
			value->type = MPR_TYPE_UNDEFINED;
		}
		return 0;
	}

	for (prop = getObjChain(obj->properties, property); prop; 
			prop = prop->forw) {
		if (prop->name &&
			prop->name[0] == property[0] && strcmp(prop->name, property) == 0) {
			break;
		}
	}
	if (prop == 0) {
		if (value) {
			value->type = MPR_TYPE_UNDEFINED;
		}
		return 0;
	}
	if (value) {
		if (prop->trigger) {
			triggerValue = *prop;
			triggerValue.allocatedVar = 0;
			triggerValue.allocatedData = 0;
			/*
			 *	Pass the trigger the current read value and may receive
			 *	a new value.
			 */ 
			rc = (prop->trigger)(MPR_VAR_READ, prop->parentProperties, prop, 
				&triggerValue, 0);
			if (rc == MPR_TRIGGER_ABORT) {
				if (value) {
					value->type = MPR_TYPE_UNDEFINED;
				}
				return 0;

			} else if (rc == MPR_TRIGGER_USE_NEW_VALUE) {
				copyVarCore(prop, &triggerValue, MPR_SHALLOW_COPY);
				mprDestroyVar(&triggerValue);
			}
		}
		/*
		 *	Clone. No copy.
		 */
		*value = *prop;
	}
	return prop;
}

/******************************************************************************/
/*
 *	Read a properties value. This returns the property's value. It does not
 *	copy object/string data but returns a pointer directly into the variable.
 *	The caller does not and should not call mprDestroy on the returned value.
 *	If value is null, just read the property and run triggers.
 */

int mprReadProperty(MprVar *prop, MprVar *value)
{
	MprVar	triggerValue;
	int		rc;

	mprAssert(prop);

	if (prop->trigger) {
		triggerValue = *prop;
		triggerValue.allocatedVar = 0;
		triggerValue.allocatedData = 0;
		rc = (prop->trigger)(MPR_VAR_READ, prop->parentProperties, prop, 
			&triggerValue, 0);

		if (rc == MPR_TRIGGER_ABORT) {
			return MPR_ERR_ABORTED;

		} else if (rc == MPR_TRIGGER_USE_NEW_VALUE) {
			copyVarCore(prop, &triggerValue, MPR_SHALLOW_COPY);
			mprDestroyVar(&triggerValue);
			return 0;
		}
	}
	if (value) {
		*value = *prop;

		/*
		 *	Just so that if the user calls mprDestroyVar on value, it will do no
		 *	harm.
		 */
		value->allocatedVar = 0;
		value->allocatedData = 0;
	}
	return 0;
}

/******************************************************************************/
/*
 *	Read a properties value. This returns a copy of the property variable. 
 *	However, if the property is an object or string, it returns a copy of the
 *	reference to the underlying data. If copyDepth is set to MPR_DEEP_COPY,
 *	then the underlying objects and strings data will be copied as well. If 
 *	copyDepth is set to MPR_SHALLOW_COPY, then only strings will be copied. If
 *	it is set to MPR_NO_COPY, then no data will be copied. In all cases, the 
 *	user must call mprDestroyVar to free resources. This routine will run any 
 *	registered triggers which may modify the value the user receives (without 
 *	updating the properties real value).
 *
 *	WARNING: the args are reversed to most other APIs. This conforms to the
 *	strcpy(dest, src) standard instead.
 */

int mprCopyProperty(MprVar *dest, MprVar *prop, int copyDepth)
{
	MprVar	triggerValue;
	int		rc;

	mprAssert(prop);
	mprAssert(dest);

	if (prop->trigger) {
		triggerValue = *prop;
		triggerValue.allocatedVar = 0;
		triggerValue.allocatedData = 0;
		rc = (prop->trigger)(MPR_VAR_READ, prop->parentProperties, prop, 
			&triggerValue, copyDepth);

		if (rc == MPR_TRIGGER_ABORT) {
			return MPR_ERR_ABORTED;

		} else if (rc == MPR_TRIGGER_USE_NEW_VALUE) {
			copyVarCore(dest, &triggerValue, MPR_SHALLOW_COPY);
			mprDestroyVar(&triggerValue);
			return 0;
		}
	}
	mprCopyVar(dest, prop, copyDepth);
	return 0;
}

/******************************************************************************/
/*
 *	Write a new value into an existing property in an object.
 */

int mprWriteProperty(MprVar *vp, MprVar *value)
{
	MprVar	triggerValue;
	int		rc;

	mprAssert(vp);
	mprAssert(value);

	if (vp->readonly) {
		return MPR_ERR_READ_ONLY;
	}

	if (vp->trigger) {
		triggerValue = *value;

		rc = (vp->trigger)(MPR_VAR_WRITE, vp->parentProperties, vp, 
			&triggerValue, 0);

		if (rc == MPR_TRIGGER_ABORT) {
			return MPR_ERR_ABORTED;

		} else if (rc == MPR_TRIGGER_USE_NEW_VALUE) {
			copyVarCore(vp, &triggerValue, MPR_SHALLOW_COPY);
			mprDestroyVar(&triggerValue);
			return 0;
		}
		/* Fall through */
	}

	copyVarCore(vp, value, MPR_SHALLOW_COPY);
	return 0;
}

/******************************************************************************/
/*
 *	Write a new value into an existing property in an object.
 */

int mprWritePropertyValue(MprVar *vp, MprVar value)
{
	mprAssert(vp);

	return mprWriteProperty(vp, &value);
}

/******************************************************************************/
/*
 *	Get the count of properties.
 */

int mprGetPropertyCount(MprVar *vp, int includeFlags)
{
	mprAssert(vp);

	if (vp->type != MPR_TYPE_OBJECT) {
		return 0;
	}
	if (includeFlags == MPR_ENUM_DATA) {
		return vp->properties->numDataItems;
	} else {
		return vp->properties->numItems;
	}
}

/******************************************************************************/
/*
 *	Get the first property in an object. Used for walking all properties in an
 *	object.
 */

MprVar *mprGetFirstProperty(MprVar *obj, int includeFlags)
{
	MprVar		*prop;
	int			i;

	mprAssert(obj);
	mprAssert(obj->type == MPR_TYPE_OBJECT);

	if (obj->type != MPR_TYPE_OBJECT) {
		mprAssert(obj->type == MPR_TYPE_OBJECT);
		return 0;
	}

	for (i = 0; i < (int) obj->properties->hashSize; i++) {
		for (prop = obj->properties->buckets[i]; prop; prop = prop->forw) {
			if (prop) {
				if (mprVarIsFunction(prop->type)) {
					if (!(includeFlags & MPR_ENUM_FUNCTIONS)) {
						continue;
					}
				} else {
					if (!(includeFlags & MPR_ENUM_DATA)) {
						continue;
					}
				}
				return prop;
			}
			break;
		}
	}
	return 0;
}

/******************************************************************************/
/*
 *	Get the next property in sequence.
 */

MprVar *mprGetNextProperty(MprVar *obj, MprVar *last, int includeFlags)
{
	MprProperties	*properties;
	int				i;

	mprAssert(obj);
	mprAssert(obj->type == MPR_TYPE_OBJECT);

	if (obj->type != MPR_TYPE_OBJECT) {
		mprAssert(obj->type == MPR_TYPE_OBJECT);
		return 0;
	}
	properties = obj->properties;

	if (last->forw) {
		return last->forw;
	}

	for (i = last->bucketIndex + 1; i < (int) properties->hashSize; i++) {
		for (last = properties->buckets[i]; last; last = last->forw) {
			if (mprVarIsFunction(last->type)) {
				if (!(includeFlags & MPR_ENUM_FUNCTIONS)) {
					continue;
				}
			} else {
				if (!(includeFlags & MPR_ENUM_DATA)) {
					continue;
				}
			}
			return last;
		}
	}
	return 0;
}

/******************************************************************************/
/************************** Internal Support Routines *************************/
/******************************************************************************/
/*
 *	Create an hash table to hold and index properties. Properties are just 
 *	variables which may contain primitive data types, functions or other
 *	objects. The hash table is the essence of an object. HashSize specifies 
 *	the size of the hash table to use and should be a prime number.
 */

static MprProperties *createProperties(const char *name, int hashSize)
{
	MprProperties	*pp;

	if (hashSize < 7) {
		hashSize = 7;
	}
	if ((pp = (MprProperties*) mprMalloc(sizeof(MprProperties))) == NULL) {
		mprAssert(0);
		return 0;
	}
	mprAssert(pp);
	memset(pp, 0, sizeof(MprProperties));

	pp->numItems = 0;
	pp->numDataItems = 0;
	pp->hashSize = hashSize;
	pp->buckets = (MprVar**) mprMalloc(pp->hashSize * sizeof(MprVar*));
	mprAssert(pp->buckets);
	memset(pp->buckets, 0, pp->hashSize * sizeof(MprVar*));
	pp->refCount = 1;

#if VAR_DEBUG
	if (objectCount == -1) {
		objectCount = 0;
		objectList.next = objectList.prev = &objectList;
	}

	mprStrcpy(pp->name, sizeof(pp->name), name);
	pp->next = &objectList;
	pp->prev = objectList.prev;
	objectList.prev->next = pp;
	objectList.prev = pp;
	objectCount++;
#endif
	return pp;
}

/******************************************************************************/
/*
 *	Release an object's properties hash table. If this is the last person 
 *	using it, free it. Return TRUE if the object is released.
 */

static bool releaseProperties(MprProperties *obj, int force)
{
	MprProperties	*pp;
	MprVar			*prop, *forw;
	int				i;

	mprAssert(obj);
	mprAssert(obj->refCount > 0);

#if VAR_DEBUG
	/*
	 *	Debug sanity check
	 */
	mprAssert(obj->refCount < 20);
#endif

	if (--obj->refCount > 0 && !force) {
		return 0;
	}

#if VAR_DEBUG
	mprAssert(obj->prev);
	mprAssert(obj->next);
	mprAssert(obj->next->prev);
	mprAssert(obj->prev->next);
	obj->next->prev = obj->prev;
	obj->prev->next = obj->next;
	objectCount--;
#endif

	for (i = 0; i < (int) obj->hashSize; i++) {
		for (prop = obj->buckets[i]; prop; prop = forw) {
			forw = prop->forw;
			if (prop->type == MPR_TYPE_OBJECT) {

				if (prop->properties == obj) {
					/* Self reference */
					continue;
				}
				pp = prop->properties;
				if (pp->visited) {
					continue;
				}

				pp->visited = 1;
				if (! freeVar(prop, pp->deleteProtect ? 0 : force)) {
					pp->visited = 0;
				}

			} else {
				freeVar(prop, force);
			}
		}
	}

	mprFree((void*) obj->buckets);
	mprFree((void*) obj);

	return 1;
}

/******************************************************************************/
/*
 *	Adjust the reference count
 */

static int adjustRefCount(MprProperties *pp, int adj)
{
	mprAssert(pp);

	/*
	 *	Debug sanity check
	 */
	mprAssert(pp->refCount < 20);

	return pp->refCount += adj;
}

/******************************************************************************/
#if VAR_DEBUG
/*
 *	Print objects held
 */

void mprPrintObjects(char *msg)
{
	MprProperties	*pp, *np;
	MprVar			*prop, *forw;
	char			*buf;
	int				i;

	mprLog(7, "%s: Object Store. %d objects.\n", msg, objectCount);
	pp = objectList.next;
	while (pp != &objectList) {
		mprLog(7, "%s: 0x%x, refCount %d, properties %d\n", 
			pp->name, pp, pp->refCount, pp->numItems);
		for (i = 0; i < (int) pp->hashSize; i++) {
			for (prop = pp->buckets[i]; prop; prop = forw) {
				forw = prop->forw;
				if (prop->properties == pp) {
					/* Self reference */
					continue;
				}
				mprVarToString(&buf, MPR_MAX_STRING, 0, prop);
				if (prop->type == MPR_TYPE_OBJECT) {
					np = objectList.next;
					while (np != &objectList) {
						if (prop->properties == np) {
							break;
						}
						np = np->next;
					}
					if (prop->properties == np) {
						mprLog(7, "    %s: OBJECT 0x%x, <%s>\n", 
							prop->name, prop->properties, prop->fullName);
					} else {
						mprLog(7, "    %s: OBJECT NOT FOUND, %s <%s>\n", 
							prop->name, buf, prop->fullName);
					}
				} else {
					mprLog(7, "    %s: <%s> = %s\n", prop->name, 
						prop->fullName, buf);
				}
				mprFree(buf);
			}
		}
		pp = pp->next;
	}
}

/******************************************************************************/

void mprPrintObjRefCount(MprVar *vp)
{
	mprLog(7, "OBJECT 0x%x, refCount %d\n", vp->properties,
		vp->properties->refCount);
}

#endif
/******************************************************************************/
/*
 *	Get the bucket chain containing a property.
 */

static MprVar *getObjChain(MprProperties *obj, const char *property)
{
	mprAssert(obj);

	return obj->buckets[hash(obj, property)];
}

/******************************************************************************/
/*
 *	Fast hash. The history of this algorithm is part of lost computer science 
 *	folk lore.
 */

static int hash(MprProperties *pp, const char *property)
{
	uint	sum;

	mprAssert(pp);
	mprAssert(property);

	sum = 0;
	while (*property) {
		sum += (sum * 33) + *property++;
	}

	return sum % pp->hashSize;
}

/******************************************************************************/
/*********************************** Constructors *****************************/
/******************************************************************************/
/*
 *	Initialize an undefined value.
 */

MprVar mprCreateUndefinedVar()
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_UNDEFINED;
	return v;
}

/******************************************************************************/
/*
 *	Initialize an null value.
 */

MprVar mprCreateNullVar()
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_NULL;
	return v;
}

/******************************************************************************/

MprVar mprCreateBoolVar(bool value)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_BOOL;
	v.boolean = value;
	return v;
}

/******************************************************************************/
/*
 *	Initialize a C function.
 */

MprVar mprCreateCFunctionVar(MprCFunction fn, void *thisPtr, int flags)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_CFUNCTION;
	v.cFunction.fn = fn;
	v.cFunction.thisPtr = thisPtr;
	v.flags = flags;

	return v;
}

/******************************************************************************/
/*
 *	Initialize a C function.
 */

MprVar mprCreateStringCFunctionVar(MprStringCFunction fn, void *thisPtr, 
	int flags)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_STRING_CFUNCTION;
	v.cFunctionWithStrings.fn = fn;
	v.cFunctionWithStrings.thisPtr = thisPtr;
	v.flags = flags;

	return v;
}

/******************************************************************************/
/*
 *	Initialize an opaque pointer.
 */

MprVar mprCreatePtrVar(void *ptr)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_PTR;
	v.ptr = ptr;

	return v;
}

/******************************************************************************/
#if BLD_FEATURE_FLOATING_POINT
/*
 *	Initialize a floating value.
 */

MprVar mprCreateFloatVar(double value)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_FLOAT;
	v.floating = value;
	return v;
}

#endif
/******************************************************************************/
/*
 *	Initialize an integer value.
 */

MprVar mprCreateIntegerVar(int value)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_INT;
	v.integer = value;
	return v;
}

/******************************************************************************/
#if BLD_FEATURE_INT64
/*
 *	Initialize a 64-bit integer value.
 */

MprVar mprCreateInteger64Var(int64 value)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_INT64;
	v.integer64 = value;
	return v;
}

#endif /* BLD_FEATURE_INT64 */
/******************************************************************************/
/*
 *	Initialize an number variable. Type is defined by configure.
 */

MprVar mprCreateNumberVar(MprNum value)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = BLD_FEATURE_NUM_TYPE_ID;
#if   BLD_FEATURE_NUM_TYPE_ID == MPR_TYPE_INT64
	v.integer64 = value;
#elif BLD_FEATURE_NUM_TYPE_ID == MPR_TYPE_FLOAT
	v.float = value;
#else
	v.integer = value;
#endif
	return v;
}

/******************************************************************************/
/*
 *	Initialize a (bare) JavaScript function. args and body can be null.
 */

MprVar mprCreateFunctionVar(char *args, char *body, int flags)
{
	MprVar		v;
	char		*cp, *arg, *last;
	int			aid;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_FUNCTION;
	v.flags = flags;

	v.function.args = mprCreateArray();

	if (args) {
		args = mprStrdup(args);
		arg = mprStrTok(args, ",", &last);
		while (arg) {
			while (isspace((int) *arg))
				arg++;
			for (cp = &arg[strlen(arg) - 1]; cp > arg; cp--) {
				if (!isspace((int) *cp)) {
					break;
				}
			}
			cp[1] = '\0';
					
			aid = mprAddToArray(v.function.args, mprStrdup(arg));
			arg = mprStrTok(0, ",", &last);
		}
		mprFree(args);
	}

	if (body) {
		v.function.body = mprStrdup(body);
	}
	v.allocatedData = 1;
	return v;
}

/******************************************************************************/
/*
 *	Initialize an object variable. Return type == MPR_TYPE_UNDEFINED if the
 *	memory allocation for the properties table failed.
 */

MprVar mprCreateObjVar(const char *name, int hashSize)
{
	MprVar	v;

	mprAssert(name && *name);

	memset(&v, 0x0, sizeof(MprVar));
	v.type = MPR_TYPE_OBJECT;
	if (hashSize <= 0) {
		hashSize = MPR_DEFAULT_HASH_SIZE;
	}
	v.properties = createProperties(name, hashSize);
	if (v.properties == 0) {
		/* Indicate failed memory allocation */
		v.type = MPR_TYPE_UNDEFINED;
	}
	v.allocatedData = 1;
	v.name = mprStrdup(name);
	mprLog(7, "mprCreateObjVar %s, 0x%p\n", name, v.properties);
	return v;
}

/******************************************************************************/
/*
 *	Initialize a string value.
 */

MprVar mprCreateStringVar(const char *value, bool allocate)
{
	MprVar	v;

	memset(&v, 0x0, sizeof(v));
	v.type = MPR_TYPE_STRING;
	if (value == 0) {
		v.string = "";
	} else if (allocate) {
		v.string = mprStrdup(value);
		v.allocatedData = 1;
	} else {
		v.string = (char*) value;
	}
	return v;
}

/******************************************************************************/
/*
 *	Copy an objects core value (only). This preserves the destination object's 
 *	name. This implements copy by reference for objects and copy by value for 
 *	strings and other types. Caller must free dest prior to calling.
 */

static void copyVarCore(MprVar *dest, MprVar *src, int copyDepth)
{
	MprVarTrigger	saveTrigger;
	MprVar			*srcProp, *destProp, *last;
	char			**srcArgs;
	int				i;

	mprAssert(dest);
	mprAssert(src);

	if (dest == src) {
		return;
	}

	/*
 	 *	FUTURE: we should allow read-only triggers where the value is never
	 *	stored in the object. Currently, triggers override the readonly
	 *	status.
	 */

	if (dest->type != MPR_TYPE_UNDEFINED && dest->readonly && !dest->trigger) {
		mprAssert(0);
		return;
	}

	if (dest->type != MPR_TYPE_UNDEFINED) {
		saveTrigger = dest->trigger;
		freeVarStorage(dest, 0);
	} else {
		saveTrigger = 0;
	}

	switch (src->type) {
	default:
	case MPR_TYPE_UNDEFINED:
	case MPR_TYPE_NULL:
		break;

	case MPR_TYPE_BOOL:
		dest->boolean = src->boolean;
		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;

#if BLD_FEATURE_FLOATING_POINT
	case MPR_TYPE_FLOAT:
		dest->floating = src->floating;
		break;
#endif

	case MPR_TYPE_INT:
		dest->integer = src->integer;
		break;

#if BLD_FEATURE_INT64
	case MPR_TYPE_INT64:
		dest->integer64 = src->integer64;
		break;
#endif

	case MPR_TYPE_OBJECT:
		if (copyDepth == MPR_DEEP_COPY) {

			dest->properties = createProperties(src->name, 
				src->properties->hashSize);
			dest->allocatedData = 1;

			for (i = 0; i < (int) src->properties->hashSize; i++) {
				last = 0;
				for (srcProp = src->properties->buckets[i]; srcProp; 
						srcProp = srcProp->forw) {
					if (srcProp->visited) {
						continue;
					}
					destProp = allocProperty(srcProp->name);
					if (destProp == 0) {
						mprAssert(destProp);
						return;
					}

					destProp->bucketIndex = i;
					if (last) {
						last->forw = destProp;
					} else {
						dest->properties->buckets[i] = destProp;
					}
					destProp->parentProperties = dest->properties;

					/*
					 *	Recursively copy the object
					 */
					srcProp->visited = 1;
					copyVarCore(destProp, srcProp, copyDepth);
					srcProp->visited = 0;
					last = srcProp;
				}
			}
			dest->properties->numItems = src->properties->numItems;
			dest->properties->numDataItems = src->properties->numDataItems;
			dest->allocatedData = 1;

		} else if (copyDepth == MPR_SHALLOW_COPY) {
			dest->properties = src->properties;
			adjustVarRefCount(src, 1);
			dest->allocatedData = 1;

		} else {
			dest->properties = src->properties;
			dest->allocatedData = 0;
		}
		break;

	case MPR_TYPE_FUNCTION:
		if (copyDepth != MPR_NO_COPY) {
			dest->function.args = mprCreateArray();
			srcArgs = (char**) src->function.args->handles;
			for (i = 0; i < src->function.args->max; i++) {
				if (srcArgs[i]) {
					mprAddToArray(dest->function.args, mprStrdup(srcArgs[i]));
				}
			}
			dest->function.body = mprStrdup(src->function.body);
			dest->allocatedData = 1;
		} else {
			dest->function.args = src->function.args;
			dest->function.body = src->function.body;
			dest->allocatedData = 0;
		}
		break;

	case MPR_TYPE_STRING:
		if (src->string && copyDepth != MPR_NO_COPY) {
			dest->string = mprStrdup(src->string);
			dest->allocatedData = 1;
		} else {
			dest->string = src->string;
			dest->allocatedData = 0;
		}
		break;
	}

	dest->type = src->type;
	dest->flags = src->flags;
	dest->trigger = saveTrigger;

	/*
	 *	Just for safety
	 */
	dest->spare = 0;
}

/******************************************************************************/
/*
 *	Copy an entire object including name.
 */

void mprCopyVar(MprVar *dest, MprVar *src, int copyDepth)
{
	mprAssert(dest);
	mprAssert(src);

	copyVarCore(dest, src, copyDepth);

	mprFree(dest->name);
	dest->name = mprStrdup(src->name);

#if VAR_DEBUG
	if (src->type == MPR_TYPE_OBJECT) {

		mprFree(dest->fullName);
		dest->fullName = mprStrdup(src->fullName);

		mprLog(7, "mprCopyVar: object \"%s\", FDQ \"%s\" 0x%x, refCount %d\n", 
			dest->name, dest->fullName, dest->properties, 
			dest->properties->refCount);
	}
#endif
}

/******************************************************************************/
/*
 *	Copy an entire object including name.
 */

void mprCopyVarValue(MprVar *dest, MprVar src, int copyDepth)
{
	mprAssert(dest);

	mprCopyVar(dest, &src, copyDepth); 
}

/******************************************************************************/
/*
 *	Copy an object. This implements copy by reference for objects and copy by
 *	value for strings and other types. Caller must free dest prior to calling.
 */

MprVar *mprDupVar(MprVar *src, int copyDepth)
{
	MprVar	*dest;

	mprAssert(src);

	dest = (MprVar*) mprMalloc(sizeof(MprVar));
	memset(dest, 0, sizeof(MprVar));

	mprCopyVar(dest, src, copyDepth);
	return dest;
}

/******************************************************************************/
/*
 *	Convert a value to a text based representation of its value
 *	FUTURE -- conver this to use the format string in all cases. Allow
 *	arbitrary format strings.
 */

void mprVarToString(char** out, int size, char *fmt, MprVar *obj)
{
	char	*src;

	mprAssert(out);

	*out = NULL;

	if (obj->trigger) {
		mprReadProperty(obj, 0);
	}

	switch (obj->type) {
	case MPR_TYPE_UNDEFINED:
		/*	FUTURE -- spec says convert to "undefined" */
		*out = mprStrdup("");
		break;

	case MPR_TYPE_NULL:
		*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");
		} else {
			*out = mprStrdup("false");
		}
		break;

#if BLD_FEATURE_FLOATING_POINT
	case MPR_TYPE_FLOAT:
		if (fmt == NULL || *fmt == '\0') {
			mprAllocSprintf(out, size, "%f", obj->floating);
		} else {
			mprAllocSprintf(out, size, fmt, obj->floating);
		}
		break;
#endif

	case MPR_TYPE_INT:
		if (fmt == NULL || *fmt == '\0') {
			mprAllocSprintf(out, size, "%d", obj->integer);
		} else {
			mprAllocSprintf(out, size, fmt, obj->integer);
		}
		break;

#if BLD_FEATURE_INT64
	case MPR_TYPE_INT64:
		if (fmt == NULL || *fmt == '\0') {
#if BLD_GOAHEAD_WEBSERVER
			mprAllocSprintf(out, size, "%d", (int) obj->integer64);
#else
			mprAllocSprintf(out, size, "%Ld", obj->integer64);
#endif
		} else {
			mprAllocSprintf(out, size, fmt, obj->integer64);
		}
		break;
#endif

	case MPR_TYPE_CFUNCTION:
		mprAllocSprintf(out, size, "[C Function]");
		break;

	case MPR_TYPE_STRING_CFUNCTION:
		mprAllocSprintf(out, size, "[C StringFunction]");
		break;

	case MPR_TYPE_FUNCTION:
		mprAllocSprintf(out, size, "[JavaScript Function]");
		break;

	case MPR_TYPE_OBJECT:
		/*	FUTURE -- really want: [object class: name]  */
		mprAllocSprintf(out, size, "[object %s]", obj->name);
		break;

	case MPR_TYPE_STRING:
		src = obj->string;

		mprAssert(src);
		if (fmt && *fmt) {
			mprAllocSprintf(out, size, fmt, src);

		} else if (src == NULL) {
			*out = mprStrdup("null");

		} else {
			*out = mprStrdup(src);
		}
		break;

	default:
		mprAssert(0);
	}
}

/******************************************************************************/
/*
 *	Parse a string based on formatting instructions and intelligently 
 *	create a variable.
 */

MprVar mprParseVar(char *buf, MprType preferredType)
{
	MprType		type = MPR_TYPE_UNDEFINED;
	char		*cp;

	mprAssert(buf);

	if (preferredType == MPR_TYPE_UNDEFINED) {
		if (*buf == '-') {
			type = MPR_NUM_VAR;

		} else if (!isdigit((int) *buf)) {
			if (strcmp(buf, "true") == 0 || strcmp(buf, "false") == 0) {
				type = MPR_TYPE_BOOL;
			} else {
				type = MPR_TYPE_STRING;
			}

		} else if (isdigit((int) *buf)) {
			type = MPR_NUM_VAR;
			cp = buf;
			if (*cp && tolower(cp[1]) == 'x') {
				cp = &cp[2];
			}
			for (cp = buf; *cp; cp++) {
				if (! isdigit((int) *cp)) {
					break;
				}
			}

			if (*cp != '\0') {
#if BLD_FEATURE_FLOATING_POINT
				if (*cp == '.' || tolower(*cp) == 'e') {
					type = MPR_TYPE_FLOAT;
				} else
#endif
				{
					type = MPR_NUM_VAR;
				}
			}
		}
	} else {
		type = preferredType;
	}

	switch (type) {
	case MPR_TYPE_OBJECT:
	case MPR_TYPE_UNDEFINED:
	case MPR_TYPE_NULL:
	case MPR_TYPE_PTR:
	default:
		break;

	case MPR_TYPE_BOOL:
		return mprCreateBoolVar(buf[0] == 't' ? 1 : 0);

	case MPR_TYPE_INT:
		return mprCreateIntegerVar(mprParseInteger(buf));

#if BLD_FEATURE_INT64
	case MPR_TYPE_INT64:
		return mprCreateInteger64Var(mprParseInteger64(buf));
#endif

	case MPR_TYPE_STRING:
		if (strcmp(buf, "null") == 0) {
			return mprCreateNullVar();
		} else if (strcmp(buf, "undefined") == 0) {
			return mprCreateUndefinedVar();
		} 
			
		return mprCreateStringVar(buf, 1);

#if BLD_FEATURE_FLOATING_POINT
	case MPR_TYPE_FLOAT:
		return mprCreateFloatVar(atof(buf));
#endif

	}
	return mprCreateUndefinedVar();
}

/******************************************************************************/
/*
 *	Convert the variable to a boolean. Only for primitive types.
 */

bool mprVarToBool(const MprVar *vp)
{
	mprAssert(vp);

	switch (vp->type) {
	case MPR_TYPE_UNDEFINED:
	case MPR_TYPE_NULL:
	case MPR_TYPE_STRING_CFUNCTION:
	case MPR_TYPE_CFUNCTION:
	case MPR_TYPE_FUNCTION:
	case MPR_TYPE_OBJECT:
		return 0;

	case MPR_TYPE_PTR:
		return (vp->ptr != NULL);

	case MPR_TYPE_BOOL:
		return vp->boolean;

#if BLD_FEATURE_FLOATING_POINT
	case MPR_TYPE_FLOAT:
		return (vp->floating != 0 && !mprIsNan(vp->floating));
#endif

	case MPR_TYPE_INT:
		return (vp->integer != 0);

#if BLD_FEATURE_INT64
	case MPR_TYPE_INT64:
		return (vp->integer64 != 0);
#endif

	case MPR_TYPE_STRING:
		mprAssert(vp->string);
		return (vp->string[0] != '\0');
	}

	/* Not reached */
	return 0;
}

/******************************************************************************/
#if BLD_FEATURE_FLOATING_POINT
/*
 *	Convert the variable to a floating point number. Only for primitive types.
 */

double mprVarToFloat(MprVar *vp)
{
	mprAssert(vp);

	switch (vp->type) {
	case MPR_TYPE_UNDEFINED:
	case MPR_TYPE_NULL:
	case MPR_TYPE_STRING_CFUNCTION:
	case MPR_TYPE_CFUNCTION:
	case MPR_TYPE_FUNCTION:
	case MPR_TYPE_OBJECT:
	case MPR_TYPE_PTR:
		return 0;

	case MPR_TYPE_BOOL:
		return (vp->boolean) ? 1.0 : 0.0;

	case MPR_TYPE_FLOAT:
		return vp->floating;

	case MPR_TYPE_INT:
		return (double) vp->integer;

#if BLD_FEATURE_INT64
	case MPR_TYPE_INT64:
		return (double) vp->integer64;
#endif

	case MPR_TYPE_STRING:
		mprAssert(vp->string);
		return atof(vp->string);
	}

	/* Not reached */
	return 0;
}

#endif
/******************************************************************************/
/*
 *	Convert the variable to a number type. Only works for primitive types.
 */

MprNum mprVarToNumber(const MprVar *vp)
{
#if BLD_FEATURE_NUM_TYPE_ID == MPR_TYPE_INT64
	return mprVarToInteger64(vp);
#elif BLD_FEATURE_NUM_TYPE_ID == MPR_TYPE_FLOAT
	return mprVarToFloat(vp);
#else 
	return mprVarToInteger(vp);
#endif
}

/******************************************************************************/
/*
 *	Convert the variable to a number type. Only works for primitive types.
 */

MprNum mprParseNumber(char *s)
{
#if BLD_FEATURE_NUM_TYPE_ID == MPR_TYPE_INT64
	return mprParseInteger64(s);
#elif BLD_FEATURE_NUM_TYPE_ID == MPR_TYPE_FLOAT
	return mprParseFloat(s);
#else 
	return mprParseInteger(s);
#endif
}

/******************************************************************************/
#if BLD_FEATURE_INT64
/*
 *	Convert the variable to an Integer64 type. Only works for primitive types.
 */

int64 mprVarToInteger64(const MprVar *vp)
{
	mprAssert(vp);

	switch (vp->type) {
	case MPR_TYPE_UNDEFINED:
	case MPR_TYPE_NULL:
	case MPR_TYPE_STRING_CFUNCTION:
	case MPR_TYPE_CFUNCTION:
	case MPR_TYPE_FUNCTION:
	case MPR_TYPE_OBJECT:
	case MPR_TYPE_PTR:
		return 0;

	case MPR_TYPE_BOOL:
		return (vp->boolean) ? 1 : 0;

#if BLD_FEATURE_FLOATING_POINT
	case MPR_TYPE_FLOAT:
		if (mprIsNan(vp->floating)) {
			return 0;
		}
		return (int64) vp->floating;
#endif

	case MPR_TYPE_INT:
		return vp->integer;

	case MPR_TYPE_INT64:
		return vp->integer64;

	case MPR_TYPE_STRING:
		return mprParseInteger64(vp->string);
	}

	/* Not reached */
	return 0;
}

/******************************************************************************/
/*
 *	Convert the string buffer to an Integer64.
 */

int64 mprParseInteger64(char *str)
{
	char	*cp;
	int64	num64;
	int		radix, c, negative;

	mprAssert(str);

	cp = str;
	num64 = 0;
	negative = 0;

	if (*cp == '-') {
		cp++;
		negative = 1;
	}

	/*
	 *	Parse a number. Observe hex and octal prefixes (0x, 0)
	 */
	if (*cp != '0') {
		/* 
		 *	Normal numbers (Radix 10)
		 */
		while (isdigit((int) *cp)) {
			num64 = (*cp - '0') + (num64 * 10);
			cp++;
		}
	} else {
		cp++;
		if (tolower(*cp) == 'x') {
			cp++;
			radix = 16;
			while (*cp) {
				c = tolower(*cp);
				if (isdigit(c)) {
					num64 = (c - '0') + (num64 * radix);
				} else if (c >= 'a' && c <= 'f') {
					num64 = (c - 'a') + (num64 * radix);
				} else {
					break;
				}
				cp++;
			}

		} else{
			radix = 8;
			while (*cp) {
				c = tolower(*cp);
				if (isdigit(c) && c < '8') {
					num64 = (c - '0') + (num64 * radix);
				} else {
					break;
				}
				cp++;
			}
		}
	}

	if (negative) {
		return 0 - num64;
	}
	return num64;
}

#endif /* BLD_FEATURE_INT64 */
/******************************************************************************/
/*
 *	Convert the variable to an Integer type. Only works for primitive types.
 */

int mprVarToInteger(const MprVar *vp)
{
	mprAssert(vp);

	switch (vp->type) {
	case MPR_TYPE_UNDEFINED:
	case MPR_TYPE_NULL:
	case MPR_TYPE_STRING_CFUNCTION:
	case MPR_TYPE_CFUNCTION:
	case MPR_TYPE_FUNCTION:
	case MPR_TYPE_OBJECT:
	case MPR_TYPE_PTR:
		return 0;

	case MPR_TYPE_BOOL:
		return (vp->boolean) ? 1 : 0;

#if BLD_FEATURE_FLOATING_POINT
	case MPR_TYPE_FLOAT:
		if (mprIsNan(vp->floating)) {
			return 0;
		}
		return (int) vp->floating;
#endif

	case MPR_TYPE_INT:
		return vp->integer;

#if BLD_FEATURE_INT64
	case MPR_TYPE_INT64:
		return (int) vp->integer64;
#endif

	case MPR_TYPE_STRING:
		return mprParseInteger(vp->string);
	}

	/* Not reached */
	return 0;
}

/******************************************************************************/
/*
 *	Convert the string buffer to an Integer.
 */

int mprParseInteger(char *str)
{
	char	*cp;
	int		num;
	int		radix, c, negative;

	mprAssert(str);

	cp = str;
	num = 0;
	negative = 0;

	if (*cp == '-') {
		cp++;
		negative = 1;
	}

	/*
	 *	Parse a number. Observe hex and octal prefixes (0x, 0)
	 */
	if (*cp != '0') {
		/* 
		 *	Normal numbers (Radix 10)
		 */
		while (isdigit((int) *cp)) {
			num = (*cp - '0') + (num * 10);
			cp++;
		}
	} else {
		cp++;
		if (tolower(*cp) == 'x') {
			cp++;
			radix = 16;
			while (*cp) {
				c = tolower(*cp);
				if (isdigit(c)) {
					num = (c - '0') + (num * radix);
				} else if (c >= 'a' && c <= 'f') {
					num = (c - 'a') + (num * radix);
				} else {
					break;
				}
				cp++;
			}

		} else{
			radix = 8;
			while (*cp) {
				c = tolower(*cp);
				if (isdigit(c) && c < '8') {
					num = (c - '0') + (num * radix);
				} else {
					break;
				}
				cp++;
			}
		}
	}

	if (negative) {
		return 0 - num;
	}
	return num;
}

/******************************************************************************/
#if BLD_FEATURE_FLOATING_POINT
/*
 *	Convert the string buffer to an Floating.
 */

double mprParseFloat(char *str)
{
	return atof(str);
}

/******************************************************************************/

bool mprIsNan(double f)
{
#if WIN
	return _isnan(f);
#elif VXWORKS
	/* FUTURE */
	return (0);
#else
	return (f == FP_NAN);
#endif
}
/******************************************************************************/

bool mprIsInfinite(double f)
{
#if WIN
	return !_finite(f);
#elif VXWORKS
	/* FUTURE */
	return (0);
#else
	return (f == FP_INFINITE);
#endif
}

#endif /* BLD_FEATURE_FLOATING_POINT */
/******************************************************************************/

/*
 * 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
 */