char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somcore: add dependencies list to Extension81eaeb3

main
2.2 KiB63 linesraw
1use avril_core::{Extension, Runtime, RuntimeOptions};
2
3fn runtime() -> Runtime {
4    Runtime::new(RuntimeOptions {
5        extensions: vec![Extension {
6            name: "ext:test",
7            dependencies: &[],
8            ops: vec![],
9            modules: &[("ext:test/main.js", r#"
10                import {clone} from 'ext:core/structured_clone';
11                const hosts=new WeakMap();
12                globalThis.hostCloneCount=0;
13                globalThis.makeHost=value=>{const object={};hosts.set(object,value);return object;};
14                globalThis.hostValue=object=>hosts.get(object);
15                const cloneHost=source=>{
16                    if(hosts.get(source)==='fail')throw globalThis.reason;
17                    hostCloneCount++;
18                    return makeHost(hosts.get(source));
19                };
20                globalThis.cloneForTest=value=>clone(value,[],object=>hosts.has(object)?1:0,cloneHost,message=>new Error(message));
21            "#)],
22            entry_point: Some("ext:test/main.js"),
23        }],
24        ..Default::default()
25    }).unwrap()
26}
27
28#[test]
29fn native_clone_bridge_preserves_extension_host_identity_without_web_policy() {
30    let mut runtime = runtime();
31    assert!(runtime.execute_script::<bool>("host graph",r#"
32        const host=makeHost('value'), source={host,again:host};source.self=source;
33        const copy=cloneForTest(source);
34        copy!==source && copy.self===copy && copy.host===copy.again && copy.host!==host && hostValue(copy.host)==='value' && hostCloneCount===1
35    "#).unwrap());
36}
37
38#[test]
39fn policy_and_getter_exceptions_preserve_identity_and_allow_reentrant_clones() {
40    let mut runtime = runtime();
41    runtime
42        .execute_script::<()>(
43            "exceptions",
44            r#"
45        globalThis.reason={reason:true};
46        for(const source of [makeHost('fail'),{get x(){throw reason;}}]){
47            try{cloneForTest(source);throw Error('Accepted');}catch(e){if(e!==reason)throw e;}
48        }
49    "#,
50        )
51        .unwrap();
52    assert!(
53        runtime
54            .execute_script::<bool>(
55                "next turn",
56                r#"
57        const result=cloneForTest({get x(){return cloneForTest({value:3});}});
58        result.x.value===3
59    "#
60            )
61            .unwrap()
62    );
63}