amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
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
// 延迟初始化 Web系统
// 实现 API 的延迟加载、按需初始化等启动优化功能

use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

// use crate::web_api::WebApiRegistry;
/// Web API 延迟加载器
pub struct LazyWebAPI {
    /// 已初始化的 API 集合
    initialized_apis: Arc<RwLock<HashSet<String>>>,
    /// 初始化队列
    initialization_queue: Arc<Mutex<Vec<ApiInitTask>>>,
    /// 初始化信号量(限制并发数)
    init_semaphore: Arc<Semaphore>,
    /// Web API 注册表
    // web_api_registry: Arc<WebApiRegistry>,
    /// 统计信息
    stats: Arc<Mutex<LazyInitStats>>,
}
/// API 初始化任务
#[derive(Debug, Clone)]
struct ApiInitTask {
    name: String,
    priority: u8,
    created_at: Instant,
}
impl ApiInitTask {
    fn new(name: String) -> Self {
        Self {
            name,
            priority: 5, // 默认优先级
            created_at: Instant::now(),
        }
    }
    fn with_priority(name: String, priority: u8) -> Self {
        Self {
            name,
            priority,
            created_at: Instant::now(),
        }
    }
}
/// 延迟初始化统计
#[derive(Debug, Clone, Default)]
pub struct LazyInitStats {
    pub total_initializations: u64,
    pub successful_initializations: u64,
    pub failed_initializations: u64,
    pub total_init_time_ms: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
}
impl LazyWebAPI {
    /// 创建新的延迟 Web API 加载器
    pub fn new() -> Self {
        Self {
            initialized_apis: Arc::new(Mutex::new(HashSet::new())),
            initialization_queue: Arc::new(Mutex::new(Vec::new())),
            init_semaphore: Arc::new(Mutex::new(Semaphore::new(10))), // 最多 10 个并发初始化
            // web_api_registry: Arc::new(Mutex::new(WebApiRegistry::new()))
            stats: Arc::new(Mutex::new(LazyInitStats::default())),
        }
    }
    /// 检查 API 是否已初始化
    pub async fn is_initialized(&self, api_name: &str) -> bool {
        let initialized: _ = self.initialized_apis.read().await;
        initialized.contains(api_name)
    }
    /// 按需初始化 Web API
    pub async fn init_on_demand(&self, api_name: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        // 先检查是否已经初始化
        if self.is_initialized(api_name).await {
            let mut stats = self.stats.lock().unwrap();
            stats.cache_hits += 1;
            return Ok(());
        }
        let mut stats = self.stats.lock().unwrap();
        stats.total_initializations += 1;
        stats.cache_misses += 1;
        drop(stats);
        // 获取信号量许可
        let _permit: _ = self.init_semaphore.acquire().await.map_err(|_| "Failed to acquire semaphore")?;
        // 双重检查模式
        if self.is_initialized(api_name).await {
            return Ok(());
        }
        let start: _ = Instant::now();
        // 执行实际初始化
        self.perform_initialization(api_name).await?;
        let elapsed: _ = start.elapsed();
        // 更新统计
        let mut stats = self.stats.lock().unwrap();
        stats.successful_initializations += 1;
        stats.total_init_time_ms += elapsed.as_millis() as u64;
        Ok(())
    }
    /// 执行实际的 API 初始化
    async fn perform_initialization(&self, api_name: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        // 模拟 API 初始化过程
        // 实际实现中会调用 WebApiRegistry
        // 小延迟模拟初始化开销
        tokio::time::sleep(Duration::from_micros(100)).await;
        // 标记为已初始化
        let mut initialized = self.initialized_apis.write().await;
        initialized.insert(api_name.to_string());
        Ok(())
    }
    /// 批量初始化多个 API
    pub async fn init_multiple(&self, apis: &[&str]) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut tasks: Vec<String> = Vec::new();
        for &api in apis {
            if !self.is_initialized(api).await {
                let api: _ = api.to_string();
                let init_result: _ = self.init_on_demand(&api).await;
                if let Err(e) = init_result {
                    return Err(e);
                }
            }
        }
        Ok(())
    }
    /// 获取统计信息
    pub fn get_stats(&self) -> LazyInitStats {
        self.stats.lock().unwrap().clone()
    }
    /// 重置统计信息
    pub fn reset_stats(&self) {
        let mut stats = self.stats.lock().unwrap();
        *stats = LazyInitStats::default();
    }
}
impl Default for LazyWebAPI {
    fn default() -> Self {
        Self::new()
    }
}
/// 通用延迟初始化器
pub struct LazyInitializer<T> {
    /// 初始化函数
    init_fn: Arc<dyn Fn() -> Result<T, Box<dyn std::error::Error + Send + Sync>> + Send + Sync>,
    /// 缓存的值
    value: Arc<Mutex<Option<T>>>,
    /// 是否已初始化
    initialized: Arc<Mutex<bool>>,
    /// 统计信息
    stats: Arc<Mutex<InitStats>>,
}
/// 初始化统计
#[derive(Debug, Clone)]
pub struct InitStats {
    pub initialization_count: u64,
    pub total_init_time: Duration,
    pub last_init_time: Option<Duration>,
}
impl InitStats {
    pub fn new() -> Self {
        Self {
            initialization_count: 0,
            total_init_time: Duration::from_nanos(0),
            last_init_time: None,
        }
    }
}
impl Default for InitStats {
    fn default() -> Self {
        Self::new()
    }
}
impl<T> LazyInitializer<T> {
    /// 创建新的延迟初始化器
    pub fn new<F>(init_fn: F) -> Self
    where
        F: Fn() -> Result<T, Box<dyn std::error::Error + Send + Sync>> + Send + Sync + 'static,
    {
        Self {
            init_fn: Arc::new(Mutex::new(init_fn)),
            value: Arc::new(Mutex::new(None)),
            initialized: Arc::new(Mutex::new(false)),
            stats: Arc::new(Mutex::new(InitStats::new())),
        }
    }
    /// 获取或初始化值
    pub async fn get(&self) -> Result<T, Box<dyn std::error::Error + Send + Sync>>
    where
        T: Clone,
    {
        // 检查是否已初始化
        {
            let initialized: _ = self.initialized.lock().unwrap();
            if *initialized {
                let value: _ = self.value.lock().unwrap();
                if let Some(ref val) = *value {
                    return Ok(val.clone());
                }
            }
        }
        // 执行初始化
        let start: _ = Instant::now();
        let init_fn: _ = self.init_fn.clone();
        let result: _ = init_fn()?;
        let elapsed: _ = start.elapsed();
        // 缓存结果
        {
            let mut value = self.value.lock().unwrap();
            *value = Some(result.clone());
        }
        {
            let mut initialized = self.initialized.lock().unwrap();
            *initialized = true;
        }
        {
            let mut stats = self.stats.lock().unwrap();
            stats.initialization_count += 1;
            stats.total_init_time += elapsed;
            stats.last_init_time = Some(elapsed);
        }
        Ok(result)
    }
    /// 强制重新初始化
    pub async fn reinit(&self) -> Result<T, Box<dyn std::error::Error + Send + Sync>>
    where
        T: Clone,
    {
        {
            let mut initialized = self.initialized.lock().unwrap();
            *initialized = false;
        }
        {
            let mut value = self.value.lock().unwrap();
            *value = None;
        }
        self.get().await
    }
    /// 获取统计信息
    pub fn get_stats(&self) -> InitStats {
        self.stats.lock().unwrap().clone()
    }
    /// 检查是否已初始化
    pub fn is_initialized(&self) -> bool {
        *self.initialized.lock().unwrap()
    }
}
/// 按需模块加载器
pub struct OnDemandLoader {
    /// 已加载的模块
    loaded_modules: Arc<RwLock<HashMap<String, LoadedModule>>>,
    /// 模块工厂
    module_factory: Arc<dyn ModuleFactory + Send + Sync>,
    /// 统计信息
    stats: Arc<Mutex<LoaderStats>>,
}
/// 已加载的模块
#[derive(Debug, Clone)]
struct LoadedModule {
    data: Vec<u8>,
    load_time: Instant,
    access_count: u64,
}
/// 模块工厂 trait
pub trait ModuleFactory {
    fn create_module(&self, name: &str) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
}
/// 简单模块工厂实现
pub struct SimpleModuleFactory;
impl ModuleFactory for SimpleModuleFactory {
    fn create_module(&self, name: &str) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
        // 模拟模块加载
        let module_data: _ = format!("// Module: {}\nconsole.log('{} loaded');", name, name);
        Ok(module_data.into_bytes())
    }
}
/// 加载器统计
#[derive(Debug, Clone, Default)]
pub struct LoaderStats {
    pub total_loads: u64,
    pub successful_loads: u64,
    pub failed_loads: u64,
    pub total_load_time_ms: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
}
impl OnDemandLoader {
    /// 创建新的按需加载器
    pub fn new() -> Self {
        Self::with_factory(Box::new(SimpleModuleFactory))
    }
    /// 使用指定工厂创建加载器
    pub fn with_factory(factory: Box<dyn ModuleFactory + Send + Sync>) -> Self {
        Self {
            loaded_modules: Arc::new(Mutex::new(HashMap::new())),
            module_factory: Arc::from(factory),
            stats: Arc::new(Mutex::new(LoaderStats::default())),
        }
    }
    /// 按需加载模块
    pub async fn load_module(&self, name: &str) -> Result<Option<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>> {
        // 先检查缓存
        let module_data: _ = {
            let modules: _ = self.loaded_modules.read().await;
            modules.get(name).map(|module| module.data.clone())
        };
        if let Some(data) = module_data {
            let mut stats = self.stats.lock().unwrap();
            stats.cache_hits += 1;
            // 更新访问计数
            let mut modules = self.loaded_modules.write().await;
            if let Some(module) = modules.get_mut(name) {
                module.access_count += 1;
            }
            return Ok(Some(data));
        }
        let mut stats = self.stats.lock().unwrap();
        stats.total_loads += 1;
        stats.cache_misses += 1;
        drop(stats);
        // 加载模块
        let start: _ = Instant::now();
        let data: _ = self.module_factory.create_module(name)?;
        let elapsed: _ = start.elapsed();
        // 缓存模块
        {
            let mut modules = self.loaded_modules.write().await;
            modules.insert(
                name.to_string(),
                LoadedModule {
                    data: data.clone(),
                    load_time: Instant::now(),
                    access_count: 1,
                },
            );
        }
        // 更新统计
        let mut stats = self.stats.lock().unwrap();
        stats.successful_loads += 1;
        stats.total_load_time_ms += elapsed.as_millis() as u64;
        Ok(Some(data))
    }
    /// 获取模块统计信息
    pub fn get_stats(&self) -> LoaderStats {
        self.stats.lock().unwrap().clone()
    }
    /// 清理未使用的模块
    pub async fn cleanup_unused(&self, max_age: Duration) -> usize {
        let mut modules = self.loaded_modules.write().await;
        let now: _ = Instant::now();
        let mut removed = 0;
        modules.retain(|_, module| {
            if now.duration_since(module.load_time) > max_age && module.access_count == 0 {
                removed += 1;
                false
            } else {
                true
            }
        });
        removed
    }
}
impl Default for OnDemandLoader {
    fn default() -> Self {
        Self::new()
    }
}
/// 启动时间优化器
pub struct StartupOptimizer {
    /// 延迟 Web API
    lazy_web_api: Arc<LazyWebAPI>,
    /// 按需加载器
    on_demand_loader: Arc<OnDemandLoader>,
    /// 启动时间记录
    startup_time: Arc<Mutex<Option<Instant>>>,
    /// 优化策略
    optimization_level: OptimizationLevel,
}
/// 优化级别
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OptimizationLevel {
    /// 无优化
    None,
    /// 基础优化
    Basic,
    /// 激进优化
    Aggressive,
    /// 最大优化
    Maximum,
}
impl StartupOptimizer {
    /// 创建新的启动优化器
    pub fn new(level: OptimizationLevel) -> Self {
        Self {
            lazy_web_api: Arc::new(Mutex::new(LazyWebAPI::new())),
            on_demand_loader: Arc::new(Mutex::new(OnDemandLoader::new())),
            startup_time: Arc::new(Mutex::new(None)),
            optimization_level: level,
        }
    }
    /// 开始优化
    pub fn start_optimization(&self) {
        let mut startup_time = self.startup_time.lock().unwrap();
        *startup_time = Some(Instant::now());
    }
    /// 获取启动时间
    pub fn get_startup_time(&self) -> Option<Duration> {
        let startup_time: _ = self.startup_time.lock().unwrap();
        startup_time.map(|start| start.elapsed())
    }
    /// 获取优化器
    pub fn get_lazy_web_api(&self) -> Arc<LazyWebAPI> {
        self.lazy_web_api.clone()
    }
    /// 获取按需加载器
    pub fn get_on_demand_loader(&self) -> Arc<OnDemandLoader> {
        self.on_demand_loader.clone()
    }
    /// 根据优化级别执行预优化
    pub async fn perform_pre_optimization(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        match self.optimization_level {
            OptimizationLevel::None => {}
            OptimizationLevel::Basic => {
                // 预初始化核心 API
                self.lazy_web_api.init_multiple(&["console", "process"]).await?;
            }
            OptimizationLevel::Aggressive => {
                // 预初始化更多 API
                let apis: _ = &["console", "process", "path", "util", "buffer"];
                self.lazy_web_api.init_multiple(apis).await?;
            }
            OptimizationLevel::Maximum => {
                // 预初始化所有常用 API 并预加载模块
                let apis: _ = &["console", "process", "path", "util", "buffer", "fs", "os", "url"];
                self.lazy_web_api.init_multiple(apis).await?;
                // 预加载常用模块
                let modules: _ = &["util", "buffer", "events", "stream"];
                for &module in modules {
                    let _: _ = self.on_demand_loader.load_module(module).await;
                }
            }
        }
        Ok(())
    }
}
impl Default for StartupOptimizer {
    fn default() -> Self {
        Self::new(OptimizationLevel::Aggressive)
    }
}