ps-blitz-script 0.7.1

Engine-neutral script execution and document contracts for Blitz
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
use std::any::Any;
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::rc::Rc;

use super::syntax::{Pattern, Stmt};
use blitz_dom::NodeId;

pub type Result<T, E = Error> = std::result::Result<T, E>;
pub type Env = Rc<RefCell<Environment>>;

#[derive(Clone)]
pub struct Error {
    pub name: String,
    pub message: String,
    pub thrown: Option<Value>,
    pub stack: Vec<String>,
}
impl Error {
    pub fn new(name: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            message: message.into(),
            thrown: None,
            stack: Vec::new(),
        }
    }
    pub fn type_error(message: impl Into<String>) -> Self {
        Self::new("TypeError", message)
    }
    pub fn syntax(message: impl Into<String>) -> Self {
        Self::new("SyntaxError", message)
    }
    pub fn thrown(value: Value) -> Self {
        let stack = value
            .get("\0error_stack")
            .items()
            .iter()
            .take(64)
            .map(Value::text)
            .collect();
        Self {
            name: value.get("name").text(),
            message: value.get("message").text(),
            thrown: Some(value),
            stack,
        }
    }
    pub fn value(&self) -> Value {
        let value = self
            .thrown
            .clone()
            .unwrap_or_else(|| Value::error(&self.name, &self.message));
        value.put_hidden(
            "\0error_stack",
            Value::array(self.stack.iter().map(Value::string).collect()),
        );
        value
    }
}
impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.name, self.message)?;
        for frame in &self.stack {
            write!(f, "\n  at {frame}")?;
        }
        let mut seen = Vec::new();
        let mut cause = self.thrown.as_ref().map(|value| value.get("cause"));
        while let Some(value) = cause {
            if matches!(value, Value::Undefined)
                || seen.len() >= 16
                || seen.iter().any(|prior: &Value| prior.same(&value))
            {
                break;
            }
            write!(
                f,
                "\nCaused by: {}: {}",
                value.get("name").text(),
                value.get("message").text()
            )?;
            for frame in value.get("\0error_stack").items().iter().take(64) {
                write!(f, "\n  at {}", frame.text())?;
            }
            cause = Some(value.get("cause"));
            seen.push(value);
        }
        Ok(())
    }
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}
impl std::error::Error for Error {}

#[derive(Clone, Default)]
pub enum Value {
    #[default]
    Undefined,
    Null,
    Bool(bool),
    Number(f64),
    String(Rc<[u16]>),
    Object(Rc<RefCell<Object>>),
}
impl fmt::Debug for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Object(object) => write!(
                f,
                "[{}]",
                object
                    .try_borrow()
                    .map_or_else(|_| String::from("borrowed"), |o| o.kind.clone())
            ),
            _ => write!(f, "{}", self.text()),
        }
    }
}
#[derive(Clone, Debug)]
pub struct Property {
    pub value: Value,
    pub get: Option<Value>,
    pub set: Option<Value>,
    pub enumerable: bool,
}
impl Property {
    pub fn data(value: Value) -> Self {
        Self {
            value,
            get: None,
            set: None,
            enumerable: true,
        }
    }
}
pub struct Object {
    pub kind: String,
    pub native: Option<String>,
    pub realm: Option<u64>,
    pub props: BTreeMap<String, Property>,
    pub indexed: Vec<Value>,
    pub prototype: Option<Value>,
    pub function: Option<Rc<Function>>,
    pub payload: Option<Rc<dyn Any>>,
    pub frozen: bool,
}
impl Object {
    pub fn new(kind: impl Into<String>) -> Self {
        Self {
            kind: kind.into(),
            native: None,
            realm: None,
            props: BTreeMap::new(),
            indexed: Vec::new(),
            prototype: None,
            function: None,
            payload: None,
            frozen: false,
        }
    }
}
#[derive(Clone)]
pub struct Function {
    pub params: Vec<Pattern>,
    pub body: Vec<Stmt>,
    pub env: Env,
    pub arrow: bool,
    pub asynchronous: bool,
    pub source_offset: Option<usize>,
    pub this: Value,
    pub name: Option<String>,
}
pub struct Environment {
    pub bindings: BTreeMap<String, Value>,
    pub parent: Option<Env>,
}
impl Environment {
    pub fn new(parent: Option<Env>) -> Env {
        let environment = Rc::new(RefCell::new(Self {
            bindings: BTreeMap::new(),
            parent,
        }));
        track_environment(&environment);
        environment
    }
}
impl Value {
    pub fn string(text: impl AsRef<str>) -> Self {
        Self::String(text.as_ref().encode_utf16().collect::<Vec<_>>().into())
    }
    pub fn object(kind: impl Into<String>) -> Self {
        let object = Rc::new(RefCell::new(Object::new(kind)));
        track_object(&object);
        Self::Object(object)
    }
    pub fn number(number: impl Into<f64>) -> Self {
        Self::Number(number.into())
    }
    pub fn array(items: Vec<Value>) -> Self {
        let value = Self::object("Array");
        if let Self::Object(object) = &value {
            object.borrow_mut().indexed = items;
        }
        value
    }
    pub fn native(name: impl Into<String>) -> Self {
        let value = Self::object("Native");
        if let Some(object) = value.as_object() {
            object.borrow_mut().native = Some(name.into());
        }
        value
    }
    pub fn promise(result: Result<Value>) -> Self {
        let promise = Self::pending_promise();
        let (state, value) = match result {
            Ok(value) => (1.0, value),
            Err(error) => (2.0, error.value()),
        };
        promise.put_hidden("state", Self::Number(state));
        promise.put_hidden("value", value);
        promise
    }
    pub fn pending_promise() -> Self {
        let value = Self::object("Promise");
        value.put_hidden("state", Self::Number(0.0));
        value.put_hidden("value", Self::Undefined);
        value
    }
    pub fn error(name: impl AsRef<str>, message: impl AsRef<str>) -> Self {
        let value = Self::object("Error");
        value.put("name", Self::string(name));
        value.put("message", Self::string(message));
        value
    }
    pub fn as_object(&self) -> Option<Rc<RefCell<Object>>> {
        if let Self::Object(o) = self {
            Some(Rc::clone(o))
        } else {
            None
        }
    }
    pub fn kind(&self) -> String {
        match self {
            Self::Undefined => "Undefined".into(),
            Self::Null => "Null".into(),
            Self::Bool(_) => "Boolean".into(),
            Self::Number(_) => "Number".into(),
            Self::String(_) => "String".into(),
            Self::Object(o) => o.borrow().kind.clone(),
        }
    }
    pub fn get(&self, key: &str) -> Value {
        let Self::Object(object) = self else {
            return Self::Undefined;
        };
        let object = object.borrow();
        if key == "__native" {
            if let Some(name) = &object.native {
                return Self::string(name);
            }
        }
        if let Some(value) = object.binary_property(key) {
            return value;
        }
        if let Some(property) = object.props.get(key) {
            return property.value.clone();
        }
        if key == "length"
            && matches!(
                object.kind.as_str(),
                "Array"
                    | "NodeList"
                    | "HTMLCollection"
                    | "Uint8Array"
                    | "Uint8ClampedArray"
                    | "Uint16Array"
                    | "Uint32Array"
                    | "Int8Array"
                    | "Int16Array"
                    | "Int32Array"
                    | "Float32Array"
                    | "Float64Array"
            )
        {
            return Self::Number(object.indexed.len() as f64);
        }
        if let Ok(index) = key.parse::<usize>() {
            return object.indexed.get(index).cloned().unwrap_or_default();
        }
        Self::Undefined
    }
    pub fn put(&self, key: &str, value: Value) {
        let Self::Object(cell) = self else {
            return;
        };
        let mut object = cell.borrow_mut();
        if object.frozen {
            return;
        }
        if let Some(payload) = object
            .payload
            .as_ref()
            .filter(|payload| {
                payload.is::<super::buffer::ArrayBuffer>() || payload.is::<super::buffer::TypedArray>()
            })
            .cloned()
        {
            // Numeric coercion can read the receiver or another view of its buffer.
            drop(object);
            if super::buffer::set_property(payload.as_ref(), key, &value) {
                return;
            }
            object = cell.borrow_mut();
        }
        if let Ok(index) = key.parse::<usize>() {
            if index < 1_048_576
                && (index < object.indexed.len()
                    || matches!(
                        object.kind.as_str(),
                        "Array"
                            | "Uint8Array"
                            | "Uint8ClampedArray"
                            | "Uint16Array"
                            | "Uint32Array"
                            | "Int8Array"
                            | "Int16Array"
                            | "Int32Array"
                            | "Float32Array"
                            | "Float64Array"
                    ))
            {
                if index >= object.indexed.len() {
                    object.indexed.resize(index + 1, Self::Undefined);
                }
                object.indexed[index] = value;
                return;
            }
        }
        if key == "length" && object.kind == "Array" {
            let length = value.number_value();
            if length.is_finite() && (0.0..=1_048_576.0).contains(&length) {
                object.indexed.resize(length as usize, Self::Undefined);
            }
            return;
        }
        if let Some(property) = object.props.get_mut(key) {
            property.value = value;
        } else {
            object.props.insert(key.into(), Property::data(value));
        }
    }
    /// Store implementation-owned metadata without exposing it to enumeration.
    pub fn put_hidden(&self, key: &str, value: Value) {
        let Self::Object(object) = self else {
            return;
        };
        let mut object = object.borrow_mut();
        if object.frozen {
            return;
        }
        object.props.insert(
            key.into(),
            Property {
                value,
                get: None,
                set: None,
                enumerable: false,
            },
        );
    }
    pub fn items(&self) -> Vec<Value> {
        match self {
            Self::Object(o) => {
                let object = o.borrow();
                if object.indexed.is_empty()
                    && let Some(blob) = object.payload.as_deref().and_then(|payload| {
                        payload.downcast_ref::<super::web::BlobBytes>()
                    })
                {
                    return blob.0.iter().map(|&byte| Self::Number(f64::from(byte))).collect();
                }
                object
                    .binary_items()
                    .unwrap_or_else(|| object.indexed.clone())
            }
            Self::String(s) => s
                .iter()
                .map(|unit| Self::String(Rc::from([*unit])))
                .collect(),
            _ => Vec::new(),
        }
    }
    pub fn node_id(&self) -> Option<NodeId> {
        let object = self.as_object()?;
        let object = object.borrow();
        if let Some(id) = object
            .payload
            .as_ref()
            .and_then(|payload| payload.downcast_ref::<NodeId>())
        {
            return Some(*id);
        }
        object
            .props
            .get("__node")?
            .value
            .text()
            .parse::<u64>()
            .ok()
            .map(NodeId::from_u64)
    }
    pub fn same(&self, other: &Value) -> bool {
        match (self, other) {
            (Self::Undefined, Self::Undefined) | (Self::Null, Self::Null) => true,
            (Self::Bool(a), Self::Bool(b)) => a == b,
            (Self::Number(a), Self::Number(b)) => a == b,
            (Self::String(a), Self::String(b)) => a == b,
            (Self::Object(a), Self::Object(b)) => Rc::ptr_eq(a, b),
            _ => false,
        }
    }
    pub fn truthy(&self) -> bool {
        match self {
            Self::Undefined | Self::Null => false,
            Self::Bool(v) => *v,
            Self::Number(v) => *v != 0.0 && !v.is_nan(),
            Self::String(v) => !v.is_empty(),
            Self::Object(_) => true,
        }
    }
    pub fn number_value(&self) -> f64 {
        match self {
            Self::Undefined => f64::NAN,
            Self::Null => 0.0,
            Self::Bool(v) => u8::from(*v) as f64,
            Self::Number(v) => *v,
            Self::String(s) => {
                let s = String::from_utf16_lossy(s);
                let s = s.trim();
                if s.is_empty() {
                    0.0
                } else if let Some(v) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
                    u64::from_str_radix(v, 16).map_or(f64::NAN, |v| v as f64)
                } else {
                    s.parse().unwrap_or(f64::NAN)
                }
            }
            Self::Object(_) => self.text().parse().unwrap_or(f64::NAN),
        }
    }
    pub fn text(&self) -> String {
        match self {
            Self::Undefined => "undefined".into(),
            Self::Null => "null".into(),
            Self::Bool(v) => v.to_string(),
            Self::Number(v) => {
                if v.is_nan() {
                    "NaN".into()
                } else if *v == 0.0 {
                    "0".into()
                } else {
                    v.to_string()
                }
            }
            Self::String(v) => String::from_utf16_lossy(v),
            Self::Object(o) => {
                let o = o.borrow();
                if matches!(
                    o.kind.as_str(),
                    "Array" | "Uint8Array" | "Uint8ClampedArray" | "Uint32Array" | "Float32Array"
                ) {
                    let items = o.binary_items();
                    items
                        .as_deref()
                        .unwrap_or(&o.indexed)
                        .iter()
                        .map(|v| {
                            if matches!(v, Self::Null | Self::Undefined) {
                                String::new()
                            } else {
                                v.text()
                            }
                        })
                        .collect::<Vec<_>>()
                        .join(",")
                } else if o.kind == "Error" || o.kind == "DOMException" {
                    let name = o
                        .props
                        .get("name")
                        .map_or("Error".into(), |p| p.value.text());
                    let message = o
                        .props
                        .get("message")
                        .map_or(String::new(), |p| p.value.text());
                    if message.is_empty() {
                        name
                    } else {
                        format!("{name}: {message}")
                    }
                } else if o.kind == "URL" {
                    o.props
                        .get("href")
                        .map_or(String::new(), |p| p.value.text())
                } else {
                    format!(
                        "[object {}]",
                        if o.kind == "Object" {
                            "Object"
                        } else {
                            &o.kind
                        }
                    )
                }
            }
        }
    }
    pub fn from_json(value: serde_json::Value) -> Value {
        match value {
            serde_json::Value::Null => Self::Null,
            serde_json::Value::Bool(v) => Self::Bool(v),
            serde_json::Value::Number(v) => Self::Number(v.as_f64().unwrap_or(f64::NAN)),
            serde_json::Value::String(v) => Self::string(v),
            serde_json::Value::Array(v) => {
                Self::array(v.into_iter().map(Self::from_json).collect())
            }
            serde_json::Value::Object(v) => {
                let object = Self::object("Object");
                for (k, v) in v {
                    object.put(&k, Self::from_json(v));
                }
                object
            }
        }
    }
    pub fn to_json(&self) -> Result<serde_json::Value> {
        self.json_inner(&mut BTreeSet::new(), 0)
    }
    fn json_inner(&self, seen: &mut BTreeSet<usize>, depth: usize) -> Result<serde_json::Value> {
        if depth > 128 {
            return Err(Error::new("RangeError", "JSON nesting limit exceeded"));
        }
        Ok(match self {
            Self::Undefined | Self::Null => serde_json::Value::Null,
            Self::Bool(v) => serde_json::Value::Bool(*v),
            Self::Number(v)
                if v.is_finite()
                    && v.fract() == 0.0
                    && *v >= i64::MIN as f64
                    && *v < i64::MAX as f64 =>
            {
                serde_json::Value::Number(serde_json::Number::from(*v as i64))
            }
            Self::Number(v)
                if v.is_finite() && v.fract() == 0.0 && *v >= 0.0 && *v < u64::MAX as f64 =>
            {
                serde_json::Value::Number(serde_json::Number::from(*v as u64))
            }
            Self::Number(v) => serde_json::Number::from_f64(*v)
                .map_or(serde_json::Value::Null, serde_json::Value::Number),
            Self::String(v) => serde_json::Value::String(String::from_utf16_lossy(v)),
            Self::Object(o) => {
                let id = Rc::as_ptr(o) as usize;
                if !seen.insert(id) {
                    return Err(Error::type_error(
                        "cyclic value cannot be represented as JSON",
                    ));
                }
                let (kind, items, properties) = {
                    let o = o.borrow();
                    (o.kind.clone(), o.indexed.clone(), o.props.clone())
                };
                let result = if matches!(kind.as_str(), "Array" | "NodeList" | "HTMLCollection") {
                    serde_json::Value::Array(
                        items
                            .iter()
                            .map(|v| v.json_inner(seen, depth + 1))
                            .collect::<Result<_>>()?,
                    )
                } else {
                    let mut map = serde_json::Map::new();
                    for (k, p) in properties {
                        if p.enumerable
                            && !matches!(p.value, Self::Undefined)
                            && !matches!(p.value.kind().as_str(), "Native" | "Function")
                        {
                            map.insert(k, p.value.json_inner(seen, depth + 1)?);
                        }
                    }
                    serde_json::Value::Object(map)
                };
                seen.remove(&id);
                result
            }
        })
    }
}

#[derive(Default)]
pub(super) struct RealmHeap {
    pub(super) realm: u64,
    objects: Vec<std::rc::Weak<RefCell<Object>>>,
    environments: Vec<std::rc::Weak<RefCell<Environment>>>,
    exhausted: bool,
}
thread_local! {
    static ACTIVE_HEAP: RefCell<Option<std::rc::Weak<RefCell<RealmHeap>>>> = const { RefCell::new(None) };
}
pub(super) struct HeapScope(Option<std::rc::Weak<RefCell<RealmHeap>>>);
impl HeapScope {
    pub(super) fn enter(heap: &Rc<RefCell<RealmHeap>>) -> Self {
        Self(ACTIVE_HEAP.with(|active| active.replace(Some(Rc::downgrade(heap)))))
    }
}
impl Drop for HeapScope {
    fn drop(&mut self) {
        ACTIVE_HEAP.with(|active| {
            active.replace(self.0.take());
        });
    }
}
impl RealmHeap {
    pub(super) fn exhausted(&mut self) -> bool {
        if self.exhausted {
            self.prune_expired();
            self.exhausted = self.objects.len() + self.environments.len() > 131_072;
        }
        self.exhausted
    }
    fn prune_expired(&mut self) {
        self.objects.retain(|object| object.strong_count() > 0);
        self.environments
            .retain(|environment| environment.strong_count() > 0);
    }
    pub(super) fn release(&mut self) {
        let objects = self
            .objects
            .drain(..)
            .filter_map(|object| object.upgrade())
            .collect::<Vec<_>>();
        let environments = self
            .environments
            .drain(..)
            .filter_map(|environment| environment.upgrade())
            .collect::<Vec<_>>();
        for object in &objects {
            if let Ok(mut object) = object.try_borrow_mut() {
                object.props.clear();
                object.indexed.clear();
                object.prototype = None;
                object.function = None;
                object.payload = None;
            }
        }
        for environment in &environments {
            if let Ok(mut environment) = environment.try_borrow_mut() {
                environment.bindings.clear();
                environment.parent = None;
            }
        }
    }
}
fn track_object(object: &Rc<RefCell<Object>>) {
    ACTIVE_HEAP.with(|active| {
        if let Some(heap) = active.borrow().as_ref().and_then(std::rc::Weak::upgrade) {
            let mut heap = heap.borrow_mut();
            object.borrow_mut().realm = Some(heap.realm);
            heap.objects.push(Rc::downgrade(object));
            if heap.objects.len() + heap.environments.len() > 131_072 {
                heap.prune_expired();
                heap.exhausted = heap.objects.len() + heap.environments.len() > 131_072;
            }
        }
    });
}
fn track_environment(environment: &Env) {
    ACTIVE_HEAP.with(|active| {
        if let Some(heap) = active.borrow().as_ref().and_then(std::rc::Weak::upgrade) {
            let mut heap = heap.borrow_mut();
            heap.environments.push(Rc::downgrade(environment));
            if heap.objects.len() + heap.environments.len() > 131_072 {
                heap.prune_expired();
                heap.exhausted = heap.objects.len() + heap.environments.len() > 131_072;
            }
        }
    });
}