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
// 预编译模块缓存系统
// 预编译常用 Node.js 模块并缓存字节码,提升执行速度

use anyhow::{Context, Result, anyhow};
use crate::code_cache::{BytecodeCache, CacheConfig};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::time::SystemTime;
use std::path::PathBuf;
use std::hash::{Hash, Hasher, DefaultHasher};

/// 预编译模块缓存统计
#[derive(Debug, Clone, Default)]
pub struct PrecompiledCacheStats {
    pub total_modules: usize,
    pub cached_modules: usize,
    pub cache_hits: u64,
    pub cache_misses: u64,
    pub average_compile_time_ms: f64,
    pub total_compile_time_ms: u64,
}
/// 预编译模块条目
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrecompiledModuleEntry {
    pub module_name: String,
    pub bytecode: Vec<u8>,
    pub source_hash: String,
    pub compile_time_ms: u64,
    pub created_at: std::time::SystemTime,
}
/// 预编译模块缓存
pub struct PrecompiledModuleCache {
    /// 缓存目录
    cache_dir: PathBuf,
    /// 内存缓存的模块
    modules: Arc<Mutex<HashMap<String, PrecompiledModuleEntry>>>,
    /// 统计信息
    stats: Arc<Mutex<PrecompiledCacheStats>>,
    /// V8 字节码缓存
    #[allow(dead_code)]
    bytecode_cache: Arc<BytecodeCache>,
}
impl PrecompiledModuleCache {
    /// 创建新的预编译模块缓存
    pub fn new() -> Result<Self> {
        let cache_dir: _ = std::env::temp_dir().join("amberjs_precompiled_cache");
        Self::new_with_path(cache_dir)
    }
    /// 使用指定路径创建预编译模块缓存
    pub fn new_with_path(cache_dir: PathBuf) -> Result<Self> {
        // 创建缓存目录
        fs::create_dir_all(&cache_dir)
            .context(format!("Failed to create cache directory: {:?}", cache_dir))?;
        let cache_config: _ = CacheConfig::default();
        Ok(Self {
            cache_dir,
            modules: Arc::new(Mutex::new(HashMap::new()))
            stats: Arc::new(Mutex::new(PrecompiledCacheStats::default()))
            bytecode_cache: Arc::new(Mutex::new(BytecodeCache::new(cache_config)))
        })
    }
    /// 从磁盘加载预编译模块缓存
    pub fn load_from_path(cache_dir: PathBuf) -> Result<Self> {
        let mut cache = PrecompiledModuleCache::new_with_path(cache_dir)?;
        // 加载已缓存的模块
        cache.load_from_disk()?;
        Ok(cache)
    }
    /// 预编译所有内置模块
    pub fn precompile_builtin_modules(&self) -> Result<()> {
        let builtin_modules: _ = Self::get_builtin_modules_list();
        for module in &builtin_modules {
            if !self.is_module_cached(module) {
                let source: _ = self.get_builtin_module_source(module)?;
                self.cache_module(module, &source)?;
            }
        }
        Ok(())
    }
    /// 获取内置模块列表
    pub fn get_builtin_modules_list() -> Vec<&'static str> {
        vec![
            "console",
            "process",
            "path",
            "fs",
            "os",
            "util",
            "events",
            "stream",
            "buffer",
            "crypto",
        ]
    }
    /// 获取内置模块源码
    fn get_builtin_module_source(&self, module_name: &str) -> Result<String> {
        match module_name {
            "console" => Ok(Self::builtin_console_module()),
            "process" => Ok(Self::builtin_process_module()),
            "path" => Ok(Self::builtin_path_module()),
            "fs" => Ok(Self::builtin_fs_module()),
            "os" => Ok(Self::builtin_os_module()),
            "util" => Ok(Self::builtin_util_module()),
            "events" => Ok(Self::builtin_events_module()),
            "stream" => Ok(Self::builtin_stream_module()),
            "buffer" => Ok(Self::builtin_buffer_module()),
            "crypto" => Ok(Self::builtin_crypto_module()),
            _ => Err(anyhow!("Unknown builtin module: {}", module_name)),
        }
    }
    /// 缓存模块
    pub fn cache_module(&self, module_name: &str, source_code: &str) -> Result<()> {
        let start_time: _ = Instant::now();
        // 计算源码哈希
        let source_hash: _ = self.calculate_source_hash(source_code);
        // 编译源码
        let bytecode: _ = self.compile_to_bytecode(source_code)?;
        let compile_time: _ = start_time.elapsed();
        let entry: _ = PrecompiledModuleEntry {
            module_name: module_name.to_string(),
            bytecode,
            source_hash,
            compile_time_ms: compile_time.as_millis() as u64,
            created_at: std::time::SystemTime::now(),
        };
        // 保存到内存缓存
        {
            let mut modules = self.modules.lock().unwrap();
            modules.insert(module_name.to_string(), entry.clone());
        }
        // 持久化到磁盘
        self.persist_module(module_name, &entry)?;
        // 更新统计
        self.update_stats(compile_time);
        Ok(())
    }
    /// 检查模块是否已缓存
    pub fn is_module_cached(&self, module_name: &str) -> bool {
        let modules: _ = self.modules.lock().unwrap();
        modules.contains_key(module_name)
    }
    /// 获取预编译字节码
    pub fn get_precompiled_bytecode(&self, module_name: &str) -> Option<Vec<u8> {
        let modules: _ = self.modules.lock().unwrap();
        if let Some(entry) = modules.get(module_name) {
            // 更新命中统计
            {
                let mut stats = self.stats.lock().unwrap();
                stats.cache_hits += 1;
            }
            Some(entry.bytecode.clone())
        } else {
            // 更新未命中统计
            {
                let mut stats = self.stats.lock().unwrap();
                stats.cache_misses += 1;
            }
            None
        }
    }
    /// 使模块缓存失效
    pub fn invalidate_module(&self, module_name: &str) -> Result<()> {
        // 从内存缓存中移除
        {
            let mut modules = self.modules.lock().unwrap();
            modules.remove(module_name);
        }
        // 从磁盘移除
        let module_file: _ = self.cache_dir.join(format!("{}.bin", module_name));
        if module_file.exists() {
            fs::remove_file(&module_file)
                .context(format!("Failed to remove cached module: {}", module_name))?;
        }
        Ok(())
    }
    /// 获取缓存统计
    pub fn get_stats(&self) -> PrecompiledCacheStats {
        let modules: _ = self.modules.lock().unwrap();
        let stats: _ = self.stats.lock().unwrap();
        PrecompiledCacheStats {
            total_modules: modules.len(),
            cached_modules: modules.len(),
            cache_hits: stats.cache_hits,
            cache_misses: stats.cache_misses,
            average_compile_time_ms: if stats.cache_hits + stats.cache_misses > 0 {
                stats.total_compile_time_ms as f64 / (stats.cache_hits + stats.cache_misses) as f64
            } else {
                0.0
            },
            total_compile_time_ms: stats.total_compile_time_ms,
        }
    }
    /// 计算源码哈希
    fn calculate_source_hash(&self, source: &str) -> String {
        let mut hasher = DefaultHasher::new();
        source.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }
    /// 编译源码为字节码
    fn compile_to_bytecode(&self, source: &str) -> Result<Vec<u8> {
        // 这里我们需要集成 V8 编译逻辑
        // 暂时返回模拟字节码
        let bytecode: _ = source.as_bytes().to_vec();
        Ok(bytecode)
    }
    /// 更新统计信息
    fn update_stats(&self, compile_time: Duration) {
        let mut stats = self.stats.lock().unwrap();
        stats.total_compile_time_ms += compile_time.as_millis() as u64;
    }
    /// 持久化模块到磁盘
    fn persist_module(&self, module_name: &str, entry: &PrecompiledModuleEntry) -> Result<()> {
        let module_file: _ = self.cache_dir.join(format!("{}.bin", module_name));
        let serialized: _ = bincode::serialize(entry)
            .context(format!("Failed to serialize module: {}", module_name))?;
        fs::write(&module_file, serialized)
            .context(format!("Failed to write cached module: {:?}", module_file))?;
        Ok(())
    }
    /// 从磁盘加载缓存
    fn load_from_disk(&mut self) -> Result<()> {
        if !self.cache_dir.exists() {
            return Ok(());
        }
        let entries: _ = fs::read_dir(&self.cache_dir)
            .context(format!("Failed to read cache directory: {:?}", self.cache_dir))?;
        for entry in entries {
            let entry: _ = entry?;
            let path: _ = entry.path();
            if path.extension().map_or(false, |ext| ext == "bin") {
                let serialized: _ = fs::read(&path)
                    .context(format!("Failed to read cached file: {:?}", path))?;
                let module_entry: PrecompiledModuleEntry = bincode::deserialize(&serialized)
                    .context(format!("Failed to deserialize module: {:?}", path))?;
                let module_name: _ = module_entry.module_name.clone();
                let mut modules = self.modules.lock().unwrap();
                modules.insert(module_name, module_entry);
            }
        }
        Ok(())
    }
    // ========== 内置模块源码实现 ==========
    fn builtin_console_module() -> String {
        r#"
        const console = {
            log: (...args) => {
                // 将日志输出到 V8
            },
            error: (...args) => {
                // 将错误输出到 stderr
            },
            warn: (...args) => {
                // 将警告输出到 stderr
            },
            info: (...args) => {
                // 将信息输出到 stdout
            },
            debug: (...args) => {
                // 调试输出
            }
        };
        module.exports = console;
        "#
        .to_string()
    }
    fn builtin_process_module() -> String {
        r#"
        const process = {
            version: 'v18.0.0',
            argv: [],
            env: {},
            cwd: () => '/',
            nextTick: (fn) => {
                // 异步执行
                setTimeout(fn, 0);
            }
        };
        module.exports = process;
        "#
        .to_string()
    }
    fn builtin_path_module() -> String {
        r#"
        const path = {
            join: (...paths) => {
                return paths.join('/');
            },
            resolve: (...paths) => {
                return '/' + paths.join('/');
            },
            dirname: (p) => {
                return p.substring(0, p.lastIndexOf('/'));
            },
            basename: (p) => {
                const parts = p.split('/');
                return parts[parts.length - 1];
            }
        };
        module.exports = path;
        "#
        .to_string()
    }
    fn builtin_fs_module() -> String {
        r#"
        const fs = {
            readFile: (path, callback) => {
                // 异步文件读取
            },
            writeFile: (path, data, callback) => {
                // 异步文件写入
            }
        };
        module.exports = fs;
        "#
        .to_string()
    }
    fn builtin_os_module() -> String {
        r#"
        const os = {
            platform: () => process.platform,
            arch: () => process.arch,
            cpus: () => []
        };
        module.exports = os;
        "#
        .to_string()
    }
    fn builtin_util_module() -> String {
        r#"
        const util = {
            format: (format, ...args) => {
                return format.replace(/%[sdif]/g, (match) => {
                    return args.shift() || match;
                });
            },
            inspect: (obj) => {
                return JSON.stringify(obj);
            }
        };
        module.exports = util;
        "#
        .to_string()
    }
    fn builtin_events_module() -> String {
        r#"
        class EventEmitter {
            constructor() {
                this.events = {};
            }
            on(event, listener) {
                if (!this.events[event]) {
                    this.events[event] = [];
                }
                this.events[event].push(listener);
            }
            emit(event, ...args) {
                if (this.events[event]) {
                    this.events[event].forEach(fn => fn(...args));
                }
            }
        }
        module.exports = EventEmitter;
        "#
        .to_string()
    }
    fn builtin_stream_module() -> String {
        r#"
        const stream = {
            Readable: class {
                constructor() {}
                on(event, listener) {}
                read() {}
            },
            Writable: class {
                constructor() {}
                on(event, listener) {}
                write(data) {}
            }
        };
        module.exports = stream;
        "#
        .to_string()
    }
    fn builtin_buffer_module() -> String {
        r#"
        class Buffer {
            constructor(data) {
                this.data = data;
            }
            toString() {
                return String(this.data);
            }
        }
        module.exports = Buffer;
        "#
        .to_string()
    }
    fn builtin_crypto_module() -> String {
        r#"
        const crypto = {
            createHash: (algorithm) => {
                return {
                    update: (data) => this,
                    digest: (encoding) => ''
                };
            }
        };
        module.exports = crypto;
        "#
        .to_string()
    }
}
#[cfg(test)]
mod tests {
    #[test]
    fn test_cache_creation() {
        let cache: _ = PrecompiledModuleCache::new();
        assert!(cache.is_ok());
    }
    #[test]
    fn test_builtin_modules_list() {
        let modules: _ = PrecompiledModuleCache::get_builtin_modules_list();
        assert!(modules.len() > 0);
        assert!(modules.contains(&"console"));
        assert!(modules.contains(&"process"));
    }
    #[test]
    fn test_module_caching() {
        let cache: _ = PrecompiledModuleCache::new().unwrap();
        let source: _ = "module.exports = { test: true };";
        let result: _ = cache.cache_module("test_module", source);
        assert!(result.is_ok());
        assert!(cache.is_module_cached("test_module"));
    }
    #[test]
    fn test_cache_stats() {
        let cache: _ = PrecompiledModuleCache::new().unwrap();
        cache.precompile_builtin_modules().unwrap();
        let stats: _ = cache.get_stats();
        assert!(stats.cached_modules > 0);
    }
}