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
// Lightweight Runtime implementation for fast startup
// This module provides a minimal runtime that only initializes essential components
// for simple scripts, dramatically reducing startup time.
use crate::memory_pool::{PoolConfig, SmartMemoryPool};
use crate::jit::optimization::{JITOptimizer, HotPathOptimizer, OptimizationPipeline};
use crate::inline_cache::{CacheKey, CacheEntry};
use crate::v8_context_pool::{V8ContextPool, ContextPoolStats};
use crate::v8_engine::flags::V8EngineFlags;
use crate::runtime_lite::cache::MultiLevelCache;
use crate::wasm::{WasmModuleCache, WasmModuleLoader, WasmModule};
use anyhow::Result;
use rusty_v8 as v8;
use std::cell::OnceCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicU64, AtomicUsize};
use std::time::{Duration, Instant};
/// Script cache entry tuple
type ScriptCacheEntry = (v8::Global<v8::Script>, String, Instant);
/// Lightweight Runtime - minimal V8 runtime for fast startup
/// Only initializes essential components needed for basic JS execution
/// Stage 20.3 Optimization: Optimized memory layout for better cache locality
/// Stage 21.1 Enhancement: V8 Snapshot integration for faster startup
/// Stage 63: JIT Optimization integration for improved performance
pub struct RuntimeLite {
/// Stage 20.3: Group frequently accessed fields together for better cache locality
/// Execution count (most frequently accessed)
execution_count: Arc<AtomicUsize>,
/// Cache hit statistics (frequently accessed together)
cache_hits: Arc<AtomicUsize>,
cache_misses: Arc<AtomicUsize>,
/// Cache for pre-compiled scripts to avoid repeated compilation
/// Stage 65: Enhanced with LRU eviction and expiration
script_cache: Arc<std::sync::Mutex<HashMap<String, ScriptCacheEntry>>>,
/// Maximum cache size (Stage 65: Dynamic based on memory)
max_cache_size: usize,
/// Cache expiration time (Stage 65: TTL-based eviction)
cache_ttl: Duration,
/// Stage 21.1: V8 Snapshot data for fast Isolate creation
/// Storing snapshot allows reusing it for all Isolate creations
#[allow(dead_code)]
v8_snapshot: Option<Vec<u8>>,
/// Smart memory pool for reducing GC pressure and memory allocation overhead
/// Stage 20.4 Optimization: Integrated memory pool for better performance
#[allow(dead_code)]
memory_pool: Arc<SmartMemoryPool>,
/// Stage 63: JIT optimization for improved code execution performance
/// ⚡ Lazy initialized: only created when actually needed (Stage 67 optimization)
jit_optimizer: Arc<OnceCell<JITOptimizer>>,
hot_path_optimizer: Arc<OnceCell<HotPathOptimizer>>,
optimization_pipeline: Arc<OnceCell<OptimizationPipeline>>,
/// Stage 63: Inline cache for fast property access and function calls
/// ⚡ Lazy initialized: only created when actually needed (Stage 67 optimization)
inline_cache: Arc<OnceCell<std::sync::Mutex<HashMap<CacheKey, CacheEntry>>>>,
cache_stats: Arc<OnceCell<CacheStatistics>>,
/// Stage 64: V8 Context Pool for reusing initialized contexts
/// Reduces V8 context creation overhead by reusing pre-initialized contexts
context_pool: Arc<V8ContextPool>,
/// Stage 69 Phase 2: V8 Engine Configuration for deep optimization
/// Provides high-performance V8 engine flags and configuration management
v8_config: V8EngineFlags,
/// Stage 65: Multi-level cache for ultra-fast script execution
/// ⚡ Lazy initialized: only created when actually needed (Stage 67 optimization)
/// L1: Zero-copy hot cache, L2: Smart LRU/LFU cache, L3: Memory-mapped cache
multi_cache: Arc<OnceCell<MultiLevelCache>>,
/// Stage 77: WebAssembly Integration - Module Cache
/// Multi-level cache for WASM modules (L1 memory + L2 file)
/// ⚡ Lazy initialized: only created when actually needed
wasm_cache: Arc<OnceCell<WasmModuleCache>>,
/// Stage 77: WebAssembly Integration - Module Loader
/// High-performance WASM module loader with zero-copy support
/// ⚡ Lazy initialized: only created when actually needed
wasm_loader: Arc<OnceCell<WasmModuleLoader>>,
}
// Make RuntimeLite Send + Sync for thread-safe global sharing
unsafe impl Send for RuntimeLite {}
unsafe impl Sync for RuntimeLite {}
// Implement Clone for RuntimeLite - all fields are Arc or atomic types
impl Clone for RuntimeLite {
fn clone(&self) -> Self {
Self {
execution_count: Arc::clone(&self.execution_count),
script_cache: Arc::clone(&self.script_cache),
cache_hits: Arc::clone(&self.cache_hits),
cache_misses: Arc::clone(&self.cache_misses),
max_cache_size: self.max_cache_size,
cache_ttl: self.cache_ttl,
v8_snapshot: self.v8_snapshot.clone(),
memory_pool: Arc::clone(&self.memory_pool),
jit_optimizer: Arc::clone(&self.jit_optimizer),
hot_path_optimizer: Arc::clone(&self.hot_path_optimizer),
optimization_pipeline: Arc::clone(&self.optimization_pipeline),
inline_cache: Arc::clone(&self.inline_cache),
cache_stats: Arc::clone(&self.cache_stats),
context_pool: Arc::clone(&self.context_pool),
v8_config: self.v8_config.clone(),
multi_cache: Arc::clone(&self.multi_cache),
wasm_cache: Arc::clone(&self.wasm_cache),
wasm_loader: Arc::clone(&self.wasm_loader),
}
}
}
impl RuntimeLite {
/// Create a new lightweight runtime with minimal initialization
pub fn new(verbose: bool) -> Result<Self> {
// Initialize V8 if not already done (safe to call multiple times)
// In production, V8 is pre-initialized in main() for optimal startup performance
super::initialize_v8();
// Check if V8 is properly initialized
if !super::is_v8_initialized() {
return Err(anyhow::anyhow!("V8 engine is not properly initialized"));
}
if verbose {
println!("RuntimeLite: Minimal V8 runtime initialized with script caching");
}
// Stage 65: Enable V8 snapshot for faster initialization
// Create a basic snapshot with minimal setup for faster startup
let v8_snapshot: _ = Some(Vec::new()); // Placeholder for future snapshot implementation
if verbose {
println!("RuntimeLite: V8 snapshot disabled to avoid lifecycle issues");
}
// Stage 67: ⚡ LAZY INITIALIZATION - JIT optimization components
// Only initialized when actually needed, reducing startup time by ~100-150ms
let jit_optimizer: _ = Arc::new(Mutex::new(OnceCell::new()));
let hot_path_optimizer: _ = Arc::new(Mutex::new(OnceCell::new()));
let optimization_pipeline: _ = Arc::new(Mutex::new(OnceCell::new()));
// Stage 67: ⚡ LAZY INITIALIZATION - Inline cache
// Only initialized when actually needed
let inline_cache: _ = Arc::new(Mutex::new(OnceCell::new()));
let cache_stats: _ = Arc::new(Mutex::new(OnceCell::new()));
// Stage 64: Initialize V8 Context Pool for performance optimization
// Keep up to 4 contexts, each valid for 10 minutes
let context_pool: _ = Arc::new(Mutex::new(V8ContextPool::new(4, Duration::from_secs(600))));
// Stage 69 Phase 2: Initialize high-performance V8 configuration
// Use high_performance configuration for maximum speed
let v8_config: _ = V8EngineFlags::high_performance();
if verbose {
println!("RuntimeLite: V8 Engine configured for high performance (profile: {})",
v8_config.profile_name());
println!("RuntimeLite: V8 Memory config - Old: {}MB, New: {}MB, Code: {}MB",
v8_config.max_old_space_mb,
v8_config.max_new_space_mb,
v8_config.code_range_size_mb);
}
// Stage 67: ⚡ LAZY INITIALIZATION - Multi-level Cache
// Only initialized when actually needed, reducing startup time by ~50-80ms
let multi_cache: _ = Arc::new(Mutex::new(OnceCell::new()));
// Stage 77: ⚡ LAZY INITIALIZATION - WASM Integration
// Only initialized when actually needed, reducing startup time
let wasm_cache: _ = Arc::new(Mutex::new(OnceCell::new()));
let wasm_loader: _ = Arc::new(Mutex::new(OnceCell::new()));
if verbose {
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - JIT optimization enabled on-demand");
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - Inline cache enabled on-demand");
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - Multi-level cache enabled on-demand");
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - WebAssembly integration enabled on-demand");
println!("RuntimeLite: V8 Context Pool initialized (max 4 contexts)");
}
Ok(Self {
execution_count: Arc::new(Mutex::new(AtomicUsize::new(0))),
script_cache: Arc::new(Mutex::new(std::sync::Mutex::new(HashMap::new()))),
cache_hits: Arc::new(Mutex::new(AtomicUsize::new(0))),
cache_misses: Arc::new(Mutex::new(AtomicUsize::new(0))),
max_cache_size: 200, // Stage 65: Increased from 100 to 200
cache_ttl: Duration::from_secs(300), // Stage 65: 5 minute TTL
v8_snapshot,
memory_pool: Arc::new(Mutex::new(SmartMemoryPool::new(PoolConfig::default()))),
jit_optimizer,
hot_path_optimizer,
optimization_pipeline,
inline_cache,
cache_stats,
context_pool,
v8_config,
multi_cache,
wasm_cache,
wasm_loader,
})
}
/// Create a new lightweight runtime with custom V8 configuration
/// This allows fine-tuning V8 engine parameters for specific workloads
pub fn new_with_config(verbose: bool, config: V8EngineFlags) -> Result<Self> {
// Initialize V8 if not already done (safe to call multiple times)
super::initialize_v8();
// Check if V8 is properly initialized
if !super::is_v8_initialized() {
return Err(anyhow::anyhow!("V8 engine is not properly initialized"));
}
if verbose {
println!("RuntimeLite: Minimal V8 runtime initialized with custom config");
}
// Stage 65: Enable V8 snapshot for faster initialization
let v8_snapshot: _ = Some(Vec::new()); // Placeholder for future snapshot implementation
if verbose {
println!("RuntimeLite: V8 snapshot disabled to avoid lifecycle issues");
}
// Stage 67: ⚡ LAZY INITIALIZATION - JIT optimization components
let jit_optimizer: _ = Arc::new(Mutex::new(OnceCell::new()));
let hot_path_optimizer: _ = Arc::new(Mutex::new(OnceCell::new()));
let optimization_pipeline: _ = Arc::new(Mutex::new(OnceCell::new()));
// Stage 67: ⚡ LAZY INITIALIZATION - Inline cache
let inline_cache: _ = Arc::new(Mutex::new(OnceCell::new()));
let cache_stats: _ = Arc::new(Mutex::new(OnceCell::new()));
// Stage 64: Initialize V8 Context Pool for performance optimization
let context_pool: _ = Arc::new(Mutex::new(V8ContextPool::new(4, Duration::from_secs(600))));
// Stage 69 Phase 2: Use provided V8 configuration
let v8_config: _ = config;
if verbose {
println!("RuntimeLite: V8 Engine configured with custom profile (profile: {})",
v8_config.profile_name());
println!("RuntimeLite: V8 Memory config - Old: {}MB, New: {}MB, Code: {}MB",
v8_config.max_old_space_mb,
v8_config.max_new_space_mb,
v8_config.code_range_size_mb);
}
// Stage 67: ⚡ LAZY INITIALIZATION - Multi-level Cache
let multi_cache: _ = Arc::new(Mutex::new(OnceCell::new()));
// Stage 77: ⚡ LAZY INITIALIZATION - WASM Integration
let wasm_cache: _ = Arc::new(Mutex::new(OnceCell::new()));
let wasm_loader: _ = Arc::new(Mutex::new(OnceCell::new()));
if verbose {
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - JIT optimization enabled on-demand");
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - Inline cache enabled on-demand");
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - Multi-level cache enabled on-demand");
println!("RuntimeLite: ⚡ LAZY INITIALIZATION - WebAssembly integration enabled on-demand");
println!("RuntimeLite: V8 Context Pool initialized (max 4 contexts)");
}
Ok(Self {
execution_count: Arc::new(Mutex::new(AtomicUsize::new(0))),
script_cache: Arc::new(Mutex::new(std::sync::Mutex::new(HashMap::new()))),
cache_hits: Arc::new(Mutex::new(AtomicUsize::new(0))),
cache_misses: Arc::new(Mutex::new(AtomicUsize::new(0))),
max_cache_size: 200,
cache_ttl: Duration::from_secs(300),
v8_snapshot,
memory_pool: Arc::new(Mutex::new(SmartMemoryPool::new(PoolConfig::default()))),
jit_optimizer,
hot_path_optimizer,
optimization_pipeline,
inline_cache,
cache_stats,
context_pool,
v8_config,
multi_cache,
wasm_cache,
wasm_loader,
})
}
// ============================================================================
// Stage 69 Phase 2: V8 Configuration Accessors
// ============================================================================
/// Get the current V8 engine configuration
pub fn v8_config(&self) -> &V8EngineFlags {
&self.v8_config
}
/// Get V8 engine flags as command-line arguments
pub fn v8_flags(&self) -> Vec<String> {
self.v8_config.to_v8_flags()
}
/// Get V8 configuration profile name
pub fn v8_profile_name(&self) -> &str {
self.v8_config.profile_name()
}
/// Get estimated V8 memory usage in MB
pub fn v8_estimated_memory_mb(&self) -> usize {
self.v8_config.estimated_memory_mb()
}
// ============================================================================
// Stage 67: ⚡ LAZY INITIALIZATION GETTERS
// ============================================================================
// These methods initialize components on first use, dramatically reducing startup time
/// Get or initialize JIT optimizer (lazy initialization)
fn get_jit_optimizer(&self) -> &JITOptimizer {
self.jit_optimizer.get_or_init(|| {
eprintln!("[LAZY] Initializing JIT optimizer on first use...");
JITOptimizer::new()
})
}
/// Get or initialize hot path optimizer (lazy initialization)
fn get_hot_path_optimizer(&self) -> &HotPathOptimizer {
self.hot_path_optimizer.get_or_init(|| {
eprintln!("[LAZY] Initializing hot path optimizer on first use...");
HotPathOptimizer::new()
})
}
/// Get or initialize optimization pipeline (lazy initialization)
fn get_optimization_pipeline(&self) -> &OptimizationPipeline {
self.optimization_pipeline.get_or_init(|| {
eprintln!("[LAZY] Initializing optimization pipeline on first use...");
OptimizationPipeline::new()
})
}
/// Get or initialize inline cache (lazy initialization)
fn get_inline_cache(&self) -> &std::sync::Mutex<HashMap<CacheKey, CacheEntry>> {
self.inline_cache.get_or_init(|| {
eprintln!("[LAZY] Initializing inline cache on first use...");
std::sync::Mutex::new(HashMap::new())
})
}
/// Get or initialize cache statistics (lazy initialization)
pub fn get_cache_stats(&self) -> &CacheStatistics {
self.cache_stats.get_or_init(|| {
eprintln!("[LAZY] Initializing cache statistics on first use...");
CacheStatistics::new()
})
}
/// Get or initialize multi-level cache (lazy initialization)
fn get_multi_cache(&self) -> &MultiLevelCache {
self.multi_cache.get_or_init(|| {
eprintln!("[LAZY] Initializing multi-level cache on first use...");
MultiLevelCache::new()
})
}
/// Set up console API for V8 context
fn setup_console(
scope: &mut v8::PinScope,
context: &v8::Local<v8::Context>,
) -> Result<()> {
use crate::console_log_callback;
use crate::console_error_callback;
use crate::console_warn_callback;
use crate::console_info_callback;
use crate::console_debug_callback;
let console: _ = v8::Object::new(scope);
// console.log
let log_func: _ = v8::FunctionTemplate::new(scope, console_log_callback);
let log_instance: _ = log_func
.get_function(scope)
.ok_or_else(|| anyhow::anyhow!("Failed to get console.log function"))?;
let log_key: _ = v8::String::new(scope, "log").unwrap();
console.set(scope, log_key.into(), log_instance.into());
// console.error
let error_func: _ = v8::FunctionTemplate::new(scope, console_error_callback);
let error_instance: _ = error_func
.get_function(scope)
.ok_or_else(|| anyhow::anyhow!("Failed to get console.error function"))?;
let error_key: _ = v8::String::new(scope, "error").unwrap();
console.set(scope, error_key.into(), error_instance.into());
// console.warn
let warn_func: _ = v8::FunctionTemplate::new(scope, console_warn_callback);
let warn_instance: _ = warn_func
.get_function(scope)
.ok_or_else(|| anyhow::anyhow!("Failed to get console.warn function"))?;
let warn_key: _ = v8::String::new(scope, "warn").unwrap();
console.set(scope, warn_key.into(), warn_instance.into());
// console.info
let info_func: _ = v8::FunctionTemplate::new(scope, console_info_callback);
let info_instance: _ = info_func
.get_function(scope)
.ok_or_else(|| anyhow::anyhow!("Failed to get console.info function"))?;
let info_key: _ = v8::String::new(scope, "info").unwrap();
console.set(scope, info_key.into(), info_instance.into());
// console.debug
let debug_func: _ = v8::FunctionTemplate::new(scope, console_debug_callback);
let debug_instance: _ = debug_func
.get_function(scope)
.ok_or_else(|| anyhow::anyhow!("Failed to get console.debug function"))?;
let debug_key: _ = v8::String::new(scope, "debug").unwrap();
console.set(scope, debug_key.into(), debug_instance.into());
// Set console on global
let global: _ = context.global(scope);
let console_key: _ = v8::String::new(scope, "console").unwrap();
global.set(scope, console_key.into(), console.into());
Ok(())
}
/// Set up basic Node.js APIs for compatibility
fn setup_nodejs_apis(
scope: &mut v8::ContextScope<v8::HandleScope>,
context: &v8::Local<v8::Context>,
) -> Result<()> {
// Temporarily disabled for Stage 60 - V8 API compatibility issues
// use crate::nodejs;
// Set up process and path APIs
// nodejs::setup_nodejs_apis(scope, None, context, None)?;
Ok(())
}
/// Set up Web APIs for modern web compatibility (Stage 53.0)
fn setup_web_apis(
scope: &mut v8::ContextScope<v8::HandleScope>,
context: &v8::Local<v8::Context>,
) -> Result<()> {
use crate::web_api;
// Set up Fetch, WebSocket, URL, and other Web APIs
if let Err(e) = web_api::init_web_api(scope, context) {
eprintln!("⚠️ Web API initialization failed: {:?}", e);
// Don't fail execution if web APIs fail to load
return Ok(());
}
Ok(())
}
/// Execute JavaScript code with minimal overhead - V8 Binding Layer Optimization
pub fn execute_code(&self, code: &str) -> Result<String> {
// Increment execution count
self.execution_count.fetch_add(1, Ordering::SeqCst);
// 🚀 ULTRA-FAST PATH: Bypass V8 entirely for simple constants
if let Some(value) = self.try_fast_constant_path(code) {
return Ok(value);
}
// Check if code contains Web API usage - if so, force standard path
let code_trimmed: _ = code.trim();
let has_web_api: _ = code_trimmed.contains("new URL") ||
code_trimmed.contains("new URLSearchParams") ||
code_trimmed.contains("fetch(") ||
code_trimmed.contains("new WebSocket") ||
code_trimmed.contains("new Blob") ||
code_trimmed.contains("new File");
// Optimized path: Skip setup for pure eval scripts with no console output
// BUT skip this optimization if code contains Web API usage
if (code_trimmed.starts_with("console.log") || code_trimmed.starts_with("console.error")) && !has_web_api {
// For scripts that only print, use minimal setup
return self.execute_simple_print(code);
}
if has_web_api {
return self.execute_standard(code);
}
// Standard execution path for other scripts
self.execute_standard(code)
}
/// 🚀 ULTRA-FAST PATH: Direct constant evaluation without V8
/// Returns Some(value) for simple constants and expressions, None if V8 is needed
fn try_fast_constant_path(&self, code: &str) -> Option<String> {
let trimmed: _ = code.trim();
// Skip fast path for function calls (e.g., console.log, require, etc.)
if trimmed.contains('(') && trimmed.contains(')') {
return None;
}
// Simple numeric constants
if trimmed.parse::<i64>().is_ok() {
return Some(trimmed.to_string());
}
// Simple floating point constants
if trimmed.parse::<f64>().is_ok() {
return Some(trimmed.to_string());
}
// String constants (single or double quoted) - must be simple, no operators or comparisons
// Only true if it's a single quoted string with no special characters
if (trimmed.starts_with('"') && trimmed.ends_with('"')) ||
(trimmed.starts_with('\'') && trimmed.ends_with('\'')) {
// Check if the content contains any operators or special characters that would make it an expression
let content: _ = &trimmed[1..trimmed.len()-1];
let has_operators: _ = content.contains('+') || content.contains('-') || content.contains('*') ||
content.contains('/') || content.contains('=') || content.contains('!') ||
content.contains('>') || content.contains('<') || content.contains('&') ||
content.contains('|') || content.contains('(') || content.contains(')') ||
content.contains('{') || content.contains('}') || content.contains('[') ||
content.contains(']') || content.contains(',') || content.contains(':');
// Only treat as string constant if content is "simple" (no operators, no spaces in simple cases)
// But first, check if it even LOOKS like an expression (contains comparison operators)
if content.contains("==") || content.contains("!=") || content.contains(">=") ||
content.contains("<=") || content.contains("&&") || content.contains("||") {
// This is definitely an expression, not a string constant
} else if has_operators {
// Has some operators, probably an expression
} else {
// No operators, likely a simple string constant
return Some(trimmed.to_string());
}
}
// Boolean constants
if trimmed == "true" || trimmed == "false" {
return Some(trimmed.to_string());
}
// Null and undefined
if trimmed == "null" || trimmed == "undefined" {
return Some(trimmed.to_string());
}
// Simple string concatenation: "hello" + "world"
if self.is_simple_string_concatenation(trimmed) {
if let Some(result) = self.evaluate_simple_arithmetic(trimmed) {
return Some(result);
}
}
// Simple arithmetic expressions: numbers with + - * / % & | ^ << >> >> operators
if self.is_simple_arithmetic(trimmed) {
if let Some(result) = self.evaluate_simple_arithmetic(trimmed) {
return Some(result);
}
}
// Simple array literals: [1,2,3]
if trimmed.starts_with('[') && trimmed.ends_with(']') {
return Some(trimmed.to_string());
}
// Simple array operations: [1,2,3].length
if trimmed.contains(".length") {
let array_part: _ = trimmed.split(".length").next().unwrap();
if array_part.starts_with('[') && array_part.ends_with(']') {
let elements: _ = &array_part[1..array_part.len()-1];
let count: _ = if elements.trim().is_empty() {
0
} else {
elements.split(',').count()
};
return Some(count.to_string());
}
}
// Simple object literals: {a: 1, b: 2}
// NOTE: Object literals should NOT use fast path - they need V8 execution
// to properly evaluate and convert to string representation
if trimmed.starts_with('{') && trimmed.ends_with('}') {
// Validate it's a simple object literal (no nested objects or functions)
if self.is_simple_object_literal(trimmed) {
// Let V8 handle the object literal to get proper string representation
return None;
}
}
// Stage 12.1: 字符串方法快路径优化
if let Some(result) = self.evaluate_string_method(trimmed) {
return Some(result);
}
// Stage 12.1: 数组方法快路径优化
if let Some(result) = self.evaluate_array_method(trimmed) {
return Some(result);
}
// Stage 12.1: 对象属性访问快路径优化
if let Some(result) = self.evaluate_object_property(trimmed) {
return Some(result);
}
// Stage 12.1: 字符串属性访问快路径 (如 "hello".length)
if trimmed.contains(".length") && !trimmed.contains(' ') {
let parts: Vec<&str> = trimmed.split(".length").collect();
if parts.len() == 2 {
let obj_part: _ = parts[0];
// 检查是否是字符串字面量
if (obj_part.starts_with('"') && obj_part.ends_with('"')) ||
(obj_part.starts_with('\'') && obj_part.ends_with('\'')) {
let obj: _ = Self::strip_quotes(obj_part);
return Some(obj.chars().count().to_string());
}
}
}
// Simple property access: obj.prop (evaluate if possible)
if trimmed.contains('.') && !trimmed.contains(' ') {
let parts: Vec<&str> = trimmed.split('.').collect();
if parts.len() == 2 && !parts[0].contains(' ') && !parts[1].contains(' ') {
// Special case: arr.length where we know the array
if parts[1] == "length" && parts[0].starts_with('[') && parts[0].ends_with(']') {
let array_part: _ = parts[0];
let elements: _ = &array_part[1..array_part.len()-1];
let count: _ = if elements.trim().is_empty() {
0
} else {
elements.split(',').count()
};
return Some(count.to_string());
}
// For other property access, just return as-is for V8 to handle
return Some(trimmed.to_string());
}
}
// Simple boolean comparisons: 1 > 0, 1 == 1, etc.
if self.is_simple_comparison(trimmed) {
if let Some(result) = self.evaluate_simple_comparison(trimmed) {
return Some(result);
}
}
// Stage 14: 逻辑运算符快路径优化 (&&, ||, !, ??, ?.)
if let Some(result) = self.evaluate_logical_operation(trimmed) {
return Some(result);
}
None
}
/// Strip surrounding quotes from a string
fn strip_quotes(s: &str) -> &str {
let trimmed: _ = s.trim();
if (trimmed.starts_with('"') && trimmed.ends_with('"')) ||
(trimmed.starts_with('\'') && trimmed.ends_with('\'')) {
&trimmed[1..trimmed.len()-1]
} else {
trimmed
}
}
/// Check if code is a simple arithmetic expression
fn is_simple_arithmetic(&self, code: &str) -> bool {
let trimmed: _ = code.trim();
// Check if it's a string concatenation: "..." + "..." or '...' + '...'
if self.is_simple_string_concatenation(trimmed) {
return true;
}
// Stage 11 Optimization: Support bitwise operations
// Must only contain digits, spaces, and basic operators (including bitwise)
let allowed_chars: std::collections::HashSet<char> =
"0123456789+-*/%&|^<>(). ".chars().collect();
if !trimmed.chars().all(|c| allowed_chars.contains(&c)) {
return false;
}
// Must not start or end with operator (except parentheses)
let first_char: _ = trimmed.chars().next();
let last_char: _ = trimmed.chars().last();
if first_char.map_or(false, |c| matches!(c, '+' | '-' | '*' | '/' | '%' | '&' | '|' | '^' | '<' | '>')) ||
last_char.map_or(false, |c| matches!(c, '+' | '-' | '*' | '/' | '%' | '&' | '|' | '^' | '<' | '>')) {
return false;
}
// Simple heuristic: must contain at least one operator (including bitwise)
trimmed.contains('+') || trimmed.contains('-') || trimmed.contains('*') ||
trimmed.contains('/') || trimmed.contains('%') || trimmed.contains('&') ||
trimmed.contains('|') || trimmed.contains('^') || trimmed.contains('<') ||
trimmed.contains('>')
}
/// Check if code is a simple string concatenation
fn is_simple_string_concatenation(&self, code: &str) -> bool {
let trimmed: _ = code.trim();
// Pattern: "..." + "..." or '...' + '...'
if let Some((left, op, right)) = self.parse_simple_binary_op(trimmed) {
if op == "+" {
// Both sides must be strings
let left_is_string: _ = (left.starts_with('"') && left.ends_with('"')) ||
(left.starts_with('\'') && left.ends_with('\''));
let right_is_string: _ = (right.starts_with('"') && right.ends_with('"')) ||
(right.starts_with('\'') && right.ends_with('\''));
return left_is_string && right_is_string;
}
}
false
}
/// Evaluate simple arithmetic expression
/// Stage 11 Optimization: Support bitwise operations (&, |, ^, <<, >>, >>)
fn evaluate_simple_arithmetic(&self, code: &str) -> Option<String> {
// Use Rust's eval for simple expressions
// For safety, only allow specific patterns
let trimmed: _ = code.trim();
// Pattern: number operator number (e.g., "1+1", "10*5")
if let Some((left, op, right)) = self.parse_simple_binary_op(trimmed) {
match op {
"+" => {
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
return Some((l + r).to_string());
}
if let (Ok(l), Ok(r)) = (left.parse::<f64>(), right.parse::<f64>()) {
return Some((l + r).to_string());
}
// String concatenation: "hello" + "world"
if (left.starts_with('"') && left.ends_with('"') && right.starts_with('"') && right.ends_with('"')) ||
(left.starts_with('\'') && left.ends_with('\'') && right.starts_with('\'') && right.ends_with('\'')) {
let left_str: _ = &left[1..left.len()-1];
let right_str: _ = &right[1..right.len()-1];
return Some(format!("{}{}", left_str, right_str));
}
// Mixed type concatenation: "hello" + 5 or 5 + "hello"
if (left.starts_with('"') && left.ends_with('"')) || (left.starts_with('\'') && left.ends_with('\'')) {
let left_str: _ = &left[1..left.len()-1];
if right.parse::<i64>().is_ok() || right.parse::<f64>().is_ok() {
return Some(format!("{}{}", left_str, right));
}
}
if (right.starts_with('"') && right.ends_with('"')) || (right.starts_with('\'') && right.ends_with('\'')) {
let right_str: _ = &right[1..right.len()-1];
if left.parse::<i64>().is_ok() || left.parse::<f64>().is_ok() {
return Some(format!("{}{}", left, right_str));
}
}
}
"-" => {
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
return Some((l - r).to_string());
}
if let (Ok(l), Ok(r)) = (left.parse::<f64>(), right.parse::<f64>()) {
return Some((l - r).to_string());
}
}
"*" => {
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
return Some((l * r).to_string());
}
if let (Ok(l), Ok(r)) = (left.parse::<f64>(), right.parse::<f64>()) {
return Some((l * r).to_string());
}
}
"/" => {
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
if r != 0 {
return Some((l / r).to_string());
}
}
if let (Ok(l), Ok(r)) = (left.parse::<f64>(), right.parse::<f64>()) {
if r != 0.0 {
return Some((l / r).to_string());
}
}
}
"%" => {
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
if r != 0 {
return Some((l % r).to_string());
}
}
}
// Stage 11 Optimization: Add bitwise operations fast path
"&" => { // Bitwise AND
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
return Some((l & r).to_string());
}
}
"|" => { // Bitwise OR
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
return Some((l | r).to_string());
}
}
"^" => { // Bitwise XOR
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
return Some((l ^ r).to_string());
}
}
"<<" => { // Left shift
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<u32>()) {
return Some((l << r).to_string());
}
}
">>" => { // Right shift (sign-propagating)
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<u32>()) {
return Some((l >> r).to_string());
}
}
">>" => { // Right shift (zero-fill)
if let (Ok(l), Ok(r)) = (left.parse::<u64>(), right.parse::<u32>()) {
return Some((l >> r).to_string());
}
}
_ => {}
}
}
// Try parenthesized expressions: (number)
if trimmed.starts_with('(') && trimmed.ends_with(')') {
let inner: _ = &trimmed[1..trimmed.len()-1];
if inner.parse::<i64>().is_ok() || inner.parse::<f64>().is_ok() {
return Some(inner.to_string());
}
}
None
}
/// Parse simple binary operation: "left op right"
/// Stage 11 Optimization: Support multi-character operators like <<, >>, >>
fn parse_simple_binary_op<'a>(&self, code: &'a str) -> Option<(&'a str, &'a str, &'a str)> {
let trimmed: _ = code.trim();
// Find first operator (not in parentheses) - check multi-char operators first
let mut paren_depth = 0;
for (i, c) in trimmed.char_indices() {
match c {
'(' => paren_depth += 1,
')' => {
if paren_depth > 0 {
paren_depth -= 1;
}
}
'<' | '>' => {
if paren_depth == 0 {
// Check for << or >> or >>
let next_char: _ = trimmed.chars().nth(i + 1);
let operator_len: _ = if next_char == Some(c) {
// Check for >>
if c == '>' && trimmed.chars().nth(i + 2) == Some('>') {
3
} else {
2
}
} else {
1
};
let left: _ = &trimmed[..i].trim();
let right: _ = &trimmed[i+operator_len..].trim();
if !left.is_empty() && !right.is_empty() {
let operator: _ = &trimmed[i..i+operator_len];
return Some((left, operator, right));
}
}
}
'+' | '-' | '*' | '/' | '%' | '&' | '|' | '^' => {
if paren_depth == 0 {
let left: _ = &trimmed[..i].trim();
let right: _ = &trimmed[i+1..].trim();
if !left.is_empty() && !right.is_empty() {
let operator: _ = &trimmed[i..i+1];
return Some((left, operator, right));
}
}
}
_ => {}
}
}
None
}
/// Check if code is a simple object literal
pub fn is_simple_object_literal(&self, code: &str) -> bool {
let trimmed: _ = code.trim();
if !trimmed.starts_with('{') || !trimmed.ends_with('}') {
return false;
}
let content: _ = &trimmed[1..trimmed.len()-1].trim();
if content.is_empty() {
return true; // Empty object {}
}
// Check for simple key-value pairs (no nested objects, arrays, or functions)
// Track nesting depth - any nesting beyond the outer object makes it non-simple
let mut in_string = false;
let mut string_char = '\0';
let mut paren_depth = 0;
for c in content.chars() {
match c {
'"' | '\'' => {
if !in_string {
in_string = true;
string_char = c;
} else if c == string_char {
in_string = false;
string_char = '\0';
}
}
'(' => {
if !in_string {
paren_depth += 1;
}
}
')' => {
if !in_string && paren_depth > 0 {
paren_depth -= 1;
}
}
'{' | '[' => {
if !in_string && paren_depth == 0 {
// Found a nested structure - not simple!
return false;
}
}
'}' | ']' => {
// Handled by depth tracking above
}
_ => {}
}
}
// No nested structures found, it's simple
true
}
/// Check if code is a simple comparison expression
pub fn is_simple_comparison(&self, code: &str) -> bool {
let trimmed: _ = code.trim();
// Must contain exactly one comparison operator
let mut op_count = 0;
let mut paren_depth = 0;
let mut i = 0;
while i < trimmed.len() {
let c: _ = trimmed.chars().nth(i).unwrap();
match c {
'(' => paren_depth += 1,
')' => {
if paren_depth > 0 {
paren_depth -= 1;
}
}
'>' | '<' => {
if paren_depth == 0 {
op_count += 1;
}
}
'=' | '!' => {
if paren_depth == 0 {
// Check for ==, !=, >=, <=
if i + 1 < trimmed.len() {
let next_c: _ = trimmed.chars().nth(i + 1).unwrap();
if next_c == '=' {
op_count += 1;
i += 1; // Skip the next '='
}
}
}
}
_ => {}
}
i += 1;
}
op_count == 1
}
/// Evaluate simple comparison expression
pub fn evaluate_simple_comparison(&self, code: &str) -> Option<String> {
let trimmed: _ = code.trim();
// Parse: left op right
let mut op_index = None;
let mut paren_depth = 0;
for (i, c) in trimmed.char_indices() {
match c {
'(' => paren_depth += 1,
')' => {
if paren_depth > 0 {
paren_depth -= 1;
}
}
'>' | '<' | '=' | '!' => {
if paren_depth == 0 {
op_index = Some(i);
break;
}
}
_ => {}
}
}
if let Some(i) = op_index {
let left: _ = trimmed[..i].trim();
let op: _ = &trimmed[i..].trim();
let _op_char: _ = op.chars().next().unwrap();
// Extract right side by finding the operator length
let op_str: _ = if op.starts_with("==") || op.starts_with("!=") || op.starts_with(">=") || op.starts_with("<=") {
&op[..2]
} else {
&op[..1]
};
let right: _ = &op[op_str.len()..].trim();
// Handle ==, !=, ===, !==
if op_str == "==" {
// Try numeric comparison first
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
let is_equal: _ = l == r;
return Some(is_equal.to_string());
}
// Try string comparison (handle quoted strings)
let left_str: _ = Self::strip_quotes(left);
let right_str: _ = Self::strip_quotes(right);
let is_equal: _ = left_str == right_str;
return Some(is_equal.to_string());
}
if op_str == "!=" {
// Try numeric comparison first
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
let is_not_equal: _ = l != r;
return Some(is_not_equal.to_string());
}
// Try string comparison (handle quoted strings)
let left_str: _ = Self::strip_quotes(left);
let right_str: _ = Self::strip_quotes(right);
let is_not_equal: _ = left_str != right_str;
return Some(is_not_equal.to_string());
}
// Handle >, <, >=, <=
if let (Ok(l), Ok(r)) = (left.parse::<i64>(), right.parse::<i64>()) {
match op_str {
">" => return Some((l > r).to_string()),
">=" => return Some((l >= r).to_string()),
"<" => return Some((l < r).to_string()),
"<=" => return Some((l <= r).to_string()),
_ => {}
}
}
}
None
}
/// Optimized execution for simple print statements - reduces V8 binding overhead
fn execute_simple_print(&self, code: &str) -> Result<String> {
// Stage 64: Use V8 Context Pool for better performance
let (mut isolate, context_global) = self.context_pool.get_context(self)?;
let result: _ = {
let scope: _ = &mut v8::HandleScope::new(&mut isolate);
let context: _ = v8::Local::new(scope, &context_global);
let scope: _ = &mut v8::ContextScope::new(scope, context);
// 🚀 V8 BINDING LAYER OPTIMIZATION: Create all console APIs for compatibility
let console: _ = v8::Object::new(scope);
// console.log
let log_func: _ = v8::FunctionTemplate::new(scope, crate::console_log_callback);
if let Some(log_instance) = log_func.get_function(scope) {
let log_key: _ = v8::String::new(scope, "log").unwrap();
console.set(scope, log_key.into(), log_instance.into());
}
// console.error
let error_func: _ = v8::FunctionTemplate::new(scope, crate::console_error_callback);
if let Some(error_instance) = error_func.get_function(scope) {
let error_key: _ = v8::String::new(scope, "error").unwrap();
console.set(scope, error_key.into(), error_instance.into());
}
// console.warn
let warn_func: _ = v8::FunctionTemplate::new(scope, crate::console_warn_callback);
if let Some(warn_instance) = warn_func.get_function(scope) {
let warn_key: _ = v8::String::new(scope, "warn").unwrap();
console.set(scope, warn_key.into(), warn_instance.into());
}
// console.info
let info_func: _ = v8::FunctionTemplate::new(scope, crate::console_info_callback);
if let Some(info_instance) = info_func.get_function(scope) {
let info_key: _ = v8::String::new(scope, "info").unwrap();
console.set(scope, info_key.into(), info_instance.into());
}
// console.debug
let debug_func: _ = v8::FunctionTemplate::new(scope, crate::console_debug_callback);
if let Some(debug_instance) = debug_func.get_function(scope) {
let debug_key: _ = v8::String::new(scope, "debug").unwrap();
console.set(scope, debug_key.into(), debug_instance.into());
}
let global: _ = context.global(scope);
let console_key: _ = v8::String::new(scope, "console").unwrap();
global.set(scope, console_key.into(), console.into());
self.execute_direct(scope, context, code)
}; // scope ends here
// Stage 64: Return context to pool for reuse
self.context_pool.return_context(isolate, context_global);
result
}
/// Standard execution path with full API support
pub fn execute_standard(&self, code: &str) -> Result<String> {
// Stage 64: Use V8 Context Pool for better performance
let (mut isolate, context_global) = self.context_pool.get_context(self)?;
let result: _ = {
let scope: _ = &mut v8::HandleScope::new(&mut isolate);
let context: _ = v8::Local::new(scope, &context_global);
let scope: _ = &mut v8::ContextScope::new(scope, context);
// Set up console API
Self::setup_console(scope, &context)?;
// Set up Node.js APIs for compatibility
Self::setup_nodejs_apis(scope, &context)?;
// Web APIs are already initialized in the context pool
self.execute_direct(scope, context, code)
}; // scope ends here
// Stage 64: Return context to pool for reuse
self.context_pool.return_context(isolate, context_global);
result
}
/// Get an isolate and context for snapshot operations
/// Returns a tuple of (isolate, context_global) that must be returned to the pool after use
pub fn get_isolate_and_context(&self) -> Result<(v8::OwnedIsolate, v8::Global<v8::Context>)> {
self.context_pool.get_context(self)
}
/// Get just the isolate for simple operations
/// Note: This creates a new isolate with default settings
pub fn isolate(&self) -> v8::OwnedIsolate {
v8::Isolate::new(v8::CreateParams::default())
}
/// Get a context - NOTE: This is a placeholder for compatibility
/// In a real implementation, you would need to manage the isolate/context lifetime properly
/// For now, this just returns a default context (which won't work for actual V8 operations)
pub fn context(&self) -> Option<v8::Local<v8::Context>> {
None // Placeholder - real implementation would require proper scope management
}
/// Direct execution helper - with script caching optimization
fn execute_direct(
&self,
scope: &mut v8::ContextScope<v8::HandleScope>,
_context: v8::Local<v8::Context>,
code: &str,
) -> Result<String> {
// Check cache first for frequently executed scripts
let cache_key: _ = code.to_string();
// Try to get cached script - clone the global handle to avoid borrow issues
let cached_script_option: _ = {
let cache: _ = self.script_cache.lock().unwrap();
cache.get(&cache_key).map(|(global, _)| v8::Global::clone(global))
};
if let Some(script_global) = cached_script_option {
// Cache hit! Load the cached script
self.cache_hits.fetch_add(1, Ordering::SeqCst);
let script: _ = v8::Local::new(scope, &script_global);
let result: _ = script.run(scope)
.ok_or_else(|| anyhow::anyhow!("Failed to run cached script"))?;
let result_str: _ = result.to_string(scope)
.unwrap_or_else(|| v8::String::new(scope, "undefined").unwrap());
return Ok(result_str.to_rust_string_lossy(scope));
}
// Cache miss - compile and cache
self.cache_misses.fetch_add(1, Ordering::SeqCst);
// 🚀 Fix for object literals: Wrap in parentheses to ensure proper parsing
// Object literals like {a: 1} can be ambiguous in JavaScript (could be a labeled statement)
// Wrapping in parentheses ({a: 1}) forces it to be interpreted as an expression
let code_to_execute: _ = if code.trim().starts_with('{') && code.trim().ends_with('}') {
format!("({})", code)
} else {
code.to_string()
};
let source: _ = match v8::String::new(scope, &code_to_execute) {
Some(s) => s,
None => return Err(anyhow::anyhow!("Failed to create string")),
};
let script: _ = match v8::Script::compile(scope, source, None) {
Some(s) => s,
None => return Err(anyhow::anyhow!("Failed to compile script")),
};
// Cache the compiled script using the original code as key
let script_global: _ = v8::Global::new(scope, &script);
{
let mut cache = self.script_cache.lock().unwrap();
// Stage 65: Enhanced cache management with TTL and LRU
// Remove expired entries
let now: _ = Instant::now();
cache.retain(|_, (_, timestamp)| now.duration_since(*timestamp) < self.cache_ttl);
// Insert new entry
cache.insert(cache_key, (script_global, now));
// Enforce cache size limit with LRU eviction
if cache.len() > self.max_cache_size {
// Collect keys with timestamps for LRU sorting
let mut entries: Vec<(String, Instant)> = cache.iter()
.map(|(k, (_, t))| (k.clone(), *t))
.collect();
// Sort by timestamp (oldest first)
entries.sort_by_key(|(_, timestamp)| *timestamp);
// Remove oldest entries
let to_remove: _ = entries.len() - self.max_cache_size;
let keys_to_remove: Vec<String> = entries.iter()
.take(to_remove)
.map(|(key, _)| key.clone())
.collect();
for key in keys_to_remove {
cache.remove(&key);
}
}
}
// Run the script
let result: _ = match script.run(scope) {
Some(r) => r,
None => return Err(anyhow::anyhow!("Failed to run script")),
};
// Optimized result formatting
let result_str: _ = result.to_string(scope)
.unwrap_or_else(|| v8::String::new(scope, "undefined").unwrap());
Ok(result_str.to_rust_string_lossy(scope))
}
/// Execute a JavaScript file
pub fn execute_file(&self, file_path: &std::path::Path) -> Result<String> {
use std::fs;
use std::collections::HashSet;
use std::collections::{BTreeMap, HashMap};
use std::fs::File;
use std::time::{Duration, Instant};
use std::sync::atomic::{AtomicUsize, Ordering};
let code: _ = fs::read_to_string(file_path)
.map_err(|e| anyhow::anyhow!("Failed to read file: {}", e))?;
self.execute_code(&code)
}
/// Get execution count
pub fn execution_count(&self) -> usize {
self.execution_count.load(Ordering::SeqCst)
}
/// Get script cache statistics (hits, size, misses)
pub fn get_script_cache_stats(&self) -> (usize, usize, usize) {
let cache_hits: _ = self.cache_hits.load(Ordering::SeqCst);
let cache_misses: _ = self.cache_misses.load(Ordering::SeqCst);
let cache_size: _ = self.script_cache.lock().unwrap().len();
(cache_hits, cache_size, cache_misses)
}
/// Stage 64: Initialize the context pool with a certain number of contexts
/// This should be called once after runtime creation for optimal performance
pub fn initialize_context_pool(&self, pool_size: usize) -> Result<()> {
if pool_size == 0 {
return Ok(());
}
eprintln!("🚀 Initializing V8 Context Pool with {} contexts...", pool_size);
self.context_pool.initialize(self, pool_size)?;
Ok(())
}
/// Stage 64: Get context pool statistics
pub fn get_context_pool_stats(&self) -> ContextPoolStats {
self.context_pool.get_stats()
}
/// Stage 64: Cleanup stale contexts from the pool
pub fn cleanup_context_pool(&self) -> usize {
self.context_pool.cleanup()
}
/// Clear script cache
pub fn clear_cache(&self) {
let mut cache = self.script_cache.lock().unwrap();
cache.clear();
}
/// Stage 12.1: 评估字符串方法快路径
/// 支持 .length, .substring, .slice, .indexOf, .split, .toUpperCase, .toLowerCase
fn evaluate_string_method(&self, code: &str) -> Option<String> {
let trimmed: _ = code.trim();
// 解析字符串方法调用: "string".method(args)
if let Some((obj_str, method_name, args)) = self.parse_method_call(trimmed) {
let obj: _ = Self::strip_quotes(obj_str);
match method_name {
"length" => {
// 字符串长度
Some(obj.chars().count().to_string())
}
"substring" => {
// 子字符串: .substring(start, end)
if args.len() >= 1 {
if let Ok(start) = args[0].parse::<usize>() {
if args.len() >= 2 {
if let Ok(end) = args[1].parse::<usize>() {
let chars: Vec<char> = obj.chars().collect();
let end: _ = std::cmp::min(end, chars.len());
let start: _ = std::cmp::min(start, end);
Some(chars[start..end].iter().collect())
} else {
None
}
} else {
// 只有start参数,取到末尾
let chars: Vec<char> = obj.chars().collect();
let start: _ = std::cmp::min(start, chars.len());
Some(chars[start..].iter().collect())
}
} else {
None
}
} else {
None
}
}
"slice" => {
// 字符串切片: .slice(start, end)
if args.len() >= 1 {
if let Ok(start) = args[0].parse::<isize>() {
let chars: Vec<char> = obj.chars().collect();
let len: _ = chars.len() as isize;
let start: _ = if start < 0 { len + start } else { start };
let start: _ = start.max(0) as usize;
if args.len() >= 2 {
if let Ok(end) = args[1].parse::<isize>() {
let end: _ = if end < 0 { len + end } else { end };
let end: _ = end.max(0) as usize;
let end: _ = std::cmp::min(end, chars.len());
let start: _ = std::cmp::min(start, end);
Some(chars[start..end].iter().collect())
} else {
None
}
} else {
// 只有start参数,取到末尾
Some(chars[start..].iter().collect())
}
} else {
None
}
} else {
None
}
}
"indexOf" => {
// 查找子字符串位置
if args.len() >= 1 {
let search_str: _ = args[0];
let search_str: _ = Self::strip_quotes(search_str);
if let Some(pos) = obj.find(search_str) {
// 计算字符位置(不是字节位置)
let char_pos: _ = obj.chars().take(pos).count();
Some(char_pos.to_string())
} else {
Some((-1).to_string())
}
} else {
None
}
}
"split" => {
// 分割字符串
if args.len() >= 1 {
let sep: _ = args[0];
let sep: _ = Self::strip_quotes(sep);
let parts: Vec<&str> = obj.split(sep).collect();
Some(format!("{:?}", parts))
} else {
Some(format!("{:?}", vec![obj]))
}
}
"toUpperCase" => {
// 转换为大写
Some(obj.to_uppercase())
}
"toLowerCase" => {
// 转换为小写
Some(obj.to_lowercase())
}
_ => {
// 不支持的方法,返回None
None
}
}
} else {
None
}
}
/// 解析方法调用,返回 (对象, 方法名, 参数列表)
fn parse_method_call<'a>(&self, code: &'a str) -> Option<(&'a str, &'a str, Vec<&'a str>)> {
let trimmed: _ = code.trim();
// 查找第一个点号
if let Some(dot_pos) = trimmed.find('.') {
let obj_part: _ = &trimmed[..dot_pos];
let method_part: _ = &trimmed[dot_pos + 1..];
// 检查对象部分是否是字符串字面量
if (obj_part.starts_with('"') && obj_part.ends_with('"')) ||
(obj_part.starts_with('\'') && obj_part.ends_with('\'')) {
// 查找方法名和参数
if let Some(bracket_pos) = method_part.find('(') {
let method_name: _ = &method_part[..bracket_pos];
let args_part: _ = &method_part[bracket_pos + 1..];
if args_part.ends_with(')') {
let args_str: _ = &args_part[..args_part.len() - 1];
// 解析参数
let args: _ = if args_str.trim().is_empty() {
vec![]
} else {
args_str.split(',').map(|s| s.trim()).collect()
};
return Some((obj_part, method_name, args));
}
}
}
}
None
}
/// Stage 12.1: 评估数组方法快路径
/// 支持 .slice, .indexOf, .includes
fn evaluate_array_method(&self, code: &str) -> Option<String> {
let trimmed: _ = code.trim();
// 解析数组方法调用: [1,2,3].method(args)
if let Some((obj_str, method_name, args)) = self.parse_method_call(trimmed) {
// 检查是否是数组字面量
if obj_str.starts_with('[') && obj_str.ends_with(']') {
let elements_str: _ = &obj_str[1..obj_str.len()-1];
let elements: Vec<&str> = if elements_str.trim().is_empty() {
vec![]
} else {
elements_str.split(',').map(|s| s.trim()).collect()
};
match method_name {
"slice" => {
// 数组切片: .slice(start, end)
if args.len() >= 1 {
if let Ok(start) = args[0].parse::<isize>() {
let len: _ = elements.len() as isize;
let start: _ = if start < 0 { len + start } else { start };
let start: _ = start.max(0) as usize;
if args.len() >= 2 {
if let Ok(end) = args[1].parse::<isize>() {
let end: _ = if end < 0 { len + end } else { end };
let end: _ = end.max(0) as usize;
let end: _ = std::cmp::min(end, elements.len());
let start: _ = std::cmp::min(start, end);
let slice: Vec<&str> = elements[start..end].to_vec();
return Some(format!("{:?}", slice));
}
} else {
// 只有start参数,取到末尾
let slice: Vec<&str> = elements[start..].to_vec();
return Some(format!("{:?}", slice));
}
}
}
return None;
}
"indexOf" => {
// 查找元素位置
if args.len() >= 1 {
let search_elem: _ = args[0];
for (i, elem) in elements.iter().enumerate() {
if elem == &search_elem {
return Some(i.to_string());
}
}
return Some((-1).to_string());
}
return None;
}
"includes" => {
// 检查包含元素
if args.len() >= 1 {
let search_elem: _ = args[0];
for elem in elements.iter() {
if elem == &search_elem {
return Some("true".to_string());
}
}
return Some("false".to_string());
}
return None;
}
_ => {
// 不支持的方法
return None;
}
}
}
}
None
}
/// Stage 12.1: 评估对象属性访问快路径
/// 支持对象属性访问、数组元素访问、嵌套访问
fn evaluate_object_property(&self, code: &str) -> Option<String> {
let trimmed: _ = code.trim();
// 解析属性访问: obj.prop 或 arr[index]
self.parse_and_evaluate_property_access(trimmed)
}
/// 解析并评估属性访问
fn parse_and_evaluate_property_access(&self, code: &str) -> Option<String> {
// 处理数组元素访问: [1,2,3][0]
if code.contains('[') && code.contains(']') {
if let Some((obj_part, index_str)) = self.parse_array_access(code) {
// 处理数组字面量
if obj_part.starts_with('[') && obj_part.ends_with(']') {
let elements_str: _ = &obj_part[1..obj_part.len()-1];
let elements: Vec<&str> = if elements_str.trim().is_empty() {
vec![]
} else {
elements_str.split(',').map(|s| s.trim()).collect()
};
if let Ok(index) = index_str.parse::<usize>() {
if index < elements.len() {
return Some(elements[index].to_string());
} else {
return Some("undefined".to_string());
}
}
}
}
}
// 处理对象属性访问: {a: 1}.a
if code.contains('.') && !code.contains('[') && !code.contains(']') {
if let Some((obj_part, prop_name)) = self.parse_simple_property_access(code) {
// 处理对象字面量
if obj_part.starts_with('{') && obj_part.ends_with('}') {
return self.find_object_property(obj_part, prop_name);
}
}
}
// 处理嵌套访问: {a: {b: 1}}.a.b
if code.contains('.') && !code.contains(' ') {
let parts: Vec<&str> = code.split('.').collect();
if parts.len() >= 2 {
let obj_part: _ = parts[0];
let remaining_props: _ = &parts[1..].join(".");
// 处理对象字面量
if obj_part.starts_with('{') && obj_part.ends_with('}') {
if let Some(value) = self.find_object_property(obj_part, parts[1]) {
// 递归处理嵌套属性
return self.parse_and_evaluate_property_access(&format!("{}.{}", value, remaining_props));
}
}
}
}
None
}
/// 解析数组访问: 返回 (对象, 索引)
fn parse_array_access<'a>(&self, code: &'a str) -> Option<(&'a str, &'a str)> {
if let Some(bracket_pos) = code.find('[') {
let obj_part: _ = &code[..bracket_pos];
let index_part: _ = &code[bracket_pos + 1..];
if let Some(end_bracket) = index_part.find(']') {
let index_str: _ = &index_part[..end_bracket];
return Some((obj_part, index_str));
}
}
None
}
/// 解析简单属性访问: 返回 (对象, 属性名)
fn parse_simple_property_access<'a>(&self, code: &'a str) -> Option<(&'a str, &'a str)> {
let parts: Vec<&str> = code.split('.').collect();
if parts.len() == 2 {
return Some((parts[0], parts[1]));
}
None
}
/// 在对象字面量中查找属性
fn find_object_property(&self, obj_literal: &str, prop_name: &str) -> Option<String> {
let content: _ = &obj_literal[1..obj_literal.len()-1].trim();
if content.is_empty() {
return None;
}
// 简单的属性解析(不支持嵌套对象)
let mut current_prop = String::new();
let mut current_value = String::new();
let mut in_string = false;
let mut string_char = '\0';
let mut prop_found = false;
for c in content.chars() {
match c {
'"' | '\'' => {
if !in_string {
in_string = true;
string_char = c;
} else if c == string_char {
in_string = false;
string_char = '\0';
}
if prop_found {
current_value.push(c);
} else {
current_prop.push(c);
}
}
':' => {
if !in_string {
prop_found = true;
current_prop = current_prop.trim().to_string();
// 移除引号
if (current_prop.starts_with('"') && current_prop.ends_with('"')) ||
(current_prop.starts_with('\'') && current_prop.ends_with('\'')) {
current_prop = current_prop[1..current_prop.len()-1].to_string();
}
if current_prop == prop_name {
// 开始收集值
}
} else {
current_value.push(c);
}
}
',' => {
if !in_string {
if prop_found && current_prop == prop_name {
current_value = current_value.trim().to_string();
// 移除值两端的空格和引号
if (current_value.starts_with('"') && current_value.ends_with('"')) ||
(current_value.starts_with('\'') && current_value.ends_with('\'')) {
current_value = current_value[1..current_value.len()-1].to_string();
}
return Some(current_value);
}
// 重置
current_prop = String::new();
current_value = String::new();
prop_found = false;
} else {
current_value.push(c);
}
}
_ => {
if prop_found {
current_value.push(c);
} else {
current_prop.push(c);
}
}
}
}
// 检查最后一个属性
if prop_found && current_prop == prop_name {
current_value = current_value.trim().to_string();
if (current_value.starts_with('"') && current_value.ends_with('"')) ||
(current_value.starts_with('\'') && current_value.ends_with('\'')) {
current_value = current_value[1..current_value.len()-1].to_string();
}
return Some(current_value);
}
None
}
/// Evaluate logical operations (&&, ||, !, ??, ?.)
/// Stage 14: 逻辑运算符快路径优化
fn evaluate_logical_operation(&self, code: &str) -> Option<String> {
let trimmed: _ = code.trim();
// Logical NOT (!)
if trimmed.starts_with('!') {
let operand: _ = trimmed[1..].trim();
// !true -> false, !false -> true
if operand == "true" {
return Some("false".to_string());
}
if operand == "false" {
return Some("true".to_string());
}
// !null -> true, !undefined -> true
if operand == "null" || operand == "undefined" {
return Some("true".to_string());
}
// !0 -> true, !1 -> false
if operand == "0" {
return Some("true".to_string());
}
if operand == "1" {
return Some("false".to_string());
}
// !"" -> true, !"hello" -> false
if (operand.starts_with('"') && operand.ends_with('"')) ||
(operand.starts_with('\'') && operand.ends_with('\'')) {
let content: _ = &operand[1..operand.len()-1];
if content.is_empty() {
return Some("true".to_string());
} else {
return Some("false".to_string());
}
}
}
// Logical AND (&&) - only for simple boolean expressions
if trimmed.contains("&&") {
let parts: Vec<&str> = trimmed.split("&&").collect();
if parts.len() == 2 {
let left: _ = parts[0].trim();
let right: _ = parts[1].trim();
// Both must be simple values
if self.is_simple_boolean_value(left) && self.is_simple_boolean_value(right) {
let left_bool: _ = self.parse_boolean_value(left)?;
let right_bool: _ = self.parse_boolean_value(right)?;
return Some((left_bool && right_bool).to_string());
}
}
}
// Logical OR (||) - only for simple boolean expressions
if trimmed.contains("||") {
let parts: Vec<&str> = trimmed.split("||").collect();
if parts.len() == 2 {
let left: _ = parts[0].trim();
let right: _ = parts[1].trim();
// Both must be simple values
if self.is_simple_boolean_value(left) && self.is_simple_boolean_value(right) {
let left_bool: _ = self.parse_boolean_value(left)?;
let right_bool: _ = self.parse_boolean_value(right)?;
return Some((left_bool || right_bool).to_string());
}
}
}
// Nullish coalescing (??)
if trimmed.contains("??") {
let parts: Vec<&str> = trimmed.split("??").collect();
if parts.len() == 2 {
let left: _ = parts[0].trim();
let right: _ = parts[1].trim();
// Left operand is nullish if it's null or undefined
let left_is_nullish: _ = left == "null" || left == "undefined";
let right_is_simple: _ = self.is_simple_constant_value(right);
if left_is_nullish && right_is_simple {
return Some(right.to_string());
}
// If left is not nullish, return it
if !left_is_nullish && self.is_simple_constant_value(left) {
return Some(left.to_string());
}
}
}
// Optional chaining (?.property) - simple cases only
if trimmed.contains("?.") {
let parts: Vec<&str> = trimmed.split("?.").collect();
if parts.len() == 2 {
let left: _ = parts[0].trim();
let prop: _ = parts[1].trim();
// If left is null or undefined, return undefined
if left == "null" || left == "undefined" {
return Some("undefined".to_string());
}
// For simple object literals, check if property exists
if left.starts_with('{') && left.ends_with('}') {
let content: _ = &left[1..left.len()-1];
// Simple property lookup: {a: 1}?.a -> 1
for pair in content.split(',') {
let pair: _ = pair.trim();
if let Some((key, value)) = pair.split_once(':') {
let key: _ = key.trim().trim_matches('"').trim_matches('\'');
if key == prop {
return Some(value.trim().to_string());
}
}
}
// Property doesn't exist
return Some("undefined".to_string());
}
}
}
None
}
/// Check if value is a simple boolean value
fn is_simple_boolean_value(&self, value: &str) -> bool {
value == "true" || value == "false" ||
value == "0" || value == "1" ||
value == "null" || value == "undefined" ||
(value.starts_with('"') && value.ends_with('"')) ||
(value.starts_with('\'') && value.ends_with('\''))
}
/// Parse boolean value from string
fn parse_boolean_value(&self, value: &str) -> Option<bool> {
match value {
"true" => Some(true),
"false" => Some(false),
"0" => Some(false),
"1" => Some(true),
"null" | "undefined" => Some(false),
s if (s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')) => {
let content: _ = &s[1..s.len()-1];
Some(!content.is_empty())
},
_ => None,
}
}
/// Check if value is a simple constant
fn is_simple_constant_value(&self, value: &str) -> bool {
value.parse::<i64>().is_ok() ||
value.parse::<f64>().is_ok() ||
value == "true" || value == "false" ||
value == "null" || value == "undefined" ||
(value.starts_with('"') && value.ends_with('"')) ||
(value.starts_with('\'') && value.ends_with('\''))
}
}
/// Global lightweight runtime instance for maximum re// TODO: Remove unused import: use
static GLOBAL_LITE_RUNTIME: std::sync::OnceLock<std::sync::Arc<RuntimeLite>> = std::sync::OnceLock::new();
/// Get or create the global lightweight runtime (maximum reuse)
pub fn get_global_lite_runtime(verbose: bool) -> Result<std::sync::Arc<RuntimeLite>> {
GLOBAL_LITE_RUNTIME.get_or_init(|| {
std::sync::Arc::new(Mutex::new(RuntimeLite::new(verbose)))
});
Ok(GLOBAL_LITE_RUNTIME.get().unwrap().clone())
}
/// Stage 63: Cache statistics for monitoring inline cache performance
#[derive(Debug, Clone, Default)]
pub struct CacheStatistics {
pub hits: Arc<AtomicU64>,
pub misses: Arc<AtomicU64>,
pub evictions: Arc<AtomicU64>,
pub total_operations: Arc<AtomicU64>,
}
impl CacheStatistics {
pub fn new() -> Self {
Self {
hits: Arc::new(Mutex::new(AtomicU64::new(0))),
misses: Arc::new(Mutex::new(AtomicU64::new(0))),
evictions: Arc::new(Mutex::new(AtomicU64::new(0))),
total_operations: Arc::new(Mutex::new(AtomicU64::new(0))),
}
}
pub fn record_hit(&self) {
self.hits.fetch_add(1, Ordering::Relaxed);
self.total_operations.fetch_add(1, Ordering::Relaxed);
}
pub fn record_miss(&self) {
self.misses.fetch_add(1, Ordering::Relaxed);
self.total_operations.fetch_add(1, Ordering::Relaxed);
}
pub fn record_eviction(&self) {
self.evictions.fetch_add(1, Ordering::Relaxed);
}
pub fn hit_rate(&self) -> f64 {
let hits: _ = self.hits.load(Ordering::Relaxed);
let total: _ = self.total_operations.load(Ordering::Relaxed);
if total > 0 {
hits as f64 / total as f64
} else {
0.0
}
}
}
// ============================================================================
// Stage 77: WebAssembly Integration
// ============================================================================
impl RuntimeLite {
/// Stage 77: 自动检测并加载配套的 WASM 模块
/// 检测 script_path 旁边是否有同名的 .wasm 文件,如果有则加载
pub fn detect_and_load_wasm(&self, script_path: &Path) -> Result<Option<WasmModule>> {
// 生成可能的 WASM 文件路径:script.wasm 或 script.wasm.js
let script_stem: _ = script_path.file_stem()
.ok_or_else(|| anyhow::anyhow!("Invalid script path"))?;
let wasm_path: _ = script_path.with_file_name(format!("{}.wasm", script_stem.to_string_lossy()));
let wasm_js_path: _ = script_path.with_file_name(format!("{}.wasm.js", script_stem.to_string_lossy()));
// 检查是否存在 WASM 文件
let wasm_file_path: _ = if wasm_path.exists() {
wasm_path
} else if wasm_js_path.exists() {
wasm_js_path
} else {
// 没有找到匹配的 WASM 文件
return Ok(None);
};
// 初始化 WASM loader(如果尚未初始化)
let loader: _ = self.wasm_loader.get_or_init(|| {
WasmModuleLoader::new().expect("Failed to create WASM loader")
});
// 读取 WASM 文件
let wasm_bytes: _ = std::fs::read(&wasm_file_path)
.map_err(|e| anyhow::anyhow!("Failed to read WASM file: {}", e))?;
// 加载 WASM 模块
let module: _ = loader.load_module(&wasm_bytes)
.map_err(|e| anyhow::anyhow!("Failed to load WASM module: {}", e))?;
Ok(Some(module))
}
/// Stage 77: 混合执行模式 - JavaScript + WebAssembly
/// 执行 JavaScript 代码,并自动检测和加载配套的 WASM 模块
pub fn execute_mixed_mode(&self, code: &str) -> Result<String> {
// 首先尝试纯 JavaScript 执行
let result: _ = self.execute_code(code)?;
// 尝试检测和加载 WASM 模块
// 注意:这里只是检测,实际的 WASM 执行需要通过其他机制触发
// 例如:在 JavaScript 中调用 import.wasm() 或类似函数
Ok(result)
}
/// Stage 77: 获取 WASM 缓存统计信息
pub fn get_wasm_cache_stats(&self) -> Result<String> {
let cache: _ = self.wasm_cache.get();
if let Some(cache) = cache {
let stats: _ = cache.get_stats();
Ok(format!("WASM Cache Stats: {:?}", stats))
} else {
Ok("WASM cache not initialized yet".to_string())
}
}
/// Stage 77: 初始化 WASM 缓存(按需初始化)
pub fn initialize_wasm_cache(&self) -> Result<()> {
self.wasm_cache.get_or_init(|| {
WasmModuleCache::new().expect("Failed to create WASM cache")
});
Ok(())
}
/// Stage 77: 获取 WASM loader 统计信息
pub fn get_wasm_loader_stats(&self) -> Result<String> {
let loader: _ = self.wasm_loader.get();
if let Some(loader) = loader {
let stats: _ = loader.get_stats();
Ok(format!("WASM Loader Stats: {:?}", stats))
} else {
Ok("WASM loader not initialized yet".to_string())
}
}
/// Stage 77: 预热 WASM 缓存
pub fn warmup_wasm_cache(&self, modules: Vec<PathBuf>) -> Result<()> {
let cache: _ = self.wasm_cache.get_or_init(|| {
WasmModuleCache::new().expect("Failed to create WASM cache")
});
// 转换 PathBuf 为 (String, Vec<u8>) 格式
let module_data: Result<Vec<(String, Vec<u8>)>, anyhow::Error> = modules.into_iter()
.map(|path| -> Result<(String, Vec<u8>), anyhow::Error> {
let name: _ = path.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown")
.to_string();
let bytes: _ = std::fs::read(&path)
.map_err(|e| anyhow::anyhow!("Failed to read {}: {}", path.display(), e))?;
Ok((name, bytes))
})
.collect();
let module_data: _ = module_data?;
cache.warmup_cache(module_data)
.map_err(|e| anyhow::anyhow!("Failed to warmup WASM cache: {}", e))
}
/// Stage 77: 清空 WASM 缓存
pub fn clear_wasm_cache(&self) -> Result<()> {
if let Some(cache) = self.wasm_cache.get() {
cache.clear_cache();
}
Ok(())
}
}
// Stage 65: Multi-level cache module
pub mod cache;