Skip to main content

aether/api/
constructors.rs

1use super::Aether;
2use crate::builtins::IOPermissions;
3use crate::evaluator::Evaluator;
4use crate::optimizer::Optimizer;
5use crate::stdlib;
6
7impl Aether {
8    /// 创建新的 Aether 引擎实例
9    ///
10    /// **用于 DSL 嵌入**:IO 操作默认禁用以确保安全性。
11    /// 使用 `with_permissions()` 或 `with_all_permissions()` 来启用 IO。
12    ///
13    /// **用于 CLI 使用**:命令行工具默认使用 `with_all_permissions()`。
14    pub fn new() -> Self {
15        Self::with_permissions(IOPermissions::default())
16    }
17
18    /// 使用自定义 IO 权限创建新的 Aether 引擎
19    pub fn with_permissions(permissions: IOPermissions) -> Self {
20        Aether {
21            evaluator: Evaluator::with_permissions(permissions),
22            cache: crate::cache::ASTCache::new(),
23            optimizer: Optimizer::new(),
24            bigint_threshold: crate::lexer::DEFAULT_BIGINT_THRESHOLD,
25        }
26    }
27
28    /// 创建启用所有 IO 权限的新 Aether 引擎
29    pub fn with_all_permissions() -> Self {
30        Self::with_permissions(IOPermissions::allow_all())
31    }
32
33    /// 创建预加载标准库的新 Aether 引擎。
34    ///
35    /// 标准库模块为纯计算(内嵌源码),加载**不需要** IO 权限——
36    /// 因此这里基于 `Aether::new()` 构建,不会静默授予文件读写能力;
37    /// 需要 IO 的宿主请显式 `with_permissions(...)`。
38    pub fn with_stdlib() -> Result<Self, String> {
39        let mut engine = Self::new();
40        stdlib::preload_stdlib(&mut engine)?;
41        Ok(engine)
42    }
43}
44
45impl Default for Aether {
46    fn default() -> Self {
47        Self::new()
48    }
49}