char/avril

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

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

Charlotte Somdeslop: consolidate public type and registry test utils7325e8c

main
2.2 KiB75 linesraw
1mod env;
2mod host;
3
4use std::cell::RefCell;
5use std::convert::Infallible;
6use std::rc::Rc;
7use std::sync::Arc;
8
9use avril_caps::{Capabilities, Category, Pledge};
10use avril_core::{Extension, Op, PublicModule};
11use avril_host::environment::Environment;
12
13pub fn init(
14    caps: Arc<Capabilities>,
15    environment: Rc<RefCell<Environment>>,
16    args: Vec<String>,
17) -> Extension {
18    let mut ops = env::ops(caps.clone(), environment);
19    ops.extend(host::ops(caps.clone(), args));
20    ops.push(Op::new(
21        "op_pledge",
22        move |request: Vec<(String, Option<Vec<String>>, Vec<String>)>| {
23            let error = request
24                .into_iter()
25                .map(|(category, allow, deny)| {
26                    category
27                        .parse::<Category>()
28                        .map(|category| (category, allow, deny))
29                })
30                .collect::<Result<Pledge, _>>()
31                .and_then(|request| caps.pledge(request))
32                .map_err(|error| error.to_string())
33                .err();
34            Ok::<_, Infallible>(error)
35        },
36    ));
37    Extension {
38        name: "ext:base",
39        dependencies: &[
40            "ext:console",
41            "ext:file",
42            "ext:http",
43            "ext:net",
44            "ext:process",
45            "ext:stdio",
46            "ext:test",
47            "ext:web",
48        ],
49        ops,
50        modules: const {
51            &[
52                PUBLIC_MODULES[0].module,
53                PUBLIC_MODULES[1].module,
54                (
55                    "ext:base/env.js",
56                    include_str!(concat!(env!("OUT_DIR"), "/env.js")),
57                ),
58                (
59                    "ext:base/host.js",
60                    include_str!(concat!(env!("OUT_DIR"), "/host.js")),
61                ),
62                (
63                    "ext:base/pledge.js",
64                    include_str!(concat!(env!("OUT_DIR"), "/pledge.js")),
65                ),
66            ]
67        },
68        entry_point: Some("ext:base/main.js"),
69    }
70}
71
72pub const PUBLIC_MODULES: &[PublicModule] = &[
73    avril_core::public_module!("avril:std", "ext:base", "main"),
74    avril_core::public_module!("avril:std/sync", "ext:base", "sync"),
75];