d4rt_rs 0.1.3

A Dart interpreter and runtime environment in Rust, embeddable as a library (QuickJS-style Context API)
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
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
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;

use crate::class::{BridgedClassDef, BridgedEnumDef, ExtensionDef};
use crate::error::InterpError;
use crate::rt::{RuntimeType, runtime_type_of};
use crate::value::Value;

pub struct Environment {
    pub enclosing: Option<Rc<RefCell<Environment>>>,
    pub values: HashMap<String, Value>,
    pub bridged_classes: HashMap<String, Arc<BridgedClassDef>>,
    pub bridged_enums: HashMap<String, Rc<BridgedEnumDef>>,
    pub unnamed_extensions: Vec<Rc<ExtensionDef>>,
    pub prefixed_imports: HashMap<String, Rc<RefCell<Environment>>>,
    pub annotated_types: HashMap<usize, Rc<RuntimeType>>,
}

impl Environment {
    pub fn new() -> Rc<RefCell<Self>> {
        Rc::new(RefCell::new(Environment {
            enclosing: None,
            values: HashMap::new(),
            bridged_classes: HashMap::new(),
            bridged_enums: HashMap::new(),
            unnamed_extensions: Vec::new(),
            prefixed_imports: HashMap::new(),
            annotated_types: HashMap::new(),
        }))
    }

    pub fn child(enclosing: &Rc<RefCell<Environment>>) -> Rc<RefCell<Environment>> {
        Rc::new(RefCell::new(Environment {
            enclosing: Some(enclosing.clone()),
            values: HashMap::new(),
            bridged_classes: HashMap::new(),
            bridged_enums: HashMap::new(),
            unnamed_extensions: Vec::new(),
            prefixed_imports: HashMap::new(),
            annotated_types: HashMap::new(),
        }))
    }

    pub fn define(this: &Rc<RefCell<Environment>>, name: &str, value: Value) {
        let mut env = this.borrow_mut();
        if env.bridged_classes.contains_key(name) || env.bridged_enums.contains_key(name) {
            crate::log::warn(&format!(
                "Redefining variable or colliding with bridged type: {name}"
            ));
        }
        env.values.insert(name.to_string(), value);
    }

    pub fn get(this: &Rc<RefCell<Environment>>, name: &str) -> Result<Value, InterpError> {
        if let Some(prefix) = this.borrow().prefixed_imports.get(name) {
            return Ok(Value::Module(prefix.clone()));
        }
        if name.contains('.') {
            let parts: Vec<&str> = name.split('.').collect();
            if parts.len() == 2 {
                let prefix = parts[0];
                let ident = parts[1];
                let env = this.borrow();
                if let Some(prefix_env) = env.prefixed_imports.get(prefix) {
                    return Environment::get(prefix_env, ident).map_err(|e| {
                        InterpError::runtime(format!(
                            "Undefined name '{ident}' in imported prefix '{prefix}'. Original error: {}",
                            e.message().unwrap_or("unknown")
                        ))
                    });
                }
            } else {
                return Err(InterpError::runtime(format!(
                    "Complex prefixed access not supported: {name}. Use the form prefix.identifier."
                )));
            }
        }

        let mut current: Rc<RefCell<Environment>> = this.clone();
        loop {
            let env = current.borrow();
            if env.prefixed_imports.contains_key(name) {
                let prefix_env = env.prefixed_imports[name].clone();
                return Ok(Value::Module(prefix_env));
            }
            if let Some(v) = env.values.get(name) {
                return Ok(v.clone());
            }
            if env.bridged_classes.contains_key(name) {
                return Ok(Value::BridgedClass(env.bridged_classes[name].clone()));
            }
            if env.bridged_enums.contains_key(name) {
                return Ok(Value::BridgedEnum(env.bridged_enums[name].clone()));
            }
            match &env.enclosing {
                Some(parent) => {
                    let parent = parent.clone();
                    drop(env);
                    current = parent;
                }
                None => return Err(InterpError::runtime(format!("Undefined variable: {name}"))),
            }
        }
    }

    pub fn assign(
        this: &Rc<RefCell<Environment>>,
        name: &str,
        value: Value,
    ) -> Result<Value, InterpError> {
        let mut current: Rc<RefCell<Environment>> = this.clone();
        loop {
            if current.borrow().values.contains_key(name) {
                current
                    .borrow_mut()
                    .values
                    .insert(name.to_string(), value.clone());
                return Ok(value);
            }
            if current.borrow().bridged_classes.contains_key(name) {
                return Err(InterpError::runtime(format!(
                    "Cannot assign to the name of a bridged class: {name}"
                )));
            }
            if current.borrow().bridged_enums.contains_key(name) {
                return Err(InterpError::runtime(format!(
                    "Cannot assign to the name of a bridged enum: {name}"
                )));
            }
            let parent = match current.borrow().enclosing.clone() {
                Some(p) => p,
                None => {
                    return Err(InterpError::runtime(format!(
                        "Assigning to undefined variable '{name}'."
                    )));
                }
            };
            current = parent;
        }
    }

    pub fn is_defined_locally(this: &Rc<RefCell<Environment>>, name: &str) -> bool {
        this.borrow().values.contains_key(name)
    }

    pub fn find_defining_environment(
        this: &Rc<RefCell<Environment>>,
        name: &str,
    ) -> Option<Rc<RefCell<Environment>>> {
        let mut current = this.clone();
        loop {
            if current.borrow().values.contains_key(name) {
                return Some(current);
            }
            let enclosing = current.borrow().enclosing.clone();
            {
                let parent = enclosing?;
                current = parent
            }
        }
    }

    pub fn define_bridge(this: &Rc<RefCell<Environment>>, bridged: Arc<BridgedClassDef>) {
        let mut env = this.borrow_mut();
        if env.values.contains_key(&bridged.name)
            || env.bridged_classes.contains_key(&bridged.name)
            || env.bridged_enums.contains_key(&bridged.name)
        {
            crate::log::warn(&format!(
                "Redefining bridged class or colliding with existing definition: {}",
                bridged.name
            ));
        }
        env.bridged_classes
            .insert(bridged.name.clone(), bridged.clone());
    }

    pub fn define_bridged_enum(this: &Rc<RefCell<Environment>>, bridged: Rc<BridgedEnumDef>) {
        let mut env = this.borrow_mut();
        if env.values.contains_key(&bridged.name)
            || env.bridged_classes.contains_key(&bridged.name)
            || env.bridged_enums.contains_key(&bridged.name)
        {
            crate::log::warn(&format!(
                "Redefining bridged enum or colliding with existing definition: {}",
                bridged.name
            ));
        }
        env.bridged_enums.insert(bridged.name.clone(), bridged);
    }

    pub fn define_prefixed_import(
        this: &Rc<RefCell<Environment>>,
        prefix: &str,
        import_env: Rc<RefCell<Environment>>,
    ) {
        this.borrow_mut()
            .prefixed_imports
            .insert(prefix.to_string(), import_env);
    }

    pub fn add_unnamed_extension(this: &Rc<RefCell<Environment>>, ext: Rc<ExtensionDef>) {
        this.borrow_mut().unnamed_extensions.push(ext);
    }

    pub fn annotate_runtime_type(
        this: &Rc<RefCell<Environment>>,
        value: &Value,
        t: Option<Rc<RuntimeType>>,
    ) {
        let (Some(t), Some(ptr)) = (t, value.ptr()) else {
            return;
        };
        if matches!(
            value,
            Value::Int(_) | Value::Double(_) | Value::Str(_) | Value::Bool(_)
        ) {
            return;
        }
        this.borrow_mut().annotated_types.insert(ptr, t);
    }

    pub fn get_annotated_runtime_type(
        this: &Rc<RefCell<Environment>>,
        value: &Value,
    ) -> Option<Rc<RuntimeType>> {
        if matches!(
            value,
            Value::Int(_) | Value::Double(_) | Value::Str(_) | Value::Bool(_)
        ) {
            return None;
        }
        let ptr = value.ptr()?;
        this.borrow().annotated_types.get(&ptr).cloned()
    }

    pub fn get_runtime_type(
        this: &Rc<RefCell<Environment>>,
        value: &Value,
    ) -> Option<Rc<RuntimeType>> {
        if let Some(ann) = Environment::get_annotated_runtime_type(this, value) {
            return Some(ann);
        }
        match value {
            Value::Module(_) => None,
            Value::Bridged(_) | Value::BridgedClass(_) | Value::BridgedEnum(_) => {
                runtime_type_of(Some(value))
            }
            _ => runtime_type_of(Some(value)),
        }
    }

    pub fn shallow_copy_filtered(
        this: &Rc<RefCell<Environment>>,
        show: Option<&[String]>,
        hide: Option<&[String]>,
    ) -> Rc<RefCell<Environment>> {
        let new_env = Environment {
            enclosing: this.borrow().enclosing.clone(),
            values: HashMap::new(),
            bridged_classes: HashMap::new(),
            bridged_enums: HashMap::new(),
            unnamed_extensions: Vec::new(),
            prefixed_imports: HashMap::new(),
            annotated_types: HashMap::new(),
        };
        let new_env = Rc::new(RefCell::new(new_env));
        let env = this.borrow();
        let include = |name: &str| -> bool {
            if let Some(show) = show {
                show.contains(&name.to_string())
            } else if let Some(hide) = hide {
                !hide.contains(&name.to_string())
            } else {
                true
            }
        };
        for (name, value) in env.values.iter() {
            if include(name) {
                new_env
                    .borrow_mut()
                    .values
                    .insert(name.clone(), value.clone());
            }
        }
        for (name, bc) in env.bridged_classes.iter() {
            if include(name) {
                new_env
                    .borrow_mut()
                    .bridged_classes
                    .insert(name.clone(), bc.clone());
            }
        }
        for (name, be) in env.bridged_enums.iter() {
            if include(name) {
                new_env
                    .borrow_mut()
                    .bridged_enums
                    .insert(name.clone(), be.clone());
            }
        }
        for (name, p) in env.prefixed_imports.iter() {
            if include(name) {
                new_env
                    .borrow_mut()
                    .prefixed_imports
                    .insert(name.clone(), p.clone());
            }
        }
        new_env
            .borrow_mut()
            .unnamed_extensions
            .extend(env.unnamed_extensions.iter().cloned());
        new_env
    }

    pub fn import_environment(
        this: &Rc<RefCell<Environment>>,
        other: &Rc<RefCell<Environment>>,
        show: Option<&[String]>,
        hide: Option<&[String]>,
    ) -> Result<(), InterpError> {
        let source = if show.is_some() || hide.is_some() {
            Environment::shallow_copy_filtered(other, show, hide)
        } else {
            other.clone()
        };
        let source_values: Vec<(String, Value)> = source
            .borrow()
            .values
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        let source_classes: Vec<(String, Arc<BridgedClassDef>)> = source
            .borrow()
            .bridged_classes
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        let source_enums: Vec<(String, Rc<BridgedEnumDef>)> = source
            .borrow()
            .bridged_enums
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        let source_imports: Vec<(String, Rc<RefCell<Environment>>)> = source
            .borrow()
            .prefixed_imports
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        let source_extensions: Vec<Rc<ExtensionDef>> = source.borrow().unnamed_extensions.clone();

        let mut env = this.borrow_mut();
        for (name, value) in source_values {
            if let Some(existing) = env.values.get(&name) {
                if !crate::value::value_eq(existing, &value) {
                    return Err(InterpError::runtime(format!(
                        "Name conflict in environment: Symbol '{name}' is already defined with a different value."
                    )));
                }
                continue;
            }
            if env.bridged_classes.contains_key(&name)
                || env.bridged_enums.contains_key(&name)
                || env.prefixed_imports.contains_key(&name)
            {
                return Err(InterpError::runtime(format!(
                    "Name conflict in environment: Symbol '{name}' is already defined."
                )));
            }
            env.values.insert(name, value);
        }
        for (name, bc) in source_classes {
            if let Some(existing) = env.bridged_classes.get(&name) {
                if !std::sync::Arc::ptr_eq(existing, &bc) {
                    return Err(InterpError::runtime(format!(
                        "Name conflict in environment: Symbol '{name}' (bridged class) is already defined with a different value."
                    )));
                }
                continue;
            }
            if env.values.contains_key(&name)
                || env.bridged_enums.contains_key(&name)
                || env.prefixed_imports.contains_key(&name)
            {
                return Err(InterpError::runtime(format!(
                    "Name conflict in environment: Symbol '{name}' (bridged class) is already defined."
                )));
            }
            env.bridged_classes.insert(name, bc.clone());
        }
        for (name, be) in source_enums {
            if let Some(existing) = env.bridged_enums.get(&name) {
                if !Rc::ptr_eq(existing, &be) {
                    return Err(InterpError::runtime(format!(
                        "Name conflict in environment: Symbol '{name}' (bridged enum) is already defined with a different value."
                    )));
                }
                continue;
            }
            if env.values.contains_key(&name)
                || env.bridged_classes.contains_key(&name)
                || env.prefixed_imports.contains_key(&name)
            {
                return Err(InterpError::runtime(format!(
                    "Name conflict in environment: Symbol '{name}' (bridged enum) is already defined."
                )));
            }
            env.bridged_enums.insert(name, be);
        }
        for (name, p) in source_imports {
            if let Some(existing) = env.prefixed_imports.get(&name) {
                if !Rc::ptr_eq(existing, &p) {
                    return Err(InterpError::runtime(format!(
                        "Name conflict in environment: Symbol '{name}' (prefixed import) is already defined with a different environment."
                    )));
                }
                continue;
            }
            if env.values.contains_key(&name)
                || env.bridged_classes.contains_key(&name)
                || env.bridged_enums.contains_key(&name)
            {
                return Err(InterpError::runtime(format!(
                    "Name conflict in environment: Symbol '{name}' (prefixed import) is already defined or collides with another symbol type."
                )));
            }
            env.prefixed_imports.insert(name, p);
        }
        env.unnamed_extensions.extend(source_extensions);
        Ok(())
    }

    /// Merges the *public* top-level names of `other` into `this`, mirroring
    /// Dart's `export` directive. Private (`_`-prefixed) names and prefixed
    /// imports stay library-local and are not re-exported; conflict
    /// detection matches [`Environment::import_environment`].
    pub fn export_environment(
        this: &Rc<RefCell<Environment>>,
        other: &Rc<RefCell<Environment>>,
        show: Option<&[String]>,
        hide: Option<&[String]>,
    ) -> Result<(), InterpError> {
        let public = Environment::new();
        {
            let src = other.borrow();
            let mut dst = public.borrow_mut();
            for (name, value) in src.values.iter() {
                if !name.starts_with('_') {
                    dst.values.insert(name.clone(), value.clone());
                }
            }
            for (name, bc) in src.bridged_classes.iter() {
                if !name.starts_with('_') {
                    dst.bridged_classes.insert(name.clone(), bc.clone());
                }
            }
            for (name, be) in src.bridged_enums.iter() {
                if !name.starts_with('_') {
                    dst.bridged_enums.insert(name.clone(), be.clone());
                }
            }
            dst.unnamed_extensions
                .extend(src.unnamed_extensions.iter().cloned());
        }
        if show.is_some() || hide.is_some() {
            let filtered = Environment::shallow_copy_filtered(&public, show, hide);
            Environment::import_environment(this, &filtered, None, None)
        } else {
            Environment::import_environment(this, &public, None, None)
        }
    }

    pub fn find_extension_member(
        this: &Rc<RefCell<Environment>>,
        target: &Value,
        name: &str,
    ) -> Option<crate::callable::Callable> {
        let target_type = Environment::get_runtime_type(this, target)?;
        let mut current = this.clone();
        loop {
            let ext_candidates: Vec<Rc<ExtensionDef>> = {
                let env = current.borrow();
                let mut v: Vec<Rc<ExtensionDef>> = env.unnamed_extensions.clone();
                for value in env.values.values() {
                    if let Value::Extension(e) = value {
                        v.push(e.clone());
                    }
                }
                v
            };
            for ext in ext_candidates {
                if target_type.is_subtype_of(&ext.on_type, Some(target))
                    && let Some(member) = ext.find_member(name)
                {
                    return Some(member);
                }
            }
            let parent = match current.borrow().enclosing.clone() {
                Some(p) => p,
                None => break,
            };
            current = parent;
        }
        None
    }

    pub fn to_bridged_class(
        this: &Rc<RefCell<Environment>>,
        value: &Value,
    ) -> Option<Arc<BridgedClassDef>> {
        let native_name = crate::value::native_type_name(value);
        let mut current = this.clone();
        loop {
            let env = current.borrow();
            if let Some(name) = native_name {
                for bc in env.bridged_classes.values() {
                    if bc.native_type.as_deref() == Some(name) {
                        return Some(bc.clone());
                    }
                }
            }
            for bc in env.bridged_classes.values() {
                if bc.is_subtype_of_fn.as_ref().is_some_and(|f| f(value)) {
                    return Some(bc.clone());
                }
            }
            match &env.enclosing {
                Some(p) => {
                    let p = p.clone();
                    drop(env);
                    current = p;
                }
                None => break,
            }
        }
        None
    }

    pub fn to_bridged_instance(this: &Rc<RefCell<Environment>>, value: &Value) -> Option<Value> {
        match value {
            Value::Bridged(_) => Some(value.clone()),
            v => {
                let bc = Environment::to_bridged_class(this, v)?;
                Some(Value::Bridged(Rc::new(RefCell::new(
                    crate::class::BridgedInstanceData {
                        bridged_class: bc,
                        native: v.clone(),
                        type_arguments: vec![],
                        bridged_super_object: None,
                    },
                ))))
            }
        }
    }
}