summaryrefslogtreecommitdiffstats
path: root/common/unqlite/jx9_api.c
blob: efad097d02a78b1aece59b22fdd575b97168a423 (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
/*
 * Symisc JX9: A Highly Efficient Embeddable Scripting Engine Based on JSON.
 * Copyright (C) 2012-2013, Symisc Systems http://jx9.symisc.net/
 * Version 1.7.2
 * For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
 * please contact Symisc Systems via:
 *       legal@symisc.net
 *       licensing@symisc.net
 *       contact@symisc.net
 * or visit:
 *      http://jx9.symisc.net/
 */
 /* $SymiscID: api.c v1.7 FreeBSD 2012-12-18 06:54 stable <chm@symisc.net> $ */
#ifndef JX9_AMALGAMATION
#include "jx9Int.h"
#endif
/* This file implement the public interfaces presented to host-applications.
 * Routines in other files are for internal use by JX9 and should not be
 * accessed by users of the library.
 */
#define JX9_ENGINE_MAGIC 0xF874BCD7
#define JX9_ENGINE_MISUSE(ENGINE) (ENGINE == 0 || ENGINE->nMagic != JX9_ENGINE_MAGIC)
#define JX9_VM_MISUSE(VM) (VM == 0 || VM->nMagic == JX9_VM_STALE)
/* If another thread have released a working instance, the following macros
 * evaluates to true. These macros are only used when the library
 * is built with threading support enabled which is not the case in
 * the default built.
 */
#define JX9_THRD_ENGINE_RELEASE(ENGINE) (ENGINE->nMagic != JX9_ENGINE_MAGIC)
#define JX9_THRD_VM_RELEASE(VM) (VM->nMagic == JX9_VM_STALE)
/* IMPLEMENTATION: jx9@embedded@symisc 311-12-32 */
/*
 * All global variables are collected in the structure named "sJx9MPGlobal".
 * That way it is clear in the code when we are using static variable because
 * its name start with sJx9MPGlobal.
 */
static struct Jx9Global_Data
{
	SyMemBackend sAllocator;                /* Global low level memory allocator */
#if defined(JX9_ENABLE_THREADS)
	const SyMutexMethods *pMutexMethods;   /* Mutex methods */
	SyMutex *pMutex;                       /* Global mutex */
	sxu32 nThreadingLevel;                 /* Threading level: 0 == Single threaded/1 == Multi-Threaded 
										    * The threading level can be set using the [jx9_lib_config()]
											* interface with a configuration verb set to
											* JX9_LIB_CONFIG_THREAD_LEVEL_SINGLE or 
											* JX9_LIB_CONFIG_THREAD_LEVEL_MULTI
											*/
#endif
	const jx9_vfs *pVfs;                    /* Underlying virtual file system */
	sxi32 nEngine;                          /* Total number of active engines */
	jx9 *pEngines;                          /* List of active engine */
	sxu32 nMagic;                           /* Sanity check against library misuse */
}sJx9MPGlobal = {
	{0, 0, 0, 0, 0, 0, 0, 0, {0}}, 
#if defined(JX9_ENABLE_THREADS)
	0, 
	0, 
	0, 
#endif
	0, 
	0, 
	0, 
	0
};
#define JX9_LIB_MAGIC  0xEA1495BA
#define JX9_LIB_MISUSE (sJx9MPGlobal.nMagic != JX9_LIB_MAGIC)
/*
 * Supported threading level.
 * These options have meaning only when the library is compiled with multi-threading
 * support.That is, the JX9_ENABLE_THREADS compile time directive must be defined
 * when JX9 is built.
 * JX9_THREAD_LEVEL_SINGLE:
 * In this mode, mutexing is disabled and the library can only be used by a single thread.
 * JX9_THREAD_LEVEL_MULTI
 * In this mode, all mutexes including the recursive mutexes on [jx9] objects
 * are enabled so that the application is free to share the same engine
 * between different threads at the same time.
 */
#define JX9_THREAD_LEVEL_SINGLE 1 
#define JX9_THREAD_LEVEL_MULTI  2
/*
 * Configure a running JX9 engine instance.
 * return JX9_OK on success.Any other return
 * value indicates failure.
 * Refer to [jx9_config()].
 */
JX9_PRIVATE sxi32 jx9EngineConfig(jx9 *pEngine, sxi32 nOp, va_list ap)
{
	jx9_conf *pConf = &pEngine->xConf;
	int rc = JX9_OK;
	/* Perform the requested operation */
	switch(nOp){									 
	case JX9_CONFIG_ERR_LOG:{
		/* Extract compile-time error log if any */
		const char **pzPtr = va_arg(ap, const char **);
		int *pLen = va_arg(ap, int *);
		if( pzPtr == 0 ){
			rc = JX9_CORRUPT;
			break;
		}
		/* NULL terminate the error-log buffer */
		SyBlobNullAppend(&pConf->sErrConsumer);
		/* Point to the error-log buffer */
		*pzPtr = (const char *)SyBlobData(&pConf->sErrConsumer);
		if( pLen ){
			if( SyBlobLength(&pConf->sErrConsumer) > 1 /* NULL '\0' terminator */ ){
				*pLen = (int)SyBlobLength(&pConf->sErrConsumer);
			}else{
				*pLen = 0;
			}
		}
		break;
							}
	case JX9_CONFIG_ERR_ABORT:
		/* Reserved for future use */
		break;
	default:
		/* Unknown configuration verb */
		rc = JX9_CORRUPT;
		break;
	} /* Switch() */
	return rc;
}
/*
 * Configure the JX9 library.
 * Return JX9_OK on success. Any other return value indicates failure.
 * Refer to [jx9_lib_config()].
 */
static sxi32 Jx9CoreConfigure(sxi32 nOp, va_list ap)
{
	int rc = JX9_OK;
	switch(nOp){
	    case JX9_LIB_CONFIG_VFS:{
			/* Install a virtual file system */
			const jx9_vfs *pVfs = va_arg(ap, const jx9_vfs *);
			sJx9MPGlobal.pVfs = pVfs;
			break;
								}
		case JX9_LIB_CONFIG_USER_MALLOC: {
			/* Use an alternative low-level memory allocation routines */
			const SyMemMethods *pMethods = va_arg(ap, const SyMemMethods *);
			/* Save the memory failure callback (if available) */
			ProcMemError xMemErr = sJx9MPGlobal.sAllocator.xMemError;
			void *pMemErr = sJx9MPGlobal.sAllocator.pUserData;
			if( pMethods == 0 ){
				/* Use the built-in memory allocation subsystem */
				rc = SyMemBackendInit(&sJx9MPGlobal.sAllocator, xMemErr, pMemErr);
			}else{
				rc = SyMemBackendInitFromOthers(&sJx9MPGlobal.sAllocator, pMethods, xMemErr, pMemErr);
			}
			break;
										  }
		case JX9_LIB_CONFIG_MEM_ERR_CALLBACK: {
			/* Memory failure callback */
			ProcMemError xMemErr = va_arg(ap, ProcMemError);
			void *pUserData = va_arg(ap, void *);
			sJx9MPGlobal.sAllocator.xMemError = xMemErr;
			sJx9MPGlobal.sAllocator.pUserData = pUserData;
			break;
												 }	  
		case JX9_LIB_CONFIG_USER_MUTEX: {
#if defined(JX9_ENABLE_THREADS)
			/* Use an alternative low-level mutex subsystem */
			const SyMutexMethods *pMethods = va_arg(ap, const SyMutexMethods *);
#if defined (UNTRUST)
			if( pMethods == 0 ){
				rc = JX9_CORRUPT;
			}
#endif
			/* Sanity check */
			if( pMethods->xEnter == 0 || pMethods->xLeave == 0 || pMethods->xNew == 0){
				/* At least three criticial callbacks xEnter(), xLeave() and xNew() must be supplied */
				rc = JX9_CORRUPT;
				break;
			}
			if( sJx9MPGlobal.pMutexMethods ){
				/* Overwrite the previous mutex subsystem */
				SyMutexRelease(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex);
				if( sJx9MPGlobal.pMutexMethods->xGlobalRelease ){
					sJx9MPGlobal.pMutexMethods->xGlobalRelease();
				}
				sJx9MPGlobal.pMutex = 0;
			}
			/* Initialize and install the new mutex subsystem */
			if( pMethods->xGlobalInit ){
				rc = pMethods->xGlobalInit();
				if ( rc != JX9_OK ){
					break;
				}
			}
			/* Create the global mutex */
			sJx9MPGlobal.pMutex = pMethods->xNew(SXMUTEX_TYPE_FAST);
			if( sJx9MPGlobal.pMutex == 0 ){
				/*
				 * If the supplied mutex subsystem is so sick that we are unable to
				 * create a single mutex, there is no much we can do here.
				 */
				if( pMethods->xGlobalRelease ){
					pMethods->xGlobalRelease();
				}
				rc = JX9_CORRUPT;
				break;
			}
			sJx9MPGlobal.pMutexMethods = pMethods;			
			if( sJx9MPGlobal.nThreadingLevel == 0 ){
				/* Set a default threading level */
				sJx9MPGlobal.nThreadingLevel = JX9_THREAD_LEVEL_MULTI; 
			}
#endif
			break;
										   }
		case JX9_LIB_CONFIG_THREAD_LEVEL_SINGLE:
#if defined(JX9_ENABLE_THREADS)
			/* Single thread mode(Only one thread is allowed to play with the library) */
			sJx9MPGlobal.nThreadingLevel = JX9_THREAD_LEVEL_SINGLE;
#endif
			break;
		case JX9_LIB_CONFIG_THREAD_LEVEL_MULTI:
#if defined(JX9_ENABLE_THREADS)
			/* Multi-threading mode (library is thread safe and JX9 engines and virtual machines
			 * may be shared between multiple threads).
			 */
			sJx9MPGlobal.nThreadingLevel = JX9_THREAD_LEVEL_MULTI;
#endif
			break;
		default:
			/* Unknown configuration option */
			rc = JX9_CORRUPT;
			break;
	}
	return rc;
}
/*
 * [CAPIREF: jx9_lib_config()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_lib_config(int nConfigOp, ...)
{
	va_list ap;
	int rc;
	if( sJx9MPGlobal.nMagic == JX9_LIB_MAGIC ){
		/* Library is already initialized, this operation is forbidden */
		return JX9_LOOKED;
	}
	va_start(ap, nConfigOp);
	rc = Jx9CoreConfigure(nConfigOp, ap);
	va_end(ap);
	return rc;
}
/*
 * Global library initialization
 * Refer to [jx9_lib_init()]
 * This routine must be called to initialize the memory allocation subsystem, the mutex 
 * subsystem prior to doing any serious work with the library.The first thread to call
 * this routine does the initialization process and set the magic number so no body later
 * can re-initialize the library.If subsequent threads call this  routine before the first
 * thread have finished the initialization process, then the subsequent threads must block 
 * until the initialization process is done.
 */
static sxi32 Jx9CoreInitialize(void)
{
	const jx9_vfs *pVfs; /* Built-in vfs */
#if defined(JX9_ENABLE_THREADS)
	const SyMutexMethods *pMutexMethods = 0;
	SyMutex *pMaster = 0;
#endif
	int rc;
	/*
	 * If the library is already initialized, then a call to this routine
	 * is a no-op.
	 */
	if( sJx9MPGlobal.nMagic == JX9_LIB_MAGIC ){
		return JX9_OK; /* Already initialized */
	}
	/* Point to the built-in vfs */
	pVfs = jx9ExportBuiltinVfs();
	/* Install it */
	jx9_lib_config(JX9_LIB_CONFIG_VFS, pVfs);
#if defined(JX9_ENABLE_THREADS)
	if( sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_SINGLE ){
		pMutexMethods = sJx9MPGlobal.pMutexMethods;
		if( pMutexMethods == 0 ){
			/* Use the built-in mutex subsystem */
			pMutexMethods = SyMutexExportMethods();
			if( pMutexMethods == 0 ){
				return JX9_CORRUPT; /* Can't happen */
			}
			/* Install the mutex subsystem */
			rc = jx9_lib_config(JX9_LIB_CONFIG_USER_MUTEX, pMutexMethods);
			if( rc != JX9_OK ){
				return rc;
			}
		}
		/* Obtain a static mutex so we can initialize the library without calling malloc() */
		pMaster = SyMutexNew(pMutexMethods, SXMUTEX_TYPE_STATIC_1);
		if( pMaster == 0 ){
			return JX9_CORRUPT; /* Can't happen */
		}
	}
	/* Lock the master mutex */
	rc = JX9_OK;
	SyMutexEnter(pMutexMethods, pMaster); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
	if( sJx9MPGlobal.nMagic != JX9_LIB_MAGIC ){
#endif
		if( sJx9MPGlobal.sAllocator.pMethods == 0 ){
			/* Install a memory subsystem */
			rc = jx9_lib_config(JX9_LIB_CONFIG_USER_MALLOC, 0); /* zero mean use the built-in memory backend */
			if( rc != JX9_OK ){
				/* If we are unable to initialize the memory backend, there is no much we can do here.*/
				goto End;
			}
		}
#if defined(JX9_ENABLE_THREADS)
		if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE ){
			/* Protect the memory allocation subsystem */
			rc = SyMemBackendMakeThreadSafe(&sJx9MPGlobal.sAllocator, sJx9MPGlobal.pMutexMethods);
			if( rc != JX9_OK ){
				goto End;
			}
		}
#endif
		/* Our library is initialized, set the magic number */
		sJx9MPGlobal.nMagic = JX9_LIB_MAGIC;
		rc = JX9_OK;
#if defined(JX9_ENABLE_THREADS)
	} /* sJx9MPGlobal.nMagic != JX9_LIB_MAGIC */
#endif
End:
#if defined(JX9_ENABLE_THREADS)
	/* Unlock the master mutex */
	SyMutexLeave(pMutexMethods, pMaster); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
	return rc;
}
/*
 * Release an active JX9 engine and it's associated active virtual machines.
 */
static sxi32 EngineRelease(jx9 *pEngine)
{
	jx9_vm *pVm, *pNext;
	/* Release all active VM */
	pVm = pEngine->pVms;
	for(;;){
		if( pEngine->iVm < 1 ){
			break;
		}
		pNext = pVm->pNext;
		jx9VmRelease(pVm);
		pVm = pNext;
		pEngine->iVm--;
	}
	/* Set a dummy magic number */
	pEngine->nMagic = 0x7635;
	/* Release the private memory subsystem */
	SyMemBackendRelease(&pEngine->sAllocator); 
	return JX9_OK;
}
/*
 * Release all resources consumed by the library.
 * If JX9 is already shut when this routine is invoked then this
 * routine is a harmless no-op.
 * Note: This call is not thread safe. Refer to [jx9_lib_shutdown()].
 */
static void JX9CoreShutdown(void)
{
	jx9 *pEngine, *pNext;
	/* Release all active engines first */
	pEngine = sJx9MPGlobal.pEngines;
	for(;;){
		if( sJx9MPGlobal.nEngine < 1 ){
			break;
		}
		pNext = pEngine->pNext;
		EngineRelease(pEngine); 
		pEngine = pNext;
		sJx9MPGlobal.nEngine--;
	}
#if defined(JX9_ENABLE_THREADS)
	/* Release the mutex subsystem */
	if( sJx9MPGlobal.pMutexMethods ){
		if( sJx9MPGlobal.pMutex ){
			SyMutexRelease(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex);
			sJx9MPGlobal.pMutex = 0;
		}
		if( sJx9MPGlobal.pMutexMethods->xGlobalRelease ){
			sJx9MPGlobal.pMutexMethods->xGlobalRelease();
		}
		sJx9MPGlobal.pMutexMethods = 0;
	}
	sJx9MPGlobal.nThreadingLevel = 0;
#endif
	if( sJx9MPGlobal.sAllocator.pMethods ){
		/* Release the memory backend */
		SyMemBackendRelease(&sJx9MPGlobal.sAllocator);
	}
	sJx9MPGlobal.nMagic = 0x1928;	
}
/*
 * [CAPIREF: jx9_lib_shutdown()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_lib_shutdown(void)
{
	if( sJx9MPGlobal.nMagic != JX9_LIB_MAGIC ){
		/* Already shut */
		return JX9_OK;
	}
	JX9CoreShutdown();
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_lib_signature()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE const char * jx9_lib_signature(void)
{
	return JX9_SIG;
}
/*
 * [CAPIREF: jx9_init()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_init(jx9 **ppEngine)
{
	jx9 *pEngine;
	int rc;
#if defined(UNTRUST)
	if( ppEngine == 0 ){
		return JX9_CORRUPT;
	}
#endif
	*ppEngine = 0;
	/* One-time automatic library initialization */
	rc = Jx9CoreInitialize();
	if( rc != JX9_OK ){
		return rc;
	}
	/* Allocate a new engine */
	pEngine = (jx9 *)SyMemBackendPoolAlloc(&sJx9MPGlobal.sAllocator, sizeof(jx9));
	if( pEngine == 0 ){
		return JX9_NOMEM;
	}
	/* Zero the structure */
	SyZero(pEngine, sizeof(jx9));
	/* Initialize engine fields */
	pEngine->nMagic = JX9_ENGINE_MAGIC;
	rc = SyMemBackendInitFromParent(&pEngine->sAllocator, &sJx9MPGlobal.sAllocator);
	if( rc != JX9_OK ){
		goto Release;
	}
#if defined(JX9_ENABLE_THREADS)
	SyMemBackendDisbaleMutexing(&pEngine->sAllocator);
#endif
	/* Default configuration */
	SyBlobInit(&pEngine->xConf.sErrConsumer, &pEngine->sAllocator);
	/* Install a default compile-time error consumer routine */
	pEngine->xConf.xErr = jx9VmBlobConsumer;
	pEngine->xConf.pErrData = &pEngine->xConf.sErrConsumer;
	/* Built-in vfs */
	pEngine->pVfs = sJx9MPGlobal.pVfs;
#if defined(JX9_ENABLE_THREADS)
	if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE ){
		 /* Associate a recursive mutex with this instance */
		 pEngine->pMutex = SyMutexNew(sJx9MPGlobal.pMutexMethods, SXMUTEX_TYPE_RECURSIVE);
		 if( pEngine->pMutex == 0 ){
			 rc = JX9_NOMEM;
			 goto Release;
		 }
	 }
#endif
	/* Link to the list of active engines */
#if defined(JX9_ENABLE_THREADS)
	/* Enter the global mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
	MACRO_LD_PUSH(sJx9MPGlobal.pEngines, pEngine);
	sJx9MPGlobal.nEngine++;
#if defined(JX9_ENABLE_THREADS)
	/* Leave the global mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
	/* Write a pointer to the new instance */
	*ppEngine = pEngine;
	return JX9_OK;
Release:
	SyMemBackendRelease(&pEngine->sAllocator);
	SyMemBackendPoolFree(&sJx9MPGlobal.sAllocator,pEngine);
	return rc;
}
/*
 * [CAPIREF: jx9_release()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_release(jx9 *pEngine)
{
	int rc;
	if( JX9_ENGINE_MISUSE(pEngine) ){
		return JX9_CORRUPT;
	}
#if defined(JX9_ENABLE_THREADS)
	 /* Acquire engine mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
		 JX9_THRD_ENGINE_RELEASE(pEngine) ){
			 return JX9_ABORT; /* Another thread have released this instance */
	 }
#endif
	/* Release the engine */
	rc = EngineRelease(&(*pEngine));
#if defined(JX9_ENABLE_THREADS)
	 /* Leave engine mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 /* Release engine mutex */
	 SyMutexRelease(sJx9MPGlobal.pMutexMethods, pEngine->pMutex) /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
#if defined(JX9_ENABLE_THREADS)
	/* Enter the global mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
	/* Unlink from the list of active engines */
	MACRO_LD_REMOVE(sJx9MPGlobal.pEngines, pEngine);
	sJx9MPGlobal.nEngine--;
#if defined(JX9_ENABLE_THREADS)
	/* Leave the global mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, sJx9MPGlobal.pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel == JX9_THREAD_LEVEL_SINGLE */
#endif
	/* Release the memory chunk allocated to this engine */
	SyMemBackendPoolFree(&sJx9MPGlobal.sAllocator, pEngine);
	return rc;
}
/*
 * Compile a raw JX9 script.
 * To execute a JX9 code, it must first be compiled into a bytecode program using this routine.
 * If something goes wrong [i.e: compile-time error], your error log [i.e: error consumer callback]
 * should  display the appropriate error message and this function set ppVm to null and return
 * an error code that is different from JX9_OK. Otherwise when the script is successfully compiled
 * ppVm should hold the JX9 bytecode and it's safe to call [jx9_vm_exec(), jx9_vm_reset(), etc.].
 * This API does not actually evaluate the JX9 code. It merely compile and prepares the JX9 script
 * for evaluation.
 */
static sxi32 ProcessScript(
	jx9 *pEngine,          /* Running JX9 engine */
	jx9_vm **ppVm,         /* OUT: A pointer to the virtual machine */
	SyString *pScript,     /* Raw JX9 script to compile */
	sxi32 iFlags,          /* Compile-time flags */
	const char *zFilePath  /* File path if script come from a file. NULL otherwise */
	)
{
	jx9_vm *pVm;
	int rc;
	/* Allocate a new virtual machine */
	pVm = (jx9_vm *)SyMemBackendPoolAlloc(&pEngine->sAllocator, sizeof(jx9_vm));
	if( pVm == 0 ){
		/* If the supplied memory subsystem is so sick that we are unable to allocate
		 * a tiny chunk of memory, there is no much we can do here. */
		if( ppVm ){
			*ppVm = 0;
		}
		return JX9_NOMEM;
	}
	if( iFlags < 0 ){
		/* Default compile-time flags */
		iFlags = 0;
	}
	/* Initialize the Virtual Machine */
	rc = jx9VmInit(pVm, &(*pEngine));
	if( rc != JX9_OK ){
		SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
		if( ppVm ){
			*ppVm = 0;
		}
		return JX9_VM_ERR;
	}
	if( zFilePath ){
		/* Push processed file path */
		jx9VmPushFilePath(pVm, zFilePath, -1, TRUE, 0);
	}
	/* Reset the error message consumer */
	SyBlobReset(&pEngine->xConf.sErrConsumer);
	/* Compile the script */
	jx9CompileScript(pVm, &(*pScript), iFlags);
	if( pVm->sCodeGen.nErr > 0 || pVm == 0){
		sxu32 nErr = pVm->sCodeGen.nErr;
		/* Compilation error or null ppVm pointer, release this VM */
		SyMemBackendRelease(&pVm->sAllocator);
		SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
		if( ppVm ){
			*ppVm = 0;
		}
		return nErr > 0 ? JX9_COMPILE_ERR : JX9_OK;
	}
	/* Prepare the virtual machine for bytecode execution */
	rc = jx9VmMakeReady(pVm);
	if( rc != JX9_OK ){
		goto Release;
	}
	/* Install local import path which is the current directory */
	jx9_vm_config(pVm, JX9_VM_CONFIG_IMPORT_PATH, "./");
#if defined(JX9_ENABLE_THREADS)
	if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE ){
		 /* Associate a recursive mutex with this instance */
		 pVm->pMutex = SyMutexNew(sJx9MPGlobal.pMutexMethods, SXMUTEX_TYPE_RECURSIVE);
		 if( pVm->pMutex == 0 ){
			 goto Release;
		 }
	 }
#endif
	/* Script successfully compiled, link to the list of active virtual machines */
	MACRO_LD_PUSH(pEngine->pVms, pVm);
	pEngine->iVm++;
	/* Point to the freshly created VM */
	*ppVm = pVm;
	/* Ready to execute JX9 bytecode */
	return JX9_OK;
Release:
	SyMemBackendRelease(&pVm->sAllocator);
	SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
	*ppVm = 0;
	return JX9_VM_ERR;
}
/*
 * [CAPIREF: jx9_compile()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_compile(jx9 *pEngine, const char *zSource, int nLen, jx9_vm **ppOutVm)
{
	SyString sScript;
	int rc;
	if( JX9_ENGINE_MISUSE(pEngine) ){
		return JX9_CORRUPT;
	}
	if( zSource == 0 ){
		/* Empty Jx9 statement ';' */
		zSource = ";";
		nLen = (int)sizeof(char);
	}
	if( nLen < 0 ){
		/* Compute input length automatically */
		nLen = (int)SyStrlen(zSource);
	}
	SyStringInitFromBuf(&sScript, zSource, nLen);
#if defined(JX9_ENABLE_THREADS)
	 /* Acquire engine mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
		 JX9_THRD_ENGINE_RELEASE(pEngine) ){
			 return JX9_ABORT; /* Another thread have released this instance */
	 }
#endif
	/* Compile the script */
	rc = ProcessScript(&(*pEngine),ppOutVm,&sScript,0,0);
#if defined(JX9_ENABLE_THREADS)
	 /* Leave engine mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
	/* Compilation result */
	return rc;
}
/*
 * [CAPIREF: jx9_compile_file()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_compile_file(jx9 *pEngine, const char *zFilePath, jx9_vm **ppOutVm)
{
	const jx9_vfs *pVfs;
	int rc;
	if( ppOutVm ){
		*ppOutVm = 0;
	}
	rc = JX9_OK; /* cc warning */
	if( JX9_ENGINE_MISUSE(pEngine) || SX_EMPTY_STR(zFilePath) ){
		return JX9_CORRUPT;
	}
#if defined(JX9_ENABLE_THREADS)
	 /* Acquire engine mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
		 JX9_THRD_ENGINE_RELEASE(pEngine) ){
			 return JX9_ABORT; /* Another thread have released this instance */
	 }
#endif
	 /*
	  * Check if the underlying vfs implement the memory map
	  * [i.e: mmap() under UNIX/MapViewOfFile() under windows] function.
	  */
	 pVfs = pEngine->pVfs;
	 if( pVfs == 0 || pVfs->xMmap == 0 ){
		 /* Memory map routine not implemented */
		 rc = JX9_IO_ERR;
	 }else{
		 void *pMapView = 0; /* cc warning */
		 jx9_int64 nSize = 0; /* cc warning */
		 SyString sScript;
		 /* Try to get a memory view of the whole file */
		 rc = pVfs->xMmap(zFilePath, &pMapView, &nSize);
		 if( rc != JX9_OK ){
			 /* Assume an IO error */
			 rc = JX9_IO_ERR;
		 }else{
			 /* Compile the file */
			 SyStringInitFromBuf(&sScript, pMapView, nSize);
			 rc = ProcessScript(&(*pEngine), ppOutVm, &sScript,0,zFilePath);
			 /* Release the memory view of the whole file */
			 if( pVfs->xUnmap ){
				 pVfs->xUnmap(pMapView, nSize);
			 }
		 }
	 }
#if defined(JX9_ENABLE_THREADS)
	 /* Leave engine mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
	/* Compilation result */
	return rc;
}
/*
 * [CAPIREF: jx9_vm_config()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_vm_config(jx9_vm *pVm, int iConfigOp, ...)
{
	va_list ap;
	int rc;
	/* Ticket 1433-002: NULL VM is harmless operation */
	if ( JX9_VM_MISUSE(pVm) ){
		return JX9_CORRUPT;
	}
#if defined(JX9_ENABLE_THREADS)
	 /* Acquire VM mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
		 JX9_THRD_VM_RELEASE(pVm) ){
			 return JX9_ABORT; /* Another thread have released this instance */
	 }
#endif
	/* Confiugure the virtual machine */
	va_start(ap, iConfigOp);
	rc = jx9VmConfigure(&(*pVm), iConfigOp, ap);
	va_end(ap);
#if defined(JX9_ENABLE_THREADS)
	 /* Leave VM mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
	return rc;
}
/*
 * [CAPIREF: jx9_vm_release()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_vm_release(jx9_vm *pVm)
{
	jx9 *pEngine;
	int rc;
	/* Ticket 1433-002: NULL VM is harmless operation */
	if ( JX9_VM_MISUSE(pVm) ){
		return JX9_CORRUPT;
	}
#if defined(JX9_ENABLE_THREADS)
	 /* Acquire VM mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
		 JX9_THRD_VM_RELEASE(pVm) ){
			 return JX9_ABORT; /* Another thread have released this instance */
	 }
#endif
	pEngine = pVm->pEngine;
	rc = jx9VmRelease(&(*pVm));
#if defined(JX9_ENABLE_THREADS)
	 /* Leave VM mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 /* Release VM mutex */
	 SyMutexRelease(sJx9MPGlobal.pMutexMethods, pVm->pMutex) /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
	if( rc == JX9_OK ){
		/* Unlink from the list of active VM */
#if defined(JX9_ENABLE_THREADS)
			/* Acquire engine mutex */
			SyMutexEnter(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
			if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
				JX9_THRD_ENGINE_RELEASE(pEngine) ){
					return JX9_ABORT; /* Another thread have released this instance */
			}
#endif
		MACRO_LD_REMOVE(pEngine->pVms, pVm);
		pEngine->iVm--;
		/* Release the memory chunk allocated to this VM */
		SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
#if defined(JX9_ENABLE_THREADS)
			/* Leave engine mutex */
			SyMutexLeave(sJx9MPGlobal.pMutexMethods, pEngine->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif	
	}
	return rc;
}
/*
 * [CAPIREF: jx9_create_function()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_create_function(jx9_vm *pVm, const char *zName, int (*xFunc)(jx9_context *, int, jx9_value **), void *pUserData)
{
	SyString sName;
	int rc;
	/* Ticket 1433-002: NULL VM is harmless operation */
	if ( JX9_VM_MISUSE(pVm) ){
		return JX9_CORRUPT;
	}
	SyStringInitFromBuf(&sName, zName, SyStrlen(zName));
	/* Remove leading and trailing white spaces */
	SyStringFullTrim(&sName);
	/* Ticket 1433-003: NULL values are not allowed */
	if( sName.nByte < 1 || xFunc == 0 ){
		return JX9_CORRUPT;
	}
#if defined(JX9_ENABLE_THREADS)
	 /* Acquire VM mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
		 JX9_THRD_VM_RELEASE(pVm) ){
			 return JX9_ABORT; /* Another thread have released this instance */
	 }
#endif
	/* Install the foreign function */
	rc = jx9VmInstallForeignFunction(&(*pVm), &sName, xFunc, pUserData); 
#if defined(JX9_ENABLE_THREADS)
	 /* Leave VM mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
	return rc;
}
JX9_PRIVATE int jx9DeleteFunction(jx9_vm *pVm,const char *zName)
{
	jx9_user_func *pFunc = 0; /* cc warning */
	int rc;
	/* Perform the deletion */
	rc = SyHashDeleteEntry(&pVm->hHostFunction, (const void *)zName, SyStrlen(zName), (void **)&pFunc);
	if( rc == JX9_OK ){
		/* Release internal fields */
		SySetRelease(&pFunc->aAux);
		SyMemBackendFree(&pVm->sAllocator, (void *)SyStringData(&pFunc->sName));
		SyMemBackendPoolFree(&pVm->sAllocator, pFunc);
	}
	return rc;
}
/*
 * [CAPIREF: jx9_create_constant()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_create_constant(jx9_vm *pVm, const char *zName, void (*xExpand)(jx9_value *, void *), void *pUserData)
{
	SyString sName;
	int rc;
	/* Ticket 1433-002: NULL VM is harmless operation */
	if ( JX9_VM_MISUSE(pVm) ){
		return JX9_CORRUPT;
	}
	SyStringInitFromBuf(&sName, zName, SyStrlen(zName));
	/* Remove leading and trailing white spaces */
	SyStringFullTrim(&sName);
	if( sName.nByte < 1 ){
		/* Empty constant name */
		return JX9_CORRUPT;
	}
	/* TICKET 1433-003: NULL pointer is harmless operation */
	if( xExpand == 0 ){
		return JX9_CORRUPT;
	}
#if defined(JX9_ENABLE_THREADS)
	 /* Acquire VM mutex */
	 SyMutexEnter(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
	 if( sJx9MPGlobal.nThreadingLevel > JX9_THREAD_LEVEL_SINGLE && 
		 JX9_THRD_VM_RELEASE(pVm) ){
			 return JX9_ABORT; /* Another thread have released this instance */
	 }
#endif
	/* Perform the registration */
	rc = jx9VmRegisterConstant(&(*pVm), &sName, xExpand, pUserData);
#if defined(JX9_ENABLE_THREADS)
	 /* Leave VM mutex */
	 SyMutexLeave(sJx9MPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sJx9MPGlobal.nThreadingLevel != JX9_THREAD_LEVEL_MULTI */
#endif
	 return rc;
}
JX9_PRIVATE int Jx9DeleteConstant(jx9_vm *pVm,const char *zName)
{
	jx9_constant *pCons;
	int rc;
	/* Query the constant hashtable */
	 rc = SyHashDeleteEntry(&pVm->hConstant, (const void *)zName, SyStrlen(zName), (void **)&pCons);
	 if( rc == JX9_OK ){
		 /* Perform the deletion */
		 SyMemBackendFree(&pVm->sAllocator, (void *)SyStringData(&pCons->sName));
		 SyMemBackendPoolFree(&pVm->sAllocator, pCons);
	 }
	 return rc;
}
/*
 * [CAPIREF: jx9_new_scalar()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE jx9_value * jx9_new_scalar(jx9_vm *pVm)
{
	jx9_value *pObj;
	/* Ticket 1433-002: NULL VM is harmless operation */
	if ( JX9_VM_MISUSE(pVm) ){
		return 0;
	}
	/* Allocate a new scalar variable */
	pObj = (jx9_value *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(jx9_value));
	if( pObj == 0 ){
		return 0;
	}
	/* Nullify the new scalar */
	jx9MemObjInit(pVm, pObj);
	return pObj;
}
/*
 * [CAPIREF: jx9_new_array()] 
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE jx9_value * jx9_new_array(jx9_vm *pVm)
{
	jx9_hashmap *pMap;
	jx9_value *pObj;
	/* Ticket 1433-002: NULL VM is harmless operation */
	if ( JX9_VM_MISUSE(pVm) ){
		return 0;
	}
	/* Create a new hashmap first */
	pMap = jx9NewHashmap(&(*pVm), 0, 0);
	if( pMap == 0 ){
		return 0;
	}
	/* Associate a new jx9_value with this hashmap */
	pObj = (jx9_value *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(jx9_value));
	if( pObj == 0 ){
		jx9HashmapRelease(pMap, TRUE);
		return 0;
	}
	jx9MemObjInitFromArray(pVm, pObj, pMap);
	return pObj;
}
/*
 * [CAPIREF: jx9_release_value()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_release_value(jx9_vm *pVm, jx9_value *pValue)
{
	/* Ticket 1433-002: NULL VM is a harmless operation */
	if ( JX9_VM_MISUSE(pVm) ){
		return JX9_CORRUPT;
	}
	if( pValue ){
		/* Release the value */
		jx9MemObjRelease(pValue);
		SyMemBackendPoolFree(&pVm->sAllocator, pValue);
	}
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_to_int()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_to_int(jx9_value *pValue)
{
	int rc;
	rc = jx9MemObjToInteger(pValue);
	if( rc != JX9_OK ){
		return 0;
	}
	return (int)pValue->x.iVal;
}
/*
 * [CAPIREF: jx9_value_to_bool()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_to_bool(jx9_value *pValue)
{
	int rc;
	rc = jx9MemObjToBool(pValue);
	if( rc != JX9_OK ){
		return 0;
	}
	return (int)pValue->x.iVal;
}
/*
 * [CAPIREF: jx9_value_to_int64()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE jx9_int64 jx9_value_to_int64(jx9_value *pValue)
{
	int rc;
	rc = jx9MemObjToInteger(pValue);
	if( rc != JX9_OK ){
		return 0;
	}
	return pValue->x.iVal;
}
/*
 * [CAPIREF: jx9_value_to_double()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE double jx9_value_to_double(jx9_value *pValue)
{
	int rc;
	rc = jx9MemObjToReal(pValue);
	if( rc != JX9_OK ){
		return (double)0;
	}
	return (double)pValue->x.rVal;
}
/*
 * [CAPIREF: jx9_value_to_string()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE const char * jx9_value_to_string(jx9_value *pValue, int *pLen)
{
	jx9MemObjToString(pValue);
	if( SyBlobLength(&pValue->sBlob) > 0 ){
		SyBlobNullAppend(&pValue->sBlob);
		if( pLen ){
			*pLen = (int)SyBlobLength(&pValue->sBlob);
		}
		return (const char *)SyBlobData(&pValue->sBlob);
	}else{
		/* Return the empty string */
		if( pLen ){
			*pLen = 0;
		}
		return "";
	}
}
/*
 * [CAPIREF: jx9_value_to_resource()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void * jx9_value_to_resource(jx9_value *pValue)
{
	if( (pValue->iFlags & MEMOBJ_RES) == 0 ){
		/* Not a resource, return NULL */
		return 0;
	}
	return pValue->x.pOther;
}
/*
 * [CAPIREF: jx9_value_compare()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_compare(jx9_value *pLeft, jx9_value *pRight, int bStrict)
{
	int rc;
	if( pLeft == 0 || pRight == 0 ){
		/* TICKET 1433-24: NULL values is harmless operation */
		return 1;
	}
	/* Perform the comparison */
	rc = jx9MemObjCmp(&(*pLeft), &(*pRight), bStrict, 0);
	/* Comparison result */
	return rc;
}
/*
 * [CAPIREF: jx9_result_int()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_int(jx9_context *pCtx, int iValue)
{
	return jx9_value_int(pCtx->pRet, iValue);
}
/*
 * [CAPIREF: jx9_result_int64()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_int64(jx9_context *pCtx, jx9_int64 iValue)
{
	return jx9_value_int64(pCtx->pRet, iValue);
}
/*
 * [CAPIREF: jx9_result_bool()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_bool(jx9_context *pCtx, int iBool)
{
	return jx9_value_bool(pCtx->pRet, iBool);
}
/*
 * [CAPIREF: jx9_result_double()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_double(jx9_context *pCtx, double Value)
{
	return jx9_value_double(pCtx->pRet, Value);
}
/*
 * [CAPIREF: jx9_result_null()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_null(jx9_context *pCtx)
{
	/* Invalidate any prior representation and set the NULL flag */
	jx9MemObjRelease(pCtx->pRet);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_result_string()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_string(jx9_context *pCtx, const char *zString, int nLen)
{
	return jx9_value_string(pCtx->pRet, zString, nLen);
}
/*
 * [CAPIREF: jx9_result_string_format()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_string_format(jx9_context *pCtx, const char *zFormat, ...)
{
	jx9_value *p;
	va_list ap;
	int rc;
	p = pCtx->pRet;
	if( (p->iFlags & MEMOBJ_STRING) == 0 ){
		/* Invalidate any prior representation */
		jx9MemObjRelease(p);
		MemObjSetType(p, MEMOBJ_STRING);
	}
	/* Format the given string */
	va_start(ap, zFormat);
	rc = SyBlobFormatAp(&p->sBlob, zFormat, ap);
	va_end(ap);
	return rc;
}
/*
 * [CAPIREF: jx9_result_value()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_value(jx9_context *pCtx, jx9_value *pValue)
{
	int rc = JX9_OK;
	if( pValue == 0 ){
		jx9MemObjRelease(pCtx->pRet);
	}else{
		rc = jx9MemObjStore(pValue, pCtx->pRet);
	}
	return rc;
}
/*
 * [CAPIREF: jx9_result_resource()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_result_resource(jx9_context *pCtx, void *pUserData)
{
	return jx9_value_resource(pCtx->pRet, pUserData);
}
/*
 * [CAPIREF: jx9_context_new_scalar()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE jx9_value * jx9_context_new_scalar(jx9_context *pCtx)
{
	jx9_value *pVal;
	pVal = jx9_new_scalar(pCtx->pVm);
	if( pVal ){
		/* Record value address so it can be freed automatically
		 * when the calling function returns. 
		 */
		SySetPut(&pCtx->sVar, (const void *)&pVal);
	}
	return pVal;
}
/*
 * [CAPIREF: jx9_context_new_array()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE jx9_value * jx9_context_new_array(jx9_context *pCtx)
{
	jx9_value *pVal;
	pVal = jx9_new_array(pCtx->pVm);
	if( pVal ){
		/* Record value address so it can be freed automatically
		 * when the calling function returns. 
		 */
		SySetPut(&pCtx->sVar, (const void *)&pVal);
	}
	return pVal;
}
/*
 * [CAPIREF: jx9_context_release_value()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void jx9_context_release_value(jx9_context *pCtx, jx9_value *pValue)
{
	jx9VmReleaseContextValue(&(*pCtx), pValue);
}
/*
 * [CAPIREF: jx9_context_alloc_chunk()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void * jx9_context_alloc_chunk(jx9_context *pCtx, unsigned int nByte, int ZeroChunk, int AutoRelease)
{
	void *pChunk;
	pChunk = SyMemBackendAlloc(&pCtx->pVm->sAllocator, nByte);
	if( pChunk ){
		if( ZeroChunk ){
			/* Zero the memory chunk */
			SyZero(pChunk, nByte);
		}
		if( AutoRelease ){
			jx9_aux_data sAux;
			/* Track the chunk so that it can be released automatically 
			 * upon this context is destroyed.
			 */
			sAux.pAuxData = pChunk;
			SySetPut(&pCtx->sChunk, (const void *)&sAux);
		}
	}
	return pChunk;
}
/*
 * Check if the given chunk address is registered in the call context
 * chunk container.
 * Return TRUE if registered.FALSE otherwise.
 * Refer to [jx9_context_realloc_chunk(), jx9_context_free_chunk()].
 */
static jx9_aux_data * ContextFindChunk(jx9_context *pCtx, void *pChunk)
{
	jx9_aux_data *aAux, *pAux;
	sxu32 n;
	if( SySetUsed(&pCtx->sChunk) < 1 ){
		/* Don't bother processing, the container is empty */
		return 0;
	}
	/* Perform the lookup */
	aAux = (jx9_aux_data *)SySetBasePtr(&pCtx->sChunk);
	for( n = 0; n < SySetUsed(&pCtx->sChunk) ; ++n ){
		pAux = &aAux[n];
		if( pAux->pAuxData == pChunk ){
			/* Chunk found */
			return pAux;
		}
	}
	/* No such allocated chunk */
	return 0;
}
/*
 * [CAPIREF: jx9_context_realloc_chunk()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void * jx9_context_realloc_chunk(jx9_context *pCtx, void *pChunk, unsigned int nByte)
{
	jx9_aux_data *pAux;
	void *pNew;
	pNew = SyMemBackendRealloc(&pCtx->pVm->sAllocator, pChunk, nByte);
	if( pNew ){
		pAux = ContextFindChunk(pCtx, pChunk);
		if( pAux ){
			pAux->pAuxData = pNew;
		}
	}
	return pNew;
}
/*
 * [CAPIREF: jx9_context_free_chunk()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void jx9_context_free_chunk(jx9_context *pCtx, void *pChunk)
{
	jx9_aux_data *pAux;
	if( pChunk == 0 ){
		/* TICKET-1433-93: NULL chunk is a harmless operation */
		return;
	}
	pAux = ContextFindChunk(pCtx, pChunk);
	if( pAux ){
		/* Mark as destroyed */
		pAux->pAuxData = 0;
	}
	SyMemBackendFree(&pCtx->pVm->sAllocator, pChunk);
}
/*
 * [CAPIREF: jx9_array_fetch()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE jx9_value * jx9_array_fetch(jx9_value *pArray, const char *zKey, int nByte)
{
	jx9_hashmap_node *pNode;
	jx9_value *pValue;
	jx9_value skey;
	int rc;
	/* Make sure we are dealing with a valid hashmap */
	if( (pArray->iFlags & MEMOBJ_HASHMAP) == 0 ){
		return 0;
	}
	if( nByte < 0 ){
		nByte = (int)SyStrlen(zKey);
	}
	/* Convert the key to a jx9_value  */
	jx9MemObjInit(pArray->pVm, &skey);
	jx9MemObjStringAppend(&skey, zKey, (sxu32)nByte);
	/* Perform the lookup */
	rc = jx9HashmapLookup((jx9_hashmap *)pArray->x.pOther, &skey, &pNode);
	jx9MemObjRelease(&skey);
	if( rc != JX9_OK ){
		/* No such entry */
		return 0;
	}
	/* Extract the target value */
	pValue = (jx9_value *)SySetAt(&pArray->pVm->aMemObj, pNode->nValIdx);
	return pValue;
}
/*
 * [CAPIREF: jx9_array_walk()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_array_walk(jx9_value *pArray, int (*xWalk)(jx9_value *pValue, jx9_value *, void *), void *pUserData)
{
	int rc;
	if( xWalk == 0 ){
		return JX9_CORRUPT;
	}
	/* Make sure we are dealing with a valid hashmap */
	if( (pArray->iFlags & MEMOBJ_HASHMAP) == 0 ){
		return JX9_CORRUPT;
	}
	/* Start the walk process */
	rc = jx9HashmapWalk((jx9_hashmap *)pArray->x.pOther, xWalk, pUserData);
	return rc != JX9_OK ? JX9_ABORT /* User callback request an operation abort*/ : JX9_OK;
}
/*
 * [CAPIREF: jx9_array_add_elem()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_array_add_elem(jx9_value *pArray, jx9_value *pKey, jx9_value *pValue)
{
	int rc;
	/* Make sure we are dealing with a valid hashmap */
	if( (pArray->iFlags & MEMOBJ_HASHMAP) == 0 ){
		return JX9_CORRUPT;
	}
	/* Perform the insertion */
	rc = jx9HashmapInsert((jx9_hashmap *)pArray->x.pOther, &(*pKey), &(*pValue));
	return rc;
}
/*
 * [CAPIREF: jx9_array_add_strkey_elem()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_array_add_strkey_elem(jx9_value *pArray, const char *zKey, jx9_value *pValue)
{
	int rc;
	/* Make sure we are dealing with a valid hashmap */
	if( (pArray->iFlags & MEMOBJ_HASHMAP) == 0 ){
		return JX9_CORRUPT;
	}
	/* Perform the insertion */
	if( SX_EMPTY_STR(zKey) ){
		/* Empty key, assign an automatic index */
		rc = jx9HashmapInsert((jx9_hashmap *)pArray->x.pOther, 0, &(*pValue));
	}else{
		jx9_value sKey;
		jx9MemObjInitFromString(pArray->pVm, &sKey, 0);
		jx9MemObjStringAppend(&sKey, zKey, (sxu32)SyStrlen(zKey));
		rc = jx9HashmapInsert((jx9_hashmap *)pArray->x.pOther, &sKey, &(*pValue));
		jx9MemObjRelease(&sKey);
	}
	return rc;
}
/*
 * [CAPIREF: jx9_array_count()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE unsigned int jx9_array_count(jx9_value *pArray)
{
	jx9_hashmap *pMap;
	/* Make sure we are dealing with a valid hashmap */
	if( (pArray->iFlags & MEMOBJ_HASHMAP) == 0 ){
		return 0;
	}
	/* Point to the internal representation of the hashmap */
	pMap = (jx9_hashmap *)pArray->x.pOther;
	return pMap->nEntry;
}
/*
 * [CAPIREF: jx9_context_output()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_context_output(jx9_context *pCtx, const char *zString, int nLen)
{
	SyString sData;
	int rc;
	if( nLen < 0 ){
		nLen = (int)SyStrlen(zString);
	}
	SyStringInitFromBuf(&sData, zString, nLen);
	rc = jx9VmOutputConsume(pCtx->pVm, &sData);
	return rc;
}
/*
 * [CAPIREF: jx9_context_throw_error()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_context_throw_error(jx9_context *pCtx, int iErr, const char *zErr)
{
	int rc = JX9_OK;
	if( zErr ){
		rc = jx9VmThrowError(pCtx->pVm, &pCtx->pFunc->sName, iErr, zErr);
	}
	return rc;
}
/*
 * [CAPIREF: jx9_context_throw_error_format()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_context_throw_error_format(jx9_context *pCtx, int iErr, const char *zFormat, ...)
{
	va_list ap;
	int rc;
	if( zFormat == 0){
		return JX9_OK;
	}
	va_start(ap, zFormat);
	rc = jx9VmThrowErrorAp(pCtx->pVm, &pCtx->pFunc->sName, iErr, zFormat, ap);
	va_end(ap);
	return rc;
}
/*
 * [CAPIREF: jx9_context_random_num()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE unsigned int jx9_context_random_num(jx9_context *pCtx)
{
	sxu32 n;
	n = jx9VmRandomNum(pCtx->pVm);
	return n;
}
/*
 * [CAPIREF: jx9_context_random_string()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_context_random_string(jx9_context *pCtx, char *zBuf, int nBuflen)
{
	if( nBuflen < 3 ){
		return JX9_CORRUPT;
	}
	jx9VmRandomString(pCtx->pVm, zBuf, nBuflen);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_context_user_data()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void * jx9_context_user_data(jx9_context *pCtx)
{
	return pCtx->pFunc->pUserData;
}
/*
 * [CAPIREF: jx9_context_push_aux_data()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_context_push_aux_data(jx9_context *pCtx, void *pUserData)
{
	jx9_aux_data sAux;
	int rc;
	sAux.pAuxData = pUserData;
	rc = SySetPut(&pCtx->pFunc->aAux, (const void *)&sAux);
	return rc;
}
/*
 * [CAPIREF: jx9_context_peek_aux_data()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void * jx9_context_peek_aux_data(jx9_context *pCtx)
{
	jx9_aux_data *pAux;
	pAux = (jx9_aux_data *)SySetPeek(&pCtx->pFunc->aAux);
	return pAux ? pAux->pAuxData : 0;
}
/*
 * [CAPIREF: jx9_context_pop_aux_data()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE void * jx9_context_pop_aux_data(jx9_context *pCtx)
{
	jx9_aux_data *pAux;
	pAux = (jx9_aux_data *)SySetPop(&pCtx->pFunc->aAux);
	return pAux ? pAux->pAuxData : 0;
}
/*
 * [CAPIREF: jx9_context_result_buf_length()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE unsigned int jx9_context_result_buf_length(jx9_context *pCtx)
{
	return SyBlobLength(&pCtx->pRet->sBlob);
}
/*
 * [CAPIREF: jx9_function_name()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE const char * jx9_function_name(jx9_context *pCtx)
{
	SyString *pName;
	pName = &pCtx->pFunc->sName;
	return pName->zString;
}
/*
 * [CAPIREF: jx9_value_int()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_int(jx9_value *pVal, int iValue)
{
	/* Invalidate any prior representation */
	jx9MemObjRelease(pVal);
	pVal->x.iVal = (jx9_int64)iValue;
	MemObjSetType(pVal, MEMOBJ_INT);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_int64()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_int64(jx9_value *pVal, jx9_int64 iValue)
{
	/* Invalidate any prior representation */
	jx9MemObjRelease(pVal);
	pVal->x.iVal = iValue;
	MemObjSetType(pVal, MEMOBJ_INT);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_bool()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_bool(jx9_value *pVal, int iBool)
{
	/* Invalidate any prior representation */
	jx9MemObjRelease(pVal);
	pVal->x.iVal = iBool ? 1 : 0;
	MemObjSetType(pVal, MEMOBJ_BOOL);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_null()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_null(jx9_value *pVal)
{
	/* Invalidate any prior representation and set the NULL flag */
	jx9MemObjRelease(pVal);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_double()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_double(jx9_value *pVal, double Value)
{
	/* Invalidate any prior representation */
	jx9MemObjRelease(pVal);
	pVal->x.rVal = (jx9_real)Value;
	MemObjSetType(pVal, MEMOBJ_REAL);
	/* Try to get an integer representation also */
	jx9MemObjTryInteger(pVal);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_string()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_string(jx9_value *pVal, const char *zString, int nLen)
{
	if((pVal->iFlags & MEMOBJ_STRING) == 0 ){
		/* Invalidate any prior representation */
		jx9MemObjRelease(pVal);
		MemObjSetType(pVal, MEMOBJ_STRING);
	}
	if( zString ){
		if( nLen < 0 ){
			/* Compute length automatically */
			nLen = (int)SyStrlen(zString);
		}
		SyBlobAppend(&pVal->sBlob, (const void *)zString, (sxu32)nLen);
	}
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_string_format()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_string_format(jx9_value *pVal, const char *zFormat, ...)
{
	va_list ap;
	int rc;
	if((pVal->iFlags & MEMOBJ_STRING) == 0 ){
		/* Invalidate any prior representation */
		jx9MemObjRelease(pVal);
		MemObjSetType(pVal, MEMOBJ_STRING);
	}
	va_start(ap, zFormat);
	rc = SyBlobFormatAp(&pVal->sBlob, zFormat, ap);
	va_end(ap);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_reset_string_cursor()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_reset_string_cursor(jx9_value *pVal)
{
	/* Reset the string cursor */
	SyBlobReset(&pVal->sBlob);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_resource()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_resource(jx9_value *pVal, void *pUserData)
{
	/* Invalidate any prior representation */
	jx9MemObjRelease(pVal);
	/* Reflect the new type */
	pVal->x.pOther = pUserData;
	MemObjSetType(pVal, MEMOBJ_RES);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_release()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_release(jx9_value *pVal)
{
	jx9MemObjRelease(pVal);
	return JX9_OK;
}
/*
 * [CAPIREF: jx9_value_is_int()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_int(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_INT) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_float()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_float(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_REAL) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_bool()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_bool(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_BOOL) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_string()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_string(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_STRING) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_null()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_null(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_NULL) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_numeric()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_numeric(jx9_value *pVal)
{
	int rc;
	rc = jx9MemObjIsNumeric(pVal);
	return rc;
}
/*
 * [CAPIREF: jx9_value_is_callable()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_callable(jx9_value *pVal)
{
	int rc;
	rc = jx9VmIsCallable(pVal->pVm, pVal);
	return rc;
}
/*
 * [CAPIREF: jx9_value_is_scalar()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_scalar(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_SCALAR) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_json_array()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_json_array(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_HASHMAP) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_json_object()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_json_object(jx9_value *pVal)
{
	jx9_hashmap *pMap;
	if( (pVal->iFlags & MEMOBJ_HASHMAP) == 0 ){
		return FALSE;
	}
	pMap = (jx9_hashmap *)pVal->x.pOther;
	if( (pMap->iFlags & HASHMAP_JSON_OBJECT) == 0 ){
		return FALSE;
	}
	return TRUE;
}
/*
 * [CAPIREF: jx9_value_is_resource()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_resource(jx9_value *pVal)
{
	return (pVal->iFlags & MEMOBJ_RES) ? TRUE : FALSE;
}
/*
 * [CAPIREF: jx9_value_is_empty()]
 * Please refer to the official documentation for function purpose and expected parameters.
 */
JX9_PRIVATE int jx9_value_is_empty(jx9_value *pVal)
{
	int rc;
	rc = jx9MemObjIsEmpty(pVal);
	return rc;
}