gpui-rhai 0.1.1

A Rhai-authored declarative UI runtime for GPUI
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
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
use std::collections::BTreeMap;
use std::fmt;
use std::sync::Mutex;

use rhai::{
    AST, ASTFlags, Dynamic, Engine, EvalAltResult, Expr, Module, ModuleResolver, Position, Scope,
    Shared, Stmt,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{ModuleCompileCache, ScriptSource, ScriptSourceError};

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct ModuleId(String);

impl ModuleId {
    /// Parse and validate a logical module identifier.
    ///
    /// # Errors
    ///
    /// Returns [`ModuleIdError`] for absolute paths, traversal, empty segments,
    /// platform paths, or unsupported characters.
    pub fn parse(value: impl Into<String>) -> Result<Self, ModuleIdError> {
        let value = value.into();
        validate_module_id(&value)?;
        Ok(Self(value))
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for ModuleId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

impl TryFrom<String> for ModuleId {
    type Error = ModuleIdError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::parse(value)
    }
}

impl From<ModuleId> for String {
    fn from(value: ModuleId) -> Self {
        value.0
    }
}

#[derive(Clone, Debug, Error, Eq, PartialEq)]
pub enum ModuleIdError {
    #[error("module id cannot be empty")]
    Empty,
    #[error("module id `{0}` must be relative and use `/` separators")]
    AbsoluteOrPlatformPath(String),
    #[error("module id `{0}` contains an empty, `.` or `..` segment")]
    InvalidSegment(String),
    #[error("module id `{0}` contains unsupported characters")]
    UnsupportedCharacters(String),
}

fn validate_module_id(value: &str) -> Result<(), ModuleIdError> {
    if value.is_empty() {
        return Err(ModuleIdError::Empty);
    }
    if value.starts_with('/')
        || value.starts_with('\\')
        || value.contains('\\')
        || value.contains(':')
    {
        return Err(ModuleIdError::AbsoluteOrPlatformPath(value.to_owned()));
    }
    if value
        .split('/')
        .any(|segment| segment.is_empty() || segment == "." || segment == "..")
    {
        return Err(ModuleIdError::InvalidSegment(value.to_owned()));
    }
    if !value
        .chars()
        .all(|character| character.is_ascii_alphanumeric() || matches!(character, '/' | '_' | '-'))
    {
        return Err(ModuleIdError::UnsupportedCharacters(value.to_owned()));
    }
    Ok(())
}

/// A resolver that can only load explicitly registered, logical module IDs.
///
/// It never reads the filesystem and rejects path traversal, absolute paths,
/// platform path syntax, and cyclic imports.
#[derive(Debug, Default)]
pub struct RestrictedModuleResolver {
    sources: BTreeMap<ModuleId, String>,
    compiled: BTreeMap<ModuleId, AST>,
    resolving: Mutex<Vec<ModuleId>>,
}

impl RestrictedModuleResolver {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Snapshot every module from a file or embedded script source.
    ///
    /// # Errors
    ///
    /// Returns [`ScriptSourceError`] when any declared module cannot be loaded.
    pub fn from_source(source: &impl ScriptSource) -> Result<Self, ScriptSourceError> {
        let mut resolver = Self::new();
        for id in source.module_ids() {
            let asset = source.load(&id)?;
            // `ModuleId` has already been validated by the source.
            resolver.sources.insert(id, asset.source);
        }
        Ok(resolver)
    }

    /// Snapshot source while reusing transactionally compiled module ASTs.
    ///
    /// # Errors
    ///
    /// Returns source errors for declared modules that cannot load.
    pub fn from_source_with_cache(
        source: &impl ScriptSource,
        cache: &ModuleCompileCache,
    ) -> Result<Self, ScriptSourceError> {
        let mut resolver = Self::from_source(source)?;
        for id in source.module_ids() {
            let asset = source.load(&id)?;
            if cache.content_hash(&id) == Some(asset.content_hash)
                && let Some(ast) = cache.ast(&id)
            {
                resolver.compiled.insert(id, ast.clone());
            }
        }
        Ok(resolver)
    }

    /// Register source under a validated logical module identifier.
    ///
    /// # Errors
    ///
    /// Returns [`ModuleIdError`] when `id` violates the resolver's path rules.
    pub fn insert(
        &mut self,
        id: impl Into<String>,
        source: impl Into<String>,
    ) -> Result<(), ModuleIdError> {
        self.sources.insert(ModuleId::parse(id)?, source.into());
        Ok(())
    }

    #[must_use]
    pub fn contains(&self, id: &ModuleId) -> bool {
        self.sources.contains_key(id)
    }

    fn begin_resolution(
        &self,
        id: &ModuleId,
        position: Position,
    ) -> Result<(), Box<EvalAltResult>> {
        let mut stack = self.resolving.lock().map_err(|_| {
            Box::new(runtime_error(
                "module resolver lock is poisoned".to_owned(),
                position,
            ))
        })?;
        if let Some(cycle_start) = stack.iter().position(|active| active == id) {
            let mut cycle = stack[cycle_start..]
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>();
            cycle.push(id.to_string());
            return Err(Box::new(runtime_error(
                format!("cyclic module import: {}", cycle.join(" -> ")),
                position,
            )));
        }
        stack.push(id.clone());
        Ok(())
    }

    fn end_resolution(&self, id: &ModuleId) {
        if let Ok(mut stack) = self.resolving.lock() {
            if stack.last() == Some(id) {
                stack.pop();
            } else if let Some(index) = stack.iter().rposition(|active| active == id) {
                stack.remove(index);
            }
        }
    }
}

impl ModuleResolver for RestrictedModuleResolver {
    fn resolve(
        &self,
        engine: &Engine,
        _source: Option<&str>,
        path: &str,
        position: Position,
    ) -> Result<Shared<Module>, Box<EvalAltResult>> {
        let id = ModuleId::parse(path)
            .map_err(|error| Box::new(runtime_error(error.to_string(), position)))?;
        let source = self.sources.get(&id).ok_or_else(|| {
            Box::new(EvalAltResult::ErrorModuleNotFound(
                path.to_owned(),
                position,
            ))
        })?;

        self.begin_resolution(&id, position)?;
        let result = (|| {
            crate::extract_imports(source).map_err(|error| {
                Box::new(EvalAltResult::ErrorInModule(
                    path.to_owned(),
                    Box::new(runtime_error(error.to_string(), position)),
                    position,
                ))
            })?;
            let ast = if let Some(ast) = self.compiled.get(&id) {
                ast.clone()
            } else {
                let mut ast = engine.compile(source).map_err(|error| {
                    Box::new(EvalAltResult::ErrorInModule(
                        path.to_owned(),
                        error.into(),
                        position,
                    ))
                })?;
                crate::engine::validate_assignment_targets(&ast).map_err(|error| {
                    Box::new(EvalAltResult::ErrorInModule(
                        path.to_owned(),
                        Box::new(runtime_error(error.to_string(), position)),
                        position,
                    ))
                })?;
                ast.set_source(path);
                ast
            };
            validate_module_init(&id, &ast, position)?;
            Module::eval_ast_as_new(Scope::new(), &ast, engine)
                .map(Into::into)
                .map_err(|error| {
                    Box::new(EvalAltResult::ErrorInModule(
                        path.to_owned(),
                        error,
                        position,
                    ))
                })
        })();
        self.end_resolution(&id);
        result
    }
}

fn validate_module_init(
    id: &ModuleId,
    ast: &AST,
    import_position: Position,
) -> Result<(), Box<EvalAltResult>> {
    let mut definitions = 0_usize;
    for statement in ast.statements() {
        let accepted = match statement {
            Stmt::Noop(_) | Stmt::Import(..) | Stmt::Export(..) => true,
            Stmt::Var(variable, options, ..) => {
                options.contains(ASTFlags::CONSTANT) && variable.1.get_literal_value(None).is_some()
            }
            Stmt::FnCall(call, ..) if call.name == "define_component" && !call.is_qualified() => {
                definitions = definitions.saturating_add(1);
                call.args.len() == 1 && pure_component_definition(&call.args[0])
            }
            _ => false,
        };
        if !accepted {
            let position = statement.position();
            let location = if position.is_none() {
                String::new()
            } else {
                format!(" at {position}")
            };
            return Err(Box::new(runtime_error(
                format!(
                    "module `{id}` init must contain only imports, literal const values, exports, and one direct define_component declaration{location}"
                ),
                if position.is_none() {
                    import_position
                } else {
                    position
                },
            )));
        }
    }
    if definitions > 1 {
        return Err(Box::new(runtime_error(
            format!("module `{id}` declares {definitions} components; exactly one is allowed"),
            import_position,
        )));
    }
    Ok(())
}

fn pure_component_definition(expression: &Expr) -> bool {
    if expression.get_literal_value(None).is_some() {
        return true;
    }
    match expression {
        Expr::Array(values, ..) => values.iter().all(pure_component_definition),
        Expr::Map(entries, ..) => entries
            .0
            .iter()
            .all(|(_, value)| pure_component_definition(value)),
        Expr::FnCall(call, ..)
            if call.name == "Fn"
                && !call.is_qualified()
                && call.args.len() == 1
                && matches!(call.args[0], Expr::StringConstant(..)) =>
        {
            true
        }
        _ => false,
    }
}

fn runtime_error(message: String, position: Position) -> EvalAltResult {
    EvalAltResult::ErrorRuntime(Dynamic::from(message), position)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::Cell;
    use std::rc::Rc;

    #[test]
    fn module_ids_reject_escape_paths() {
        for invalid in [
            "",
            "/absolute",
            "../outside",
            "a/../b",
            "C:/ui",
            "a\\b",
            "a//b",
        ] {
            assert!(ModuleId::parse(invalid).is_err(), "accepted `{invalid}`");
        }
        assert_eq!(
            ModuleId::parse("components/button").unwrap().as_str(),
            "components/button"
        );
    }

    #[test]
    fn registered_modules_can_be_imported() {
        let mut resolver = RestrictedModuleResolver::new();
        resolver
            .insert("components/greeting", "fn greeting() { \"hello\" }")
            .unwrap();

        let mut engine = Engine::new();
        engine.set_module_resolver(resolver);
        let value: String = engine
            .eval(
                r#"
                    import "components/greeting" as greeting;
                    greeting::greeting()
                "#,
            )
            .unwrap();
        assert_eq!(value, "hello");
    }

    #[test]
    fn module_init_rejects_effectful_calls_before_evaluation() {
        let touched = Rc::new(Cell::new(false));
        let observer = Rc::clone(&touched);
        let mut resolver = RestrictedModuleResolver::new();
        resolver
            .insert(
                "components/effectful",
                "touch(); fn greeting() { \"hello\" }",
            )
            .unwrap();
        let mut engine = Engine::new();
        engine.register_fn("touch", move || observer.set(true));
        engine.set_module_resolver(resolver);
        let error = engine
            .eval::<Dynamic>("import \"components/effectful\" as effectful;")
            .unwrap_err();
        assert!(
            error.to_string().contains("init must contain only"),
            "{error}"
        );
        assert!(!touched.get());
    }

    #[test]
    fn module_init_accepts_literal_consts_and_direct_pure_definition() {
        let engine = Engine::new();
        let id = ModuleId::parse("components/pure").unwrap();
        let ast = engine
            .compile(
                r#"
                    const LIMITS = #{ min: 1, values: [2, 3] };
                    define_component(#{
                        metadata: #{ id: "components/pure" },
                        render: Fn("render_Pure")
                    });
                    fn render_Pure(ctx, props) { () }
                "#,
            )
            .unwrap();
        validate_module_init(&id, &ast, Position::NONE).unwrap();
    }

    #[test]
    fn module_init_rejects_mutable_globals_and_computed_definitions() {
        let engine = Engine::new();
        let id = ModuleId::parse("components/impure").unwrap();
        for source in [
            "let count = 0; fn read() { count }",
            "fn build() { #{} } define_component(build());",
            "define_component(#{}); define_component(#{});",
        ] {
            let ast = engine.compile(source).unwrap();
            assert!(validate_module_init(&id, &ast, Position::NONE).is_err());
        }
    }

    #[test]
    fn resolver_reuses_only_content_matching_cached_asts() {
        let id = ModuleId::parse("components/greeting").unwrap();
        let source = crate::EmbeddedScriptSource::new(BTreeMap::from([(
            id.clone(),
            "fn greeting() { \"hello\" }".to_owned(),
        )]));
        let engine = Engine::new();
        let mut cache = ModuleCompileCache::new();
        cache.refresh(&engine, &source, [id.clone()]).unwrap();
        let resolver = RestrictedModuleResolver::from_source_with_cache(&source, &cache).unwrap();
        assert!(resolver.compiled.contains_key(&id));

        let changed = crate::EmbeddedScriptSource::new(BTreeMap::from([(
            id.clone(),
            "fn greeting() { \"changed\" }".to_owned(),
        )]));
        let resolver = RestrictedModuleResolver::from_source_with_cache(&changed, &cache).unwrap();
        assert!(!resolver.compiled.contains_key(&id));
    }

    #[test]
    fn missing_modules_are_diagnostic_errors() {
        let mut engine = Engine::new();
        engine.set_module_resolver(RestrictedModuleResolver::new());
        let error = engine
            .eval::<Dynamic>("import \"components/missing\" as missing;")
            .unwrap_err();
        assert!(error.to_string().contains("components/missing"));
    }

    #[test]
    fn cyclic_imports_report_the_cycle() {
        let mut resolver = RestrictedModuleResolver::new();
        resolver
            .insert("components/a", "import \"components/b\" as b;")
            .unwrap();
        resolver
            .insert("components/b", "import \"components/a\" as a;")
            .unwrap();

        let mut engine = Engine::new();
        engine.set_module_resolver(resolver);
        let error = engine
            .eval::<Dynamic>("import \"components/a\" as a;")
            .unwrap_err();
        let message = error.to_string();
        assert!(message.contains("cyclic module import"), "{message}");
        assert!(message.contains("components/a -> components/b -> components/a"));
    }
}