summaryrefslogtreecommitdiffstats
path: root/common/unqlite/jx9_vm.c
blob: 1585b132551f50f5ca99fd4533d37d6f1f057b03 (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
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
/*
 * 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: jx9_vm.c v1.0 FreeBSD 2012-12-09 00:19 stable <chm@symisc.net> $ */
#ifndef JX9_AMALGAMATION
#include "jx9Int.h"
#endif
/*
 * The code in this file implements execution method of the JX9 Virtual Machine.
 * The JX9 compiler (implemented in 'compiler.c' and 'parse.c') generates a bytecode program
 * which is then executed by the virtual machine implemented here to do the work of the JX9
 * statements.
 * JX9 bytecode programs are similar in form to assembly language. The program consists
 * of a linear sequence of operations .Each operation has an opcode and 3 operands.
 * Operands P1 and P2 are integers where the first is signed while the second is unsigned.
 * Operand P3 is an arbitrary pointer specific to each instruction. The P2 operand is usually
 * the jump destination used by the OP_JMP, OP_JZ, OP_JNZ, ... instructions.
 * Opcodes will typically ignore one or more operands. Many opcodes ignore all three operands.
 * Computation results are stored on a stack. Each entry on the stack is of type jx9_value.
 * JX9 uses the jx9_value object to represent all values that can be stored in a JX9 variable.
 * Since JX9 uses dynamic typing for the values it stores. Values stored in jx9_value objects
 * can be integers, floating point values, strings, arrays, object instances (object in the JX9 jargon)
 * and so on.
 * Internally, the JX9 virtual machine manipulates nearly all values as jx9_values structures.
 * Each jx9_value may cache multiple representations(string, integer etc.) of the same value.
 * An implicit conversion from one type to the other occurs as necessary.
 * Most of the code in this file is taken up by the [VmByteCodeExec()] function which does
 * the work of interpreting a JX9 bytecode program. But other routines are also provided
 * to help in building up a program instruction by instruction.
 */
/*
 * Each active virtual machine frame is represented by an instance 
 * of the following structure.
 * VM Frame hold local variables and other stuff related to function call.
 */
struct VmFrame
{
	VmFrame *pParent; /* Parent frame or NULL if global scope */
	void *pUserData;  /* Upper layer private data associated with this frame */
	SySet sLocal;     /* Local variables container (VmSlot instance) */
	jx9_vm *pVm;      /* VM that own this frame */
	SyHash hVar;      /* Variable hashtable for fast lookup */
	SySet sArg;       /* Function arguments container */
	sxi32 iFlags;     /* Frame configuration flags (See below)*/
	sxu32 iExceptionJump; /* Exception jump destination */
};
/*
 * When a user defined variable is  garbage collected, memory object index
 * is stored in an instance of the following structure and put in the free object
 * table so that it can be reused again without allocating a new memory object.
 */
typedef struct VmSlot VmSlot;
struct VmSlot
{
	sxu32 nIdx;      /* Index in pVm->aMemObj[] */ 
	void *pUserData; /* Upper-layer private data */
};
/*
 * Each parsed URI is recorded and stored in an instance of the following structure.
 * This structure and it's related routines are taken verbatim from the xHT project
 * [A modern embeddable HTTP engine implementing all the RFC2616 methods]
 * the xHT project is developed internally by Symisc Systems.
 */
typedef struct SyhttpUri SyhttpUri;
struct SyhttpUri 
{ 
	SyString sHost;     /* Hostname or IP address */ 
	SyString sPort;     /* Port number */ 
	SyString sPath;     /* Mandatory resource path passed verbatim (Not decoded) */ 
	SyString sQuery;    /* Query part */	 
	SyString sFragment; /* Fragment part */ 
	SyString sScheme;   /* Scheme */ 
	SyString sUser;     /* Username */ 
	SyString sPass;     /* Password */
	SyString sRaw;      /* Raw URI */
};
/* 
 * An instance of the following structure is used to record all MIME headers seen
 * during a HTTP interaction. 
 * This structure and it's related routines are taken verbatim from the xHT project
 * [A modern embeddable HTTP engine implementing all the RFC2616 methods]
 * the xHT project is developed internally by Symisc Systems.
 */  
typedef struct SyhttpHeader SyhttpHeader;
struct SyhttpHeader 
{ 
	SyString sName;    /* Header name [i.e:"Content-Type", "Host", "User-Agent"]. NOT NUL TERMINATED */ 
	SyString sValue;   /* Header values [i.e: "text/html"]. NOT NUL TERMINATED */ 
};
/*
 * Supported HTTP methods.
 */
#define HTTP_METHOD_GET  1 /* GET */
#define HTTP_METHOD_HEAD 2 /* HEAD */
#define HTTP_METHOD_POST 3 /* POST */
#define HTTP_METHOD_PUT  4 /* PUT */
#define HTTP_METHOD_OTHR 5 /* Other HTTP methods [i.e: DELETE, TRACE, OPTIONS...]*/
/*
 * Supported HTTP protocol version.
 */
#define HTTP_PROTO_10 1 /* HTTP/1.0 */
#define HTTP_PROTO_11 2 /* HTTP/1.1 */
/*
 * Register a constant and it's associated expansion callback so that
 * it can be expanded from the target JX9 program.
 * The constant expansion mechanism under JX9 is extremely powerful yet
 * simple and work as follows:
 * Each registered constant have a C procedure associated with it.
 * This procedure known as the constant expansion callback is responsible
 * of expanding the invoked constant to the desired value, for example:
 * The C procedure associated with the "__PI__" constant expands to 3.14 (the value of PI).
 * The "__OS__" constant procedure expands to the name of the host Operating Systems
 * (Windows, Linux, ...) and so on.
 * Please refer to the official documentation for additional information.
 */
JX9_PRIVATE sxi32 jx9VmRegisterConstant(
	jx9_vm *pVm,            /* Target VM */
	const SyString *pName,  /* Constant name */
	ProcConstant xExpand,   /* Constant expansion callback */
	void *pUserData         /* Last argument to xExpand() */
	)
{
	jx9_constant *pCons;
	SyHashEntry *pEntry;
	char *zDupName;
	sxi32 rc;
	pEntry = SyHashGet(&pVm->hConstant, (const void *)pName->zString, pName->nByte);
	if( pEntry ){
		/* Overwrite the old definition and return immediately */
		pCons = (jx9_constant *)pEntry->pUserData;
		pCons->xExpand = xExpand;
		pCons->pUserData = pUserData;
		return SXRET_OK;
	}
	/* Allocate a new constant instance */
	pCons = (jx9_constant *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(jx9_constant));
	if( pCons == 0 ){
		return 0;
	}
	/* Duplicate constant name */
	zDupName = SyMemBackendStrDup(&pVm->sAllocator, pName->zString, pName->nByte);
	if( zDupName == 0 ){
		SyMemBackendPoolFree(&pVm->sAllocator, pCons);
		return 0;
	}
	/* Install the constant */
	SyStringInitFromBuf(&pCons->sName, zDupName, pName->nByte);
	pCons->xExpand = xExpand;
	pCons->pUserData = pUserData;
	rc = SyHashInsert(&pVm->hConstant, (const void *)zDupName, SyStringLength(&pCons->sName), pCons);
	if( rc != SXRET_OK ){
		SyMemBackendFree(&pVm->sAllocator, zDupName);
		SyMemBackendPoolFree(&pVm->sAllocator, pCons);
		return rc;
	}
	/* All done, constant can be invoked from JX9 code */
	return SXRET_OK;
}
/*
 * Allocate a new foreign function instance.
 * This function return SXRET_OK on success. Any other
 * return value indicates failure.
 * Please refer to the official documentation for an introduction to
 * the foreign function mechanism.
 */
static sxi32 jx9NewForeignFunction(
	jx9_vm *pVm,              /* Target VM */
	const SyString *pName,    /* Foreign function name */
	ProcHostFunction xFunc,  /* Foreign function implementation */
	void *pUserData,          /* Foreign function private data */
	jx9_user_func **ppOut     /* OUT: VM image of the foreign function */
	)
{
	jx9_user_func *pFunc;
	char *zDup;
	/* Allocate a new user function */
	pFunc = (jx9_user_func *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(jx9_user_func));
	if( pFunc == 0 ){
		return SXERR_MEM;
	}
	/* Duplicate function name */
	zDup = SyMemBackendStrDup(&pVm->sAllocator, pName->zString, pName->nByte);
	if( zDup == 0 ){
		SyMemBackendPoolFree(&pVm->sAllocator, pFunc);
		return SXERR_MEM;
	}
	/* Zero the structure */
	SyZero(pFunc, sizeof(jx9_user_func));
	/* Initialize structure fields */
	SyStringInitFromBuf(&pFunc->sName, zDup, pName->nByte);
	pFunc->pVm   = pVm;
	pFunc->xFunc = xFunc;
	pFunc->pUserData = pUserData;
	SySetInit(&pFunc->aAux, &pVm->sAllocator, sizeof(jx9_aux_data));
	/* Write a pointer to the new function */
	*ppOut = pFunc;
	return SXRET_OK;
}
/*
 * Install a foreign function and it's associated callback so that
 * it can be invoked from the target JX9 code.
 * This function return SXRET_OK on successful registration. Any other
 * return value indicates failure.
 * Please refer to the official documentation for an introduction to
 * the foreign function mechanism.
 */
JX9_PRIVATE sxi32 jx9VmInstallForeignFunction(
	jx9_vm *pVm,              /* Target VM */
	const SyString *pName,    /* Foreign function name */
	ProcHostFunction xFunc,  /* Foreign function implementation */
	void *pUserData           /* Foreign function private data */
	)
{
	jx9_user_func *pFunc;
	SyHashEntry *pEntry;
	sxi32 rc;
	/* Overwrite any previously registered function with the same name */
	pEntry = SyHashGet(&pVm->hHostFunction, pName->zString, pName->nByte);
	if( pEntry ){
		pFunc = (jx9_user_func *)pEntry->pUserData;
		pFunc->pUserData = pUserData;
		pFunc->xFunc = xFunc;
		SySetReset(&pFunc->aAux);
		return SXRET_OK;
	}
	/* Create a new user function */
	rc = jx9NewForeignFunction(&(*pVm), &(*pName), xFunc, pUserData, &pFunc);
	if( rc != SXRET_OK ){
		return rc;
	}
	/* Install the function in the corresponding hashtable */
	rc = SyHashInsert(&pVm->hHostFunction, SyStringData(&pFunc->sName), pName->nByte, pFunc);
	if( rc != SXRET_OK ){
		SyMemBackendFree(&pVm->sAllocator, (void *)SyStringData(&pFunc->sName));
		SyMemBackendPoolFree(&pVm->sAllocator, pFunc);
		return rc;
	}
	/* User function successfully installed */
	return SXRET_OK;
}
/*
 * Initialize a VM function.
 */
JX9_PRIVATE sxi32 jx9VmInitFuncState(
	jx9_vm *pVm,        /* Target VM */
	jx9_vm_func *pFunc, /* Target Fucntion */
	const char *zName,  /* Function name */
	sxu32 nByte,        /* zName length */
	sxi32 iFlags,       /* Configuration flags */
	void *pUserData     /* Function private data */
	)
{
	/* Zero the structure */
	SyZero(pFunc, sizeof(jx9_vm_func));	
	/* Initialize structure fields */
	/* Arguments container */
	SySetInit(&pFunc->aArgs, &pVm->sAllocator, sizeof(jx9_vm_func_arg));
	/* Static variable container */
	SySetInit(&pFunc->aStatic, &pVm->sAllocator, sizeof(jx9_vm_func_static_var));
	/* Bytecode container */
	SySetInit(&pFunc->aByteCode, &pVm->sAllocator, sizeof(VmInstr));
    /* Preallocate some instruction slots */
	SySetAlloc(&pFunc->aByteCode, 0x10);
	pFunc->iFlags = iFlags;
	pFunc->pUserData = pUserData;
	SyStringInitFromBuf(&pFunc->sName, zName, nByte);
	return SXRET_OK;
}
/*
 * Install a user defined function in the corresponding VM container.
 */
JX9_PRIVATE sxi32 jx9VmInstallUserFunction(
	jx9_vm *pVm,        /* Target VM */
	jx9_vm_func *pFunc, /* Target function */
	SyString *pName     /* Function name */
	)
{
	SyHashEntry *pEntry;
	sxi32 rc;
	if( pName == 0 ){
		/* Use the built-in name */
		pName = &pFunc->sName;
	}
	/* Check for duplicates (functions with the same name) first */
	pEntry = SyHashGet(&pVm->hFunction, pName->zString, pName->nByte);
	if( pEntry ){
		jx9_vm_func *pLink = (jx9_vm_func *)pEntry->pUserData;
		if( pLink != pFunc ){
			/* Link */
			pFunc->pNextName = pLink;
			pEntry->pUserData = pFunc;
		}
		return SXRET_OK;
	}
	/* First time seen */
	pFunc->pNextName = 0;
	rc = SyHashInsert(&pVm->hFunction, pName->zString, pName->nByte, pFunc);
	return rc;
}
/*
 * Instruction builder interface.
 */
JX9_PRIVATE sxi32 jx9VmEmitInstr(
	jx9_vm *pVm,  /* Target VM */
	sxi32 iOp,    /* Operation to perform */
	sxi32 iP1,    /* First operand */
	sxu32 iP2,    /* Second operand */
	void *p3,     /* Third operand */
	sxu32 *pIndex /* Instruction index. NULL otherwise */
	)
{
	VmInstr sInstr;
	sxi32 rc;
	/* Fill the VM instruction */
	sInstr.iOp = (sxu8)iOp; 
	sInstr.iP1 = iP1; 
	sInstr.iP2 = iP2; 
	sInstr.p3  = p3;  
	if( pIndex ){
		/* Instruction index in the bytecode array */
		*pIndex = SySetUsed(pVm->pByteContainer);
	}
	/* Finally, record the instruction */
	rc = SySetPut(pVm->pByteContainer, (const void *)&sInstr);
	if( rc != SXRET_OK ){
		jx9GenCompileError(&pVm->sCodeGen, E_ERROR, 1, "Fatal, Cannot emit instruction due to a memory failure");
		/* Fall throw */
	}
	return rc;
}
/*
 * Swap the current bytecode container with the given one.
 */
JX9_PRIVATE sxi32 jx9VmSetByteCodeContainer(jx9_vm *pVm, SySet *pContainer)
{
	if( pContainer == 0 ){
		/* Point to the default container */
		pVm->pByteContainer = &pVm->aByteCode;
	}else{
		/* Change container */
		pVm->pByteContainer = &(*pContainer);
	}
	return SXRET_OK;
}
/*
 * Return the current bytecode container.
 */
JX9_PRIVATE SySet * jx9VmGetByteCodeContainer(jx9_vm *pVm)
{
	return pVm->pByteContainer;
}
/*
 * Extract the VM instruction rooted at nIndex.
 */
JX9_PRIVATE VmInstr * jx9VmGetInstr(jx9_vm *pVm, sxu32 nIndex)
{
	VmInstr *pInstr;
	pInstr = (VmInstr *)SySetAt(pVm->pByteContainer, nIndex);
	return pInstr;
}
/*
 * Return the total number of VM instructions recorded so far.
 */
JX9_PRIVATE sxu32 jx9VmInstrLength(jx9_vm *pVm)
{
	return SySetUsed(pVm->pByteContainer);
}
/*
 * Pop the last VM instruction.
 */
JX9_PRIVATE VmInstr * jx9VmPopInstr(jx9_vm *pVm)
{
	return (VmInstr *)SySetPop(pVm->pByteContainer);
}
/*
 * Peek the last VM instruction.
 */
JX9_PRIVATE VmInstr * jx9VmPeekInstr(jx9_vm *pVm)
{
	return (VmInstr *)SySetPeek(pVm->pByteContainer);
}
/*
 * Allocate a new virtual machine frame.
 */
static VmFrame * VmNewFrame(
	jx9_vm *pVm,              /* Target VM */
	void *pUserData          /* Upper-layer private data */
	)
{
	VmFrame *pFrame;
	/* Allocate a new vm frame */
	pFrame = (VmFrame *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(VmFrame));
	if( pFrame == 0 ){
		return 0;
	}
	/* Zero the structure */
	SyZero(pFrame, sizeof(VmFrame));
	/* Initialize frame fields */
	pFrame->pUserData = pUserData;
	pFrame->pVm = pVm;
	SyHashInit(&pFrame->hVar, &pVm->sAllocator, 0, 0);
	SySetInit(&pFrame->sArg, &pVm->sAllocator, sizeof(VmSlot));
	SySetInit(&pFrame->sLocal, &pVm->sAllocator, sizeof(VmSlot));
	return pFrame;
}
/*
 * Enter a VM frame.
 */
static sxi32 VmEnterFrame(
	jx9_vm *pVm,               /* Target VM */
	void *pUserData,           /* Upper-layer private data */
	VmFrame **ppFrame          /* OUT: Top most active frame */
	)
{
	VmFrame *pFrame;
	/* Allocate a new frame */
	pFrame = VmNewFrame(&(*pVm), pUserData);
	if( pFrame == 0 ){
		return SXERR_MEM;
	}
	/* Link to the list of active VM frame */
	pFrame->pParent = pVm->pFrame;
	pVm->pFrame = pFrame;
	if( ppFrame ){
		/* Write a pointer to the new VM frame */
		*ppFrame = pFrame;
	}
	return SXRET_OK;
}
/*
 * Link a foreign variable with the TOP most active frame.
 * Refer to the JX9_OP_UPLINK instruction implementation for more
 * information.
 */
static sxi32 VmFrameLink(jx9_vm *pVm,SyString *pName)
{
	VmFrame *pTarget, *pFrame;
	SyHashEntry *pEntry = 0;
	sxi32 rc;
	/* Point to the upper frame */
	pFrame = pVm->pFrame;
	pTarget = pFrame;
	pFrame = pTarget->pParent;
	while( pFrame ){
		/* Query the current frame */
		pEntry = SyHashGet(&pFrame->hVar, (const void *)pName->zString, pName->nByte);
		if( pEntry ){
			/* Variable found */
			break;
		}		
		/* Point to the upper frame */
		pFrame = pFrame->pParent;
	}
	if( pEntry == 0 ){
		/* Inexistant variable */
		return SXERR_NOTFOUND;
	}
	/* Link to the current frame */
	rc = SyHashInsert(&pTarget->hVar, pEntry->pKey, pEntry->nKeyLen, pEntry->pUserData);
	return rc;
}
/*
 * Leave the top-most active frame.
 */
static void VmLeaveFrame(jx9_vm *pVm)
{
	VmFrame *pFrame = pVm->pFrame;
	if( pFrame ){
		/* Unlink from the list of active VM frame */
		pVm->pFrame = pFrame->pParent;
		if( pFrame->pParent  ){
			VmSlot  *aSlot;
			sxu32 n;
			/* Restore local variable to the free pool so that they can be reused again */
			aSlot = (VmSlot *)SySetBasePtr(&pFrame->sLocal);
			for(n = 0 ; n < SySetUsed(&pFrame->sLocal) ; ++n ){
				/* Unset the local variable */
				jx9VmUnsetMemObj(&(*pVm), aSlot[n].nIdx);
			}
		}
		/* Release internal containers */
		SyHashRelease(&pFrame->hVar);
		SySetRelease(&pFrame->sArg);
		SySetRelease(&pFrame->sLocal);
		/* Release the whole structure */
		SyMemBackendPoolFree(&pVm->sAllocator, pFrame);
	}
}
/*
 * Compare two functions signature and return the comparison result.
 */
static int VmOverloadCompare(SyString *pFirst, SyString *pSecond)
{
	const char *zSend = &pSecond->zString[pSecond->nByte];
	const char *zFend = &pFirst->zString[pFirst->nByte];
	const char *zSin = pSecond->zString;
	const char *zFin = pFirst->zString;
	const char *zPtr = zFin;
	for(;;){
		if( zFin >= zFend || zSin >= zSend ){
			break;
		}
		if( zFin[0] != zSin[0] ){
			/* mismatch */
			break;
		}
		zFin++;
		zSin++;
	}
	return (int)(zFin-zPtr);
}
/*
 * Select the appropriate VM function for the current call context.
 * This is the implementation of the powerful 'function overloading' feature
 * introduced by the version 2 of the JX9 engine.
 * Refer to the official documentation for more information.
 */
static jx9_vm_func * VmOverload(
	jx9_vm *pVm,         /* Target VM */
	jx9_vm_func *pList,  /* Linked list of candidates for overloading */
	jx9_value *aArg,     /* Array of passed arguments */
	int nArg             /* Total number of passed arguments  */
	)
{
	int iTarget, i, j, iCur, iMax;
	jx9_vm_func *apSet[10];   /* Maximum number of candidates */
	jx9_vm_func *pLink;
	SyString sArgSig;
	SyBlob sSig;

	pLink = pList;
	i = 0;
	/* Put functions expecting the same number of passed arguments */
	while( i < (int)SX_ARRAYSIZE(apSet) ){
		if( pLink == 0 ){
			break;
		}
		if( (int)SySetUsed(&pLink->aArgs) == nArg ){
			/* Candidate for overloading */
			apSet[i++] = pLink;
		}
		/* Point to the next entry */
		pLink = pLink->pNextName;
	}
	if( i < 1 ){
		/* No candidates, return the head of the list */
		return pList;
	}
	if( nArg < 1 || i < 2 ){
		/* Return the only candidate */
		return apSet[0];
	}
	/* Calculate function signature */
	SyBlobInit(&sSig, &pVm->sAllocator);
	for( j = 0 ; j < nArg ; j++ ){
		int c = 'n'; /* null */
		if( aArg[j].iFlags & MEMOBJ_HASHMAP ){
			/* Hashmap */
			c = 'h';
		}else if( aArg[j].iFlags & MEMOBJ_BOOL ){
			/* bool */
			c = 'b';
		}else if( aArg[j].iFlags & MEMOBJ_INT ){
			/* int */
			c = 'i';
		}else if( aArg[j].iFlags & MEMOBJ_STRING ){
			/* String */
			c = 's';
		}else if( aArg[j].iFlags & MEMOBJ_REAL ){
			/* Float */
			c = 'f';
		}
		if( c > 0 ){
			SyBlobAppend(&sSig, (const void *)&c, sizeof(char));
		}
	}
	SyStringInitFromBuf(&sArgSig, SyBlobData(&sSig), SyBlobLength(&sSig));
	iTarget = 0;
	iMax = -1;
	/* Select the appropriate function */
	for( j = 0 ; j < i ; j++ ){
		/* Compare the two signatures */
		iCur = VmOverloadCompare(&sArgSig, &apSet[j]->sSignature);
		if( iCur > iMax ){
			iMax = iCur;
			iTarget = j;
		}
	}
	SyBlobRelease(&sSig);
	/* Appropriate function for the current call context */
	return apSet[iTarget];
}
/* 
 * Dummy read-only buffer used for slot reservation.
 */
static const char zDummy[sizeof(jx9_value)] = { 0 }; /* Must be >= sizeof(jx9_value) */ 
/*
 * Reserve a constant memory object.
 * Return a pointer to the raw jx9_value on success. NULL on failure.
 */
JX9_PRIVATE jx9_value * jx9VmReserveConstObj(jx9_vm *pVm, sxu32 *pIndex)
{
	jx9_value *pObj;
	sxi32 rc;
	if( pIndex ){
		/* Object index in the object table */
		*pIndex = SySetUsed(&pVm->aLitObj);
	}
	/* Reserve a slot for the new object */
	rc = SySetPut(&pVm->aLitObj, (const void *)zDummy);
	if( rc != SXRET_OK ){
		/* 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.
		 */
		return 0;
	}
	pObj = (jx9_value *)SySetPeek(&pVm->aLitObj);
	return pObj;
}
/*
 * Reserve a memory object.
 * Return a pointer to the raw jx9_value on success. NULL on failure.
 */
static jx9_value * VmReserveMemObj(jx9_vm *pVm, sxu32 *pIndex)
{
	jx9_value *pObj;
	sxi32 rc;
	if( pIndex ){
		/* Object index in the object table */
		*pIndex = SySetUsed(&pVm->aMemObj);
	}
	/* Reserve a slot for the new object */
	rc = SySetPut(&pVm->aMemObj, (const void *)zDummy);
	if( rc != SXRET_OK ){
		/* 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.
		 */
		return 0;
	}
	pObj = (jx9_value *)SySetPeek(&pVm->aMemObj);
	return pObj;
}
/* Forward declaration */
static sxi32 VmEvalChunk(jx9_vm *pVm, jx9_context *pCtx, SyString *pChunk, int iFlags, int bTrueReturn);
/*
 * Built-in functions that cannot be implemented directly as foreign functions.
 */
#define JX9_BUILTIN_LIB \
	"function scandir(string $directory, int $sort_order = SCANDIR_SORT_ASCENDING)"\
    "{"\
	"  if( func_num_args() < 1 ){ return FALSE; }"\
	"  $aDir = [];"\
	"  $pHandle = opendir($directory);"\
	"  if( $pHandle == FALSE ){ return FALSE; }"\
	"  while(FALSE !== ($pEntry = readdir($pHandle)) ){"\
	"      $aDir[] = $pEntry;"\
	"   }"\
	"  closedir($pHandle);"\
	"  if( $sort_order == SCANDIR_SORT_DESCENDING ){"\
	"      rsort($aDir);"\
	"  }else if( $sort_order == SCANDIR_SORT_ASCENDING ){"\
	"      sort($aDir);"\
	"  }"\
	"  return $aDir;"\
	"}"\
	"function glob(string $pattern, int $iFlags = 0){"\
	"/* Open the target directory */"\
	"$zDir = dirname($pattern);"\
	"if(!is_string($zDir) ){ $zDir = './'; }"\
	"$pHandle = opendir($zDir);"\
	"if( $pHandle == FALSE ){"\
	"   /* IO error while opening the current directory, return FALSE */"\
	"	return FALSE;"\
	"}"\
	"$pattern = basename($pattern);"\
	"$pArray = []; /* Empty array */"\
	"/* Loop throw available entries */"\
	"while( FALSE !== ($pEntry = readdir($pHandle)) ){"\
	" /* Use the built-in strglob function which is a Symisc eXtension for wildcard comparison*/"\
	"	$rc = strglob($pattern, $pEntry);"\
	"	if( $rc ){"\
	"	   if( is_dir($pEntry) ){"\
	"	      if( $iFlags & GLOB_MARK ){"\
	"		     /* Adds a slash to each directory returned */"\
	"			 $pEntry .= DIRECTORY_SEPARATOR;"\
	"		  }"\
	"	   }else if( $iFlags & GLOB_ONLYDIR ){"\
	"	     /* Not a directory, ignore */"\
	"		 continue;"\
	"	   }"\
	"	   /* Add the entry */"\
	"	   $pArray[] = $pEntry;"\
	"	}"\
	" }"\
	"/* Close the handle */"\
	"closedir($pHandle);"\
	"if( ($iFlags & GLOB_NOSORT) == 0 ){"\
	"  /* Sort the array */"\
	"  sort($pArray);"\
	"}"\
	"if( ($iFlags & GLOB_NOCHECK) && sizeof($pArray) < 1 ){"\
	"  /* Return the search pattern if no files matching were found */"\
	"  $pArray[] = $pattern;"\
	"}"\
	"/* Return the created array */"\
	"return $pArray;"\
   "}"\
   "/* Creates a temporary file */"\
   "function tmpfile(){"\
   "  /* Extract the temp directory */"\
   "  $zTempDir = sys_get_temp_dir();"\
   "  if( strlen($zTempDir) < 1 ){"\
   "    /* Use the current dir */"\
   "    $zTempDir = '.';"\
   "  }"\
   "  /* Create the file */"\
   "  $pHandle = fopen($zTempDir.DIRECTORY_SEPARATOR.'JX9'.rand_str(12), 'w+');"\
   "  return $pHandle;"\
   "}"\
   "/* Creates a temporary filename */"\
   "function tempnam(string $zDir = sys_get_temp_dir() /* Symisc eXtension */, string $zPrefix = 'JX9')"\
   "{"\
   "   return $zDir.DIRECTORY_SEPARATOR.$zPrefix.rand_str(12);"\
   "}"\
	"function max(){"\
    "  $pArgs = func_get_args();"\
    " if( sizeof($pArgs) < 1 ){"\
	"  return null;"\
    " }"\
    " if( sizeof($pArgs) < 2 ){"\
    " $pArg = $pArgs[0];"\
	" if( !is_array($pArg) ){"\
	"   return $pArg; "\
	" }"\
	" if( sizeof($pArg) < 1 ){"\
	"   return null;"\
	" }"\
	" $pArg = array_copy($pArgs[0]);"\
	" reset($pArg);"\
	" $max = current($pArg);"\
	" while( FALSE !== ($val = next($pArg)) ){"\
	"   if( $val > $max ){"\
	"     $max = $val;"\
    " }"\
	" }"\
	" return $max;"\
    " }"\
    " $max = $pArgs[0];"\
    " for( $i = 1; $i < sizeof($pArgs) ; ++$i ){"\
    " $val = $pArgs[$i];"\
	"if( $val > $max ){"\
	" $max = $val;"\
	"}"\
    " }"\
	" return $max;"\
    "}"\
	"function min(){"\
    "  $pArgs = func_get_args();"\
    " if( sizeof($pArgs) < 1 ){"\
	"  return null;"\
    " }"\
    " if( sizeof($pArgs) < 2 ){"\
    " $pArg = $pArgs[0];"\
	" if( !is_array($pArg) ){"\
	"   return $pArg; "\
	" }"\
	" if( sizeof($pArg) < 1 ){"\
	"   return null;"\
	" }"\
	" $pArg = array_copy($pArgs[0]);"\
	" reset($pArg);"\
	" $min = current($pArg);"\
	" while( FALSE !== ($val = next($pArg)) ){"\
	"   if( $val < $min ){"\
	"     $min = $val;"\
    " }"\
	" }"\
	" return $min;"\
    " }"\
    " $min = $pArgs[0];"\
    " for( $i = 1; $i < sizeof($pArgs) ; ++$i ){"\
    " $val = $pArgs[$i];"\
	"if( $val < $min ){"\
	" $min = $val;"\
	" }"\
    " }"\
	" return $min;"\
	"}"
/*
 * Initialize a freshly allocated JX9 Virtual Machine so that we can
 * start compiling the target JX9 program.
 */
JX9_PRIVATE sxi32 jx9VmInit(
	 jx9_vm *pVm, /* Initialize this */
	 jx9 *pEngine /* Master engine */
	 )
{
	SyString sBuiltin;
	jx9_value *pObj;
	sxi32 rc;
	/* Zero the structure */
	SyZero(pVm, sizeof(jx9_vm));
	/* Initialize VM fields */
	pVm->pEngine = &(*pEngine);
	SyMemBackendInitFromParent(&pVm->sAllocator, &pEngine->sAllocator);
	/* Instructions containers */
	SySetInit(&pVm->aByteCode, &pVm->sAllocator, sizeof(VmInstr));
	SySetAlloc(&pVm->aByteCode, 0xFF);
	pVm->pByteContainer = &pVm->aByteCode;
	/* Object containers */
	SySetInit(&pVm->aMemObj, &pVm->sAllocator, sizeof(jx9_value));
	SySetAlloc(&pVm->aMemObj, 0xFF);
	/* Virtual machine internal containers */
	SyBlobInit(&pVm->sConsumer, &pVm->sAllocator);
	SyBlobInit(&pVm->sWorker, &pVm->sAllocator);
	SyBlobInit(&pVm->sArgv, &pVm->sAllocator);
	SySetInit(&pVm->aLitObj, &pVm->sAllocator, sizeof(jx9_value));
	SySetAlloc(&pVm->aLitObj, 0xFF);
	SyHashInit(&pVm->hHostFunction, &pVm->sAllocator, 0, 0);
	SyHashInit(&pVm->hFunction, &pVm->sAllocator, 0, 0);
	SyHashInit(&pVm->hConstant, &pVm->sAllocator, 0, 0);
	SyHashInit(&pVm->hSuper, &pVm->sAllocator, 0, 0);
	SySetInit(&pVm->aFreeObj, &pVm->sAllocator, sizeof(VmSlot));
	/* Configuration containers */
	SySetInit(&pVm->aFiles, &pVm->sAllocator, sizeof(SyString));
	SySetInit(&pVm->aPaths, &pVm->sAllocator, sizeof(SyString));
	SySetInit(&pVm->aIncluded, &pVm->sAllocator, sizeof(SyString));
	SySetInit(&pVm->aIOstream, &pVm->sAllocator, sizeof(jx9_io_stream *));
	/* Error callbacks containers */
	jx9MemObjInit(&(*pVm), &pVm->sAssertCallback);
	/* Set a default recursion limit */
#if defined(__WINNT__) || defined(__UNIXES__)
	pVm->nMaxDepth = 32;
#else
	pVm->nMaxDepth = 16;
#endif
	/* Default assertion flags */
	pVm->iAssertFlags = JX9_ASSERT_WARNING; /* Issue a warning for each failed assertion */
	/* PRNG context */
	SyRandomnessInit(&pVm->sPrng, 0, 0);
	/* Install the null constant */
	pObj = jx9VmReserveConstObj(&(*pVm), 0);
	if( pObj == 0 ){
		rc = SXERR_MEM;
		goto Err;
	}
	jx9MemObjInit(pVm, pObj);
	/* Install the boolean TRUE constant */
	pObj = jx9VmReserveConstObj(&(*pVm), 0);
	if( pObj == 0 ){
		rc = SXERR_MEM;
		goto Err;
	}
	jx9MemObjInitFromBool(pVm, pObj, 1);
	/* Install the boolean FALSE constant */
	pObj = jx9VmReserveConstObj(&(*pVm), 0);
	if( pObj == 0 ){
		rc = SXERR_MEM;
		goto Err;
	}
	jx9MemObjInitFromBool(pVm, pObj, 0);
	/* Create the global frame */
	rc = VmEnterFrame(&(*pVm), 0, 0);
	if( rc != SXRET_OK ){
		goto Err;
	}
	/* Initialize the code generator */
	rc = jx9InitCodeGenerator(pVm, pEngine->xConf.xErr, pEngine->xConf.pErrData);
	if( rc != SXRET_OK ){
		goto Err;
	}
	/* VM correctly initialized, set the magic number */
	pVm->nMagic = JX9_VM_INIT;
	SyStringInitFromBuf(&sBuiltin,JX9_BUILTIN_LIB, sizeof(JX9_BUILTIN_LIB)-1);
	/* Compile the built-in library */
	VmEvalChunk(&(*pVm), 0, &sBuiltin, 0, FALSE);
	/* Reset the code generator */
	jx9ResetCodeGenerator(&(*pVm), pEngine->xConf.xErr, pEngine->xConf.pErrData);
	return SXRET_OK;
Err:
	SyMemBackendRelease(&pVm->sAllocator);
	return rc;
}
/*
 * Default VM output consumer callback.That is, all VM output is redirected to this
 * routine which store the output in an internal blob.
 * The output can be extracted later after program execution [jx9_vm_exec()] via
 * the [jx9_vm_config()] interface with a configuration verb set to
 * jx9VM_CONFIG_EXTRACT_OUTPUT.
 * Refer to the official docurmentation for additional information.
 * Note that for performance reason it's preferable to install a VM output
 * consumer callback via (jx9VM_CONFIG_OUTPUT) rather than waiting for the VM
 * to finish executing and extracting the output.
 */
JX9_PRIVATE sxi32 jx9VmBlobConsumer(
	const void *pOut,   /* VM Generated output*/
	unsigned int nLen,  /* Generated output length */
	void *pUserData     /* User private data */
	)
{
	 sxi32 rc;
	 /* Store the output in an internal BLOB */
	 rc = SyBlobAppend((SyBlob *)pUserData, pOut, nLen);
	 return rc;
}
#define VM_STACK_GUARD 16
/*
 * Allocate a new operand stack so that we can start executing
 * our compiled JX9 program.
 * Return a pointer to the operand stack (array of jx9_values)
 * on success. NULL (Fatal error) on failure.
 */
static jx9_value * VmNewOperandStack(
	jx9_vm *pVm, /* Target VM */
	sxu32 nInstr /* Total numer of generated bytecode instructions */
	)
{
	jx9_value *pStack;
  /* No instruction ever pushes more than a single element onto the
  ** stack and the stack never grows on successive executions of the
  ** same loop. So the total number of instructions is an upper bound
  ** on the maximum stack depth required.
  **
  ** Allocation all the stack space we will ever need.
  */
	nInstr += VM_STACK_GUARD;
	pStack = (jx9_value *)SyMemBackendAlloc(&pVm->sAllocator, nInstr * sizeof(jx9_value));
	if( pStack == 0 ){
		return 0;
	}
	/* Initialize the operand stack */
	while( nInstr > 0 ){
		jx9MemObjInit(&(*pVm), &pStack[nInstr - 1]);
		--nInstr;
	}
	/* Ready for bytecode execution */
	return pStack;
}
/* Forward declaration */
static sxi32 VmRegisterSpecialFunction(jx9_vm *pVm);
/*
 * Prepare the Virtual Machine for bytecode execution.
 * This routine gets called by the JX9 engine after
 * successful compilation of the target JX9 program.
 */
JX9_PRIVATE sxi32 jx9VmMakeReady(
	jx9_vm *pVm /* Target VM */
	)
{
	sxi32 rc;
	if( pVm->nMagic != JX9_VM_INIT ){
		/* Initialize your VM first */
		return SXERR_CORRUPT;
	}
	/* Mark the VM ready for bytecode execution */
	pVm->nMagic = JX9_VM_RUN; 
	/* Release the code generator now we have compiled our program */
	jx9ResetCodeGenerator(pVm, 0, 0);
	/* Emit the DONE instruction */
	rc = jx9VmEmitInstr(&(*pVm), JX9_OP_DONE, 0, 0, 0, 0);
	if( rc != SXRET_OK ){
		return SXERR_MEM;
	}
	/* Script return value */
	jx9MemObjInit(&(*pVm), &pVm->sExec); /* Assume a NULL return value */
	/* Allocate a new operand stack */	
	pVm->aOps = VmNewOperandStack(&(*pVm), SySetUsed(pVm->pByteContainer));
	if( pVm->aOps == 0 ){
		return SXERR_MEM;
	}
	/* Set the default VM output consumer callback and it's 
	 * private data. */
	pVm->sVmConsumer.xConsumer = jx9VmBlobConsumer;
	pVm->sVmConsumer.pUserData = &pVm->sConsumer;
	/* Register special functions first [i.e: print, func_get_args(), die, etc.] */
	rc = VmRegisterSpecialFunction(&(*pVm));
	if( rc != SXRET_OK ){
		/* Don't worry about freeing memory, everything will be released shortly */
		return rc;
	}
	/* Create superglobals [i.e: $GLOBALS, $_GET, $_POST...] */
	rc = jx9HashmapLoadBuiltin(&(*pVm));
	if( rc != SXRET_OK ){
		/* Don't worry about freeing memory, everything will be released shortly */
		return rc;
	}
	/* Register built-in constants [i.e: JX9_EOL, JX9_OS...] */
	jx9RegisterBuiltInConstant(&(*pVm));
	/* Register built-in functions [i.e: is_null(), array_diff(), strlen(), etc.] */
	jx9RegisterBuiltInFunction(&(*pVm));
	/* VM is ready for bytecode execution */
	return SXRET_OK;
}
/*
 * Reset a Virtual Machine to it's initial state.
 */
JX9_PRIVATE sxi32 jx9VmReset(jx9_vm *pVm)
{
	if( pVm->nMagic != JX9_VM_RUN && pVm->nMagic != JX9_VM_EXEC ){
		return SXERR_CORRUPT;
	}
	/* TICKET 1433-003: As of this version, the VM is automatically reset */
	SyBlobReset(&pVm->sConsumer);
	jx9MemObjRelease(&pVm->sExec);
	/* Set the ready flag */
	pVm->nMagic = JX9_VM_RUN;
	return SXRET_OK;
}
/*
 * Release a Virtual Machine.
 * Every virtual machine must be destroyed in order to avoid memory leaks.
 */
JX9_PRIVATE sxi32 jx9VmRelease(jx9_vm *pVm)
{
	/* Set the stale magic number */
	pVm->nMagic = JX9_VM_STALE;
	/* Release the private memory subsystem */
	SyMemBackendRelease(&pVm->sAllocator);
	return SXRET_OK;
}
/*
 * Initialize a foreign function call context.
 * The context in which a foreign function executes is stored in a jx9_context object.
 * A pointer to a jx9_context object is always first parameter to application-defined foreign
 * functions.
 * The application-defined foreign function implementation will pass this pointer through into
 * calls to dozens of interfaces, these includes jx9_result_int(), jx9_result_string(), jx9_result_value(), 
 * jx9_context_new_scalar(), jx9_context_alloc_chunk(), jx9_context_output(), jx9_context_throw_error()
 * and many more. Refer to the C/C++ Interfaces documentation for additional information.
 */
static sxi32 VmInitCallContext(
	jx9_context *pOut,    /* Call Context */
	jx9_vm *pVm,          /* Target VM */
	jx9_user_func *pFunc, /* Foreign function to execute shortly */
	jx9_value *pRet,      /* Store return value here*/
	sxi32 iFlags          /* Control flags */
	)
{
	pOut->pFunc = pFunc;
	pOut->pVm   = pVm;
	SySetInit(&pOut->sVar, &pVm->sAllocator, sizeof(jx9_value *));
	SySetInit(&pOut->sChunk, &pVm->sAllocator, sizeof(jx9_aux_data));
	/* Assume a null return value */
	MemObjSetType(pRet, MEMOBJ_NULL);
	pOut->pRet = pRet;
	pOut->iFlags = iFlags;
	return SXRET_OK;
}
/*
 * Release a foreign function call context and cleanup the mess
 * left behind.
 */
static void VmReleaseCallContext(jx9_context *pCtx)
{
	sxu32 n;
	if( SySetUsed(&pCtx->sVar) > 0 ){
		jx9_value **apObj = (jx9_value **)SySetBasePtr(&pCtx->sVar);
		for( n = 0 ; n < SySetUsed(&pCtx->sVar) ; ++n ){
			if( apObj[n] == 0 ){
				/* Already released */
				continue;
			}
			jx9MemObjRelease(apObj[n]);
			SyMemBackendPoolFree(&pCtx->pVm->sAllocator, apObj[n]);
		}
		SySetRelease(&pCtx->sVar);
	}
	if( SySetUsed(&pCtx->sChunk) > 0 ){
		jx9_aux_data *aAux;
		void *pChunk;
		/* Automatic release of dynamically allocated chunk 
		 * using [jx9_context_alloc_chunk()].
		 */
		aAux = (jx9_aux_data *)SySetBasePtr(&pCtx->sChunk);
		for( n = 0; n < SySetUsed(&pCtx->sChunk) ; ++n ){
			pChunk = aAux[n].pAuxData;
			/* Release the chunk */
			if( pChunk ){
				SyMemBackendFree(&pCtx->pVm->sAllocator, pChunk);
			}
		}
		SySetRelease(&pCtx->sChunk);
	}
}
/*
 * Release a jx9_value allocated from the body of a foreign function.
 * Refer to [jx9_context_release_value()] for additional information.
 */
JX9_PRIVATE void jx9VmReleaseContextValue(
	jx9_context *pCtx, /* Call context */
	jx9_value *pValue  /* Release this value */
	)
{
	if( pValue == 0 ){
		/* NULL value is a harmless operation */
		return;
	}
	if( SySetUsed(&pCtx->sVar) > 0 ){
		jx9_value **apObj = (jx9_value **)SySetBasePtr(&pCtx->sVar);
		sxu32 n;
		for( n = 0 ; n < SySetUsed(&pCtx->sVar) ; ++n ){
			if( apObj[n] == pValue ){
				jx9MemObjRelease(pValue);
				SyMemBackendPoolFree(&pCtx->pVm->sAllocator, pValue);
				/* Mark as released */
				apObj[n] = 0;
				break;
			}
		}
	}
}
/*
 * Pop and release as many memory object from the operand stack.
 */
static void VmPopOperand(
	jx9_value **ppTos, /* Operand stack */
	sxi32 nPop         /* Total number of memory objects to pop */
	)
{
	jx9_value *pTos = *ppTos;
	while( nPop > 0 ){
		jx9MemObjRelease(pTos);
		pTos--;
		nPop--;
	}
	/* Top of the stack */
	*ppTos = pTos;
}
/*
 * Reserve a memory object.
 * Return a pointer to the raw jx9_value on success. NULL on failure.
 */
JX9_PRIVATE jx9_value * jx9VmReserveMemObj(jx9_vm *pVm,sxu32 *pIdx)
{
	jx9_value *pObj = 0;
	VmSlot *pSlot;
	sxu32 nIdx;
	/* Check for a free slot */
	nIdx = SXU32_HIGH; /* cc warning */
	pSlot = (VmSlot *)SySetPop(&pVm->aFreeObj);
	if( pSlot ){
		pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pSlot->nIdx);
		nIdx = pSlot->nIdx;
	}
	if( pObj == 0 ){
		/* Reserve a new memory object */
		pObj = VmReserveMemObj(&(*pVm), &nIdx);
		if( pObj == 0 ){
			return 0;
		}
	}
	/* Set a null default value */
	jx9MemObjInit(&(*pVm), pObj);
	if( pIdx ){
		*pIdx = nIdx;
	}
	pObj->nIdx = nIdx;
	return pObj;
}
/*
 * Extract a variable value from the top active VM frame.
 * Return a pointer to the variable value on success. 
 * NULL otherwise (non-existent variable/Out-of-memory, ...).
 */
static jx9_value * VmExtractMemObj(
	jx9_vm *pVm,           /* Target VM */
	const SyString *pName, /* Variable name */
	int bDup,              /* True to duplicate variable name */
	int bCreate            /* True to create the variable if non-existent */
	)
{
	int bNullify = FALSE;
	SyHashEntry *pEntry;
	VmFrame *pFrame;
	jx9_value *pObj;
	sxu32 nIdx;
	sxi32 rc;
	/* Point to the top active frame */
	pFrame = pVm->pFrame;
	/* Perform the lookup */
	if( pName == 0 || pName->nByte < 1 ){
		static const SyString sAnnon = { " " , sizeof(char) };
		pName = &sAnnon;
		/* Always nullify the object */
		bNullify = TRUE;
		bDup = FALSE;
	}
	/* Check the superglobals table first */
	pEntry = SyHashGet(&pVm->hSuper, (const void *)pName->zString, pName->nByte);
	if( pEntry == 0 ){
		/* Query the top active frame */
		pEntry = SyHashGet(&pFrame->hVar, (const void *)pName->zString, pName->nByte);
		if( pEntry == 0 ){
			char *zName = (char *)pName->zString;
			VmSlot sLocal;
			if( !bCreate ){
				/* Do not create the variable, return NULL */
				return 0;
			}
			/* No such variable, automatically create a new one and install
			 * it in the current frame.
			 */
			pObj = jx9VmReserveMemObj(&(*pVm),&nIdx);
			if( pObj == 0 ){
				return 0;
			}
			if( bDup ){
				/* Duplicate name */
				zName = SyMemBackendStrDup(&pVm->sAllocator, pName->zString, pName->nByte);
				if( zName == 0 ){
					return 0;
				}
			}
			/* Link to the top active VM frame */
			rc = SyHashInsert(&pFrame->hVar, zName, pName->nByte, SX_INT_TO_PTR(nIdx));
			if( rc != SXRET_OK ){
				/* Return the slot to the free pool */
				sLocal.nIdx = nIdx;
				sLocal.pUserData = 0;
				SySetPut(&pVm->aFreeObj, (const void *)&sLocal);
				return 0;
			}
			if( pFrame->pParent != 0 ){
				/* Local variable */
				sLocal.nIdx = nIdx;
				SySetPut(&pFrame->sLocal, (const void *)&sLocal);
			}
		}else{
			/* Extract variable contents */
			nIdx = (sxu32)SX_PTR_TO_INT(pEntry->pUserData);
			pObj = (jx9_value *)SySetAt(&pVm->aMemObj, nIdx);
			if( bNullify && pObj ){
				jx9MemObjRelease(pObj);
			}
		}
	}else{
		/* Superglobal */
		nIdx = (sxu32)SX_PTR_TO_INT(pEntry->pUserData);
		pObj = (jx9_value *)SySetAt(&pVm->aMemObj, nIdx);
	}
	return pObj;
}
/*
 * Extract a superglobal variable such as $_GET, $_POST, $_HEADERS, .... 
 * Return a pointer to the variable value on success.NULL otherwise.
 */
static jx9_value * VmExtractSuper(
	jx9_vm *pVm,       /* Target VM */
	const char *zName, /* Superglobal name: NOT NULL TERMINATED */
	sxu32 nByte        /* zName length */
	)
{
	SyHashEntry *pEntry;
	jx9_value *pValue;
	sxu32 nIdx;
	/* Query the superglobal table */
	pEntry = SyHashGet(&pVm->hSuper, (const void *)zName, nByte);
	if( pEntry == 0 ){
		/* No such entry */
		return 0;
	}
	/* Extract the superglobal index in the global object pool */
	nIdx = SX_PTR_TO_INT(pEntry->pUserData);
	/* Extract the variable value  */
	pValue = (jx9_value *)SySetAt(&pVm->aMemObj, nIdx);
	return pValue;
}
/*
 * Perform a raw hashmap insertion.
 * Refer to the [jx9VmConfigure()] implementation for additional information.
 */
static sxi32 VmHashmapInsert(
	jx9_hashmap *pMap,  /* Target hashmap  */
	const char *zKey,   /* Entry key */
	int nKeylen,        /* zKey length*/
	const char *zData,  /* Entry data */
	int nLen            /* zData length */
	)
{
	jx9_value sKey,sValue;
	jx9_value *pKey;
	sxi32 rc;
	pKey = 0;
	jx9MemObjInit(pMap->pVm, &sKey);
	jx9MemObjInitFromString(pMap->pVm, &sValue, 0);
	if( zKey ){
		if( nKeylen < 0 ){
			nKeylen = (int)SyStrlen(zKey);
		}
		jx9MemObjStringAppend(&sKey, zKey, (sxu32)nKeylen);
		pKey = &sKey;
	}
	if( zData ){
		if( nLen < 0 ){
			/* Compute length automatically */
			nLen = (int)SyStrlen(zData);
		}
		jx9MemObjStringAppend(&sValue, zData, (sxu32)nLen);
	}
	/* Perform the insertion */
	rc = jx9HashmapInsert(&(*pMap),pKey,&sValue);
	jx9MemObjRelease(&sKey);
	jx9MemObjRelease(&sValue);
	return rc;
}
/* Forward declaration */
static sxi32 VmHttpProcessRequest(jx9_vm *pVm, const char *zRequest, int nByte);
/*
 * Configure a working virtual machine instance.
 *
 * This routine is used to configure a JX9 virtual machine obtained by a prior
 * successful call to one of the compile interface such as jx9_compile()
 * jx9_compile_v2() or jx9_compile_file().
 * The second argument to this function is an integer configuration option
 * that determines what property of the JX9 virtual machine is to be configured.
 * Subsequent arguments vary depending on the configuration option in the second
 * argument. There are many verbs but the most important are JX9_VM_CONFIG_OUTPUT, 
 * JX9_VM_CONFIG_HTTP_REQUEST and JX9_VM_CONFIG_ARGV_ENTRY.
 * Refer to the official documentation for the list of allowed verbs.
 */
JX9_PRIVATE sxi32 jx9VmConfigure(
	jx9_vm *pVm, /* Target VM */
	sxi32 nOp,   /* Configuration verb */
	va_list ap   /* Subsequent option arguments */
	)
{
	sxi32 rc = SXRET_OK;
	switch(nOp){
	case JX9_VM_CONFIG_OUTPUT: {
		ProcConsumer xConsumer = va_arg(ap, ProcConsumer);
		void *pUserData = va_arg(ap, void *);
		/* VM output consumer callback */
#ifdef UNTRUST
		if( xConsumer == 0 ){
			rc = SXERR_CORRUPT;
			break;
		}
#endif
		/* Install the output consumer */
		pVm->sVmConsumer.xConsumer = xConsumer;
		pVm->sVmConsumer.pUserData = pUserData;
		break;
							   }
	case JX9_VM_CONFIG_IMPORT_PATH: {
		/* Import path */
		  const char *zPath;
		  SyString sPath;
		  zPath = va_arg(ap, const char *);
#if defined(UNTRUST)
		  if( zPath == 0 ){
			  rc = SXERR_EMPTY;
			  break;
		  }
#endif
		  SyStringInitFromBuf(&sPath, zPath, SyStrlen(zPath));
		  /* Remove trailing slashes and backslashes */
#ifdef __WINNT__
		  SyStringTrimTrailingChar(&sPath, '\\');
#endif
		  SyStringTrimTrailingChar(&sPath, '/');
		  /* Remove leading and trailing white spaces */
		  SyStringFullTrim(&sPath);
		  if( sPath.nByte > 0 ){
			  /* Store the path in the corresponding conatiner */
			  rc = SySetPut(&pVm->aPaths, (const void *)&sPath);
		  }
		  break;
									 }
	case JX9_VM_CONFIG_ERR_REPORT:
		/* Run-Time Error report */
		pVm->bErrReport = 1;
		break;
	case JX9_VM_CONFIG_RECURSION_DEPTH:{
		/* Recursion depth */
		int nDepth = va_arg(ap, int);
		if( nDepth > 2 && nDepth < 1024 ){
			pVm->nMaxDepth = nDepth;
		}
		break;
									   }
	case JX9_VM_OUTPUT_LENGTH: {
		/* VM output length in bytes */
		sxu32 *pOut = va_arg(ap, sxu32 *);
#ifdef UNTRUST
		if( pOut == 0 ){
			rc = SXERR_CORRUPT;
			break;
		}
#endif
		*pOut = pVm->nOutputLen;
		break;
							   }
	case JX9_VM_CONFIG_CREATE_VAR: {
		/* Create a new superglobal/global variable */
		const char *zName = va_arg(ap, const char *);
		jx9_value *pValue = va_arg(ap, jx9_value *);
		SyHashEntry *pEntry;
		jx9_value *pObj;
		sxu32 nByte;
		sxu32 nIdx; 
#ifdef UNTRUST
		if( SX_EMPTY_STR(zName) || pValue == 0 ){
			rc = SXERR_CORRUPT;
			break;
		}
#endif
		nByte = SyStrlen(zName);
		/* Check if the superglobal is already installed */
		pEntry = SyHashGet(&pVm->hSuper, (const void *)zName, nByte);
		if( pEntry ){
			/* Variable already installed */
			nIdx = SX_PTR_TO_INT(pEntry->pUserData);
			/* Extract contents */
			pObj = (jx9_value *)SySetAt(&pVm->aMemObj, nIdx);
			if( pObj ){
				/* Overwrite old contents */
				jx9MemObjStore(pValue, pObj);
			}
		}else{
			/* Install a new variable */
			pObj = jx9VmReserveMemObj(&(*pVm),&nIdx);
			if( pObj == 0 ){
				rc = SXERR_MEM;
				break;
			}
			/* Copy value */
			jx9MemObjStore(pValue, pObj);
			/* Install the superglobal */
			rc = SyHashInsert(&pVm->hSuper, (const void *)zName, nByte, SX_INT_TO_PTR(nIdx));
		}
		break;
									}
	case JX9_VM_CONFIG_SERVER_ATTR:
	case JX9_VM_CONFIG_ENV_ATTR:  {
		const char *zKey   = va_arg(ap, const char *);
		const char *zValue = va_arg(ap, const char *);
		int nLen = va_arg(ap, int);
		jx9_hashmap *pMap;
		jx9_value *pValue;
		if( nOp == JX9_VM_CONFIG_ENV_ATTR ){
			/* Extract the $_ENV superglobal */
			pValue = VmExtractSuper(&(*pVm), "_ENV", sizeof("_ENV")-1);
		}else{
			/* Extract the $_SERVER superglobal */
			pValue = VmExtractSuper(&(*pVm), "_SERVER", sizeof("_SERVER")-1);
		}
		if( pValue == 0 || (pValue->iFlags & MEMOBJ_HASHMAP) == 0 ){
			/* No such entry */
			rc = SXERR_NOTFOUND;
			break;
		}
		/* Point to the hashmap */
		pMap = (jx9_hashmap *)pValue->x.pOther;
		/* Perform the insertion */
		rc = VmHashmapInsert(pMap, zKey, -1, zValue, nLen);
		break;
								   }
	case JX9_VM_CONFIG_ARGV_ENTRY:{
		/* Script arguments */
		const char *zValue = va_arg(ap, const char *);
		jx9_hashmap *pMap;
		jx9_value *pValue;
		/* Extract the $argv array */
		pValue = VmExtractSuper(&(*pVm), "argv", sizeof("argv")-1);
		if( pValue == 0 || (pValue->iFlags & MEMOBJ_HASHMAP) == 0 ){
			/* No such entry */
			rc = SXERR_NOTFOUND;
			break;
		}
		/* Point to the hashmap */
		pMap = (jx9_hashmap *)pValue->x.pOther;
		/* Perform the insertion */
		rc = VmHashmapInsert(pMap, 0, 0, zValue,-1);
		if( rc == SXRET_OK && zValue && zValue[0] != 0 ){
			if( pMap->nEntry > 1 ){
				/* Append space separator first */
				SyBlobAppend(&pVm->sArgv, (const void *)" ", sizeof(char));
			}
			SyBlobAppend(&pVm->sArgv, (const void *)zValue,SyStrlen(zValue));
		}
		break;
								  }
	case JX9_VM_CONFIG_EXEC_VALUE: {
		/* Script return value */
		jx9_value **ppValue = va_arg(ap, jx9_value **);
#ifdef UNTRUST
		if( ppValue == 0 ){
			rc = SXERR_CORRUPT;
			break;
		}
#endif
		*ppValue = &pVm->sExec;
		break;
								   }
	case JX9_VM_CONFIG_IO_STREAM: {
		/* Register an IO stream device */
		const jx9_io_stream *pStream = va_arg(ap, const jx9_io_stream *);
		/* Make sure we are dealing with a valid IO stream */
		if( pStream == 0 || pStream->zName == 0 || pStream->zName[0] == 0 ||
			pStream->xOpen == 0 || pStream->xRead == 0 ){
				/* Invalid stream */
				rc = SXERR_INVALID;
				break;
		}
		if( pVm->pDefStream == 0 && SyStrnicmp(pStream->zName, "file", sizeof("file")-1) == 0 ){
			/* Make the 'file://' stream the defaut stream device */
			pVm->pDefStream = pStream;
		}
		/* Insert in the appropriate container */
		rc = SySetPut(&pVm->aIOstream, (const void *)&pStream);
		break;
								  }
	case JX9_VM_CONFIG_EXTRACT_OUTPUT: {
		/* Point to the VM internal output consumer buffer */
		const void **ppOut = va_arg(ap, const void **);
		unsigned int *pLen = va_arg(ap, unsigned int *);
#ifdef UNTRUST
		if( ppOut == 0 || pLen == 0 ){
			rc = SXERR_CORRUPT;
			break;
		}
#endif
		*ppOut = SyBlobData(&pVm->sConsumer);
		*pLen  = SyBlobLength(&pVm->sConsumer);
		break;
									   }
	case JX9_VM_CONFIG_HTTP_REQUEST:{
		/* Raw HTTP request*/
		const char *zRequest = va_arg(ap, const char *);
		int nByte = va_arg(ap, int);
		if( SX_EMPTY_STR(zRequest) ){
			rc = SXERR_EMPTY;
			break;
		}
		if( nByte < 0 ){
			/* Compute length automatically */
			nByte = (int)SyStrlen(zRequest);
		}
		/* Process the request */
		rc = VmHttpProcessRequest(&(*pVm), zRequest, nByte);
		break;
									}
	default:
		/* Unknown configuration option */
		rc = SXERR_UNKNOWN;
		break;
	}
	return rc;
}
/* Forward declaration */
static const char * VmInstrToString(sxi32 nOp);
/*
 * This routine is used to dump JX9 bytecode instructions to a human readable
 * format.
 * The dump is redirected to the given consumer callback which is responsible
 * of consuming the generated dump perhaps redirecting it to its standard output
 * (STDOUT).
 */
static sxi32 VmByteCodeDump(
	SySet *pByteCode,       /* Bytecode container */
	ProcConsumer xConsumer, /* Dump consumer callback */
	void *pUserData         /* Last argument to xConsumer() */
	)
{
	static const char zDump[] = {
		"====================================================\n"
		"JX9 VM Dump   Copyright (C) 2012-2013 Symisc Systems\n"
		"                              http://jx9.symisc.net/\n"
		"====================================================\n"
	};
	VmInstr *pInstr, *pEnd;
	sxi32 rc = SXRET_OK;
	sxu32 n;
	/* Point to the JX9 instructions */
	pInstr = (VmInstr *)SySetBasePtr(pByteCode);
	pEnd   = &pInstr[SySetUsed(pByteCode)];
	n = 0;
	xConsumer((const void *)zDump, sizeof(zDump)-1, pUserData);
	/* Dump instructions */
	for(;;){
		if( pInstr >= pEnd ){
			/* No more instructions */
			break;
		}
		/* Format and call the consumer callback */
		rc = SyProcFormat(xConsumer, pUserData, "%s %8d %8u %#8x [%u]\n", 
			VmInstrToString(pInstr->iOp), pInstr->iP1, pInstr->iP2, 
			SX_PTR_TO_INT(pInstr->p3), n);
		if( rc != SXRET_OK ){
			/* Consumer routine request an operation abort */
			return rc;
		}
		++n;
		pInstr++; /* Next instruction in the stream */
	}
	return rc;
}
/*
 * Consume a generated run-time error message by invoking the VM output
 * consumer callback.
 */
static sxi32 VmCallErrorHandler(jx9_vm *pVm, SyBlob *pMsg)
{
	jx9_output_consumer *pCons = &pVm->sVmConsumer;
	sxi32 rc = SXRET_OK;
	/* Append a new line */
#ifdef __WINNT__
	SyBlobAppend(pMsg, "\r\n", sizeof("\r\n")-1);
#else
	SyBlobAppend(pMsg, "\n", sizeof(char));
#endif
	/* Invoke the output consumer callback */
	rc = pCons->xConsumer(SyBlobData(pMsg), SyBlobLength(pMsg), pCons->pUserData);
	/* Increment output length */
	pVm->nOutputLen += SyBlobLength(pMsg);
	
	return rc;
}
/*
 * Throw a run-time error and invoke the supplied VM output consumer callback.
 * Refer to the implementation of [jx9_context_throw_error()] for additional
 * information.
 */
JX9_PRIVATE sxi32 jx9VmThrowError(
	jx9_vm *pVm,         /* Target VM */
	SyString *pFuncName, /* Function name. NULL otherwise */
	sxi32 iErr,          /* Severity level: [i.e: Error, Warning or Notice]*/
	const char *zMessage /* Null terminated error message */
	)
{
	SyBlob *pWorker = &pVm->sWorker;
	SyString *pFile;
	char *zErr;
	sxi32 rc;
	if( !pVm->bErrReport ){
		/* Don't bother reporting errors */
		return SXRET_OK;
	}
	/* Reset the working buffer */
	SyBlobReset(pWorker);
	/* Peek the processed file if available */
	pFile = (SyString *)SySetPeek(&pVm->aFiles);
	if( pFile ){
		/* Append file name */
		SyBlobAppend(pWorker, pFile->zString, pFile->nByte);
		SyBlobAppend(pWorker, (const void *)" ", sizeof(char));
	}
	zErr = "Error: ";
	switch(iErr){
	case JX9_CTX_WARNING: zErr = "Warning: "; break;
	case JX9_CTX_NOTICE:  zErr = "Notice: ";  break;
	default:
		iErr = JX9_CTX_ERR;
		break;
	}
	SyBlobAppend(pWorker, zErr, SyStrlen(zErr));
	if( pFuncName ){
		/* Append function name first */
		SyBlobAppend(pWorker, pFuncName->zString, pFuncName->nByte);
		SyBlobAppend(pWorker, "(): ", sizeof("(): ")-1);
	}
	SyBlobAppend(pWorker, zMessage, SyStrlen(zMessage));
	/* Consume the error message */
	rc = VmCallErrorHandler(&(*pVm), pWorker);
	return rc;
}
/*
 * Format and throw a run-time error and invoke the supplied VM output consumer callback.
 * Refer to the implementation of [jx9_context_throw_error_format()] for additional
 * information.
 */
static sxi32 VmThrowErrorAp(
	jx9_vm *pVm,         /* Target VM */
	SyString *pFuncName, /* Function name. NULL otherwise */
	sxi32 iErr,          /* Severity level: [i.e: Error, Warning or Notice] */
	const char *zFormat, /* Format message */
	va_list ap           /* Variable list of arguments */
	)
{
	SyBlob *pWorker = &pVm->sWorker;
	SyString *pFile;
	char *zErr;
	sxi32 rc;
	if( !pVm->bErrReport ){
		/* Don't bother reporting errors */
		return SXRET_OK;
	}
	/* Reset the working buffer */
	SyBlobReset(pWorker);
	/* Peek the processed file if available */
	pFile = (SyString *)SySetPeek(&pVm->aFiles);
	if( pFile ){
		/* Append file name */
		SyBlobAppend(pWorker, pFile->zString, pFile->nByte);
		SyBlobAppend(pWorker, (const void *)" ", sizeof(char));
	}
	zErr = "Error: ";
	switch(iErr){
	case JX9_CTX_WARNING: zErr = "Warning: "; break;
	case JX9_CTX_NOTICE:  zErr = "Notice: ";  break;
	default:
		iErr = JX9_CTX_ERR;
		break;
	}
	SyBlobAppend(pWorker, zErr, SyStrlen(zErr));
	if( pFuncName ){
		/* Append function name first */
		SyBlobAppend(pWorker, pFuncName->zString, pFuncName->nByte);
		SyBlobAppend(pWorker, "(): ", sizeof("(): ")-1);
	}
	SyBlobFormatAp(pWorker, zFormat, ap);
	/* Consume the error message */
	rc = VmCallErrorHandler(&(*pVm), pWorker);
	return rc;
}
/*
 * Format and throw a run-time error and invoke the supplied VM output consumer callback.
 * Refer to the implementation of [jx9_context_throw_error_format()] for additional
 * information.
 * ------------------------------------
 * Simple boring wrapper function.
 * ------------------------------------
 */
static sxi32 VmErrorFormat(jx9_vm *pVm, sxi32 iErr, const char *zFormat, ...)
{
	va_list ap;
	sxi32 rc;
	va_start(ap, zFormat);
	rc = VmThrowErrorAp(&(*pVm), 0, iErr, zFormat, ap);
	va_end(ap);
	return rc;
}
/*
 * Format and throw a run-time error and invoke the supplied VM output consumer callback.
 * Refer to the implementation of [jx9_context_throw_error_format()] for additional
 * information.
 * ------------------------------------
 * Simple boring wrapper function.
 * ------------------------------------
 */
JX9_PRIVATE sxi32 jx9VmThrowErrorAp(jx9_vm *pVm, SyString *pFuncName, sxi32 iErr, const char *zFormat, va_list ap)
{
	sxi32 rc;
	rc = VmThrowErrorAp(&(*pVm), &(*pFuncName), iErr, zFormat, ap);
	return rc;
}
/* Forward declaration */
static sxi32 VmLocalExec(jx9_vm *pVm,SySet *pByteCode,jx9_value *pResult);
/*
 * Execute as much of a JX9 bytecode program as we can then return.
 *
 * [jx9VmMakeReady()] must be called before this routine in order to
 * close the program with a final OP_DONE and to set up the default
 * consumer routines and other stuff. Refer to the implementation
 * of [jx9VmMakeReady()] for additional information.
 * If the installed VM output consumer callback ever returns JX9_ABORT
 * then the program execution is halted.
 * After this routine has finished, [jx9VmRelease()] or [jx9VmReset()]
 * should be used respectively to clean up the mess that was left behind
 * or to reset the VM to it's initial state.
 */
static sxi32 VmByteCodeExec(
	jx9_vm *pVm,         /* Target VM */
	VmInstr *aInstr,     /* JX9 bytecode program */
	jx9_value *pStack,   /* Operand stack */
	int nTos,            /* Top entry in the operand stack (usually -1) */
	jx9_value *pResult  /* Store program return value here. NULL otherwise */
	)
{
	VmInstr *pInstr;
	jx9_value *pTos;
	SySet aArg;
	sxi32 pc;
	sxi32 rc;
	/* Argument container */
	SySetInit(&aArg, &pVm->sAllocator, sizeof(jx9_value *));
	if( nTos < 0 ){
		pTos = &pStack[-1];
	}else{
		pTos = &pStack[nTos];
	}
	pc = 0;
	/* Execute as much as we can */
	for(;;){
		/* Fetch the instruction to execute */
		pInstr = &aInstr[pc];
		rc = SXRET_OK;
/*
 * What follows here is a massive switch statement where each case implements a
 * separate instruction in the virtual machine.  If we follow the usual
 * indentation convention each case should be indented by 6 spaces.  But
 * that is a lot of wasted space on the left margin.  So the code within
 * the switch statement will break with convention and be flush-left.
 */
		switch(pInstr->iOp){
/*
 * DONE: P1 * * 
 *
 * Program execution completed: Clean up the mess left behind
 * and return immediately.
 */
case JX9_OP_DONE:
	if( pInstr->iP1 ){
#ifdef UNTRUST
		if( pTos < pStack ){
			goto Abort;
		}
#endif
		if( pResult ){
			/* Execution result */
			jx9MemObjStore(pTos, pResult);
		}		
		VmPopOperand(&pTos, 1);
	}
	goto Done;
/*
 * HALT: P1 * *
 *
 * Program execution aborted: Clean up the mess left behind
 * and abort immediately.
 */
case JX9_OP_HALT:
	if( pInstr->iP1 ){
#ifdef UNTRUST
		if( pTos < pStack ){
			goto Abort;
		}
#endif
		if( pTos->iFlags & MEMOBJ_STRING ){
			if( SyBlobLength(&pTos->sBlob) > 0 ){
				/* Output the exit message */
				pVm->sVmConsumer.xConsumer(SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob), 
					pVm->sVmConsumer.pUserData);
					/* Increment output length */
					pVm->nOutputLen += SyBlobLength(&pTos->sBlob);
			}
		}else if(pTos->iFlags & MEMOBJ_INT ){
			/* Record exit status */
			pVm->iExitStatus = (sxi32)pTos->x.iVal;
		}
		VmPopOperand(&pTos, 1);
	}
	goto Abort;
/*
 * JMP: * P2 *
 *
 * Unconditional jump: The next instruction executed will be 
 * the one at index P2 from the beginning of the program.
 */
case JX9_OP_JMP:
	pc = pInstr->iP2 - 1;
	break;
/*
 * JZ: P1 P2 *
 *
 * Take the jump if the top value is zero (FALSE jump).Pop the top most
 * entry in the stack if P1 is zero. 
 */
case JX9_OP_JZ:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Get a boolean value */
	if((pTos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pTos);
	}
	if( !pTos->x.iVal ){
		/* Take the jump */
		pc = pInstr->iP2 - 1;
	}
	if( !pInstr->iP1 ){
		VmPopOperand(&pTos, 1);
	}
	break;
/*
 * JNZ: P1 P2 *
 *
 * Take the jump if the top value is not zero (TRUE jump).Pop the top most
 * entry in the stack if P1 is zero.
 */
case JX9_OP_JNZ:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Get a boolean value */
	if((pTos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pTos);
	}
	if( pTos->x.iVal ){
		/* Take the jump */
		pc = pInstr->iP2 - 1;
	}
	if( !pInstr->iP1 ){
		VmPopOperand(&pTos, 1);
	}
	break;
/*
 * NOOP: * * *
 *
 * Do nothing. This instruction is often useful as a jump
 * destination.
 */
case JX9_OP_NOOP:
	break;
/*
 * POP: P1 * *
 *
 * Pop P1 elements from the operand stack.
 */
case JX9_OP_POP: {
	sxi32 n = pInstr->iP1;
	if( &pTos[-n+1] < pStack ){
		/* TICKET 1433-51 Stack underflow must be handled at run-time */
		n = (sxi32)(pTos - pStack);
	}
	VmPopOperand(&pTos, n);
	break;
				 }
/*
 * CVT_INT: * * *
 *
 * Force the top of the stack to be an integer.
 */
case JX9_OP_CVT_INT:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if((pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	/* Invalidate any prior representation */
	MemObjSetType(pTos, MEMOBJ_INT);
	break;
/*
 * CVT_REAL: * * *
 *
 * Force the top of the stack to be a real.
 */
case JX9_OP_CVT_REAL:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if((pTos->iFlags & MEMOBJ_REAL) == 0 ){
		jx9MemObjToReal(pTos);
	}
	/* Invalidate any prior representation */
	MemObjSetType(pTos, MEMOBJ_REAL);
	break;
/*
 * CVT_STR: * * *
 *
 * Force the top of the stack to be a string.
 */
case JX9_OP_CVT_STR:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if( (pTos->iFlags & MEMOBJ_STRING) == 0 ){
		jx9MemObjToString(pTos);
	}
	break;
/*
 * CVT_BOOL: * * *
 *
 * Force the top of the stack to be a boolean.
 */
case JX9_OP_CVT_BOOL:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if( (pTos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pTos);
	}
	break;
/*
 * CVT_NULL: * * *
 *
 * Nullify the top of the stack.
 */
case JX9_OP_CVT_NULL:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	jx9MemObjRelease(pTos);
	break;
/*
 * CVT_NUMC: * * *
 *
 * Force the top of the stack to be a numeric type (integer, real or both).
 */
case JX9_OP_CVT_NUMC:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Force a numeric cast */
	jx9MemObjToNumeric(pTos);
	break;
/*
 * CVT_ARRAY: * * *
 *
 * Force the top of the stack to be a hashmap aka 'array'.
 */
case JX9_OP_CVT_ARRAY:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Force a hashmap cast */
	rc = jx9MemObjToHashmap(pTos);
	if( rc != SXRET_OK ){
		/* Not so fatal, emit a simple warning */
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_WARNING, 
			"JX9 engine is running out of memory while performing an array cast");
	}
	break;
/*
 * LOADC P1 P2 *
 *
 * Load a constant [i.e: JX9_EOL, JX9_OS, __TIME__, ...] indexed at P2 in the constant pool.
 * If P1 is set, then this constant is candidate for expansion via user installable callbacks.
 */
case JX9_OP_LOADC: {
	jx9_value *pObj;
	/* Reserve a room */
	pTos++;
	if( (pObj = (jx9_value *)SySetAt(&pVm->aLitObj, pInstr->iP2)) != 0 ){
		if( pInstr->iP1 == 1 && SyBlobLength(&pObj->sBlob) <= 64 ){
			SyHashEntry *pEntry;
			/* Candidate for expansion via user defined callbacks */
			pEntry = SyHashGet(&pVm->hConstant, SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob));
			if( pEntry ){
				jx9_constant *pCons = (jx9_constant *)pEntry->pUserData;
				/* Set a NULL default value */
				MemObjSetType(pTos, MEMOBJ_NULL);
				SyBlobReset(&pTos->sBlob);
				/* Invoke the callback and deal with the expanded value */
				pCons->xExpand(pTos, pCons->pUserData);
				/* Mark as constant */
				pTos->nIdx = SXU32_HIGH;
				break;
			}
		}
		jx9MemObjLoad(pObj, pTos);
	}else{
		/* Set a NULL value */
		MemObjSetType(pTos, MEMOBJ_NULL);
	}
	/* Mark as constant */
	pTos->nIdx = SXU32_HIGH;
	break;
				  }
/*
 * LOAD: P1 * P3
 *
 * Load a variable where it's name is taken from the top of the stack or
 * from the P3 operand.
 * If P1 is set, then perform a lookup only.In other words do not create
 * the variable if non existent and push the NULL constant instead.
 */
case JX9_OP_LOAD:{
	jx9_value *pObj;
	SyString sName;
	if( pInstr->p3 == 0 ){
		/* Take the variable name from the top of the stack */
#ifdef UNTRUST
		if( pTos < pStack ){
			goto Abort;
		}
#endif
		/* Force a string cast */
		if( (pTos->iFlags & MEMOBJ_STRING) == 0 ){
			jx9MemObjToString(pTos);
		}
		SyStringInitFromBuf(&sName, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob));
	}else{
		SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3));
		/* Reserve a room for the target object */
		pTos++;
	}
	/* Extract the requested memory object */
	pObj = VmExtractMemObj(&(*pVm), &sName, pInstr->p3 ? FALSE : TRUE, pInstr->iP1 != 1);
	if( pObj == 0 ){
		if( pInstr->iP1 ){
			/* Variable not found, load NULL */
			if( !pInstr->p3 ){
				jx9MemObjRelease(pTos);
			}else{
				MemObjSetType(pTos, MEMOBJ_NULL);
			}
			pTos->nIdx = SXU32_HIGH; /* Mark as constant */
			break;
		}else{
			/* Fatal error */
			VmErrorFormat(&(*pVm), JX9_CTX_ERR, "Fatal, JX9 engine is running out of memory while loading variable '%z'", &sName);
			goto Abort;
		}
	}
	/* Load variable contents */
	jx9MemObjLoad(pObj, pTos);
	pTos->nIdx = pObj->nIdx;
	break;
				   }
/*
 * LOAD_MAP P1 * *
 *
 * Allocate a new empty hashmap (array in the JX9 jargon) and push it on the stack.
 * If the P1 operand is greater than zero then pop P1 elements from the
 * stack and insert them (key => value pair) in the new hashmap.
 */
case JX9_OP_LOAD_MAP: {
	jx9_hashmap *pMap;
	int is_json_object; /* TRUE if we are dealing with a JSON object */
	int iIncr = 1;
	/* Allocate a new hashmap instance */
	pMap = jx9NewHashmap(&(*pVm), 0, 0);
	if( pMap == 0 ){
		VmErrorFormat(&(*pVm), JX9_CTX_ERR, 
			"Fatal, JX9 engine is running out of memory while loading JSON array/object at instruction #:%d", pc);
		goto Abort;
	}
	is_json_object = 0;
	if( pInstr->iP2 ){
		/* JSON object, record that */
		pMap->iFlags |= HASHMAP_JSON_OBJECT;
		is_json_object = 1;
		iIncr = 2;
	}
	if( pInstr->iP1 > 0 ){
		jx9_value *pEntry = &pTos[-pInstr->iP1+1]; /* Point to the first entry */
		/* Perform the insertion */
		while( pEntry <= pTos ){
			/* Standard insertion */
			jx9HashmapInsert(pMap, 
				is_json_object ? pEntry : 0 /* Automatic index assign */,
				is_json_object ? &pEntry[1] : pEntry
			);			
			/* Next pair on the stack */
			pEntry += iIncr;
		}
		/* Pop P1 elements */
		VmPopOperand(&pTos, pInstr->iP1);
	}
	/* Push the hashmap */
	pTos++;
	pTos->x.pOther = pMap;
	MemObjSetType(pTos, MEMOBJ_HASHMAP);
	break;
					  }
/*
 * LOAD_IDX: P1 P2 *
 *
 * Load a hasmap entry where it's index (either numeric or string) is taken
 * from the stack.
 * If the index does not refer to a valid element, then push the NULL constant
 * instead.
 */
case JX9_OP_LOAD_IDX: {
	jx9_hashmap_node *pNode = 0; /* cc warning */
	jx9_hashmap *pMap = 0;
	jx9_value *pIdx;
	pIdx = 0;
	if( pInstr->iP1 == 0 ){
		if( !pInstr->iP2){
			/* No available index, load NULL */
			if( pTos >= pStack ){
				jx9MemObjRelease(pTos);
			}else{
				/* TICKET 1433-020: Empty stack */
				pTos++;
				MemObjSetType(pTos, MEMOBJ_NULL);
				pTos->nIdx = SXU32_HIGH;
			}
			/* Emit a notice */
			jx9VmThrowError(&(*pVm), 0, JX9_CTX_NOTICE, 
				"JSON Array/Object: Attempt to access an undefined member, JX9 is loading NULL");
			break;
		}
	}else{
		pIdx = pTos;
		pTos--;
	}
	if( pTos->iFlags & MEMOBJ_STRING ){
		/* String access */
		if( pIdx ){
			sxu32 nOfft;
			if( (pIdx->iFlags & MEMOBJ_INT) == 0 ){
				/* Force an int cast */
				jx9MemObjToInteger(pIdx);
			}
			nOfft = (sxu32)pIdx->x.iVal;
			if( nOfft >= SyBlobLength(&pTos->sBlob) ){
				/* Invalid offset, load null */
				jx9MemObjRelease(pTos);
			}else{
				const char *zData = (const char *)SyBlobData(&pTos->sBlob);
				int c = zData[nOfft];
				jx9MemObjRelease(pTos);
				MemObjSetType(pTos, MEMOBJ_STRING);
				SyBlobAppend(&pTos->sBlob, (const void *)&c, sizeof(char));
			}
		}else{
			/* No available index, load NULL */
			MemObjSetType(pTos, MEMOBJ_NULL);
		}
		break;
	}
	if( pInstr->iP2 && (pTos->iFlags & MEMOBJ_HASHMAP) == 0 ){
		if( pTos->nIdx != SXU32_HIGH ){
			jx9_value *pObj;
			if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
				jx9MemObjToHashmap(pObj);
				jx9MemObjLoad(pObj, pTos);
			}
		}
	}
	rc = SXERR_NOTFOUND; /* Assume the index is invalid */
	if( pTos->iFlags & MEMOBJ_HASHMAP ){
		/* Point to the hashmap */
		pMap = (jx9_hashmap *)pTos->x.pOther;
		if( pIdx ){
			/* Load the desired entry */
			rc = jx9HashmapLookup(pMap, pIdx, &pNode);
		}
		if( rc != SXRET_OK && pInstr->iP2 ){
			/* Create a new empty entry */
			rc = jx9HashmapInsert(pMap, pIdx, 0);
			if( rc == SXRET_OK ){
				/* Point to the last inserted entry */
				pNode = pMap->pLast;
			}
		}
	}
	if( pIdx ){
		jx9MemObjRelease(pIdx);
	}
	if( rc == SXRET_OK ){
		/* Load entry contents */
		if( pMap->iRef < 2 ){
			/* TICKET 1433-42: Array will be deleted shortly, so we will make a copy
			 * of the entry value, rather than pointing to it.
			 */
			pTos->nIdx = SXU32_HIGH;
			jx9HashmapExtractNodeValue(pNode, pTos, TRUE);
		}else{
			pTos->nIdx = pNode->nValIdx;
			jx9HashmapExtractNodeValue(pNode, pTos, FALSE);
			jx9HashmapUnref(pMap);
		}
	}else{
		/* No such entry, load NULL */
		jx9MemObjRelease(pTos);
		pTos->nIdx = SXU32_HIGH;
	}
	break;
					  }
/*
 * STORE * P2 P3
 *
 * Perform a store (Assignment) operation.
 */
case JX9_OP_STORE: {
	jx9_value *pObj;
	SyString sName;
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if( pInstr->iP2 ){
		sxu32 nIdx;
		/* Member store operation */
		nIdx = pTos->nIdx;
		VmPopOperand(&pTos, 1);
		if( nIdx == SXU32_HIGH ){
			jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, 
				"Cannot perform assignment on a constant object attribute, JX9 is loading NULL");
			pTos->nIdx = SXU32_HIGH;
		}else{
			/* Point to the desired memory object */
			pObj = (jx9_value *)SySetAt(&pVm->aMemObj, nIdx);
			if( pObj ){
				/* Perform the store operation */
				jx9MemObjStore(pTos, pObj);
			}
		}
		break;
	}else if( pInstr->p3 == 0 ){
		/* Take the variable name from the next on the stack */
		if( (pTos->iFlags & MEMOBJ_STRING) == 0 ){
			/* Force a string cast */
			jx9MemObjToString(pTos);
		}
		SyStringInitFromBuf(&sName, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob));
		pTos--;
#ifdef UNTRUST
		if( pTos < pStack  ){
			goto Abort;
		}
#endif
	}else{
		SyStringInitFromBuf(&sName, pInstr->p3, SyStrlen((const char *)pInstr->p3));
	}
	/* Extract the desired variable and if not available dynamically create it */
	pObj = VmExtractMemObj(&(*pVm), &sName, pInstr->p3 ? FALSE : TRUE, TRUE);
	if( pObj == 0 ){
		VmErrorFormat(&(*pVm), JX9_CTX_ERR, 
			"Fatal, JX9 engine is running out of memory while loading variable '%z'", &sName);
		goto Abort;
	}
	if( !pInstr->p3 ){
		jx9MemObjRelease(&pTos[1]);
	}
	/* Perform the store operation */
	jx9MemObjStore(pTos, pObj);
	break;
				   }
/*
 * STORE_IDX:   P1 * P3
 *
 * Perfrom a store operation an a hashmap entry.
 */
case JX9_OP_STORE_IDX: {
	jx9_hashmap *pMap = 0; /* cc  warning */
	jx9_value *pKey;
	sxu32 nIdx;
	if( pInstr->iP1 ){
		/* Key is next on stack */
		pKey = pTos;
		pTos--;
	}else{
		pKey = 0;
	}
	nIdx = pTos->nIdx;
	if( pTos->iFlags & MEMOBJ_HASHMAP ){
		/* Hashmap already loaded */
		pMap = (jx9_hashmap *)pTos->x.pOther;
		if( pMap->iRef < 2 ){
			/* TICKET 1433-48: Prevent garbage collection */
			pMap->iRef = 2;
		}
	}else{
		jx9_value *pObj;
		pObj = (jx9_value *)SySetAt(&pVm->aMemObj, nIdx);
		if( pObj == 0 ){
			if( pKey ){
			  jx9MemObjRelease(pKey);
			}
			VmPopOperand(&pTos, 1);
			break;
		}
		/* Phase#1: Load the array */
		if( (pObj->iFlags & MEMOBJ_STRING)  ){
			VmPopOperand(&pTos, 1);
			if( (pTos->iFlags&MEMOBJ_STRING) == 0 ){
				/* Force a string cast */
				jx9MemObjToString(pTos);
			}
			if( pKey == 0 ){
				/* Append string */
				if( SyBlobLength(&pTos->sBlob) > 0 ){
					SyBlobAppend(&pObj->sBlob, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob));
				}
			}else{
				sxu32 nOfft;
				if((pKey->iFlags & MEMOBJ_INT)){
					/* Force an int cast */
					jx9MemObjToInteger(pKey);
				}
				nOfft = (sxu32)pKey->x.iVal;
				if( nOfft < SyBlobLength(&pObj->sBlob) && SyBlobLength(&pTos->sBlob) > 0 ){
					const char *zBlob = (const char *)SyBlobData(&pTos->sBlob);
					char *zData = (char *)SyBlobData(&pObj->sBlob);
					zData[nOfft] = zBlob[0];
				}else{
					if( SyBlobLength(&pTos->sBlob) >= sizeof(char) ){
						/* Perform an append operation */
						SyBlobAppend(&pObj->sBlob, SyBlobData(&pTos->sBlob), sizeof(char));
					}
				}
			}
			if( pKey ){
			  jx9MemObjRelease(pKey);
			}
			break;
		}else if( (pObj->iFlags & MEMOBJ_HASHMAP) == 0 ){
			/* Force a hashmap cast  */
			rc = jx9MemObjToHashmap(pObj);
			if( rc != SXRET_OK ){
				VmErrorFormat(&(*pVm), JX9_CTX_ERR, "Fatal, JX9 engine is running out of memory while creating a new array");
				goto Abort;
			}
		}
		pMap = (jx9_hashmap *)pObj->x.pOther;
	}
	VmPopOperand(&pTos, 1);
	/* Phase#2: Perform the insertion */
	jx9HashmapInsert(pMap, pKey, pTos);	
	if( pKey ){
		jx9MemObjRelease(pKey);
	}
	break;
					   }
/*
 * INCR: P1 * *
 *
 * Force a numeric cast and increment the top of the stack by 1.
 * If the P1 operand is set then perform a duplication of the top of
 * the stack and increment after that.
 */
case JX9_OP_INCR:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if( (pTos->iFlags & (MEMOBJ_HASHMAP|MEMOBJ_RES)) == 0 ){
		if( pTos->nIdx != SXU32_HIGH ){
			jx9_value *pObj;
			if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
				/* Force a numeric cast */
				jx9MemObjToNumeric(pObj);
				if( pObj->iFlags & MEMOBJ_REAL ){
					pObj->x.rVal++;
					/* Try to get an integer representation */
					jx9MemObjTryInteger(pTos);
				}else{
					pObj->x.iVal++;
					MemObjSetType(pTos, MEMOBJ_INT);
				}
				if( pInstr->iP1 ){
					/* Pre-icrement */
					jx9MemObjStore(pObj, pTos);
				}
			}
		}else{
			if( pInstr->iP1 ){
				/* Force a numeric cast */
				jx9MemObjToNumeric(pTos);
				/* Pre-increment */
				if( pTos->iFlags & MEMOBJ_REAL ){
					pTos->x.rVal++;
					/* Try to get an integer representation */
					jx9MemObjTryInteger(pTos);
				}else{
					pTos->x.iVal++;
					MemObjSetType(pTos, MEMOBJ_INT);
				}
			}
		}
	}
	break;
/*
 * DECR: P1 * *
 *
 * Force a numeric cast and decrement the top of the stack by 1.
 * If the P1 operand is set then perform a duplication of the top of the stack 
 * and decrement after that.
 */
case JX9_OP_DECR:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if( (pTos->iFlags & (MEMOBJ_HASHMAP|MEMOBJ_RES|MEMOBJ_NULL)) == 0 ){
		/* Force a numeric cast */
		jx9MemObjToNumeric(pTos);
		if( pTos->nIdx != SXU32_HIGH ){
			jx9_value *pObj;
			if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
				/* Force a numeric cast */
				jx9MemObjToNumeric(pObj);
				if( pObj->iFlags & MEMOBJ_REAL ){
					pObj->x.rVal--;
					/* Try to get an integer representation */
					jx9MemObjTryInteger(pTos);
				}else{
					pObj->x.iVal--;
					MemObjSetType(pTos, MEMOBJ_INT);
				}
				if( pInstr->iP1 ){
					/* Pre-icrement */
					jx9MemObjStore(pObj, pTos);
				}
			}
		}else{
			if( pInstr->iP1 ){
				/* Pre-increment */
				if( pTos->iFlags & MEMOBJ_REAL ){
					pTos->x.rVal--;
					/* Try to get an integer representation */
					jx9MemObjTryInteger(pTos);
				}else{
					pTos->x.iVal--;
					MemObjSetType(pTos, MEMOBJ_INT);
				}
			}
		}
	}
	break;
/*
 * UMINUS: * * *
 *
 * Perform a unary minus operation.
 */
case JX9_OP_UMINUS:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Force a numeric (integer, real or both) cast */
	jx9MemObjToNumeric(pTos);
	if( pTos->iFlags & MEMOBJ_REAL ){
		pTos->x.rVal = -pTos->x.rVal;
	}
	if( pTos->iFlags & MEMOBJ_INT ){
		pTos->x.iVal = -pTos->x.iVal;
	}
	break;				   
/*
 * UPLUS: * * *
 *
 * Perform a unary plus operation.
 */
case JX9_OP_UPLUS:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Force a numeric (integer, real or both) cast */
	jx9MemObjToNumeric(pTos);
	if( pTos->iFlags & MEMOBJ_REAL ){
		pTos->x.rVal = +pTos->x.rVal;
	}
	if( pTos->iFlags & MEMOBJ_INT ){
		pTos->x.iVal = +pTos->x.iVal;
	}
	break;
/*
 * OP_LNOT: * * *
 *
 * Interpret the top of the stack as a boolean value.  Replace it
 * with its complement.
 */
case JX9_OP_LNOT:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Force a boolean cast */
	if( (pTos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pTos);
	}
	pTos->x.iVal = !pTos->x.iVal;
	break;
/*
 * OP_BITNOT: * * *
 *
 * Interpret the top of the stack as an value.Replace it
 * with its ones-complement.
 */
case JX9_OP_BITNOT:
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	/* Force an integer cast */
	if( (pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	pTos->x.iVal = ~pTos->x.iVal;
	break;
/* OP_MUL * * *
 * OP_MUL_STORE * * *
 *
 * Pop the top two elements from the stack, multiply them together, 
 * and push the result back onto the stack.
 */
case JX9_OP_MUL:
case JX9_OP_MUL_STORE: {
	jx9_value *pNos = &pTos[-1];
	/* Force the operand to be numeric */
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	jx9MemObjToNumeric(pTos);
	jx9MemObjToNumeric(pNos);
	/* Perform the requested operation */
	if( MEMOBJ_REAL & (pTos->iFlags|pNos->iFlags) ){
		/* Floating point arithemic */
		jx9_real a, b, r;
		if( (pTos->iFlags & MEMOBJ_REAL) == 0 ){
			jx9MemObjToReal(pTos);
		}
		if( (pNos->iFlags & MEMOBJ_REAL) == 0 ){
			jx9MemObjToReal(pNos);
		}
		a = pNos->x.rVal;
		b = pTos->x.rVal;
		r = a * b;
		/* Push the result */
		pNos->x.rVal = r;
		MemObjSetType(pNos, MEMOBJ_REAL);
		/* Try to get an integer representation */
		jx9MemObjTryInteger(pNos);
	}else{
		/* Integer arithmetic */
		sxi64 a, b, r;
		a = pNos->x.iVal;
		b = pTos->x.iVal;
		r = a * b;
		/* Push the result */
		pNos->x.iVal = r;
		MemObjSetType(pNos, MEMOBJ_INT);
	}
	if( pInstr->iOp == JX9_OP_MUL_STORE ){
		jx9_value *pObj;
		if( pTos->nIdx == SXU32_HIGH ){
			jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
		}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
			jx9MemObjStore(pNos, pObj);
		}
	}
	VmPopOperand(&pTos, 1);
	break;
				 }
/* OP_ADD * * *
 *
 * Pop the top two elements from the stack, add them together, 
 * and push the result back onto the stack.
 */
case JX9_OP_ADD:{
	jx9_value *pNos = &pTos[-1];
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Perform the addition */
	jx9MemObjAdd(pNos, pTos, FALSE);
	VmPopOperand(&pTos, 1);
	break;
				}
/*
 * OP_ADD_STORE * * *
 *
 * Pop the top two elements from the stack, add them together, 
 * and push the result back onto the stack.
 */
case JX9_OP_ADD_STORE:{
	jx9_value *pNos = &pTos[-1];
	jx9_value *pObj;
	sxu32 nIdx;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Perform the addition */
	nIdx = pTos->nIdx;
	jx9MemObjAdd(pTos, pNos, TRUE);
	/* Peform the store operation */
	if( nIdx == SXU32_HIGH ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
	}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, nIdx)) != 0 ){
		jx9MemObjStore(pTos, pObj);
	}
	/* Ticket 1433-35: Perform a stack dup */
	jx9MemObjStore(pTos, pNos);
	VmPopOperand(&pTos, 1);
	break;
				}
/* OP_SUB * * *
 *
 * Pop the top two elements from the stack, subtract the
 * first (what was next on the stack) from the second (the
 * top of the stack) and push the result back onto the stack.
 */
case JX9_OP_SUB: {
	jx9_value *pNos = &pTos[-1];
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	if( MEMOBJ_REAL & (pTos->iFlags|pNos->iFlags) ){
		/* Floating point arithemic */
		jx9_real a, b, r;
		if( (pTos->iFlags & MEMOBJ_REAL) == 0 ){
			jx9MemObjToReal(pTos);
		}
		if( (pNos->iFlags & MEMOBJ_REAL) == 0 ){
			jx9MemObjToReal(pNos);
		}
		a = pNos->x.rVal;
		b = pTos->x.rVal;
		r = a - b; 
		/* Push the result */
		pNos->x.rVal = r;
		MemObjSetType(pNos, MEMOBJ_REAL);
		/* Try to get an integer representation */
		jx9MemObjTryInteger(pNos);
	}else{
		/* Integer arithmetic */
		sxi64 a, b, r;
		a = pNos->x.iVal;
		b = pTos->x.iVal;
		r = a - b;
		/* Push the result */
		pNos->x.iVal = r;
		MemObjSetType(pNos, MEMOBJ_INT);
	}
	VmPopOperand(&pTos, 1);
	break;
				 }
/* OP_SUB_STORE * * *
 *
 * Pop the top two elements from the stack, subtract the
 * first (what was next on the stack) from the second (the
 * top of the stack) and push the result back onto the stack.
 */
case JX9_OP_SUB_STORE: {
	jx9_value *pNos = &pTos[-1];
	jx9_value *pObj;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	if( MEMOBJ_REAL & (pTos->iFlags|pNos->iFlags) ){
		/* Floating point arithemic */
		jx9_real a, b, r;
		if( (pTos->iFlags & MEMOBJ_REAL) == 0 ){
			jx9MemObjToReal(pTos);
		}
		if( (pNos->iFlags & MEMOBJ_REAL) == 0 ){
			jx9MemObjToReal(pNos);
		}
		a = pTos->x.rVal;
		b = pNos->x.rVal;
		r = a - b; 
		/* Push the result */
		pNos->x.rVal = r;
		MemObjSetType(pNos, MEMOBJ_REAL);
		/* Try to get an integer representation */
		jx9MemObjTryInteger(pNos);
	}else{
		/* Integer arithmetic */
		sxi64 a, b, r;
		a = pTos->x.iVal;
		b = pNos->x.iVal;
		r = a - b;
		/* Push the result */
		pNos->x.iVal = r;
		MemObjSetType(pNos, MEMOBJ_INT);
	}
	if( pTos->nIdx == SXU32_HIGH ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
	}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
		jx9MemObjStore(pNos, pObj);
	}
	VmPopOperand(&pTos, 1);
	break;
				 }

/*
 * OP_MOD * * *
 *
 * Pop the top two elements from the stack, divide the
 * first (what was next on the stack) from the second (the
 * top of the stack) and push the remainder after division 
 * onto the stack.
 * Note: Only integer arithemtic is allowed.
 */
case JX9_OP_MOD:{
	jx9_value *pNos = &pTos[-1];
	sxi64 a, b, r;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be integer */
	if( (pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pNos);
	}
	/* Perform the requested operation */
	a = pNos->x.iVal;
	b = pTos->x.iVal;
	if( b == 0 ){
		r = 0;
		VmErrorFormat(&(*pVm), JX9_CTX_ERR, "Division by zero %qd%%0", a);
		/* goto Abort; */
	}else{
		r = a%b;
	}
	/* Push the result */
	pNos->x.iVal = r;
	MemObjSetType(pNos, MEMOBJ_INT);
	VmPopOperand(&pTos, 1);
	break;
				}
/*
 * OP_MOD_STORE * * *
 *
 * Pop the top two elements from the stack, divide the
 * first (what was next on the stack) from the second (the
 * top of the stack) and push the remainder after division 
 * onto the stack.
 * Note: Only integer arithemtic is allowed.
 */
case JX9_OP_MOD_STORE: {
	jx9_value *pNos = &pTos[-1];
	jx9_value *pObj;
	sxi64 a, b, r;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be integer */
	if( (pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pNos);
	}
	/* Perform the requested operation */
	a = pTos->x.iVal;
	b = pNos->x.iVal;
	if( b == 0 ){
		r = 0;
		VmErrorFormat(&(*pVm), JX9_CTX_ERR, "Division by zero %qd%%0", a);
		/* goto Abort; */
	}else{
		r = a%b;
	}
	/* Push the result */
	pNos->x.iVal = r;
	MemObjSetType(pNos, MEMOBJ_INT);
	if( pTos->nIdx == SXU32_HIGH ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
	}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
		jx9MemObjStore(pNos, pObj);
	}
	VmPopOperand(&pTos, 1);
	break;
				}
/*
 * OP_DIV * * *
 * 
 * Pop the top two elements from the stack, divide the
 * first (what was next on the stack) from the second (the
 * top of the stack) and push the result onto the stack.
 * Note: Only floating point arithemtic is allowed.
 */
case JX9_OP_DIV:{
	jx9_value *pNos = &pTos[-1];
	jx9_real a, b, r;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be real */
	if( (pTos->iFlags & MEMOBJ_REAL) == 0 ){
		jx9MemObjToReal(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_REAL) == 0 ){
		jx9MemObjToReal(pNos);
	}
	/* Perform the requested operation */
	a = pNos->x.rVal;
	b = pTos->x.rVal;
	if( b == 0 ){
		/* Division by zero */
		r = 0;
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Division by zero");
		/* goto Abort; */
	}else{
		r = a/b;
		/* Push the result */
		pNos->x.rVal = r;
		MemObjSetType(pNos, MEMOBJ_REAL);
		/* Try to get an integer representation */
		jx9MemObjTryInteger(pNos);
	}
	VmPopOperand(&pTos, 1);
	break;
				}
/*
 * OP_DIV_STORE * * *
 * 
 * Pop the top two elements from the stack, divide the
 * first (what was next on the stack) from the second (the
 * top of the stack) and push the result onto the stack.
 * Note: Only floating point arithemtic is allowed.
 */
case JX9_OP_DIV_STORE:{
	jx9_value *pNos = &pTos[-1];
	jx9_value *pObj;
	jx9_real a, b, r;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be real */
	if( (pTos->iFlags & MEMOBJ_REAL) == 0 ){
		jx9MemObjToReal(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_REAL) == 0 ){
		jx9MemObjToReal(pNos);
	}
	/* Perform the requested operation */
	a = pTos->x.rVal;
	b = pNos->x.rVal;
	if( b == 0 ){
		/* Division by zero */
		r = 0;
		VmErrorFormat(&(*pVm), JX9_CTX_ERR, "Division by zero %qd/0", a);
		/* goto Abort; */
	}else{
		r = a/b;
		/* Push the result */
		pNos->x.rVal = r;
		MemObjSetType(pNos, MEMOBJ_REAL);
		/* Try to get an integer representation */
		jx9MemObjTryInteger(pNos);
	}
	if( pTos->nIdx == SXU32_HIGH ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
	}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
		jx9MemObjStore(pNos, pObj);
	}
	VmPopOperand(&pTos, 1);
	break;
				}
/* OP_BAND * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the bit-wise AND of the
 * two elements.
*/
/* OP_BOR * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the bit-wise OR of the
 * two elements.
 */
/* OP_BXOR * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the bit-wise XOR of the
 * two elements.
 */
case JX9_OP_BAND:
case JX9_OP_BOR:
case JX9_OP_BXOR:{
	jx9_value *pNos = &pTos[-1];
	sxi64 a, b, r;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be integer */
	if( (pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pNos);
	}
	/* Perform the requested operation */
	a = pNos->x.iVal;
	b = pTos->x.iVal;
	switch(pInstr->iOp){
	case JX9_OP_BOR_STORE:
	case JX9_OP_BOR:  r = a|b; break;
	case JX9_OP_BXOR_STORE:
	case JX9_OP_BXOR: r = a^b; break;
	case JX9_OP_BAND_STORE:
	case JX9_OP_BAND:
	default:          r = a&b; break;
	}
	/* Push the result */
	pNos->x.iVal = r;
	MemObjSetType(pNos, MEMOBJ_INT);
	VmPopOperand(&pTos, 1);
	break;
				 }
/* OP_BAND_STORE * * * 
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the bit-wise AND of the
 * two elements.
*/
/* OP_BOR_STORE * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the bit-wise OR of the
 * two elements.
 */
/* OP_BXOR_STORE * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the bit-wise XOR of the
 * two elements.
 */
case JX9_OP_BAND_STORE:
case JX9_OP_BOR_STORE:
case JX9_OP_BXOR_STORE:{
	jx9_value *pNos = &pTos[-1];
	jx9_value *pObj;
	sxi64 a, b, r;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be integer */
	if( (pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pNos);
	}
	/* Perform the requested operation */
	a = pTos->x.iVal;
	b = pNos->x.iVal;
	switch(pInstr->iOp){
	case JX9_OP_BOR_STORE:
	case JX9_OP_BOR:  r = a|b; break;
	case JX9_OP_BXOR_STORE:
	case JX9_OP_BXOR: r = a^b; break;
	case JX9_OP_BAND_STORE:
	case JX9_OP_BAND:
	default:          r = a&b; break;
	}
	/* Push the result */
	pNos->x.iVal = r;
	MemObjSetType(pNos, MEMOBJ_INT);
	if( pTos->nIdx == SXU32_HIGH ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
	}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
		jx9MemObjStore(pNos, pObj);
	}
	VmPopOperand(&pTos, 1);
	break;
				 }
/* OP_SHL * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the second element shifted
 * left by N bits where N is the top element on the stack.
 * Note: Only integer arithmetic is allowed.
 */
/* OP_SHR * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the second element shifted
 * right by N bits where N is the top element on the stack.
 * Note: Only integer arithmetic is allowed.
 */
case JX9_OP_SHL:
case JX9_OP_SHR: {
	jx9_value *pNos = &pTos[-1];
	sxi64 a, r;
	sxi32 b;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be integer */
	if( (pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pNos);
	}
	/* Perform the requested operation */
	a = pNos->x.iVal;
	b = (sxi32)pTos->x.iVal;
	if( pInstr->iOp == JX9_OP_SHL ){
		r = a << b;
	}else{
		r = a >> b;
	}
	/* Push the result */
	pNos->x.iVal = r;
	MemObjSetType(pNos, MEMOBJ_INT);
	VmPopOperand(&pTos, 1);
	break;
				 }
/*  OP_SHL_STORE * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the second element shifted
 * left by N bits where N is the top element on the stack.
 * Note: Only integer arithmetic is allowed.
 */
/* OP_SHR_STORE * * *
 *
 * Pop the top two elements from the stack.  Convert both elements
 * to integers.  Push back onto the stack the second element shifted
 * right by N bits where N is the top element on the stack.
 * Note: Only integer arithmetic is allowed.
 */
case JX9_OP_SHL_STORE:
case JX9_OP_SHR_STORE: {
	jx9_value *pNos = &pTos[-1];
	jx9_value *pObj;
	sxi64 a, r;
	sxi32 b;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force the operands to be integer */
	if( (pTos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pTos);
	}
	if( (pNos->iFlags & MEMOBJ_INT) == 0 ){
		jx9MemObjToInteger(pNos);
	}
	/* Perform the requested operation */
	a = pTos->x.iVal;
	b = (sxi32)pNos->x.iVal;
	if( pInstr->iOp == JX9_OP_SHL_STORE ){
		r = a << b;
	}else{
		r = a >> b;
	}
	/* Push the result */
	pNos->x.iVal = r;
	MemObjSetType(pNos, MEMOBJ_INT);
	if( pTos->nIdx == SXU32_HIGH ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
	}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
		jx9MemObjStore(pNos, pObj);
	}
	VmPopOperand(&pTos, 1);
	break;
				 }
/* CAT:  P1 * *
 *
 * Pop P1 elements from the stack. Concatenate them togeher and push the result
 * back.
 */
case JX9_OP_CAT:{
	jx9_value *pNos, *pCur;
	if( pInstr->iP1 < 1 ){
		pNos = &pTos[-1];
	}else{
		pNos = &pTos[-pInstr->iP1+1];
	}
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force a string cast */
	if( (pNos->iFlags & MEMOBJ_STRING) == 0 ){
		jx9MemObjToString(pNos);
	}
	pCur = &pNos[1];
	while( pCur <= pTos ){
		if( (pCur->iFlags & MEMOBJ_STRING) == 0 ){
			jx9MemObjToString(pCur);
		}
		/* Perform the concatenation */
		if( SyBlobLength(&pCur->sBlob) > 0 ){
			jx9MemObjStringAppend(pNos, (const char *)SyBlobData(&pCur->sBlob), SyBlobLength(&pCur->sBlob));
		}
		SyBlobRelease(&pCur->sBlob);
		pCur++;
	}
	pTos = pNos;
	break;
				}
/*  CAT_STORE: * * *
 *
 * Pop two elements from the stack. Concatenate them togeher and push the result
 * back.
 */
case JX9_OP_CAT_STORE:{
	jx9_value *pNos = &pTos[-1];
	jx9_value *pObj;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	if((pTos->iFlags & MEMOBJ_STRING) == 0 ){
		/* Force a string cast */
		jx9MemObjToString(pTos);
	}
	if((pNos->iFlags & MEMOBJ_STRING) == 0 ){
		/* Force a string cast */
		jx9MemObjToString(pNos);
	}
	/* Perform the concatenation (Reverse order) */
	if( SyBlobLength(&pNos->sBlob) > 0 ){
		jx9MemObjStringAppend(pTos, (const char *)SyBlobData(&pNos->sBlob), SyBlobLength(&pNos->sBlob));
	}
	/* Perform the store operation */
	if( pTos->nIdx == SXU32_HIGH ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "Cannot perform assignment on a constant object attribute");
	}else if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pTos->nIdx)) != 0 ){
		jx9MemObjStore(pTos, pObj);
	}
	jx9MemObjStore(pTos, pNos);
	VmPopOperand(&pTos, 1);
	break;
				}
/* OP_AND: * * *
 *
 * Pop two values off the stack.  Take the logical AND of the
 * two values and push the resulting boolean value back onto the
 * stack. 
 */
/* OP_OR: * * *
 *
 * Pop two values off the stack.  Take the logical OR of the
 * two values and push the resulting boolean value back onto the
 * stack. 
 */
case JX9_OP_LAND:
case JX9_OP_LOR: {
	jx9_value *pNos = &pTos[-1];
	sxi32 v1, v2;    /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force a boolean cast */
	if((pTos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pTos);
	}
	if((pNos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pNos);
	}
	v1 = pNos->x.iVal == 0 ? 1 : 0;
	v2 = pTos->x.iVal == 0 ? 1 : 0;
	if( pInstr->iOp == JX9_OP_LAND ){
		static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
		v1 = and_logic[v1*3+v2];
	}else{
		static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
		v1 = or_logic[v1*3+v2];
	}
	if( v1 == 2 ){
		v1 = 1;
	}
	VmPopOperand(&pTos, 1);
	pTos->x.iVal = v1 == 0 ? 1 : 0;
	MemObjSetType(pTos, MEMOBJ_BOOL);
	break;
				 }
/* OP_LXOR: * * *
 *
 * Pop two values off the stack. Take the logical XOR of the
 * two values and push the resulting boolean value back onto the
 * stack.
 * According to the JX9 language reference manual:
 *  $a xor $b is evaluated to TRUE if either $a or $b is 
 *  TRUE, but not both.
 */
case JX9_OP_LXOR:{
	jx9_value *pNos = &pTos[-1];
	sxi32 v = 0;
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	/* Force a boolean cast */
	if((pTos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pTos);
	}
	if((pNos->iFlags & MEMOBJ_BOOL) == 0 ){
		jx9MemObjToBool(pNos);
	}
	if( (pNos->x.iVal && !pTos->x.iVal) || (pTos->x.iVal && !pNos->x.iVal) ){
		v = 1;
	}
	VmPopOperand(&pTos, 1);
	pTos->x.iVal = v;
	MemObjSetType(pTos, MEMOBJ_BOOL);
	break;
				 }
/* OP_EQ P1 P2 P3
 *
 * Pop the top two elements from the stack.  If they are equal, then
 * jump to instruction P2.  Otherwise, continue to the next instruction.
 * If P2 is zero, do not jump.  Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not. 
 */
/* OP_NEQ P1 P2 P3
 *
 * Pop the top two elements from the stack. If they are not equal, then
 * jump to instruction P2. Otherwise, continue to the next instruction.
 * If P2 is zero, do not jump.  Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not.
 */
case JX9_OP_EQ:
case JX9_OP_NEQ: {
	jx9_value *pNos = &pTos[-1];
	/* Perform the comparison and act accordingly */
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	rc = jx9MemObjCmp(pNos, pTos, FALSE, 0);
	if( pInstr->iOp == JX9_OP_EQ ){
		rc = rc == 0;
	}else{
		rc = rc != 0;
	}
	VmPopOperand(&pTos, 1);
	if( !pInstr->iP2 ){
		/* Push comparison result without taking the jump */
		jx9MemObjRelease(pTos);
		pTos->x.iVal = rc;
		/* Invalidate any prior representation */
		MemObjSetType(pTos, MEMOBJ_BOOL);
	}else{
		if( rc ){
			/* Jump to the desired location */
			pc = pInstr->iP2 - 1;
			VmPopOperand(&pTos, 1);
		}
	}
	break;
				 }
/* OP_TEQ P1 P2 *
 *
 * Pop the top two elements from the stack. If they have the same type and are equal
 * then jump to instruction P2. Otherwise, continue to the next instruction.
 * If P2 is zero, do not jump. Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not. 
 */
case JX9_OP_TEQ: {
	jx9_value *pNos = &pTos[-1];
	/* Perform the comparison and act accordingly */
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	rc = jx9MemObjCmp(pNos, pTos, TRUE, 0) == 0;
	VmPopOperand(&pTos, 1);
	if( !pInstr->iP2 ){
		/* Push comparison result without taking the jump */
		jx9MemObjRelease(pTos);
		pTos->x.iVal = rc;
		/* Invalidate any prior representation */
		MemObjSetType(pTos, MEMOBJ_BOOL);
	}else{
		if( rc ){
			/* Jump to the desired location */
			pc = pInstr->iP2 - 1;
			VmPopOperand(&pTos, 1);
		}
	}
	break;
				 }
/* OP_TNE P1 P2 *
 *
 * Pop the top two elements from the stack.If they are not equal an they are not 
 * of the same type, then jump to instruction P2. Otherwise, continue to the next 
 * instruction.
 * If P2 is zero, do not jump. Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not.
 * 
 */
case JX9_OP_TNE: {
	jx9_value *pNos = &pTos[-1];
	/* Perform the comparison and act accordingly */
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	rc = jx9MemObjCmp(pNos, pTos, TRUE, 0) != 0;
	VmPopOperand(&pTos, 1);
	if( !pInstr->iP2 ){
		/* Push comparison result without taking the jump */
		jx9MemObjRelease(pTos);
		pTos->x.iVal = rc;
		/* Invalidate any prior representation */
		MemObjSetType(pTos, MEMOBJ_BOOL);
	}else{
		if( rc ){
			/* Jump to the desired location */
			pc = pInstr->iP2 - 1;
			VmPopOperand(&pTos, 1);
		}
	}
	break;
				 }
/* OP_LT P1 P2 P3
 *
 * Pop the top two elements from the stack. If the second element (the top of stack)
 * is less than the first (next on stack), then jump to instruction P2.Otherwise
 * continue to the next instruction. In other words, jump if pNos<pTos.
 * If P2 is zero, do not jump.Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not.
 * 
 */
/* OP_LE P1 P2 P3
 *
 * Pop the top two elements from the stack. If the second element (the top of stack)
 * is less than or equal to the first (next on stack), then jump to instruction P2.
 * Otherwise continue to the next instruction. In other words, jump if pNos<pTos.
 * If P2 is zero, do not jump.Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not.
 * 
 */
case JX9_OP_LT:
case JX9_OP_LE: {
	jx9_value *pNos = &pTos[-1];
	/* Perform the comparison and act accordingly */
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	rc = jx9MemObjCmp(pNos, pTos, FALSE, 0);
	if( pInstr->iOp == JX9_OP_LE ){
		rc = rc < 1;
	}else{
		rc = rc < 0;
	}
	VmPopOperand(&pTos, 1);
	if( !pInstr->iP2 ){
		/* Push comparison result without taking the jump */
		jx9MemObjRelease(pTos);
		pTos->x.iVal = rc;
		/* Invalidate any prior representation */
		MemObjSetType(pTos, MEMOBJ_BOOL);
	}else{
		if( rc ){
			/* Jump to the desired location */
			pc = pInstr->iP2 - 1;
			VmPopOperand(&pTos, 1);
		}
	}
	break;
				}
/* OP_GT P1 P2 P3
 *
 * Pop the top two elements from the stack. If the second element (the top of stack)
 * is greater than the first (next on stack), then jump to instruction P2.Otherwise
 * continue to the next instruction. In other words, jump if pNos<pTos.
 * If P2 is zero, do not jump.Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not.
 * 
 */
/* OP_GE P1 P2 P3
 *
 * Pop the top two elements from the stack. If the second element (the top of stack)
 * is greater than or equal to the first (next on stack), then jump to instruction P2.
 * Otherwise continue to the next instruction. In other words, jump if pNos<pTos.
 * If P2 is zero, do not jump.Instead, push a boolean 1 (TRUE) onto the
 * stack if the jump would have been taken, or a 0 (FALSE) if not.
 * 
 */
case JX9_OP_GT:
case JX9_OP_GE: {
	jx9_value *pNos = &pTos[-1];
	/* Perform the comparison and act accordingly */
#ifdef UNTRUST
	if( pNos < pStack ){
		goto Abort;
	}
#endif
	rc = jx9MemObjCmp(pNos, pTos, FALSE, 0);
	if( pInstr->iOp == JX9_OP_GE ){
		rc = rc >= 0;
	}else{
		rc = rc > 0;
	}
	VmPopOperand(&pTos, 1);
	if( !pInstr->iP2 ){
		/* Push comparison result without taking the jump */
		jx9MemObjRelease(pTos);
		pTos->x.iVal = rc;
		/* Invalidate any prior representation */
		MemObjSetType(pTos, MEMOBJ_BOOL);
	}else{
		if( rc ){
			/* Jump to the desired location */
			pc = pInstr->iP2 - 1;
			VmPopOperand(&pTos, 1);
		}
	}
	break;
				}
/*
 * OP_FOREACH_INIT * P2 P3
 * Prepare a foreach step.
 */
case JX9_OP_FOREACH_INIT: {
	jx9_foreach_info *pInfo = (jx9_foreach_info *)pInstr->p3;
	void *pName;
#ifdef UNTRUST
	if( pTos < pStack ){
		goto Abort;
	}
#endif
	if( SyStringLength(&pInfo->sValue) < 1 ){
		/* Take the variable name from the top of the stack */
		if( (pTos->iFlags & MEMOBJ_STRING) == 0 ){
			/* Force a string cast */
			jx9MemObjToString(pTos);
		}
		/* Duplicate name */
		if( SyBlobLength(&pTos->sBlob) > 0 ){
			pName = SyMemBackendDup(&pVm->sAllocator, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob));
			SyStringInitFromBuf(&pInfo->sValue, pName, SyBlobLength(&pTos->sBlob));
		}
		VmPopOperand(&pTos, 1);
	}
	if( (pInfo->iFlags & JX9_4EACH_STEP_KEY) && SyStringLength(&pInfo->sKey) < 1 ){
		if( (pTos->iFlags & MEMOBJ_STRING) == 0 ){
			/* Force a string cast */
			jx9MemObjToString(pTos);
		}
		/* Duplicate name */
		if( SyBlobLength(&pTos->sBlob) > 0 ){
			pName = SyMemBackendDup(&pVm->sAllocator, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob));
			SyStringInitFromBuf(&pInfo->sKey, pName, SyBlobLength(&pTos->sBlob));
		}
		VmPopOperand(&pTos, 1);
	}
	/* Make sure we are dealing with a hashmap [i.e. JSON array or object ]*/
	if( (pTos->iFlags & (MEMOBJ_HASHMAP)) == 0 || SyStringLength(&pInfo->sValue) < 1 ){
		/* Jump out of the loop */
		if( (pTos->iFlags & MEMOBJ_NULL) == 0 ){
			jx9VmThrowError(&(*pVm), 0, JX9_CTX_WARNING,
				"Invalid argument supplied for the foreach statement, expecting JSON array or object instance");
		}
		pc = pInstr->iP2 - 1;
	}else{
		jx9_foreach_step *pStep;
		pStep = (jx9_foreach_step *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(jx9_foreach_step));
		if( pStep == 0 ){
			jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "JX9 is running out of memory while preparing the 'foreach' step");
			/* Jump out of the loop */
			pc = pInstr->iP2 - 1;
		}else{
			/* Zero the structure */
			SyZero(pStep, sizeof(jx9_foreach_step));
			/* Prepare the step */
			pStep->iFlags = pInfo->iFlags;
			if( pTos->iFlags & MEMOBJ_HASHMAP ){
				jx9_hashmap *pMap = (jx9_hashmap *)pTos->x.pOther;
				/* Reset the internal loop cursor */
				jx9HashmapResetLoopCursor(pMap);
				/* Mark the step */
				pStep->pMap = pMap;
				pMap->iRef++;
			}
		}
		if( SXRET_OK != SySetPut(&pInfo->aStep, (const void *)&pStep) ){
			jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, "JX9 is running out of memory while preparing the 'foreach' step");
			SyMemBackendPoolFree(&pVm->sAllocator, pStep);
			/* Jump out of the loop */
			pc = pInstr->iP2 - 1;
		}
	}
	VmPopOperand(&pTos, 1);
	break;
						  }
/*
 * OP_FOREACH_STEP * P2 P3
 * Perform a foreach step. Jump to P2 at the end of the step.
 */
case JX9_OP_FOREACH_STEP: {
	jx9_foreach_info *pInfo = (jx9_foreach_info *)pInstr->p3;
	jx9_foreach_step **apStep, *pStep;
	jx9_hashmap_node *pNode;
	jx9_hashmap *pMap;
	jx9_value *pValue;
	/* Peek the last step */
	apStep = (jx9_foreach_step **)SySetBasePtr(&pInfo->aStep);
	pStep = apStep[SySetUsed(&pInfo->aStep) - 1];
	pMap = pStep->pMap;
	/* Extract the current node value */
	pNode = jx9HashmapGetNextEntry(pMap);
	if( pNode == 0 ){
		/* No more entry to process */
		pc = pInstr->iP2 - 1; /* Jump to this destination */
		/* Automatically reset the loop cursor */
		jx9HashmapResetLoopCursor(pMap);
		/* Cleanup the mess left behind */
		SyMemBackendPoolFree(&pVm->sAllocator, pStep);
		SySetPop(&pInfo->aStep);
		jx9HashmapUnref(pMap);
	}else{
		if( (pStep->iFlags & JX9_4EACH_STEP_KEY) && SyStringLength(&pInfo->sKey) > 0 ){
			jx9_value *pKey = VmExtractMemObj(&(*pVm), &pInfo->sKey, FALSE, TRUE);
			if( pKey ){
				jx9HashmapExtractNodeKey(pNode, pKey);
			}
		}
		/* Make a copy of the entry value */
		pValue = VmExtractMemObj(&(*pVm), &pInfo->sValue, FALSE, TRUE);
		if( pValue ){
			jx9HashmapExtractNodeValue(pNode, pValue, TRUE);
		}
	}
	break;
						  }
/*
 * OP_MEMBER P1 P2
 * Load JSON object entry on the stack.
 */
case JX9_OP_MEMBER: {
	jx9_hashmap_node *pNode = 0; /* cc warning */
	jx9_hashmap *pMap = 0;
	jx9_value *pIdx;
	pIdx = pTos;
	pTos--;
	rc = SXERR_NOTFOUND; /* Assume the index is invalid */
	if( pTos->iFlags & MEMOBJ_HASHMAP ){
		/* Point to the hashmap */
		pMap = (jx9_hashmap *)pTos->x.pOther;
		/* Load the desired entry */
		rc = jx9HashmapLookup(pMap, pIdx, &pNode);
	}
	jx9MemObjRelease(pIdx);	
	if( rc == SXRET_OK ){
		/* Load entry contents */
		if( pMap->iRef < 2 ){
			/* TICKET 1433-42: Array will be deleted shortly, so we will make a copy
			 * of the entry value, rather than pointing to it.
			 */
			pTos->nIdx = SXU32_HIGH;
			jx9HashmapExtractNodeValue(pNode, pTos, TRUE);
		}else{
			pTos->nIdx = pNode->nValIdx;
			jx9HashmapExtractNodeValue(pNode, pTos, FALSE);
			jx9HashmapUnref(pMap);
		}
	}else{
		/* No such entry, load NULL */
		jx9MemObjRelease(pTos);
		pTos->nIdx = SXU32_HIGH;
	}
	break;
					}
/*
 * OP_SWITCH * * P3
 *  This is the bytecode implementation of the complex switch() JX9 construct.
 */
case JX9_OP_SWITCH: {
	jx9_switch *pSwitch = (jx9_switch *)pInstr->p3;
	jx9_case_expr *aCase, *pCase;
	jx9_value sValue, sCaseValue; 
	sxu32 n, nEntry;
#ifdef UNTRUST
	if( pSwitch == 0 || pTos < pStack ){
		goto Abort;
	}
#endif
	/* Point to the case table  */
	aCase = (jx9_case_expr *)SySetBasePtr(&pSwitch->aCaseExpr);
	nEntry = SySetUsed(&pSwitch->aCaseExpr);
	/* Select the appropriate case block to execute */
	jx9MemObjInit(pVm, &sValue);
	jx9MemObjInit(pVm, &sCaseValue);
	for( n = 0 ; n < nEntry ; ++n ){
		pCase = &aCase[n];
		jx9MemObjLoad(pTos, &sValue);
		/* Execute the case expression first */
		VmLocalExec(pVm,&pCase->aByteCode, &sCaseValue);
		/* Compare the two expression */
		rc = jx9MemObjCmp(&sValue, &sCaseValue, FALSE, 0);
		jx9MemObjRelease(&sValue);
		jx9MemObjRelease(&sCaseValue);
		if( rc == 0 ){
			/* Value match, jump to this block */
			pc = pCase->nStart - 1;
			break;
		}
	}
	VmPopOperand(&pTos, 1);
	if( n >= nEntry ){
		/* No approprite case to execute, jump to the default case */
		if( pSwitch->nDefault > 0 ){
			pc = pSwitch->nDefault - 1;
		}else{
			/* No default case, jump out of this switch */
			pc = pSwitch->nOut - 1;
		}
	}
	break;
					}
/*
 * OP_UPLINK P1 * *
 * Link a variable to the top active VM frame. 
 * This is used to implement the 'uplink' JX9 construct.
 */
case JX9_OP_UPLINK: {
	if( pVm->pFrame->pParent ){
		jx9_value *pLink = &pTos[-pInstr->iP1+1];
		SyString sName;
		/* Perform the link */
		while( pLink <= pTos ){
			if((pLink->iFlags & MEMOBJ_STRING) == 0 ){
				/* Force a string cast */
				jx9MemObjToString(pLink);
			}
			SyStringInitFromBuf(&sName, SyBlobData(&pLink->sBlob), SyBlobLength(&pLink->sBlob));
			if( sName.nByte > 0 ){
				VmFrameLink(&(*pVm), &sName);
			}
			pLink++;
		}
	}
	VmPopOperand(&pTos, pInstr->iP1);
	break;
					}
/*
 * OP_CALL P1 * *
 *  Call a JX9 or a foreign function and push the return value of the called
 *  function on the stack.
 */
case JX9_OP_CALL: {
	jx9_value *pArg = &pTos[-pInstr->iP1];
	SyHashEntry *pEntry;
	SyString sName;
	/* Extract function name */
	if( (pTos->iFlags & MEMOBJ_STRING) == 0 ){
		/* Raise exception: Invalid function name */
		VmErrorFormat(&(*pVm), JX9_CTX_WARNING, "Invalid function name, JX9 is returning NULL.");
		/* Pop given arguments */
		if( pInstr->iP1 > 0 ){
			VmPopOperand(&pTos, pInstr->iP1);
		}
		/* Assume a null return value so that the program continue it's execution normally */
		jx9MemObjRelease(pTos);
		break;
	}
	SyStringInitFromBuf(&sName, SyBlobData(&pTos->sBlob), SyBlobLength(&pTos->sBlob));
	/* Check for a compiled function first */
	pEntry = SyHashGet(&pVm->hFunction, (const void *)sName.zString, sName.nByte);
	if( pEntry ){
		jx9_vm_func_arg *aFormalArg;
		jx9_value *pFrameStack;
		jx9_vm_func *pVmFunc;
		VmFrame *pFrame;
		jx9_value *pObj;
		VmSlot sArg;
		sxu32 n;
		pVmFunc = (jx9_vm_func *)pEntry->pUserData;
		/* Check The recursion limit */
		if( pVm->nRecursionDepth > pVm->nMaxDepth ){
			VmErrorFormat(&(*pVm), JX9_CTX_ERR, 
				"Recursion limit reached while invoking user function '%z', JX9 will set a NULL return value", 
				&pVmFunc->sName);
			/* Pop given arguments */
			if( pInstr->iP1 > 0 ){
				VmPopOperand(&pTos, pInstr->iP1);
			}
			/* Assume a null return value so that the program continue it's execution normally */
			jx9MemObjRelease(pTos);
			break;
		}
		if( pVmFunc->pNextName ){
			/* Function is candidate for overloading, select the appropriate function to call */
			pVmFunc = VmOverload(&(*pVm), pVmFunc, pArg, (int)(pTos-pArg));
		}
		/* Extract the formal argument set */
		aFormalArg = (jx9_vm_func_arg *)SySetBasePtr(&pVmFunc->aArgs);
		/* Create a new VM frame  */
		rc = VmEnterFrame(&(*pVm),pVmFunc,&pFrame);
		if( rc != SXRET_OK ){
			/* Raise exception: Out of memory */
			VmErrorFormat(&(*pVm), JX9_CTX_ERR, 
				"JX9 is running out of memory while calling function '%z', JX9 is returning NULL.", 
				&pVmFunc->sName);
			/* Pop given arguments */
			if( pInstr->iP1 > 0 ){
				VmPopOperand(&pTos, pInstr->iP1);
			}
			/* Assume a null return value so that the program continue it's execution normally */
			jx9MemObjRelease(pTos);
			break;
		}
		if( SySetUsed(&pVmFunc->aStatic) > 0 ){
			jx9_vm_func_static_var *pStatic, *aStatic;
			/* Install static variables */
			aStatic = (jx9_vm_func_static_var *)SySetBasePtr(&pVmFunc->aStatic);
			for( n = 0 ; n < SySetUsed(&pVmFunc->aStatic) ; ++n ){
				pStatic = &aStatic[n];
				if( pStatic->nIdx == SXU32_HIGH ){
					/* Initialize the static variables */
					pObj = VmReserveMemObj(&(*pVm), &pStatic->nIdx);
					if( pObj ){
						/* Assume a NULL initialization value */
						jx9MemObjInit(&(*pVm), pObj);
						if( SySetUsed(&pStatic->aByteCode) > 0 ){
							/* Evaluate initialization expression (Any complex expression) */
							VmLocalExec(&(*pVm), &pStatic->aByteCode, pObj);
						}
						pObj->nIdx = pStatic->nIdx;
					}else{
						continue;
					}
				}
				/* Install in the current frame */
				SyHashInsert(&pFrame->hVar, SyStringData(&pStatic->sName), SyStringLength(&pStatic->sName), 
					SX_INT_TO_PTR(pStatic->nIdx));
			}
		}
		/* Push arguments in the local frame */
		n = 0;
		while( pArg < pTos ){
			if( n < SySetUsed(&pVmFunc->aArgs) ){
				if( (pArg->iFlags & MEMOBJ_NULL) && SySetUsed(&aFormalArg[n].aByteCode) > 0 ){
					/* NULL values are redirected to default arguments */
					rc = VmLocalExec(&(*pVm), &aFormalArg[n].aByteCode, pArg);
					if( rc == JX9_ABORT ){
						goto Abort;
					}
				}
				/* Make sure the given arguments are of the correct type */
				if( aFormalArg[n].nType > 0 ){
				 if( ((pArg->iFlags & aFormalArg[n].nType) == 0) ){
						ProcMemObjCast xCast = jx9MemObjCastMethod(aFormalArg[n].nType);
						/* Cast to the desired type */
						if( xCast ){
							xCast(pArg);
						}
					}
				}
				/* Pass by value, make a copy of the given argument */
				pObj = VmExtractMemObj(&(*pVm), &aFormalArg[n].sName, FALSE, TRUE);
			}else{
				char zName[32];
				SyString sName;
				/* Set a dummy name */
				sName.nByte = SyBufferFormat(zName, sizeof(zName), "[%u]apArg", n);
				sName.zString = zName;
				/* Annonymous argument */
				pObj = VmExtractMemObj(&(*pVm), &sName, TRUE, TRUE);
			}
			if( pObj ){
				jx9MemObjStore(pArg, pObj);
				/* Insert argument index  */
				sArg.nIdx = pObj->nIdx;
				sArg.pUserData = 0;
				SySetPut(&pFrame->sArg, (const void *)&sArg);
			}
			jx9MemObjRelease(pArg);
			pArg++;
			++n;
		}
		/* Process default values */
		while( n < SySetUsed(&pVmFunc->aArgs) ){
			if( SySetUsed(&aFormalArg[n].aByteCode) > 0 ){
				pObj = VmExtractMemObj(&(*pVm), &aFormalArg[n].sName, FALSE, TRUE);
				if( pObj ){
					/* Evaluate the default value and extract it's result */
					rc = VmLocalExec(&(*pVm), &aFormalArg[n].aByteCode, pObj);
					if( rc == JX9_ABORT ){
						goto Abort;
					}
					/* Insert argument index */
					sArg.nIdx = pObj->nIdx;
					sArg.pUserData = 0;
					SySetPut(&pFrame->sArg, (const void *)&sArg);
					/* Make sure the default argument is of the correct type */
					if( aFormalArg[n].nType > 0 && ((pObj->iFlags & aFormalArg[n].nType) == 0) ){
						ProcMemObjCast xCast = jx9MemObjCastMethod(aFormalArg[n].nType);
						/* Cast to the desired type */
						xCast(pObj);
					}
				}
			}
			++n;
		}
		/* Pop arguments, function name from the operand stack and assume the function 
		 * does not return anything.
		 */
		jx9MemObjRelease(pTos);
		pTos = &pTos[-pInstr->iP1];
		/* Allocate a new operand stack and evaluate the function body */
		pFrameStack = VmNewOperandStack(&(*pVm), SySetUsed(&pVmFunc->aByteCode));
		if( pFrameStack == 0 ){
			/* Raise exception: Out of memory */
			VmErrorFormat(&(*pVm), JX9_CTX_ERR, "JX9 is running out of memory while calling function '%z', JX9 is returning NULL.", 
				&pVmFunc->sName);
			if( pInstr->iP1 > 0 ){
				VmPopOperand(&pTos, pInstr->iP1);
			}
			break;
		}
		/* Increment nesting level */
		pVm->nRecursionDepth++;
		/* Execute function body */
		rc = VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(&pVmFunc->aByteCode), pFrameStack, -1, pTos);
		/* Decrement nesting level */
		pVm->nRecursionDepth--;
		/* Free the operand stack */
		SyMemBackendFree(&pVm->sAllocator, pFrameStack);
		/* Leave the frame */
		VmLeaveFrame(&(*pVm));
		if( rc == JX9_ABORT ){
			/* Abort processing immeditaley */
			goto Abort;
		}
	}else{
		jx9_user_func *pFunc; 
		jx9_context sCtx;
		jx9_value sRet;
		/* Look for an installed foreign function */
		pEntry = SyHashGet(&pVm->hHostFunction, (const void *)sName.zString, sName.nByte);
		if( pEntry == 0 ){
			/* Call to undefined function */
			VmErrorFormat(&(*pVm), JX9_CTX_WARNING, "Call to undefined function '%z', JX9 is returning NULL.", &sName);
			/* Pop given arguments */
			if( pInstr->iP1 > 0 ){
				VmPopOperand(&pTos, pInstr->iP1);
			}
			/* Assume a null return value so that the program continue it's execution normally */
			jx9MemObjRelease(pTos);
			break;
		}
		pFunc = (jx9_user_func *)pEntry->pUserData;
		/* Start collecting function arguments */
		SySetReset(&aArg);
		while( pArg < pTos ){
			SySetPut(&aArg, (const void *)&pArg);
			pArg++;
		}
		/* Assume a null return value */
		jx9MemObjInit(&(*pVm), &sRet);
		/* Init the call context */
		VmInitCallContext(&sCtx, &(*pVm), pFunc, &sRet, 0);
		/* Call the foreign function */
		rc = pFunc->xFunc(&sCtx, (int)SySetUsed(&aArg), (jx9_value **)SySetBasePtr(&aArg));
		/* Release the call context */
		VmReleaseCallContext(&sCtx);
		if( rc == JX9_ABORT ){
			goto Abort;
		}
		if( pInstr->iP1 > 0 ){
			/* Pop function name and arguments */
			VmPopOperand(&pTos, pInstr->iP1);
		}
		/* Save foreign function return value */
		jx9MemObjStore(&sRet, pTos);
		jx9MemObjRelease(&sRet);
	}
	break;
				  }
/*
 * OP_CONSUME: P1 * *
 * Consume (Invoke the installed VM output consumer callback) and POP P1 elements from the stack.
 */
case JX9_OP_CONSUME: {
	jx9_output_consumer *pCons = &pVm->sVmConsumer;
	jx9_value *pCur, *pOut = pTos;

	pOut = &pTos[-pInstr->iP1 + 1];
	pCur = pOut;
	/* Start the consume process  */
	while( pOut <= pTos ){
		/* Force a string cast */
		if( (pOut->iFlags & MEMOBJ_STRING) == 0 ){
			jx9MemObjToString(pOut);
		}
		if( SyBlobLength(&pOut->sBlob) > 0 ){
			/*SyBlobNullAppend(&pOut->sBlob);*/
			/* Invoke the output consumer callback */
			rc = pCons->xConsumer(SyBlobData(&pOut->sBlob), SyBlobLength(&pOut->sBlob), pCons->pUserData);
			/* Increment output length */
			pVm->nOutputLen += SyBlobLength(&pOut->sBlob);
			SyBlobRelease(&pOut->sBlob);
			if( rc == SXERR_ABORT ){
				/* Output consumer callback request an operation abort. */
				goto Abort;
			}
		}
		pOut++;
	}
	pTos = &pCur[-1];
	break;
					 }

		} /* Switch() */
		pc++; /* Next instruction in the stream */
	} /* For(;;) */
Done:
	SySetRelease(&aArg);
	return SXRET_OK;
Abort:
	SySetRelease(&aArg);
	while( pTos >= pStack ){
		jx9MemObjRelease(pTos);
		pTos--;
	}
	return JX9_ABORT;
}
/*
 * Execute as much of a local JX9 bytecode program as we can then return.
 * This function is a wrapper around [VmByteCodeExec()].
 * See block-comment on that function for additional information.
 */
static sxi32 VmLocalExec(jx9_vm *pVm, SySet *pByteCode,jx9_value *pResult)
{
	jx9_value *pStack;
	sxi32 rc;
	/* Allocate a new operand stack */
	pStack = VmNewOperandStack(&(*pVm), SySetUsed(pByteCode));
	if( pStack == 0 ){
		return SXERR_MEM;
	}
	/* Execute the program */
	rc = VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(pByteCode), pStack, -1, &(*pResult));
	/* Free the operand stack */
	SyMemBackendFree(&pVm->sAllocator, pStack);
	/* Execution result */
	return rc;
}
/*
 * Execute as much of a JX9 bytecode program as we can then return.
 * This function is a wrapper around [VmByteCodeExec()].
 * See block-comment on that function for additional information.
 */
JX9_PRIVATE sxi32 jx9VmByteCodeExec(jx9_vm *pVm)
{
	/* Make sure we are ready to execute this program */
	if( pVm->nMagic != JX9_VM_RUN ){
		return pVm->nMagic == JX9_VM_EXEC ? SXERR_LOCKED /* Locked VM */ : SXERR_CORRUPT; /* Stale VM */
	}
	/* Set the execution magic number  */
	pVm->nMagic = JX9_VM_EXEC;
	/* Execute the program */
	VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(pVm->pByteContainer), pVm->aOps, -1, &pVm->sExec);
	/*
	 * TICKET 1433-100: Do not remove the JX9_VM_EXEC magic number
	 * so that any following call to [jx9_vm_exec()] without calling
	 * [jx9_vm_reset()] first would fail.
	 */
	return SXRET_OK;
}
/*
 * Extract a memory object (i.e. a variable) from the running script.
 * This function must be called after calling jx9_vm_exec(). Otherwise
 * NULL is returned.
 */
JX9_PRIVATE jx9_value * jx9VmExtractVariable(jx9_vm *pVm,SyString *pVar)
{
	jx9_value *pValue;
	if( pVm->nMagic != JX9_VM_EXEC ){
		/* call jx9_vm_exec() first */
		return 0;
	}
	/* Perform the lookup */
	pValue = VmExtractMemObj(pVm,pVar,FALSE,FALSE);
	/* Lookup result */
	return pValue;
}
/*
 * Invoke the installed VM output consumer callback to consume
 * the desired message.
 * Refer to the implementation of [jx9_context_output()] defined
 * in 'api.c' for additional information.
 */
JX9_PRIVATE sxi32 jx9VmOutputConsume(
	jx9_vm *pVm,      /* Target VM */
	SyString *pString /* Message to output */
	)
{
	jx9_output_consumer *pCons = &pVm->sVmConsumer;
	sxi32 rc = SXRET_OK;
	/* Call the output consumer */
	if( pString->nByte > 0 ){
		rc = pCons->xConsumer((const void *)pString->zString, pString->nByte, pCons->pUserData);
		/* Increment output length */
		pVm->nOutputLen += pString->nByte;
	}
	return rc;
}
/*
 * Format a message and invoke the installed VM output consumer
 * callback to consume the formatted message.
 * Refer to the implementation of [jx9_context_output_format()] defined
 * in 'api.c' for additional information.
 */
JX9_PRIVATE sxi32 jx9VmOutputConsumeAp(
	jx9_vm *pVm,         /* Target VM */
	const char *zFormat, /* Formatted message to output */
	va_list ap           /* Variable list of arguments */ 
	)
{
	jx9_output_consumer *pCons = &pVm->sVmConsumer;
	sxi32 rc = SXRET_OK;
	SyBlob sWorker;
	/* Format the message and call the output consumer */
	SyBlobInit(&sWorker, &pVm->sAllocator);
	SyBlobFormatAp(&sWorker, zFormat, ap);
	if( SyBlobLength(&sWorker) > 0 ){
		/* Consume the formatted message */
		rc = pCons->xConsumer(SyBlobData(&sWorker), SyBlobLength(&sWorker), pCons->pUserData);
	}
	/* Increment output length */
	pVm->nOutputLen += SyBlobLength(&sWorker);
	/* Release the working buffer */
	SyBlobRelease(&sWorker);
	return rc;
}
/*
 * Return a string representation of the given JX9 OP code.
 * This function never fail and always return a pointer
 * to a null terminated string.
 */
static const char * VmInstrToString(sxi32 nOp)
{
	const char *zOp = "Unknown     ";
	switch(nOp){
	case JX9_OP_DONE:       zOp = "DONE       "; break;
	case JX9_OP_HALT:       zOp = "HALT       "; break;
	case JX9_OP_LOAD:       zOp = "LOAD       "; break;
	case JX9_OP_LOADC:      zOp = "LOADC      "; break;
	case JX9_OP_LOAD_MAP:   zOp = "LOAD_MAP   "; break;
	case JX9_OP_LOAD_IDX:   zOp = "LOAD_IDX   "; break;
	case JX9_OP_NOOP:       zOp = "NOOP       "; break;
	case JX9_OP_JMP:        zOp = "JMP        "; break;
	case JX9_OP_JZ:         zOp = "JZ         "; break;
	case JX9_OP_JNZ:        zOp = "JNZ        "; break;
	case JX9_OP_POP:        zOp = "POP        "; break;
	case JX9_OP_CAT:        zOp = "CAT        "; break;
	case JX9_OP_CVT_INT:    zOp = "CVT_INT    "; break;
	case JX9_OP_CVT_STR:    zOp = "CVT_STR    "; break;
	case JX9_OP_CVT_REAL:   zOp = "CVT_REAL   "; break;
	case JX9_OP_CALL:       zOp = "CALL       "; break;
	case JX9_OP_UMINUS:     zOp = "UMINUS     "; break;
	case JX9_OP_UPLUS:      zOp = "UPLUS      "; break;
	case JX9_OP_BITNOT:     zOp = "BITNOT     "; break;
	case JX9_OP_LNOT:       zOp = "LOGNOT     "; break;
	case JX9_OP_MUL:        zOp = "MUL        "; break;
	case JX9_OP_DIV:        zOp = "DIV        "; break;
	case JX9_OP_MOD:        zOp = "MOD        "; break;
	case JX9_OP_ADD:        zOp = "ADD        "; break;
	case JX9_OP_SUB:        zOp = "SUB        "; break;
	case JX9_OP_SHL:        zOp = "SHL        "; break;
	case JX9_OP_SHR:        zOp = "SHR        "; break;
	case JX9_OP_LT:         zOp = "LT         "; break;
	case JX9_OP_LE:         zOp = "LE         "; break;
	case JX9_OP_GT:         zOp = "GT         "; break;
	case JX9_OP_GE:         zOp = "GE         "; break;
	case JX9_OP_EQ:         zOp = "EQ         "; break;
	case JX9_OP_NEQ:        zOp = "NEQ        "; break;
	case JX9_OP_TEQ:        zOp = "TEQ        "; break;
	case JX9_OP_TNE:        zOp = "TNE        "; break;
	case JX9_OP_BAND:       zOp = "BITAND     "; break;
	case JX9_OP_BXOR:       zOp = "BITXOR     "; break;
	case JX9_OP_BOR:        zOp = "BITOR      "; break;
	case JX9_OP_LAND:       zOp = "LOGAND     "; break;
	case JX9_OP_LOR:        zOp = "LOGOR      "; break;
	case JX9_OP_LXOR:       zOp = "LOGXOR     "; break;
	case JX9_OP_STORE:      zOp = "STORE      "; break;
	case JX9_OP_STORE_IDX:  zOp = "STORE_IDX  "; break;
	case JX9_OP_PULL:       zOp = "PULL       "; break;
	case JX9_OP_SWAP:       zOp = "SWAP       "; break;
	case JX9_OP_YIELD:      zOp = "YIELD      "; break;
	case JX9_OP_CVT_BOOL:   zOp = "CVT_BOOL   "; break;
	case JX9_OP_CVT_NULL:   zOp = "CVT_NULL   "; break;
	case JX9_OP_CVT_ARRAY:  zOp = "CVT_JSON   "; break;
	case JX9_OP_CVT_NUMC:   zOp = "CVT_NUMC   "; break;
	case JX9_OP_INCR:       zOp = "INCR       "; break;
	case JX9_OP_DECR:       zOp = "DECR       "; break;
	case JX9_OP_ADD_STORE:  zOp = "ADD_STORE  "; break;
	case JX9_OP_SUB_STORE:  zOp = "SUB_STORE  "; break;
	case JX9_OP_MUL_STORE:  zOp = "MUL_STORE  "; break;
	case JX9_OP_DIV_STORE:  zOp = "DIV_STORE  "; break;
	case JX9_OP_MOD_STORE:  zOp = "MOD_STORE  "; break;
	case JX9_OP_CAT_STORE:  zOp = "CAT_STORE  "; break;
	case JX9_OP_SHL_STORE:  zOp = "SHL_STORE  "; break;
	case JX9_OP_SHR_STORE:  zOp = "SHR_STORE  "; break;
	case JX9_OP_BAND_STORE: zOp = "BAND_STORE "; break;
	case JX9_OP_BOR_STORE:  zOp = "BOR_STORE  "; break;
	case JX9_OP_BXOR_STORE: zOp = "BXOR_STORE "; break;
	case JX9_OP_CONSUME:    zOp = "CONSUME    "; break;
	case JX9_OP_MEMBER:     zOp = "MEMBER     "; break;
	case JX9_OP_UPLINK:     zOp = "UPLINK     "; break;
	case JX9_OP_SWITCH:     zOp = "SWITCH     "; break;
	case JX9_OP_FOREACH_INIT:
		                    zOp = "4EACH_INIT "; break;
	case JX9_OP_FOREACH_STEP:
						    zOp = "4EACH_STEP "; break;
	default:
		break;
	}
	return zOp;
}
/*
 * Dump JX9 bytecodes instructions to a human readable format.
 * The xConsumer() callback which is an used defined function
 * is responsible of consuming the generated dump.
 */
JX9_PRIVATE sxi32 jx9VmDump(
	jx9_vm *pVm,            /* Target VM */
	ProcConsumer xConsumer, /* Output [i.e: dump] consumer callback */
	void *pUserData         /* Last argument to xConsumer() */
	)
{
	sxi32 rc;
	rc = VmByteCodeDump(pVm->pByteContainer, xConsumer, pUserData);
	return rc;
}
/*
 * Default constant expansion callback used by the 'const' statement if used
 * outside a object body [i.e: global or function scope].
 * Refer to the implementation of [JX9_CompileConstant()] defined
 * in 'compile.c' for additional information.
 */
JX9_PRIVATE void jx9VmExpandConstantValue(jx9_value *pVal, void *pUserData)
{
	SySet *pByteCode = (SySet *)pUserData;
	/* Evaluate and expand constant value */
	VmLocalExec((jx9_vm *)SySetGetUserData(pByteCode), pByteCode, (jx9_value *)pVal);
}
/*
 * Section:
 *  Function handling functions.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 * Status:
 *    Stable.
 */
/*
 * int func_num_args(void)
 *   Returns the number of arguments passed to the function.
 * Parameters
 *   None.
 * Return
 *  Total number of arguments passed into the current user-defined function
 *  or -1 if called from the globe scope.
 */
static int vm_builtin_func_num_args(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	VmFrame *pFrame;
	jx9_vm *pVm;
	/* Point to the target VM */
	pVm = pCtx->pVm;
	/* Current frame */
	pFrame = pVm->pFrame;
	if( pFrame->pParent == 0 ){
		SXUNUSED(nArg);
		SXUNUSED(apArg);
		/* Global frame, return -1 */
		jx9_result_int(pCtx, -1);
		return SXRET_OK;
	}
	/* Total number of arguments passed to the enclosing function */
	nArg = (int)SySetUsed(&pFrame->sArg);
	jx9_result_int(pCtx, nArg);
	return SXRET_OK;
}
/*
 * value func_get_arg(int $arg_num)
 *   Return an item from the argument list.
 * Parameters
 *  Argument number(index start from zero).
 * Return
 *  Returns the specified argument or FALSE on error.
 */
static int vm_builtin_func_get_arg(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	jx9_value *pObj = 0;
	VmSlot *pSlot = 0;
	VmFrame *pFrame;
	jx9_vm *pVm;
	/* Point to the target VM */
	pVm = pCtx->pVm;
	/* Current frame */
	pFrame = pVm->pFrame;
	if( nArg < 1 || pFrame->pParent == 0 ){
		/* Global frame or Missing arguments, return FALSE */
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "Called in the global scope");
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	/* Extract the desired index */
	nArg = jx9_value_to_int(apArg[0]);
	if( nArg < 0 || nArg >= (int)SySetUsed(&pFrame->sArg) ){
		/* Invalid index, return FALSE */
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	/* Extract the desired argument */
	if( (pSlot = (VmSlot *)SySetAt(&pFrame->sArg, (sxu32)nArg)) != 0 ){
		if( (pObj = (jx9_value *)SySetAt(&pVm->aMemObj, pSlot->nIdx)) != 0 ){
			/* Return the desired argument */
			jx9_result_value(pCtx, (jx9_value *)pObj);
		}else{
			/* No such argument, return false */
			jx9_result_bool(pCtx, 0);
		}
	}else{
		/* CAN'T HAPPEN */
		jx9_result_bool(pCtx, 0);
	}
	return SXRET_OK;
}
/*
 * array func_get_args(void)
 *   Returns an array comprising a copy of function's argument list.
 * Parameters
 *  None.
 * Return
 *  Returns an array in which each element is a copy of the corresponding
 *  member of the current user-defined function's argument list.
 *  Otherwise FALSE is returned on failure.
 */
static int vm_builtin_func_get_args(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	jx9_value *pObj = 0;
	jx9_value *pArray;
	VmFrame *pFrame;
	VmSlot *aSlot;
	sxu32 n;
	/* Point to the current frame */
	pFrame = pCtx->pVm->pFrame;
	if( pFrame->pParent == 0 ){
		/* Global frame, return FALSE */
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "Called in the global scope");
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	/* Create a new array */
	pArray = jx9_context_new_array(pCtx);
	if( pArray == 0 ){
		SXUNUSED(nArg); /* cc warning */
		SXUNUSED(apArg);
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	/* Start filling the array with the given arguments */
	aSlot = (VmSlot *)SySetBasePtr(&pFrame->sArg);
	for( n = 0;  n < SySetUsed(&pFrame->sArg) ; n++ ){
		pObj = (jx9_value *)SySetAt(&pCtx->pVm->aMemObj, aSlot[n].nIdx);
		if( pObj ){
			jx9_array_add_elem(pArray, 0/* Automatic index assign*/, pObj);
		}
	}
	/* Return the freshly created array */
	jx9_result_value(pCtx, pArray);
	return SXRET_OK;
}
/*
 * bool function_exists(string $name)
 *  Return TRUE if the given function has been defined.
 * Parameters
 *  The name of the desired function.
 * Return
 *  Return TRUE if the given function has been defined.False otherwise
 */
static int vm_builtin_func_exists(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const char *zName;
	jx9_vm *pVm;
	int nLen;
	int res;
	if( nArg < 1 ){
		/* Missing argument, return FALSE */
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	/* Point to the target VM */
	pVm = pCtx->pVm;
	/* Extract the function name */
	zName = jx9_value_to_string(apArg[0], &nLen);
	/* Assume the function is not defined */
	res = 0;
	/* Perform the lookup */
	if( SyHashGet(&pVm->hFunction, (const void *)zName, (sxu32)nLen) != 0 ||
		SyHashGet(&pVm->hHostFunction, (const void *)zName, (sxu32)nLen) != 0 ){
			/* Function is defined */
			res = 1;
	}
	jx9_result_bool(pCtx, res);
	return SXRET_OK;
}
/*
 * Verify that the contents of a variable can be called as a function.
 * [i.e: Whether it is callable or not].
 * Return TRUE if callable.FALSE otherwise.
 */
JX9_PRIVATE int jx9VmIsCallable(jx9_vm *pVm, jx9_value *pValue)
{
	int res = 0;
	if( pValue->iFlags & MEMOBJ_STRING ){
		const char *zName;
		int nLen;
		/* Extract the name */
		zName = jx9_value_to_string(pValue, &nLen);
		/* Perform the lookup */
		if( SyHashGet(&pVm->hFunction, (const void *)zName, (sxu32)nLen) != 0 ||
			SyHashGet(&pVm->hHostFunction, (const void *)zName, (sxu32)nLen) != 0 ){
				/* Function is callable */
				res = 1;
		}
	}
	return res;
}
/*
 * bool is_callable(callable $name[, bool $syntax_only = false])
 * Verify that the contents of a variable can be called as a function.
 * Parameters
 * $name
 *    The callback function to check
 * $syntax_only
 *    If set to TRUE the function only verifies that name might be a function or method.
 *    It will only reject simple variables that are not strings, or an array that does
 *    not have a valid structure to be used as a callback. The valid ones are supposed
 *    to have only 2 entries, the first of which is an object or a string, and the second
 *    a string.
 * Return
 *  TRUE if name is callable, FALSE otherwise.
 */
static int vm_builtin_is_callable(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	jx9_vm *pVm;	
	int res;
	if( nArg < 1 ){
		/* Missing arguments, return FALSE */
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	/* Point to the target VM */
	pVm = pCtx->pVm;
	/* Perform the requested operation */
	res = jx9VmIsCallable(pVm, apArg[0]);
	jx9_result_bool(pCtx, res);
	return SXRET_OK;
}
/*
 * Hash walker callback used by the [get_defined_functions()] function
 * defined below.
 */
static int VmHashFuncStep(SyHashEntry *pEntry, void *pUserData)
{
	jx9_value *pArray = (jx9_value *)pUserData;
	jx9_value sName;
	sxi32 rc;
	/* Prepare the function name for insertion */
	jx9MemObjInitFromString(pArray->pVm, &sName, 0);
	jx9MemObjStringAppend(&sName, (const char *)pEntry->pKey, pEntry->nKeyLen);
	/* Perform the insertion */
	rc = jx9_array_add_elem(pArray, 0/* Automatic index assign */, &sName); /* Will make it's own copy */
	jx9MemObjRelease(&sName);
	return rc;
}
/*
 * array get_defined_functions(void)
 *  Returns an array of all defined functions.
 * Parameter
 *  None.
 * Return
 *  Returns an multidimensional array containing a list of all defined functions
 *  both built-in (internal) and user-defined.
 *  The internal functions will be accessible via $arr["internal"], and the user 
 *  defined ones using $arr["user"]. 
 * Note:
 *  NULL is returned on failure.
 */
static int vm_builtin_get_defined_func(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	jx9_value *pArray;
	/* NOTE:
	 * Don't worry about freeing memory here, every allocated resource will be released
	 * automatically by the engine as soon we return from this foreign function.
	 */
	pArray = jx9_context_new_array(pCtx);
 	if( pArray == 0 ){
		SXUNUSED(nArg); /* cc warning */
		SXUNUSED(apArg);
		/* Return NULL */
		jx9_result_null(pCtx);
		return SXRET_OK;
	}
	/* Fill with the appropriate information */
	SyHashForEach(&pCtx->pVm->hHostFunction,VmHashFuncStep,pArray);
	/* Fill with the appropriate information */
	SyHashForEach(&pCtx->pVm->hFunction, VmHashFuncStep,pArray);
	/* Return a copy of the array array */
	jx9_result_value(pCtx, pArray);
	return SXRET_OK;
}
/*
 * Call a user defined or foreign function where the name of the function
 * is stored in the pFunc parameter and the given arguments are stored
 * in the apArg[] array.
 * Return SXRET_OK if the function was successfuly called.Any other
 * return value indicates failure.
 */
JX9_PRIVATE sxi32 jx9VmCallUserFunction(
	jx9_vm *pVm,       /* Target VM */
	jx9_value *pFunc,  /* Callback name */
	int nArg,          /* Total number of given arguments */
	jx9_value **apArg, /* Callback arguments */
	jx9_value *pResult /* Store callback return value here. NULL otherwise */
	)
{
	jx9_value *aStack;
	VmInstr aInstr[2];
	int i;
	if((pFunc->iFlags & (MEMOBJ_STRING)) == 0 ){
		/* Don't bother processing, it's invalid anyway */
		if( pResult ){
			/* Assume a null return value */
			jx9MemObjRelease(pResult);
		}
		return SXERR_INVALID;
	}
	/* Create a new operand stack */
	aStack = VmNewOperandStack(&(*pVm), 1+nArg);
	if( aStack == 0 ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, 
			"JX9 is running out of memory while invoking user callback");
		if( pResult ){
			/* Assume a null return value */
			jx9MemObjRelease(pResult);
		}
		return SXERR_MEM;
	}
	/* Fill the operand stack with the given arguments */
	for( i = 0 ; i < nArg ; i++ ){
		jx9MemObjLoad(apArg[i], &aStack[i]);
		aStack[i].nIdx = apArg[i]->nIdx;
	}
	/* Push the function name */
	jx9MemObjLoad(pFunc, &aStack[i]);
	aStack[i].nIdx = SXU32_HIGH; /* Mark as constant */
	/* Emit the CALL istruction */
	aInstr[0].iOp = JX9_OP_CALL;
	aInstr[0].iP1 = nArg; /* Total number of given arguments */
	aInstr[0].iP2 = 0;
	aInstr[0].p3  = 0;
	/* Emit the DONE instruction */
	aInstr[1].iOp = JX9_OP_DONE;
	aInstr[1].iP1 = 1;   /* Extract function return value if available */
	aInstr[1].iP2 = 0;
	aInstr[1].p3  = 0;
	/* Execute the function body (if available) */
	VmByteCodeExec(&(*pVm), aInstr, aStack, nArg, pResult);
	/* Clean up the mess left behind */
	SyMemBackendFree(&pVm->sAllocator, aStack);
	return JX9_OK;
}
/*
 * Call a user defined or foreign function whith a varibale number
 * of arguments where the name of the function is stored in the pFunc
 * parameter.
 * Return SXRET_OK if the function was successfuly called.Any other
 * return value indicates failure.
 */
JX9_PRIVATE sxi32 jx9VmCallUserFunctionAp(
	jx9_vm *pVm,       /* Target VM */
	jx9_value *pFunc,  /* Callback name */
	jx9_value *pResult, /* Store callback return value here. NULL otherwise */
	...                /* 0 (Zero) or more Callback arguments */ 
	)
{
	jx9_value *pArg;
	SySet aArg;
	va_list ap;
	sxi32 rc;
	SySetInit(&aArg, &pVm->sAllocator, sizeof(jx9_value *));
	/* Copy arguments one after one */
	va_start(ap, pResult);
	for(;;){
		pArg = va_arg(ap, jx9_value *);
		if( pArg == 0 ){
			break;
		}
		SySetPut(&aArg, (const void *)&pArg);
	}
	/* Call the core routine */
	rc = jx9VmCallUserFunction(&(*pVm), pFunc, (int)SySetUsed(&aArg), (jx9_value **)SySetBasePtr(&aArg), pResult);
	/* Cleanup */
	SySetRelease(&aArg);
	return rc;
}
/*
 * bool defined(string $name)
 *  Checks whether a given named constant exists.
 * Parameter:
 *  Name of the desired constant.
 * Return
 *  TRUE if the given constant exists.FALSE otherwise.
 */
static int vm_builtin_defined(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const char *zName;
	int nLen = 0;
	int res = 0;
	if( nArg < 1 ){
		/* Missing constant name, return FALSE */
		jx9_context_throw_error(pCtx,JX9_CTX_NOTICE,"Missing constant name");
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	/* Extract constant name */
	zName = jx9_value_to_string(apArg[0], &nLen);
	/* Perform the lookup */
	if( nLen > 0 && SyHashGet(&pCtx->pVm->hConstant, (const void *)zName, (sxu32)nLen) != 0 ){
		/* Already defined */
		res = 1;
	}
	jx9_result_bool(pCtx, res);
	return SXRET_OK;
}
/*
 * Hash walker callback used by the [get_defined_constants()] function
 * defined below.
 */
static int VmHashConstStep(SyHashEntry *pEntry, void *pUserData)
{
	jx9_value *pArray = (jx9_value *)pUserData;
	jx9_value sName;
	sxi32 rc;
	/* Prepare the constant name for insertion */
	jx9MemObjInitFromString(pArray->pVm, &sName, 0);
	jx9MemObjStringAppend(&sName, (const char *)pEntry->pKey, pEntry->nKeyLen);
	/* Perform the insertion */
	rc = jx9_array_add_elem(pArray, 0, &sName); /* Will make it's own copy */
	jx9MemObjRelease(&sName);
	return rc;
}
/*
 * array get_defined_constants(void)
 *  Returns an associative array with the names of all defined
 *  constants.
 * Parameters
 *  NONE.
 * Returns
 *  Returns the names of all the constants currently defined.
 */
static int vm_builtin_get_defined_constants(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	jx9_value *pArray;
	/* Create the array first*/
	pArray = jx9_context_new_array(pCtx);
	if( pArray == 0 ){
		SXUNUSED(nArg); /* cc warning */
		SXUNUSED(apArg);
		/* Return NULL */
		jx9_result_null(pCtx);
		return SXRET_OK;
	}
	/* Fill the array with the defined constants */
	SyHashForEach(&pCtx->pVm->hConstant, VmHashConstStep, pArray);
	/* Return the created array */
	jx9_result_value(pCtx, pArray);
	return SXRET_OK;
}
/*
 * Section:
 *  Random numbers/string generators.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 * Status:
 *    Stable.
 */
/*
 * Generate a random 32-bit unsigned integer.
 * JX9 use it's own private PRNG which is based on the one
 * used by te SQLite3 library.
 */
JX9_PRIVATE sxu32 jx9VmRandomNum(jx9_vm *pVm)
{
	sxu32 iNum;
	SyRandomness(&pVm->sPrng, (void *)&iNum, sizeof(sxu32));
	return iNum;
}
/*
 * Generate a random string (English Alphabet) of length nLen.
 * Note that the generated string is NOT null terminated.
 * JX9 use it's own private PRNG which is based on the one used
 * by te SQLite3 library.
 */
JX9_PRIVATE void jx9VmRandomString(jx9_vm *pVm, char *zBuf, int nLen)
{
	static const char zBase[] = {"abcdefghijklmnopqrstuvwxyz"}; /* English Alphabet */
	int i;
	/* Generate a binary string first */
	SyRandomness(&pVm->sPrng, zBuf, (sxu32)nLen);
	/* Turn the binary string into english based alphabet */
	for( i = 0 ; i < nLen ; ++i ){
		 zBuf[i] = zBase[zBuf[i] % (sizeof(zBase)-1)];
	 }
}
/*
 * int rand()
 *  Generate a random (unsigned 32-bit) integer.
 * Parameter
 *  $min
 *    The lowest value to return (default: 0)
 *  $max
 *   The highest value to return (default: getrandmax())
 * Return
 *   A pseudo random value between min (or 0) and max (or getrandmax(), inclusive).
 * Note:
 *  JX9 use it's own private PRNG which is based on the one used
 *  by te SQLite3 library.
 */
static int vm_builtin_rand(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	sxu32 iNum;
	/* Generate the random number */
	iNum = jx9VmRandomNum(pCtx->pVm);
	if( nArg > 1 ){
		sxu32 iMin, iMax;
		iMin = (sxu32)jx9_value_to_int(apArg[0]);
		iMax = (sxu32)jx9_value_to_int(apArg[1]);
		if( iMin < iMax ){
			sxu32 iDiv = iMax+1-iMin;
			if( iDiv > 0 ){
				iNum = (iNum % iDiv)+iMin;
			}
		}else if(iMax > 0 ){
			iNum %= iMax;
		}
	}
	/* Return the number */
	jx9_result_int64(pCtx, (jx9_int64)iNum);
	return SXRET_OK;
}
/*
 * int getrandmax(void)
 *   Show largest possible random value
 * Return
 *  The largest possible random value returned by rand() which is in
 *  this implementation 0xFFFFFFFF.
 * Note:
 *  JX9 use it's own private PRNG which is based on the one used
 *  by te SQLite3 library.
 */
static int vm_builtin_getrandmax(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	SXUNUSED(nArg); /* cc warning */
	SXUNUSED(apArg);
	jx9_result_int64(pCtx, SXU32_HIGH);
	return SXRET_OK;
}
/*
 * string rand_str()
 * string rand_str(int $len)
 *  Generate a random string (English alphabet).
 * Parameter
 *  $len
 *    Length of the desired string (default: 16, Min: 1, Max: 1024)
 * Return
 *   A pseudo random string.
 * Note:
 *  JX9 use it's own private PRNG which is based on the one used
 *  by te SQLite3 library.
 */
static int vm_builtin_rand_str(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	char zString[1024];
	int iLen = 0x10;
	if( nArg > 0 ){
		/* Get the desired length */
		iLen = jx9_value_to_int(apArg[0]);
		if( iLen < 1 || iLen > 1024 ){
			/* Default length */
			iLen = 0x10;
		}
	}
	/* Generate the random string */
	jx9VmRandomString(pCtx->pVm, zString, iLen);
	/* Return the generated string */
	jx9_result_string(pCtx, zString, iLen); /* Will make it's own copy */
	return SXRET_OK;
}
/*
 * Section:
 *  Language construct implementation as foreign functions.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 * Status:
 *    Stable.
 */
/*
 * void print($string...)
 *  Output one or more messages.
 * Parameters
 *  $string
 *   Message to output.
 * Return
 *  NULL.
 */
static int vm_builtin_print(jx9_context *pCtx, int nArg,jx9_value **apArg)
{
	const char *zData;
	int nDataLen = 0;
	jx9_vm *pVm;
	int i, rc;
	/* Point to the target VM */
	pVm = pCtx->pVm;
	/* Output */
	for( i = 0 ; i < nArg ; ++i ){
		zData = jx9_value_to_string(apArg[i], &nDataLen);
		if( nDataLen > 0 ){
			rc = pVm->sVmConsumer.xConsumer((const void *)zData, (unsigned int)nDataLen, pVm->sVmConsumer.pUserData);
			/* Increment output length */
			pVm->nOutputLen += nDataLen;
			if( rc == SXERR_ABORT ){
				/* Output consumer callback request an operation abort */
				return JX9_ABORT;
			}
		}
	}
	return SXRET_OK;
}
/*
 * void exit(string $msg)
 * void exit(int $status)
 * void die(string $ms)
 * void die(int $status)
 *   Output a message and terminate program execution.
 * Parameter
 *  If status is a string, this function prints the status just before exiting.
 *  If status is an integer, that value will be used as the exit status 
 *  and not printed
 * Return
 *  NULL
 */
static int vm_builtin_exit(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	if( nArg > 0 ){
		if( jx9_value_is_string(apArg[0]) ){
			const char *zData;
			int iLen = 0;
			/* Print exit message */
			zData = jx9_value_to_string(apArg[0], &iLen);
			jx9_context_output(pCtx, zData, iLen);
		}else if(jx9_value_is_int(apArg[0]) ){
			sxi32 iExitStatus;
			/* Record exit status code */
			iExitStatus = jx9_value_to_int(apArg[0]);
			pCtx->pVm->iExitStatus = iExitStatus;
		}
	}
	/* Abort processing immediately */
	return JX9_ABORT;
}
/*
 * Unset a memory object [i.e: a jx9_value].
 */
JX9_PRIVATE sxi32 jx9VmUnsetMemObj(jx9_vm *pVm,sxu32 nObjIdx)
{
	jx9_value *pObj;
	pObj = (jx9_value *)SySetAt(&pVm->aMemObj, nObjIdx);
	if( pObj ){
		VmSlot sFree;
		/* Release the object */
		jx9MemObjRelease(pObj);
		/* Restore to the free list */
		sFree.nIdx = nObjIdx;
		sFree.pUserData = 0;
		SySetPut(&pVm->aFreeObj, (const void *)&sFree);
	}				
	return SXRET_OK;
}
/*
 * string gettype($var)
 *  Get the type of a variable
 * Parameters
 *   $var
 *    The variable being type checked.
 * Return
 *   String representation of the given variable type.
 */
static int vm_builtin_gettype(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const char *zType = "null";
	if( nArg > 0 ){
		zType = jx9MemObjTypeDump(apArg[0]);
	}
	/* Return the variable type */
	jx9_result_string(pCtx, zType, -1/*Compute length automatically*/);
	return SXRET_OK;
}
/*
 * string get_resource_type(resource $handle)
 *  This function gets the type of the given resource.
 * Parameters
 *  $handle
 *  The evaluated resource handle.
 * Return
 *  If the given handle is a resource, this function will return a string 
 *  representing its type. If the type is not identified by this function
 *  the return value will be the string Unknown.
 *  This function will return FALSE and generate an error if handle
 *  is not a resource.
 */
static int vm_builtin_get_resource_type(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	if( nArg < 1 || !jx9_value_is_resource(apArg[0]) ){
		/* Missing/Invalid arguments, return FALSE*/
		jx9_result_bool(pCtx, 0);
		return SXRET_OK;
	}
	jx9_result_string_format(pCtx, "resID_%#x", apArg[0]->x.pOther);
	return SXRET_OK;
}
/*
 * void dump(expression, ....)
 *   dump — Dumps information about a variable
 * Parameters
 *   One or more expression to dump.
 * Returns
 *  Nothing.
 */
static int vm_builtin_dump(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	SyBlob sDump; /* Generated dump is stored here */
	int i;
	SyBlobInit(&sDump,&pCtx->pVm->sAllocator);
	/* Dump one or more expressions */
	for( i = 0 ; i < nArg ; i++ ){
		jx9_value *pObj = apArg[i];
		/* Reset the working buffer */
		SyBlobReset(&sDump);
		/* Dump the given expression */
		jx9MemObjDump(&sDump,pObj);
		/* Output */
		if( SyBlobLength(&sDump) > 0 ){
			jx9_context_output(pCtx, (const char *)SyBlobData(&sDump), (int)SyBlobLength(&sDump));
		}
	}
	/* Release the working buffer */
	SyBlobRelease(&sDump);
	return SXRET_OK;
}
/*
 * Section:
 *  Version, Credits and Copyright related functions.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 *    Stable.
 */
/*
 * string jx9_version(void)
 * string jx9_credits(void)
 *  Returns the running version of the jx9 version.
 * Parameters
 *  None
 * Return
 * Current jx9 version.
 */
static int vm_builtin_jx9_version(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	SXUNUSED(nArg);
	SXUNUSED(apArg); /* cc warning */
	/* Current engine version, signature and cipyright notice */
	jx9_result_string_format(pCtx,"%s %s, %s",JX9_VERSION,JX9_SIG,JX9_COPYRIGHT);
	return JX9_OK;
}
/*
 * Section:
 *    URL related routines.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 * Status:
 *    Stable.
 */
/* Forward declaration */
static sxi32 VmHttpSplitURI(SyhttpUri *pOut, const char *zUri, sxu32 nLen);
/*
 * value parse_url(string $url [, int $component = -1 ])
 *  Parse a URL and return its fields.
 * Parameters
 *  $url
 *   The URL to parse.
 * $component
 *  Specify one of JX9_URL_SCHEME, JX9_URL_HOST, JX9_URL_PORT, JX9_URL_USER
 *  JX9_URL_PASS, JX9_URL_PATH, JX9_URL_QUERY or JX9_URL_FRAGMENT to retrieve
 *  just a specific URL component as a string (except when JX9_URL_PORT is given
 *  in which case the return value will be an integer).
 * Return
 *  If the component parameter is omitted, an associative array is returned.
 *  At least one element will be present within the array. Potential keys within
 *  this array are:
 *   scheme - e.g. http
 *   host
 *   port
 *   user
 *   pass
 *   path
 *   query - after the question mark ?
 *   fragment - after the hashmark #
 * Note:
 *  FALSE is returned on failure.
 *  This function work with relative URL unlike the one shipped
 *  with the standard JX9 engine.
 */
static int vm_builtin_parse_url(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const char *zStr; /* Input string */
	SyString *pComp;  /* Pointer to the URI component */
	SyhttpUri sURI;   /* Parse of the given URI */
	int nLen;
	sxi32 rc;
	if( nArg < 1 || !jx9_value_is_string(apArg[0]) ){
		/* Missing/Invalid arguments, return FALSE */
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Extract the given URI */
	zStr = jx9_value_to_string(apArg[0], &nLen);
	if( nLen < 1 ){
		/* Nothing to process, return FALSE */
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Get a parse */
	rc = VmHttpSplitURI(&sURI, zStr, (sxu32)nLen);
	if( rc != SXRET_OK ){
		/* Malformed input, return FALSE */
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	if( nArg > 1 ){
		int nComponent = jx9_value_to_int(apArg[1]);
		/* Refer to constant.c for constants values */
		switch(nComponent){
		case 1: /* JX9_URL_SCHEME */
			pComp = &sURI.sScheme;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				jx9_result_string(pCtx, pComp->zString, (int)pComp->nByte);
			}
			break;
		case 2: /* JX9_URL_HOST */
			pComp = &sURI.sHost;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				jx9_result_string(pCtx, pComp->zString, (int)pComp->nByte);
			}
			break;
		case 3: /* JX9_URL_PORT */
			pComp = &sURI.sPort;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				int iPort = 0;
				/* Cast the value to integer */
				SyStrToInt32(pComp->zString, pComp->nByte, (void *)&iPort, 0);
				jx9_result_int(pCtx, iPort);
			}
			break;
		case 4: /* JX9_URL_USER */
			pComp = &sURI.sUser;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				jx9_result_string(pCtx, pComp->zString, (int)pComp->nByte);
			}
			break;
		case 5: /* JX9_URL_PASS */
			pComp = &sURI.sPass;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				jx9_result_string(pCtx, pComp->zString, (int)pComp->nByte);
			}
			break;
		case 7: /* JX9_URL_QUERY */
			pComp = &sURI.sQuery;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				jx9_result_string(pCtx, pComp->zString, (int)pComp->nByte);
			}
			break;
		case 8: /* JX9_URL_FRAGMENT */
			pComp = &sURI.sFragment;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				jx9_result_string(pCtx, pComp->zString, (int)pComp->nByte);
			}
			break;
		case 6: /*  JX9_URL_PATH */
			pComp = &sURI.sPath;
			if( pComp->nByte < 1 ){
				/* No available value, return NULL */
				jx9_result_null(pCtx);
			}else{
				jx9_result_string(pCtx, pComp->zString, (int)pComp->nByte);
			}
			break;
		default:
			/* No such entry, return NULL */
			jx9_result_null(pCtx);
			break;
		}
	}else{
		jx9_value *pArray, *pValue;
		/* Return an associative array */
		pArray = jx9_context_new_array(pCtx);  /* Empty array */
		pValue = jx9_context_new_scalar(pCtx); /* Array value */
		if( pArray == 0 || pValue == 0 ){
			/* Out of memory */
			jx9_context_throw_error(pCtx, JX9_CTX_ERR, "jx9 engine is running out of memory");
			/* Return false */
			jx9_result_bool(pCtx, 0);
			return JX9_OK;
		}
		/* Fill the array */
		pComp = &sURI.sScheme;
		if( pComp->nByte > 0 ){
			jx9_value_string(pValue, pComp->zString, (int)pComp->nByte);
			jx9_array_add_strkey_elem(pArray, "scheme", pValue); /* Will make it's own copy */
		}
		/* Reset the string cursor */
		jx9_value_reset_string_cursor(pValue);
		pComp = &sURI.sHost;
		if( pComp->nByte > 0 ){
			jx9_value_string(pValue, pComp->zString, (int)pComp->nByte);
			jx9_array_add_strkey_elem(pArray, "host", pValue); /* Will make it's own copy */
		}
		/* Reset the string cursor */
		jx9_value_reset_string_cursor(pValue);
		pComp = &sURI.sPort;
		if( pComp->nByte > 0 ){
			int iPort = 0;/* cc warning */
			/* Convert to integer */
			SyStrToInt32(pComp->zString, pComp->nByte, (void *)&iPort, 0);
			jx9_value_int(pValue, iPort);
			jx9_array_add_strkey_elem(pArray, "port", pValue); /* Will make it's own copy */
		}
		/* Reset the string cursor */
		jx9_value_reset_string_cursor(pValue);
		pComp = &sURI.sUser;
		if( pComp->nByte > 0 ){
			jx9_value_string(pValue, pComp->zString, (int)pComp->nByte);
			jx9_array_add_strkey_elem(pArray, "user", pValue); /* Will make it's own copy */
		}
		/* Reset the string cursor */
		jx9_value_reset_string_cursor(pValue);
		pComp = &sURI.sPass;
		if( pComp->nByte > 0 ){
			jx9_value_string(pValue, pComp->zString, (int)pComp->nByte);
			jx9_array_add_strkey_elem(pArray, "pass", pValue); /* Will make it's own copy */
		}
		/* Reset the string cursor */
		jx9_value_reset_string_cursor(pValue);
		pComp = &sURI.sPath;
		if( pComp->nByte > 0 ){
			jx9_value_string(pValue, pComp->zString, (int)pComp->nByte);
			jx9_array_add_strkey_elem(pArray, "path", pValue); /* Will make it's own copy */
		}
		/* Reset the string cursor */
		jx9_value_reset_string_cursor(pValue);
		pComp = &sURI.sQuery;
		if( pComp->nByte > 0 ){
			jx9_value_string(pValue, pComp->zString, (int)pComp->nByte);
			jx9_array_add_strkey_elem(pArray, "query", pValue); /* Will make it's own copy */
		}
		/* Reset the string cursor */
		jx9_value_reset_string_cursor(pValue);
		pComp = &sURI.sFragment;
		if( pComp->nByte > 0 ){
			jx9_value_string(pValue, pComp->zString, (int)pComp->nByte);
			jx9_array_add_strkey_elem(pArray, "fragment", pValue); /* Will make it's own copy */
		}
		/* Return the created array */
		jx9_result_value(pCtx, pArray);
		/* NOTE:
		 * Don't worry about freeing 'pValue', everything will be released
		 * automatically as soon we return from this function.
		 */
	}
	/* All done */
	return JX9_OK;
}
/*
 * Section:
 *   Array related routines.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 * Status:
 *    Stable.
 * Note 2012-5-21 01:04:15:
 *  Array related functions that need access to the underlying
 *  virtual machine are implemented here rather than 'hashmap.c'
 */
/*
 * The [extract()] function store it's state information in an instance
 * of the following structure.
 */
typedef struct extract_aux_data extract_aux_data;
struct extract_aux_data
{
	jx9_vm *pVm;          /* VM that own this instance */
	int iCount;           /* Number of variables successfully imported  */
	const char *zPrefix;  /* Prefix name */
	int Prefixlen;        /* Prefix  length */
	int iFlags;           /* Control flags */
	char zWorker[1024];   /* Working buffer */
};
/* Forward declaration */
static int VmExtractCallback(jx9_value *pKey, jx9_value *pValue, void *pUserData);
/*
 * int extract(array $var_array[, int $extract_type = EXTR_OVERWRITE[, string $prefix = NULL ]])
 *   Import variables into the current symbol table from an array.
 * Parameters
 * $var_array
 *  An associative array. This function treats keys as variable names and values
 *  as variable values. For each key/value pair it will create a variable in the current symbol
 *  table, subject to extract_type and prefix parameters.
 *  You must use an associative array; a numerically indexed array will not produce results
 *  unless you use EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID.
 * $extract_type
 *  The way invalid/numeric keys and collisions are treated is determined by the extract_type.
 *  It can be one of the following values:
 *   EXTR_OVERWRITE
 *       If there is a collision, overwrite the existing variable. 
 *   EXTR_SKIP
 *       If there is a collision, don't overwrite the existing variable. 
 *   EXTR_PREFIX_SAME
 *       If there is a collision, prefix the variable name with prefix. 
 *   EXTR_PREFIX_ALL
 *       Prefix all variable names with prefix. 
 *   EXTR_PREFIX_INVALID
 *       Only prefix invalid/numeric variable names with prefix. 
 *   EXTR_IF_EXISTS
 *       Only overwrite the variable if it already exists in the current symbol table
 *       otherwise do nothing.
 *       This is useful for defining a list of valid variables and then extracting only those
 *       variables you have defined out of $_REQUEST, for example. 
 *   EXTR_PREFIX_IF_EXISTS
 *       Only create prefixed variable names if the non-prefixed version of the same variable exists in 
 *      the current symbol table.
 * $prefix
 *  Note that prefix is only required if extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL
 *  EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the prefixed result is not a valid variable name
 *  it is not imported into the symbol table. Prefixes are automatically separated from the array key by an
 *  underscore character.
 * Return
 *   Returns the number of variables successfully imported into the symbol table.
 */
static int vm_builtin_extract(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	extract_aux_data sAux;
	jx9_hashmap *pMap;
	if( nArg < 1 || !jx9_value_is_json_array(apArg[0]) ){
		/* Missing/Invalid arguments, return 0 */
		jx9_result_int(pCtx, 0);
		return JX9_OK;
	}
	/* Point to the target hashmap */
	pMap = (jx9_hashmap *)apArg[0]->x.pOther;
	if( pMap->nEntry < 1 ){
		/* Empty map, return  0 */
		jx9_result_int(pCtx, 0);
		return JX9_OK;
	}
	/* Prepare the aux data */
	SyZero(&sAux, sizeof(extract_aux_data)-sizeof(sAux.zWorker));
	if( nArg > 1 ){
		sAux.iFlags = jx9_value_to_int(apArg[1]);
		if( nArg > 2 ){
			sAux.zPrefix = jx9_value_to_string(apArg[2], &sAux.Prefixlen);
		}
	}
	sAux.pVm = pCtx->pVm;
	/* Invoke the worker callback */
	jx9HashmapWalk(pMap, VmExtractCallback, &sAux);
	/* Number of variables successfully imported */
	jx9_result_int(pCtx, sAux.iCount);
	return JX9_OK;
}
/*
 * Worker callback for the [extract()] function defined
 * below.
 */
static int VmExtractCallback(jx9_value *pKey, jx9_value *pValue, void *pUserData)
{
	extract_aux_data *pAux = (extract_aux_data *)pUserData;
	int iFlags = pAux->iFlags;
	jx9_vm *pVm = pAux->pVm;
	jx9_value *pObj;
	SyString sVar;
	if( (iFlags & 0x10/* EXTR_PREFIX_INVALID */) && (pKey->iFlags & (MEMOBJ_INT|MEMOBJ_BOOL|MEMOBJ_REAL))){
		iFlags |= 0x08; /*EXTR_PREFIX_ALL*/
	}
	/* Perform a string cast */
	jx9MemObjToString(pKey);
	if( SyBlobLength(&pKey->sBlob) < 1 ){
		/* Unavailable variable name */
		return SXRET_OK;
	}
	sVar.nByte = 0; /* cc warning */
	if( (iFlags & 0x08/*EXTR_PREFIX_ALL*/ ) && pAux->Prefixlen > 0 ){
		sVar.nByte = (sxu32)SyBufferFormat(pAux->zWorker, sizeof(pAux->zWorker), "%.*s_%.*s", 
			pAux->Prefixlen, pAux->zPrefix, 
			SyBlobLength(&pKey->sBlob), SyBlobData(&pKey->sBlob)
			);
	}else{
		sVar.nByte = (sxu32) SyMemcpy(SyBlobData(&pKey->sBlob), pAux->zWorker, 
			SXMIN(SyBlobLength(&pKey->sBlob), sizeof(pAux->zWorker)));
	}
	sVar.zString = pAux->zWorker;
	/* Try to extract the variable */
	pObj = VmExtractMemObj(pVm, &sVar, TRUE, FALSE);
	if( pObj ){
		/* Collision */
		if( iFlags & 0x02 /* EXTR_SKIP */ ){
			return SXRET_OK;
		}
		if( iFlags & 0x04 /* EXTR_PREFIX_SAME */ ){
			if( (iFlags & 0x08/*EXTR_PREFIX_ALL*/) || pAux->Prefixlen < 1){
				/* Already prefixed */
				return SXRET_OK;
			}
			sVar.nByte = SyBufferFormat(
				pAux->zWorker, sizeof(pAux->zWorker),
				"%.*s_%.*s", 
				pAux->Prefixlen, pAux->zPrefix, 
				SyBlobLength(&pKey->sBlob), SyBlobData(&pKey->sBlob)
				);
			pObj = VmExtractMemObj(pVm, &sVar, TRUE, TRUE);
		}
	}else{
		/* Create the variable */
		pObj = VmExtractMemObj(pVm, &sVar, TRUE, TRUE);
	}
	if( pObj ){
		/* Overwrite the old value */
		jx9MemObjStore(pValue, pObj);
		/* Increment counter */
		pAux->iCount++;
	}
	return SXRET_OK;
}
/*
 * Compile and evaluate a JX9 chunk at run-time.
 * Refer to the include language construct implementation for more
 * information.
 */
static sxi32 VmEvalChunk(
	jx9_vm *pVm,        /* Underlying Virtual Machine */
	jx9_context *pCtx,  /* Call Context */
	SyString *pChunk,   /* JX9 chunk to evaluate */ 
	int iFlags,         /* Compile flag */
	int bTrueReturn     /* TRUE to return execution result */
	)
{
	SySet *pByteCode, aByteCode;
	ProcConsumer xErr = 0;
	void *pErrData = 0;
	/* Initialize bytecode container */
	SySetInit(&aByteCode, &pVm->sAllocator, sizeof(VmInstr));
	SySetAlloc(&aByteCode, 0x20);
	/* Reset the code generator */
	if( bTrueReturn ){
		/* Included file, log compile-time errors */
		xErr = pVm->pEngine->xConf.xErr;
		pErrData = pVm->pEngine->xConf.pErrData;
	}
	jx9ResetCodeGenerator(pVm, xErr, pErrData);
	/* Swap bytecode container */
	pByteCode = pVm->pByteContainer;
	pVm->pByteContainer = &aByteCode;
	/* Compile the chunk */
	jx9CompileScript(pVm, pChunk, iFlags);
	if( pVm->sCodeGen.nErr > 0 ){
		/* Compilation error, return false */
		if( pCtx ){
			jx9_result_bool(pCtx, 0);
		}
	}else{
		jx9_value sResult; /* Return value */
		if( SXRET_OK != jx9VmEmitInstr(pVm, JX9_OP_DONE, 0, 0, 0, 0) ){
			/* Out of memory */
			if( pCtx ){
				jx9_result_bool(pCtx, 0);
			}
			goto Cleanup;
		}
		if( bTrueReturn ){
			/* Assume a boolean true return value */
			jx9MemObjInitFromBool(pVm, &sResult, 1);
		}else{
			/* Assume a null return value */
			jx9MemObjInit(pVm, &sResult);
		}
		/* Execute the compiled chunk */
		VmLocalExec(pVm, &aByteCode, &sResult);
		if( pCtx ){
			/* Set the execution result */
			jx9_result_value(pCtx, &sResult);
		}
		jx9MemObjRelease(&sResult);
	}
Cleanup:
	/* Cleanup the mess left behind */
	pVm->pByteContainer = pByteCode;
	SySetRelease(&aByteCode);
	return SXRET_OK;
}
/*
 * Check if a file path is already included.
 */
static int VmIsIncludedFile(jx9_vm *pVm, SyString *pFile)
{
	SyString *aEntries;
	sxu32 n;
	aEntries = (SyString *)SySetBasePtr(&pVm->aIncluded);
	/* Perform a linear search */
	for( n = 0 ; n < SySetUsed(&pVm->aIncluded) ; ++n ){
		if( SyStringCmp(pFile, &aEntries[n], SyMemcmp) == 0 ){
			/* Already included */
			return TRUE;
		}
	}
	return FALSE;
}
/*
 * Push a file path in the appropriate VM container.
 */
JX9_PRIVATE sxi32 jx9VmPushFilePath(jx9_vm *pVm, const char *zPath, int nLen, sxu8 bMain, sxi32 *pNew)
{
	SyString sPath;
	char *zDup;
#ifdef __WINNT__
	char *zCur;
#endif
	sxi32 rc;
	if( nLen < 0 ){
		nLen = SyStrlen(zPath);
	}
	/* Duplicate the file path first */
	zDup = SyMemBackendStrDup(&pVm->sAllocator, zPath, nLen);
	if( zDup == 0 ){
		return SXERR_MEM;
	}
#ifdef __WINNT__
	/* Normalize path on windows
	 * Example:
	 *    Path/To/File.jx9
	 * becomes
	 *   path\to\file.jx9
	 */
	zCur = zDup;
	while( zCur[0] != 0 ){
		if( zCur[0] == '/' ){
			zCur[0] = '\\';
		}else if( (unsigned char)zCur[0] < 0xc0 && SyisUpper(zCur[0]) ){
			int c = SyToLower(zCur[0]);
			zCur[0] = (char)c; /* MSVC stupidity */
		}
		zCur++;
	}
#endif
	/* Install the file path */
	SyStringInitFromBuf(&sPath, zDup, nLen);
	if( !bMain ){
		if( VmIsIncludedFile(&(*pVm), &sPath) ){
			/* Already included */
			*pNew = 0;
		}else{
			/* Insert in the corresponding container */
			rc = SySetPut(&pVm->aIncluded, (const void *)&sPath);
			if( rc != SXRET_OK ){
				SyMemBackendFree(&pVm->sAllocator, zDup);
				return rc;
			}
			*pNew = 1;
		}
	}
	SySetPut(&pVm->aFiles, (const void *)&sPath);
	return SXRET_OK;
}
/*
 * Compile and Execute a JX9 script at run-time.
 * SXRET_OK is returned on sucessful evaluation.Any other return values
 * indicates failure.
 * Note that the JX9 script to evaluate can be a local or remote file.In
 * either cases the [jx9StreamReadWholeFile()] function handle all the underlying
 * operations.
 * If the [jJX9_DISABLE_BUILTIN_FUNC] compile-time directive is defined, then
 * this function is a no-op.
 * Refer to the implementation of the include(), import() language
 * constructs for more information.
 */
static sxi32 VmExecIncludedFile(
	 jx9_context *pCtx, /* Call Context */
	 SyString *pPath,   /* Script path or URL*/
	 int IncludeOnce    /* TRUE if called from import() or require_once() */
	 )
{
	sxi32 rc;
#ifndef JX9_DISABLE_BUILTIN_FUNC
	const jx9_io_stream *pStream;
	SyBlob sContents;
	void *pHandle;
	jx9_vm *pVm;
	int isNew;
	/* Initialize fields */
	pVm = pCtx->pVm;
	SyBlobInit(&sContents, &pVm->sAllocator);
	isNew = 0;
	/* Extract the associated stream */
	pStream = jx9VmGetStreamDevice(pVm, &pPath->zString, pPath->nByte);
	/*
	 * Open the file or the URL [i.e: http://jx9.symisc.net/example/hello.jx9.txt"] 
	 * in a read-only mode.
	 */
	pHandle = jx9StreamOpenHandle(pVm, pStream,pPath->zString, JX9_IO_OPEN_RDONLY, TRUE, 0, TRUE, &isNew);
	if( pHandle == 0 ){
		return SXERR_IO;
	}
	rc = SXRET_OK; /* Stupid cc warning */
	if( IncludeOnce && !isNew ){
		/* Already included */
		rc = SXERR_EXISTS;
	}else{
		/* Read the whole file contents */
		rc = jx9StreamReadWholeFile(pHandle, pStream, &sContents);
		if( rc == SXRET_OK ){
			SyString sScript;
			/* Compile and execute the script */
			SyStringInitFromBuf(&sScript, SyBlobData(&sContents), SyBlobLength(&sContents));
			VmEvalChunk(pCtx->pVm, &(*pCtx), &sScript, 0, TRUE);
		}
	}
	/* Pop from the set of included file */
	(void)SySetPop(&pVm->aFiles);
	/* Close the handle */
	jx9StreamCloseHandle(pStream, pHandle);
	/* Release the working buffer */
	SyBlobRelease(&sContents);
#else
	pCtx = 0; /* cc warning */
	pPath = 0;
	IncludeOnce = 0;
	rc = SXERR_IO;
#endif /* JX9_DISABLE_BUILTIN_FUNC */
	return rc;
}
/* * include:
 * According to the JX9 reference manual.
 *  The include() function includes and evaluates the specified file.
 *  Files are included based on the file path given or, if none is given
 *  the include_path specified.If the file isn't found in the include_path
 *  include() will finally check in the calling script's own directory
 *  and the current working directory before failing. The include()
 *  construct will emit a warning if it cannot find a file; this is different
 *  behavior from require(), which will emit a fatal error.
 *  If a path is defined — whether absolute (starting with a drive letter
 *  or \ on Windows, or / on Unix/Linux systems) or relative to the current
 *  directory (starting with . or ..) — the include_path will be ignored altogether.
 *  For example, if a filename begins with ../, the parser will look in the parent
 *  directory to find the requested file.
 *  When a file is included, the code it contains inherits the variable scope
 *  of the line on which the include occurs. Any variables available at that line
 *  in the calling file will be available within the called file, from that point forward.
 *  However, all functions and objectes defined in the included file have the global scope. 
 */
static int vm_builtin_include(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	SyString sFile;
	sxi32 rc;
	if( nArg < 1 ){
		/* Nothing to evaluate, return NULL */
		jx9_result_null(pCtx);
		return SXRET_OK;
	}
	/* File to include */
	sFile.zString = jx9_value_to_string(apArg[0], (int *)&sFile.nByte);
	if( sFile.nByte < 1 ){
		/* Empty string, return NULL */
		jx9_result_null(pCtx);
		return SXRET_OK;
	}
	/* Open, compile and execute the desired script */
	rc = VmExecIncludedFile(&(*pCtx), &sFile, FALSE);
	if( rc != SXRET_OK ){
		/* Emit a warning and return false */
		jx9_context_throw_error_format(pCtx, JX9_CTX_WARNING, "IO error while importing: '%z'", &sFile);
		jx9_result_bool(pCtx, 0);
	}
	return SXRET_OK;
}
/*
 * import:
 *  According to the JX9 reference manual.
 *   The import() statement includes and evaluates the specified file during
 *   the execution of the script. This is a behavior similar to the include() 
 *   statement, with the only difference being that if the code from a file has already
 *   been included, it will not be included again. As the name suggests, it will be included
 *   just once.
 */
static int vm_builtin_import(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	SyString sFile;
	sxi32 rc;
	if( nArg < 1 ){
		/* Nothing to evaluate, return NULL */
		jx9_result_null(pCtx);
		return SXRET_OK;
	}
	/* File to include */
	sFile.zString = jx9_value_to_string(apArg[0], (int *)&sFile.nByte);
	if( sFile.nByte < 1 ){
		/* Empty string, return NULL */
		jx9_result_null(pCtx);
		return SXRET_OK;
	}
	/* Open, compile and execute the desired script */
	rc = VmExecIncludedFile(&(*pCtx), &sFile, TRUE);
	if( rc == SXERR_EXISTS ){
		/* File already included, return TRUE */
		jx9_result_bool(pCtx, 1);
		return SXRET_OK;
	}
	if( rc != SXRET_OK ){
		/* Emit a warning and return false */
		jx9_context_throw_error_format(pCtx, JX9_CTX_WARNING, "IO error while importing: '%z'", &sFile);
		jx9_result_bool(pCtx, 0);
 	}
	return SXRET_OK;
}
/*
 * Section:
 *  Command line arguments processing.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 * Status:
 *    Stable.
 */
/*
 * Check if a short option argument [i.e: -c] is available in the command
 * line string. Return a pointer to the start of the stream on success.
 * NULL otherwise.
 */
static const char * VmFindShortOpt(int c, const char *zIn, const char *zEnd)
{
	while( zIn < zEnd ){
		if( zIn[0] == '-' && &zIn[1] < zEnd && (int)zIn[1] == c ){
			/* Got one */
			return &zIn[1];
		}
		/* Advance the cursor */
		zIn++;
	}
	/* No such option */
	return 0;
}
/*
 * Check if a long option argument [i.e: --opt] is available in the command
 * line string. Return a pointer to the start of the stream on success.
 * NULL otherwise.
 */
static const char * VmFindLongOpt(const char *zLong, int nByte, const char *zIn, const char *zEnd)
{
	const char *zOpt;
	while( zIn < zEnd ){
		if( zIn[0] == '-' && &zIn[1] < zEnd && (int)zIn[1] == '-' ){
			zIn += 2;
			zOpt = zIn;
			while( zIn < zEnd && !SyisSpace(zIn[0]) ){
				if( zIn[0] == '=' /* --opt=val */){
					break;
				}
				zIn++;
			}
			/* Test */
			if( (int)(zIn-zOpt) == nByte && SyMemcmp(zOpt, zLong, nByte) == 0 ){
				/* Got one, return it's value */
				return zIn;
			}

		}else{
			zIn++;
		}
	}
	/* No such option */
	return 0;
}
/*
 * Long option [i.e: --opt] arguments private data structure.
 */
struct getopt_long_opt
{
	const char *zArgIn, *zArgEnd; /* Command line arguments */
	jx9_value *pWorker;  /* Worker variable*/
	jx9_value *pArray;   /* getopt() return value */
	jx9_context *pCtx;   /* Call Context */
};
/* Forward declaration */
static int VmProcessLongOpt(jx9_value *pKey, jx9_value *pValue, void *pUserData);
/*
 * Extract short or long argument option values.
 */
static void VmExtractOptArgValue(
	jx9_value *pArray,  /* getopt() return value */
	jx9_value *pWorker, /* Worker variable */
	const char *zArg,   /* Argument stream */
	const char *zArgEnd, /* End of the argument stream  */
	int need_val,       /* TRUE to fetch option argument */
	jx9_context *pCtx,  /* Call Context */
	const char *zName   /* Option name */)
{
	jx9_value_bool(pWorker, 0);
	if( !need_val ){
		/* 
		 * Option does not need arguments.
		 * Insert the option name and a boolean FALSE.
		 */
		jx9_array_add_strkey_elem(pArray, (const char *)zName, pWorker); /* Will make it's own copy */
	}else{
		const char *zCur;
		/* Extract option argument */
		zArg++;
		if( zArg < zArgEnd && zArg[0] == '=' ){
			zArg++;
		}
		while( zArg < zArgEnd && (unsigned char)zArg[0] < 0xc0 && SyisSpace(zArg[0]) ){
			zArg++;
		}
		if( zArg >= zArgEnd || zArg[0] == '-' ){
			/*
			 * Argument not found.
			 * Insert the option name and a boolean FALSE.
			 */
			jx9_array_add_strkey_elem(pArray, (const char *)zName, pWorker); /* Will make it's own copy */
			return;
		}
		/* Delimit the value */
		zCur = zArg;
		if( zArg[0] == '\'' || zArg[0] == '"' ){
			int d = zArg[0];
			/* Delimt the argument */
			zArg++;
			zCur = zArg;
			while( zArg < zArgEnd ){
				if( zArg[0] == d && zArg[-1] != '\\' ){
					/* Delimiter found, exit the loop  */
					break;
				}
				zArg++;
			}
			/* Save the value */
			jx9_value_string(pWorker, zCur, (int)(zArg-zCur));
			if( zArg < zArgEnd ){ zArg++; }
		}else{
			while( zArg < zArgEnd && !SyisSpace(zArg[0]) ){
				zArg++;
			}
			/* Save the value */
			jx9_value_string(pWorker, zCur, (int)(zArg-zCur));
		}
		/*
		 * Check if we are dealing with multiple values.
		 * If so, create an array to hold them, rather than a scalar variable.
		 */
		while( zArg < zArgEnd && (unsigned char)zArg[0] < 0xc0 && SyisSpace(zArg[0]) ){
			zArg++;
		}
		if( zArg < zArgEnd && zArg[0] != '-' ){
			jx9_value *pOptArg; /* Array of option arguments */
			pOptArg = jx9_context_new_array(pCtx);
			if( pOptArg == 0 ){
				jx9_context_throw_error(pCtx, JX9_CTX_ERR, "JX9 is running out of memory");
			}else{
				/* Insert the first value */
				jx9_array_add_elem(pOptArg, 0, pWorker); /* Will make it's own copy */
				for(;;){
					if( zArg >= zArgEnd || zArg[0] == '-' ){
						/* No more value */
						break;
					}
					/* Delimit the value */
					zCur = zArg;
					if( zArg < zArgEnd && zArg[0] == '\\' ){
						zArg++;
						zCur = zArg;
					}
					while( zArg < zArgEnd && !SyisSpace(zArg[0]) ){
						zArg++;
					}
					/* Reset the string cursor */
					jx9_value_reset_string_cursor(pWorker);
					/* Save the value */
					jx9_value_string(pWorker, zCur, (int)(zArg-zCur));
					/* Insert */
					jx9_array_add_elem(pOptArg, 0, pWorker); /* Will make it's own copy */
					/* Jump trailing white spaces */
					while( zArg < zArgEnd && (unsigned char)zArg[0] < 0xc0 && SyisSpace(zArg[0]) ){
						zArg++;
					}
				}
				/* Insert the option arg array */
				jx9_array_add_strkey_elem(pArray, (const char *)zName, pOptArg); /* Will make it's own copy */
				/* Safely release */
				jx9_context_release_value(pCtx, pOptArg);
			}
		}else{
			/* Single value */
			jx9_array_add_strkey_elem(pArray, (const char *)zName, pWorker); /* Will make it's own copy */
		}
	}
}
/*
 * array getopt(string $options[, array $longopts ])
 *   Gets options from the command line argument list.
 * Parameters
 *  $options
 *   Each character in this string will be used as option characters
 *   and matched against options passed to the script starting with
 *   a single hyphen (-). For example, an option string "x" recognizes
 *   an option -x. Only a-z, A-Z and 0-9 are allowed.
 *  $longopts
 *   An array of options. Each element in this array will be used as option
 *   strings and matched against options passed to the script starting with
 *   two hyphens (--). For example, an longopts element "opt" recognizes an
 *   option --opt. 
 * Return
 *  This function will return an array of option / argument pairs or FALSE
 *  on failure. 
 */
static int vm_builtin_getopt(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const char *zIn, *zEnd, *zArg, *zArgIn, *zArgEnd;
	struct getopt_long_opt sLong;
	jx9_value *pArray, *pWorker;
	SyBlob *pArg;
	int nByte;
	if( nArg < 1 || !jx9_value_is_string(apArg[0]) ){
		/* Missing/Invalid arguments, return FALSE */
		jx9_context_throw_error(pCtx, JX9_CTX_ERR, "Missing/Invalid option arguments");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Extract option arguments */
	zIn  = jx9_value_to_string(apArg[0], &nByte);
	zEnd = &zIn[nByte];
	/* Point to the string representation of the $argv[] array */
	pArg = &pCtx->pVm->sArgv;
	/* Create a new empty array and a worker variable */
	pArray = jx9_context_new_array(pCtx);
	pWorker = jx9_context_new_scalar(pCtx);
	if( pArray == 0 || pWorker == 0 ){
		jx9_context_throw_error(pCtx,JX9_CTX_ERR, "JX9 is running out of memory");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	if( SyBlobLength(pArg) < 1 ){
		/* Empty command line, return the empty array*/
		jx9_result_value(pCtx, pArray);
		/* Everything will be released automatically when we return 
		 * from this function.
		 */
		return JX9_OK;
	}
	zArgIn = (const char *)SyBlobData(pArg);
	zArgEnd = &zArgIn[SyBlobLength(pArg)];
	/* Fill the long option structure */
	sLong.pArray = pArray;
	sLong.pWorker = pWorker;
	sLong.zArgIn =  zArgIn;
	sLong.zArgEnd = zArgEnd;
	sLong.pCtx = pCtx;
	/* Start processing */
	while( zIn < zEnd ){
		int c = zIn[0];
		int need_val = 0;
		/* Advance the stream cursor */
		zIn++;
		/* Ignore non-alphanum characters */
		if( !SyisAlphaNum(c) ){
			continue;
		}
		if( zIn < zEnd && zIn[0] == ':' ){
			zIn++;
			need_val = 1;
			if( zIn < zEnd && zIn[0] == ':' ){
				zIn++;
			}
		}
		/* Find option */
		zArg = VmFindShortOpt(c, zArgIn, zArgEnd);
		if( zArg == 0 ){
			/* No such option */
			continue;
		}
		/* Extract option argument value */
		VmExtractOptArgValue(pArray, pWorker, zArg, zArgEnd, need_val, pCtx, (const char *)&c);	
	}
	if( nArg > 1 && jx9_value_is_json_array(apArg[1]) && jx9_array_count(apArg[1]) > 0 ){
		/* Process long options */
		jx9_array_walk(apArg[1], VmProcessLongOpt, &sLong);
	}
	/* Return the option array */
	jx9_result_value(pCtx, pArray);
	/* 
	 * Don't worry about freeing memory, everything will be released
	 * automatically as soon we return from this foreign function.
	 */
	return JX9_OK;
}
/*
 * Array walker callback used for processing long options values.
 */
static int VmProcessLongOpt(jx9_value *pKey, jx9_value *pValue, void *pUserData)
{
	struct getopt_long_opt *pOpt = (struct getopt_long_opt *)pUserData;
	const char *zArg, *zOpt, *zEnd;
	int need_value = 0;
	int nByte;
	/* Value must be of type string */
	if( !jx9_value_is_string(pValue) ){
		/* Simply ignore */
		return JX9_OK;
	}
	zOpt = jx9_value_to_string(pValue, &nByte);
	if( nByte < 1 ){
		/* Empty string, ignore */
		return JX9_OK;
	}
	zEnd = &zOpt[nByte - 1];
	if( zEnd[0] == ':' ){
		char *zTerm;
		/* Try to extract a value */
		need_value = 1;
		while( zEnd >= zOpt && zEnd[0] == ':' ){
			zEnd--;
		}
		if( zOpt >= zEnd ){
			/* Empty string, ignore */
			SXUNUSED(pKey);
			return JX9_OK;
		}
		zEnd++;
		zTerm = (char *)zEnd;
		zTerm[0] = 0;
	}else{
		zEnd = &zOpt[nByte];
	}
	/* Find the option */
	zArg = VmFindLongOpt(zOpt, (int)(zEnd-zOpt), pOpt->zArgIn, pOpt->zArgEnd);
	if( zArg == 0 ){
		/* No such option, return immediately */
		return JX9_OK;
	}
	/* Try to extract a value */
	VmExtractOptArgValue(pOpt->pArray, pOpt->pWorker, zArg, pOpt->zArgEnd, need_value, pOpt->pCtx, zOpt);
	return JX9_OK;
}
/*
 * int utf8_encode(string $input)
 *  UTF-8 encoding.
 *  This function encodes the string data to UTF-8, and returns the encoded version.
 *  UTF-8 is a standard mechanism used by Unicode for encoding wide character values
 * into a byte stream. UTF-8 is transparent to plain ASCII characters, is self-synchronized
 * (meaning it is possible for a program to figure out where in the bytestream characters start)
 * and can be used with normal string comparison functions for sorting and such.
 *  Notes on UTF-8 (According to SQLite3 authors):
 *  Byte-0    Byte-1    Byte-2    Byte-3    Value
 *  0xxxxxxx                                 00000000 00000000 0xxxxxxx
 *  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
 *  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
 *  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
 * Parameters
 * $input
 *   String to encode or NULL on failure.
 * Return
 *  An UTF-8 encoded string.
 */
static int vm_builtin_utf8_encode(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const unsigned char *zIn, *zEnd;
	int nByte, c, e;
	if( nArg < 1 ){
		/* Missing arguments, return null */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	/* Extract the target string */
	zIn = (const unsigned char *)jx9_value_to_string(apArg[0], &nByte);
	if( nByte < 1 ){
		/* Empty string, return null */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	zEnd = &zIn[nByte];
	/* Start the encoding process */
	for(;;){
		if( zIn >= zEnd ){
			/* End of input */
			break;
		}
		c = zIn[0];
		/* Advance the stream cursor */
		zIn++;
		/* Encode */
		if( c<0x00080 ){
			e = (c&0xFF);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
		}else if( c<0x00800 ){
			e = 0xC0 + ((c>>6)&0x1F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
			e = 0x80 + (c & 0x3F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
		}else if( c<0x10000 ){
			e = 0xE0 + ((c>>12)&0x0F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
			e = 0x80 + ((c>>6) & 0x3F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
			e = 0x80 + (c & 0x3F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
		}else{
			e = 0xF0 + ((c>>18) & 0x07);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
			e = 0x80 + ((c>>12) & 0x3F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
			e = 0x80 + ((c>>6) & 0x3F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
			e = 0x80 + (c & 0x3F);
			jx9_result_string(pCtx, (const char *)&e, (int)sizeof(char));
		} 
	}
	/* All done */
	return JX9_OK;
}
/*
 * UTF-8 decoding routine extracted from the sqlite3 source tree.
 * Original author: D. Richard Hipp (http://www.sqlite.org)
 * Status: Public Domain
 */
/*
** This lookup table is used to help decode the first byte of
** a multi-byte UTF8 character.
*/
static const unsigned char UtfTrans1[] = {
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 
  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, 
};
/*
** Translate a single UTF-8 character.  Return the unicode value.
**
** During translation, assume that the byte that zTerm points
** is a 0x00.
**
** Write a pointer to the next unread byte back into *pzNext.
**
** Notes On Invalid UTF-8:
**
**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
**     be encoded as a multi-byte character.  Any multi-byte character that
**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
**
**  *  This routine never allows a UTF16 surrogate value to be encoded.
**     If a multi-byte character attempts to encode a value between
**     0xd800 and 0xe000 then it is rendered as 0xfffd.
**
**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
**     byte of a character are interpreted as single-byte characters
**     and rendered as themselves even though they are technically
**     invalid characters.
**
**  *  This routine accepts an infinite number of different UTF8 encodings
**     for unicode values 0x80 and greater.  It do not change over-length
**     encodings to 0xfffd as some systems recommend.
*/
#define READ_UTF8(zIn, zTerm, c)                           \
  c = *(zIn++);                                            \
  if( c>=0xc0 ){                                           \
    c = UtfTrans1[c-0xc0];                                 \
    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
      c = (c<<6) + (0x3f & *(zIn++));                      \
    }                                                      \
    if( c<0x80                                             \
        || (c&0xFFFFF800)==0xD800                          \
        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
  }
JX9_PRIVATE int jx9Utf8Read(
  const unsigned char *z,         /* First byte of UTF-8 character */
  const unsigned char *zTerm,     /* Pretend this byte is 0x00 */
  const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
){
  int c;
  READ_UTF8(z, zTerm, c);
  *pzNext = z;
  return c;
}
/*
 * string utf8_decode(string $data)
 *  This function decodes data, assumed to be UTF-8 encoded, to unicode.
 * Parameters
 * data
 *  An UTF-8 encoded string.
 * Return
 *  Unicode decoded string or NULL on failure.
 */
static int vm_builtin_utf8_decode(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const unsigned char *zIn, *zEnd;
	int nByte, c;
	if( nArg < 1 ){
		/* Missing arguments, return null */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	/* Extract the target string */
	zIn = (const unsigned char *)jx9_value_to_string(apArg[0], &nByte);
	if( nByte < 1 ){
		/* Empty string, return null */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	zEnd = &zIn[nByte];
	/* Start the decoding process */
	while( zIn < zEnd ){
		c = jx9Utf8Read(zIn, zEnd, &zIn);
		if( c == 0x0 ){
			break;
		}
		jx9_result_string(pCtx, (const char *)&c, (int)sizeof(char));
	}
	return JX9_OK;
}
/*
 * string json_encode(mixed $value)
 *  Returns a string containing the JSON representation of value.
 * Parameters
 *  $value
 *  The value being encoded. Can be any type except a resource.
 * Return
 *  Returns a JSON encoded string on success. FALSE otherwise
 */
static int vm_builtin_json_encode(jx9_context *pCtx,int nArg,jx9_value **apArg)
{
	SyBlob sBlob;
	if( nArg < 1 ){
		/* Missing arguments, return FALSE */
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Init the working buffer */
	SyBlobInit(&sBlob,&pCtx->pVm->sAllocator);
	/* Perform the encoding operation */
	jx9JsonSerialize(apArg[0],&sBlob);
	/* Return the serialized value */
	jx9_result_string(pCtx,(const char *)SyBlobData(&sBlob),(int)SyBlobLength(&sBlob));
	/* Cleanup */
	SyBlobRelease(&sBlob);
	/* All done */
	return JX9_OK;
}
/*
 * mixed json_decode(string $json)
 *  Takes a JSON encoded string and converts it into a JX9 variable.
 * Parameters
 *  $json
 *    The json string being decoded.
 * Return
 *  The value encoded in json in appropriate JX9 type. Values true, false and null (case-insensitive)
 *  are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded
 *  or if the encoded data is deeper than the recursion limit.
 */
static int vm_builtin_json_decode(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const char *zJSON;
	int nByte;
	if( nArg < 1 || !jx9_value_is_string(apArg[0]) ){
		/* Missing/Invalid arguments, return NULL */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	/* Extract the JSON string */
	zJSON = jx9_value_to_string(apArg[0], &nByte);
	if( nByte < 1 ){
		/* Empty string, return NULL */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	/* Decode the raw JSON */
	jx9JsonDecode(pCtx,zJSON,nByte);
	return JX9_OK;
}
/* Table of built-in VM functions. */
static const jx9_builtin_func aVmFunc[] = {
	     /* JSON Encoding/Decoding */
	{ "json_encode",     vm_builtin_json_encode   },
	{ "json_decode",     vm_builtin_json_decode   },
	     /* Functions calls */
	{ "func_num_args"  , vm_builtin_func_num_args }, 
	{ "func_get_arg"   , vm_builtin_func_get_arg  }, 
	{ "func_get_args"  , vm_builtin_func_get_args }, 
	{ "function_exists", vm_builtin_func_exists   }, 
	{ "is_callable"    , vm_builtin_is_callable   }, 
	{ "get_defined_functions", vm_builtin_get_defined_func },  
	    /* Constants management */
	{ "defined",  vm_builtin_defined              }, 
	{ "get_defined_constants", vm_builtin_get_defined_constants }, 
	   /* Random numbers/strings generators */
	{ "rand",          vm_builtin_rand            }, 
	{ "rand_str",      vm_builtin_rand_str        }, 
	{ "getrandmax",    vm_builtin_getrandmax      }, 
	   /* Language constructs functions */
	{ "print", vm_builtin_print                   }, 
	{ "exit",  vm_builtin_exit                    }, 
	{ "die",   vm_builtin_exit                    },  
	  /* Variable handling functions */ 
	{ "gettype",   vm_builtin_gettype              }, 
	{ "get_resource_type", vm_builtin_get_resource_type},
	 /* Variable dumping */
	{ "dump",     vm_builtin_dump                 },
	  /* Release info */
	{"jx9_version",       vm_builtin_jx9_version  }, 
	{"jx9_credits",       vm_builtin_jx9_version  }, 
	{"jx9_info",          vm_builtin_jx9_version  },
	{"jx9_copyright",     vm_builtin_jx9_version  }, 
	  /* hashmap */
	{"extract",          vm_builtin_extract       }, 
	  /* URL related function */
	{"parse_url",        vm_builtin_parse_url     }, 
	   /* UTF-8 encoding/decoding */
	{"utf8_encode",    vm_builtin_utf8_encode}, 
	{"utf8_decode",    vm_builtin_utf8_decode}, 
	   /* Command line processing */
	{"getopt",         vm_builtin_getopt     }, 
	   /* Files/URI inclusion facility */
	{ "include",      vm_builtin_include          }, 
	{ "import", vm_builtin_import     }
};
/*
 * Register the built-in VM functions defined above.
 */
static sxi32 VmRegisterSpecialFunction(jx9_vm *pVm)
{
	sxi32 rc;
	sxu32 n;
	for( n = 0 ; n < SX_ARRAYSIZE(aVmFunc) ; ++n ){
		/* Note that these special functions have access
		 * to the underlying virtual machine as their
		 * private data.
		 */
		rc = jx9_create_function(&(*pVm), aVmFunc[n].zName, aVmFunc[n].xFunc, &(*pVm));
		if( rc != SXRET_OK ){
			return rc;
		}
	}
	return SXRET_OK;
}
#ifndef JX9_DISABLE_BUILTIN_FUNC
/*
 * Extract the IO stream device associated with a given scheme.
 * Return a pointer to an instance of jx9_io_stream when the scheme
 * have an associated IO stream registered with it. NULL otherwise.
 * If no scheme:// is avalilable then the file:// scheme is assumed.
 * For more information on how to register IO stream devices, please
 * refer to the official documentation.
 */
JX9_PRIVATE const jx9_io_stream * jx9VmGetStreamDevice(
	jx9_vm *pVm,           /* Target VM */
	const char **pzDevice, /* Full path, URI, ... */
	int nByte              /* *pzDevice length*/
	)
{
	const char *zIn, *zEnd, *zCur, *zNext;
	jx9_io_stream **apStream, *pStream;
	SyString sDev, sCur;
	sxu32 n, nEntry;
	int rc;
	/* Check if a scheme [i.e: file://, http://, zip://...] is available */
	zNext = zCur = zIn = *pzDevice;
	zEnd = &zIn[nByte];
	while( zIn < zEnd ){
		if( zIn < &zEnd[-3]/*://*/ && zIn[0] == ':' && zIn[1] == '/' && zIn[2] == '/' ){
			/* Got one */
			zNext = &zIn[sizeof("://")-1];
			break;
		}
		/* Advance the cursor */
		zIn++;
	}
	if( zIn >= zEnd ){
		/* No such scheme, return the default stream */
		return pVm->pDefStream;
	}
	SyStringInitFromBuf(&sDev, zCur, zIn-zCur);
	/* Remove leading and trailing white spaces */
	SyStringFullTrim(&sDev);
	/* Perform a linear lookup on the installed stream devices */
	apStream = (jx9_io_stream **)SySetBasePtr(&pVm->aIOstream);
	nEntry = SySetUsed(&pVm->aIOstream);
	for( n = 0 ; n < nEntry ; n++ ){
		pStream = apStream[n];
		SyStringInitFromBuf(&sCur, pStream->zName, SyStrlen(pStream->zName));
		/* Perfrom a case-insensitive comparison */
		rc = SyStringCmp(&sDev, &sCur, SyStrnicmp);
		if( rc == 0 ){
			/* Stream device found */
			*pzDevice = zNext;
			return pStream;
		}
	}
	/* No such stream, return NULL */
	return 0;
}
#endif /* JX9_DISABLE_BUILTIN_FUNC */
/*
 * Section:
 *    HTTP/URI related routines.
 * Authors:
 *    Symisc Systems, devel@symisc.net.
 *    Copyright (C) Symisc Systems, http://jx9.symisc.net
 * Status:
 *    Stable.
 */ 
 /*
  * URI Parser: Split an URI into components [i.e: Host, Path, Query, ...].
  * URI syntax: [method:/][/[user[:pwd]@]host[:port]/][document]
  * This almost, but not quite, RFC1738 URI syntax.
  * This routine is not a validator, it does not check for validity
  * nor decode URI parts, the only thing this routine does is splitting
  * the input to its fields.
  * Upper layer are responsible of decoding and validating URI parts.
  * On success, this function populate the "SyhttpUri" structure passed
  * as the first argument. Otherwise SXERR_* is returned when a malformed
  * input is encountered.
  */
 static sxi32 VmHttpSplitURI(SyhttpUri *pOut, const char *zUri, sxu32 nLen)
 {
	 const char *zEnd = &zUri[nLen];
	 sxu8 bHostOnly = FALSE;
	 sxu8 bIPv6 = FALSE	; 
	 const char *zCur;
	 SyString *pComp;
	 sxu32 nPos = 0;
	 sxi32 rc;
	 /* Zero the structure first */
	 SyZero(pOut, sizeof(SyhttpUri));
	 /* Remove leading and trailing white spaces  */
	 SyStringInitFromBuf(&pOut->sRaw, zUri, nLen);
	 SyStringFullTrim(&pOut->sRaw);
	 /* Find the first '/' separator */
	 rc = SyByteFind(zUri, (sxu32)(zEnd - zUri), '/', &nPos);
	 if( rc != SXRET_OK ){
		 /* Assume a host name only */
		 zCur = zEnd;
		 bHostOnly = TRUE;
		 goto ProcessHost;
	 }
	 zCur = &zUri[nPos];
	 if( zUri != zCur && zCur[-1] == ':' ){
		 /* Extract a scheme:
		  * Not that we can get an invalid scheme here.
		  * Fortunately the caller can discard any URI by comparing this scheme with its 
		  * registered schemes and will report the error as soon as his comparison function
		  * fail.
		  */
	 	pComp = &pOut->sScheme;
		SyStringInitFromBuf(pComp, zUri, (sxu32)(zCur - zUri - 1));
		SyStringLeftTrim(pComp);		
	 }
	 if( zCur[1] != '/' ){
		 if( zCur == zUri || zCur[-1] == ':' ){
		  /* No authority */
		  goto PathSplit;
		}
		 /* There is something here , we will assume its an authority
		  * and someone has forgot the two prefix slashes "//", 
		  * sooner or later we will detect if we are dealing with a malicious
		  * user or not, but now assume we are dealing with an authority
		  * and let the caller handle all the validation process.
		  */
		 goto ProcessHost;
	 }	 
	 zUri = &zCur[2];
	 zCur = zEnd;
	 rc = SyByteFind(zUri, (sxu32)(zEnd - zUri), '/', &nPos);
	 if( rc == SXRET_OK ){
		 zCur = &zUri[nPos];
	 }
 ProcessHost:
	 /* Extract user information if present */
	 rc = SyByteFind(zUri, (sxu32)(zCur - zUri), '@', &nPos);
	 if( rc == SXRET_OK ){
		 if( nPos > 0 ){
			 sxu32 nPassOfft; /* Password offset */
			 pComp = &pOut->sUser;
			 SyStringInitFromBuf(pComp, zUri, nPos);
			 /* Extract the password if available */
			 rc = SyByteFind(zUri, (sxu32)(zCur - zUri), ':', &nPassOfft);
			 if( rc == SXRET_OK && nPassOfft < nPos){
				 pComp->nByte = nPassOfft;
				 pComp = &pOut->sPass;
				 pComp->zString = &zUri[nPassOfft+sizeof(char)];
				 pComp->nByte = nPos - nPassOfft - 1;
			 }
			 /* Update the cursor */
			 zUri = &zUri[nPos+1];
		 }else{
			 zUri++;
		 }
	 }
	 pComp = &pOut->sHost;
	 while( zUri < zCur && SyisSpace(zUri[0])){
		 zUri++;
	 }	
	 SyStringInitFromBuf(pComp, zUri, (sxu32)(zCur - zUri));
	 if( pComp->zString[0] == '[' ){
		 /* An IPv6 Address: Make a simple naive test
		  */
		 zUri++; pComp->zString++; pComp->nByte = 0;
		 while( ((unsigned char)zUri[0] < 0xc0 && SyisHex(zUri[0])) || zUri[0] == ':' ){
			 zUri++; pComp->nByte++;
		 }
		 if( zUri[0] != ']' ){
			 return SXERR_CORRUPT; /* Malformed IPv6 address */
		 }
		 zUri++;
		 bIPv6 = TRUE;
	 }
	 /* Extract a port number if available */
	 rc = SyByteFind(zUri, (sxu32)(zCur - zUri), ':', &nPos);
	 if( rc == SXRET_OK ){
		 if( bIPv6 == FALSE ){
			 pComp->nByte = (sxu32)(&zUri[nPos] - zUri);
		 }
		 pComp = &pOut->sPort;
		 SyStringInitFromBuf(pComp, &zUri[nPos+1], (sxu32)(zCur - &zUri[nPos+1]));	
	 }
	 if( bHostOnly == TRUE ){
		 return SXRET_OK;
	 }
PathSplit:
	 zUri = zCur;
	 pComp = &pOut->sPath;
	 SyStringInitFromBuf(pComp, zUri, (sxu32)(zEnd-zUri));
	 if( pComp->nByte == 0 ){
		 return SXRET_OK; /* Empty path */
	 }
	 if( SXRET_OK == SyByteFind(zUri, (sxu32)(zEnd-zUri), '?', &nPos) ){
		 pComp->nByte = nPos; /* Update path length */
		 pComp = &pOut->sQuery;
		 SyStringInitFromBuf(pComp, &zUri[nPos+1], (sxu32)(zEnd-&zUri[nPos+1]));
	 }
	 if( SXRET_OK == SyByteFind(zUri, (sxu32)(zEnd-zUri), '#', &nPos) ){
		 /* Update path or query length */
		 if( pComp == &pOut->sPath ){
			 pComp->nByte = nPos;
		 }else{
			 if( &zUri[nPos] < (char *)SyStringData(pComp) ){
				 /* Malformed syntax : Query must be present before fragment */
				 return SXERR_SYNTAX;
			 }
			 pComp->nByte -= (sxu32)(zEnd - &zUri[nPos]);
		 }
		 pComp = &pOut->sFragment;
		 SyStringInitFromBuf(pComp, &zUri[nPos+1], (sxu32)(zEnd-&zUri[nPos+1]))
	 }
	 return SXRET_OK;
 }
 /*
 * Extract a single line from a raw HTTP request.
 * Return SXRET_OK on success, SXERR_EOF when end of input
 * and SXERR_MORE when more input is needed.
 */
static sxi32 VmGetNextLine(SyString *pCursor, SyString *pCurrent)
{ 
  	const char *zIn;
  	sxu32 nPos; 
	/* Jump leading white spaces */
	SyStringLeftTrim(pCursor);
	if( pCursor->nByte < 1 ){
		SyStringInitFromBuf(pCurrent, 0, 0);
		return SXERR_EOF; /* End of input */
	}
	zIn = SyStringData(pCursor);
	if( SXRET_OK != SyByteListFind(pCursor->zString, pCursor->nByte, "\r\n", &nPos) ){
		/* Line not found, tell the caller to read more input from source */
		SyStringDupPtr(pCurrent, pCursor);
		return SXERR_MORE;
	}
  	pCurrent->zString = zIn;
  	pCurrent->nByte	= nPos;	
  	/* advance the cursor so we can call this routine again */
  	pCursor->zString = &zIn[nPos];
  	pCursor->nByte -= nPos;
  	return SXRET_OK;
 }
 /*
  * Split a single MIME header into a name value pair. 
  * This function return SXRET_OK, SXERR_CONTINUE on success.
  * Otherwise SXERR_NEXT is returned when a malformed header
  * is encountered.
  * Note: This function handle also mult-line headers.
  */
 static sxi32 VmHttpProcessOneHeader(SyhttpHeader *pHdr, SyhttpHeader *pLast, const char *zLine, sxu32 nLen)
 {
	 SyString *pName;
	 sxu32 nPos;
	 sxi32 rc;
	 if( nLen < 1 ){
		 return SXERR_NEXT;
	 }
	 /* Check for multi-line header */
	if( pLast && (zLine[-1] == ' ' || zLine[-1] == '\t') ){
		SyString *pTmp = &pLast->sValue;
		SyStringFullTrim(pTmp);
		if( pTmp->nByte == 0 ){
			SyStringInitFromBuf(pTmp, zLine, nLen);
		}else{
			/* Update header value length */
			pTmp->nByte = (sxu32)(&zLine[nLen] - pTmp->zString);
		}
		 /* Simply tell the caller to reset its states and get another line */
		 return SXERR_CONTINUE;
	 }
	/* Split the header */
	pName = &pHdr->sName;
	rc = SyByteFind(zLine, nLen, ':', &nPos);
	if(rc != SXRET_OK ){
		return SXERR_NEXT; /* Malformed header;Check the next entry */
	}
	SyStringInitFromBuf(pName, zLine, nPos);
	SyStringFullTrim(pName);
	/* Extract a header value */
	SyStringInitFromBuf(&pHdr->sValue, &zLine[nPos + 1], nLen - nPos - 1);
	/* Remove leading and trailing whitespaces */
	SyStringFullTrim(&pHdr->sValue);
	return SXRET_OK;
 }
 /*
  * Extract all MIME headers associated with a HTTP request.
  * After processing the first line of a HTTP request, the following
  * routine is called in order to extract MIME headers.
  * This function return SXRET_OK on success, SXERR_MORE when it needs
  * more inputs.
  * Note: Any malformed header is simply discarded.
  */
 static sxi32 VmHttpExtractHeaders(SyString *pRequest, SySet *pOut)
 {
	 SyhttpHeader *pLast = 0;
	 SyString sCurrent;
	 SyhttpHeader sHdr;
	 sxu8 bEol;
	 sxi32 rc;
	 if( SySetUsed(pOut) > 0 ){
		 pLast = (SyhttpHeader *)SySetAt(pOut, SySetUsed(pOut)-1);
	 }
	 bEol = FALSE;
	 for(;;){
		 SyZero(&sHdr, sizeof(SyhttpHeader));
		 /* Extract a single line from the raw HTTP request */
		 rc = VmGetNextLine(pRequest, &sCurrent);
		 if(rc != SXRET_OK ){
			 if( sCurrent.nByte < 1 ){
				 break;
			 }
			 bEol = TRUE;
		 }
		 /* Process the header */
		 if( SXRET_OK == VmHttpProcessOneHeader(&sHdr, pLast, sCurrent.zString, sCurrent.nByte)){
			 if( SXRET_OK != SySetPut(pOut, (const void *)&sHdr) ){
				 break;
			 }
			 /* Retrieve the last parsed header so we can handle multi-line header
			  * in case we face one of them.
			  */
			 pLast = (SyhttpHeader *)SySetPeek(pOut);
		 }
		 if( bEol ){
			 break;
		 }
	 } /* for(;;) */
	 return SXRET_OK;
 }
 /*
  * Process the first line of a HTTP request.
  * This routine perform the following operations
  *  1) Extract the HTTP method.
  *  2) Split the request URI to it's fields [ie: host, path, query, ...].
  *  3) Extract the HTTP protocol version.
  */
 static sxi32 VmHttpProcessFirstLine(
	 SyString *pRequest, /* Raw HTTP request */
	 sxi32 *pMethod,     /* OUT: HTTP method */
	 SyhttpUri *pUri,    /* OUT: Parse of the URI */
	 sxi32 *pProto       /* OUT: HTTP protocol */
	 )
 {
	 static const char *azMethods[] = { "get", "post", "head", "put"};
	 static const sxi32 aMethods[]  = { HTTP_METHOD_GET, HTTP_METHOD_POST, HTTP_METHOD_HEAD, HTTP_METHOD_PUT};
	 const char *zIn, *zEnd, *zPtr;
	 SyString sLine;
	 sxu32 nLen;
	 sxi32 rc;
	 /* Extract the first line and update the pointer */
	 rc = VmGetNextLine(pRequest, &sLine);
	 if( rc != SXRET_OK ){
		 return rc;
	 }
	 if ( sLine.nByte < 1 ){
		 /* Empty HTTP request */
		 return SXERR_EMPTY;
	 }
	 /* Delimit the line and ignore trailing and leading white spaces */
	 zIn = sLine.zString;
	 zEnd = &zIn[sLine.nByte];
	 while( zIn < zEnd && (unsigned char)zIn[0] < 0xc0 && SyisSpace(zIn[0]) ){
		 zIn++;
	 }
	 /* Extract the HTTP method */
	 zPtr = zIn;
	 while( zIn < zEnd && !SyisSpace(zIn[0]) ){
		 zIn++;
	 }
	 *pMethod = HTTP_METHOD_OTHR;
	 if( zIn > zPtr ){
		 sxu32 i;
		 nLen = (sxu32)(zIn-zPtr);
		 for( i = 0 ; i < SX_ARRAYSIZE(azMethods) ; ++i ){
			 if( SyStrnicmp(azMethods[i], zPtr, nLen) == 0 ){
				 *pMethod = aMethods[i];
				 break;
			 }
		 }
	 }
	 /* Jump trailing white spaces */
	 while( zIn < zEnd && (unsigned char)zIn[0] < 0xc0 && SyisSpace(zIn[0]) ){
		 zIn++;
	 }
	  /* Extract the request URI */
	 zPtr = zIn;
	 while( zIn < zEnd && !SyisSpace(zIn[0]) ){
		 zIn++;
	 } 
	 if( zIn > zPtr ){
		 nLen = (sxu32)(zIn-zPtr);
		 /* Split raw URI to it's fields */
		 VmHttpSplitURI(pUri, zPtr, nLen);
	 }
	 /* Jump trailing white spaces */
	 while( zIn < zEnd && (unsigned char)zIn[0] < 0xc0 && SyisSpace(zIn[0]) ){
		 zIn++;
	 }
	 /* Extract the HTTP version */
	 zPtr = zIn;
	 while( zIn < zEnd && !SyisSpace(zIn[0]) ){
		 zIn++;
	 }
	 *pProto = HTTP_PROTO_11; /* HTTP/1.1 */
	 rc = 1;
	 if( zIn > zPtr ){
		 rc = SyStrnicmp(zPtr, "http/1.0", (sxu32)(zIn-zPtr));
	 }
	 if( !rc ){
		 *pProto = HTTP_PROTO_10; /* HTTP/1.0 */
	 }
	 return SXRET_OK;
 }
 /*
  * Tokenize, decode and split a raw query encoded as: "x-www-form-urlencoded" 
  * into a name value pair.
  * Note that this encoding is implicit in GET based requests.
  * After the tokenization process, register the decoded queries
  * in the $_GET/$_POST/$_REQUEST superglobals arrays.
  */
 static sxi32 VmHttpSplitEncodedQuery(
	 jx9_vm *pVm,       /* Target VM */
	 SyString *pQuery,  /* Raw query to decode */
	 SyBlob *pWorker,   /* Working buffer */
	 int is_post        /* TRUE if we are dealing with a POST request */
	 )
 {
	 const char *zEnd = &pQuery->zString[pQuery->nByte];
	 const char *zIn = pQuery->zString;
	 jx9_value *pGet, *pRequest;
	 SyString sName, sValue;
	 const char *zPtr;
	 sxu32 nBlobOfft;
	 /* Extract superglobals */
	 if( is_post ){
		 /* $_POST superglobal */
		 pGet = VmExtractSuper(&(*pVm), "_POST", sizeof("_POST")-1);
	 }else{
		 /* $_GET superglobal */
		 pGet = VmExtractSuper(&(*pVm), "_GET", sizeof("_GET")-1);
	 }
	 pRequest = VmExtractSuper(&(*pVm), "_REQUEST", sizeof("_REQUEST")-1);
	 /* Split up the raw query */
	 for(;;){
		 /* Jump leading white spaces */
		 while(zIn < zEnd  && SyisSpace(zIn[0]) ){
			 zIn++;
		 }
		 if( zIn >= zEnd ){
			 break;
		 }
		 zPtr = zIn;
		 while( zPtr < zEnd && zPtr[0] != '=' && zPtr[0] != '&' && zPtr[0] != ';' ){
			 zPtr++;
		 }
		 /* Reset the working buffer */
		 SyBlobReset(pWorker);
		 /* Decode the entry */
		 SyUriDecode(zIn, (sxu32)(zPtr-zIn), jx9VmBlobConsumer, pWorker, TRUE);
		 /* Save the entry */
		 sName.nByte = SyBlobLength(pWorker);
		 sValue.zString = 0;
		 sValue.nByte = 0;
		 if( zPtr < zEnd && zPtr[0] == '=' ){
			 zPtr++;
			 zIn = zPtr;
			 /* Store field value */
			 while( zPtr < zEnd && zPtr[0] != '&' && zPtr[0] != ';' ){
				 zPtr++;
			 }
			 if( zPtr > zIn ){
				 /* Decode the value */
				  nBlobOfft = SyBlobLength(pWorker);
				  SyUriDecode(zIn, (sxu32)(zPtr-zIn), jx9VmBlobConsumer, pWorker, TRUE);
				  sValue.zString = (const char *)SyBlobDataAt(pWorker, nBlobOfft);
				  sValue.nByte = SyBlobLength(pWorker) - nBlobOfft;
				 
			 }
			 /* Synchronize pointers */
			 zIn = zPtr;
		 }
		 sName.zString = (const char *)SyBlobData(pWorker);
		 /* Install the decoded query in the $_GET/$_REQUEST array */
		 if( pGet && (pGet->iFlags & MEMOBJ_HASHMAP) ){
			 VmHashmapInsert((jx9_hashmap *)pGet->x.pOther, 
				 sName.zString, (int)sName.nByte, 
				 sValue.zString, (int)sValue.nByte
				 );
		 }
		 if( pRequest && (pRequest->iFlags & MEMOBJ_HASHMAP) ){
			 VmHashmapInsert((jx9_hashmap *)pRequest->x.pOther, 
				 sName.zString, (int)sName.nByte, 
				 sValue.zString, (int)sValue.nByte
					 );
		 }
		 /* Advance the pointer */
		 zIn = &zPtr[1];
	 }
	/* All done*/
	return SXRET_OK;
 }
 /*
  * Extract MIME header value from the given set.
  * Return header value on success. NULL otherwise.
  */
 static SyString * VmHttpExtractHeaderValue(SySet *pSet, const char *zMime, sxu32 nByte)
 {
	 SyhttpHeader *aMime, *pMime;
	 SyString sMime;
	 sxu32 n;
	 SyStringInitFromBuf(&sMime, zMime, nByte);
	 /* Point to the MIME entries */
	 aMime = (SyhttpHeader *)SySetBasePtr(pSet);
	 /* Perform the lookup */
	 for( n = 0 ; n < SySetUsed(pSet) ; ++n ){
		 pMime = &aMime[n];
		 if( SyStringCmp(&sMime, &pMime->sName, SyStrnicmp) == 0 ){
			 /* Header found, return it's associated value */
			 return &pMime->sValue;
		 }
	 }
	 /* No such MIME header */
	 return 0;
 }
 /*
  * Tokenize and decode a raw "Cookie:" MIME header into a name value pair
  * and insert it's fields [i.e name, value] in the $_COOKIE superglobal.
  */
 static sxi32 VmHttpPorcessCookie(jx9_vm *pVm, SyBlob *pWorker, const char *zIn, sxu32 nByte)
 {
	 const char *zPtr, *zDelimiter, *zEnd = &zIn[nByte];
	 SyString sName, sValue;
	 jx9_value *pCookie;
	 sxu32 nOfft;
	 /* Make sure the $_COOKIE superglobal is available */
	 pCookie = VmExtractSuper(&(*pVm), "_COOKIE", sizeof("_COOKIE")-1);
	 if( pCookie == 0 || (pCookie->iFlags & MEMOBJ_HASHMAP) == 0 ){
		 /* $_COOKIE superglobal not available */
		 return SXERR_NOTFOUND;
	 }	
	 for(;;){
		  /* Jump leading white spaces */
		 while( zIn < zEnd && SyisSpace(zIn[0]) ){
			 zIn++;
		 }
		 if( zIn >= zEnd ){
			 break;
		 }
		  /* Reset the working buffer */
		 SyBlobReset(pWorker);
		 zDelimiter = zIn;
		 /* Delimit the name[=value]; pair */ 
		 while( zDelimiter < zEnd && zDelimiter[0] != ';' ){
			 zDelimiter++;
		 }
		 zPtr = zIn;
		 while( zPtr < zDelimiter && zPtr[0] != '=' ){
			 zPtr++;
		 }
		 /* Decode the cookie */
		 SyUriDecode(zIn, (sxu32)(zPtr-zIn), jx9VmBlobConsumer, pWorker, TRUE);
		 sName.nByte = SyBlobLength(pWorker);
		 zPtr++;
		 sValue.zString = 0;
		 sValue.nByte = 0;
		 if( zPtr < zDelimiter ){
			 /* Got a Cookie value */
			 nOfft = SyBlobLength(pWorker);
			 SyUriDecode(zPtr, (sxu32)(zDelimiter-zPtr), jx9VmBlobConsumer, pWorker, TRUE);
			 SyStringInitFromBuf(&sValue, SyBlobDataAt(pWorker, nOfft), SyBlobLength(pWorker)-nOfft);
		 }
		 /* Synchronize pointers */
		 zIn = &zDelimiter[1];
		 /* Perform the insertion */
		 sName.zString = (const char *)SyBlobData(pWorker);
		 VmHashmapInsert((jx9_hashmap *)pCookie->x.pOther, 
			 sName.zString, (int)sName.nByte, 
			 sValue.zString, (int)sValue.nByte
			 );
	 }
	 return SXRET_OK;
 }
 /*
  * Process a full HTTP request and populate the appropriate arrays
  * such as $_SERVER, $_GET, $_POST, $_COOKIE, $_REQUEST, ... with the information
  * extracted from the raw HTTP request. As an extension Symisc introduced 
  * the $_HEADER array which hold a copy of the processed HTTP MIME headers
  * and their associated values. [i.e: $_HEADER['Server'], $_HEADER['User-Agent'], ...].
  * This function return SXRET_OK on success. Any other return value indicates
  * a malformed HTTP request.
  */
 static sxi32 VmHttpProcessRequest(jx9_vm *pVm, const char *zRequest, int nByte)
 {
	 SyString *pName, *pValue, sRequest; /* Raw HTTP request */
	 jx9_value *pHeaderArray;          /* $_HEADER superglobal (Symisc eXtension to the JX9 specification)*/
	 SyhttpHeader *pHeader;            /* MIME header */
	 SyhttpUri sUri;     /* Parse of the raw URI*/
	 SyBlob sWorker;     /* General purpose working buffer */
	 SySet sHeader;      /* MIME headers set */
	 sxi32 iMethod;      /* HTTP method [i.e: GET, POST, HEAD...]*/
	 sxi32 iVer;         /* HTTP protocol version */
	 sxi32 rc;
	 SyStringInitFromBuf(&sRequest, zRequest, nByte);
	 SySetInit(&sHeader, &pVm->sAllocator, sizeof(SyhttpHeader));
	 SyBlobInit(&sWorker, &pVm->sAllocator);
	 /* Ignore leading and trailing white spaces*/
	 SyStringFullTrim(&sRequest);
	 /* Process the first line */
	 rc = VmHttpProcessFirstLine(&sRequest, &iMethod, &sUri, &iVer);
	 if( rc != SXRET_OK ){
		 return rc;
	 }
	 /* Process MIME headers */
	 VmHttpExtractHeaders(&sRequest, &sHeader);
	 /*
	  * Setup $_SERVER environments 
	  */
	 /* 'SERVER_PROTOCOL': Name and revision of the information protocol via which the page was requested */
	 jx9_vm_config(pVm, 
		 JX9_VM_CONFIG_SERVER_ATTR, 
		 "SERVER_PROTOCOL", 
		 iVer == HTTP_PROTO_10 ? "HTTP/1.0" : "HTTP/1.1", 
		 sizeof("HTTP/1.1")-1
		 );
	 /* 'REQUEST_METHOD':  Which request method was used to access the page */
	 jx9_vm_config(pVm, 
		 JX9_VM_CONFIG_SERVER_ATTR, 
		 "REQUEST_METHOD", 
		 iMethod == HTTP_METHOD_GET ?   "GET" : 
		 (iMethod == HTTP_METHOD_POST ? "POST":
		 (iMethod == HTTP_METHOD_PUT  ? "PUT" :
		 (iMethod == HTTP_METHOD_HEAD ?  "HEAD" : "OTHER"))), 
		 -1 /* Compute attribute length automatically */
		 );
	 if( SyStringLength(&sUri.sQuery) > 0 && iMethod == HTTP_METHOD_GET ){
		 pValue = &sUri.sQuery;
		 /* 'QUERY_STRING': The query string, if any, via which the page was accessed */
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "QUERY_STRING", 
			 pValue->zString, 
			 pValue->nByte
			 );
		 /* Decoded the raw query */
		 VmHttpSplitEncodedQuery(&(*pVm), pValue, &sWorker, FALSE);
	 }
	 /* REQUEST_URI: The URI which was given in order to access this page; for instance, '/index.html' */
	 pValue = &sUri.sRaw;
	 jx9_vm_config(pVm, 
		 JX9_VM_CONFIG_SERVER_ATTR, 
		 "REQUEST_URI", 
		 pValue->zString, 
		 pValue->nByte
		 );
	 /*
	  * 'PATH_INFO'
	  * 'ORIG_PATH_INFO' 
      * Contains any client-provided pathname information trailing the actual script filename but preceding
	  * the query string, if available. For instance, if the current script was accessed via the URL
	  * http://www.example.com/jx9/path_info.jx9/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain
	  * /some/stuff. 
	  */
	 pValue = &sUri.sPath;
	 jx9_vm_config(pVm, 
		 JX9_VM_CONFIG_SERVER_ATTR, 
		 "PATH_INFO", 
		 pValue->zString, 
		 pValue->nByte
		 );
	 jx9_vm_config(pVm, 
		 JX9_VM_CONFIG_SERVER_ATTR, 
		 "ORIG_PATH_INFO", 
		 pValue->zString, 
		 pValue->nByte
		 );
	 /* 'HTTP_ACCEPT': Contents of the Accept: header from the current request, if there is one */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Accept", sizeof("Accept")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_ACCEPT", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	 /* 'HTTP_ACCEPT_CHARSET': Contents of the Accept-Charset: header from the current request, if there is one. */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Accept-Charset", sizeof("Accept-Charset")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_ACCEPT_CHARSET", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	 /* 'HTTP_ACCEPT_ENCODING': Contents of the Accept-Encoding: header from the current request, if there is one. */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Accept-Encoding", sizeof("Accept-Encoding")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_ACCEPT_ENCODING", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	  /* 'HTTP_ACCEPT_LANGUAGE': Contents of the Accept-Language: header from the current request, if there is one */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Accept-Language", sizeof("Accept-Language")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_ACCEPT_LANGUAGE", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	 /* 'HTTP_CONNECTION': Contents of the Connection: header from the current request, if there is one. */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Connection", sizeof("Connection")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_CONNECTION", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	 /* 'HTTP_HOST': Contents of the Host: header from the current request, if there is one. */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Host", sizeof("Host")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_HOST", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	 /* 'HTTP_REFERER': Contents of the Referer: header from the current request, if there is one. */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Referer", sizeof("Referer")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_REFERER", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	 /* 'HTTP_USER_AGENT': Contents of the Referer: header from the current request, if there is one. */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "User-Agent", sizeof("User-Agent")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "HTTP_USER_AGENT", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	  /* 'JX9_AUTH_DIGEST': When doing Digest HTTP authentication this variable is set to the 'Authorization'
	   * header sent by the client (which you should then use to make the appropriate validation).
	   */
	 pValue = VmHttpExtractHeaderValue(&sHeader, "Authorization", sizeof("Authorization")-1);
	 if( pValue ){
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "JX9_AUTH_DIGEST", 
			 pValue->zString, 
			 pValue->nByte
		 );
		 jx9_vm_config(pVm, 
			 JX9_VM_CONFIG_SERVER_ATTR, 
			 "JX9_AUTH", 
			 pValue->zString, 
			 pValue->nByte
		 );
	 }
	 /* Install all clients HTTP headers in the $_HEADER superglobal */
	 pHeaderArray = VmExtractSuper(&(*pVm), "_HEADER", sizeof("_HEADER")-1);
	 /* Iterate throw the available MIME headers*/
	 SySetResetCursor(&sHeader);
	 pHeader = 0; /* stupid cc warning */
	 while( SXRET_OK == SySetGetNextEntry(&sHeader, (void **)&pHeader) ){
		 pName  = &pHeader->sName;
		 pValue = &pHeader->sValue;
		 if( pHeaderArray && (pHeaderArray->iFlags & MEMOBJ_HASHMAP)){
			 /* Insert the MIME header and it's associated value */
			 VmHashmapInsert((jx9_hashmap *)pHeaderArray->x.pOther, 
				 pName->zString, (int)pName->nByte, 
				 pValue->zString, (int)pValue->nByte
				 );
		 }
		 if( pName->nByte == sizeof("Cookie")-1 && SyStrnicmp(pName->zString, "Cookie", sizeof("Cookie")-1) == 0 
			 && pValue->nByte > 0){
				 /* Process the name=value pair and insert them in the $_COOKIE superglobal array */
				 VmHttpPorcessCookie(&(*pVm), &sWorker, pValue->zString, pValue->nByte);
		 }
	 }
	 if( iMethod == HTTP_METHOD_POST ){
		 /* Extract raw POST data */
		 pValue = VmHttpExtractHeaderValue(&sHeader, "Content-Type", sizeof("Content-Type") - 1);
		 if( pValue && pValue->nByte >= sizeof("application/x-www-form-urlencoded") - 1 &&
			 SyMemcmp("application/x-www-form-urlencoded", pValue->zString, pValue->nByte) == 0 ){
				 /* Extract POST data length */
				 pValue = VmHttpExtractHeaderValue(&sHeader, "Content-Length", sizeof("Content-Length") - 1);
				 if( pValue ){
					 sxi32 iLen = 0; /* POST data length */
					 SyStrToInt32(pValue->zString, pValue->nByte, (void *)&iLen, 0);
					 if( iLen > 0 ){
						 /* Remove leading and trailing white spaces */
						 SyStringFullTrim(&sRequest);
						 if( (int)sRequest.nByte > iLen ){
							 sRequest.nByte = (sxu32)iLen;
						 }
						 /* Decode POST data now */
						 VmHttpSplitEncodedQuery(&(*pVm), &sRequest, &sWorker, TRUE);
					 }
				 }
		 }
	 }
	 /* All done, clean-up the mess left behind */
	 SySetRelease(&sHeader);
	 SyBlobRelease(&sWorker);
	 return SXRET_OK;
 }