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
use rquickjs::{
    cstr,
    module::{Declarations, Exports, ModuleDef},
    Ctx, Function, Result,
};

use super::module::export_default;

// use crate::{module_builder::ModuleInfo, modules::module::export_default};

// use super::console::format_plain;

pub struct UtilModule;

impl ModuleDef for UtilModule {
    fn declare(declare: &Declarations<'_>) -> Result<()> {
        declare.declare(stringify!(TextDecoder))?;
        declare.declare(stringify!(TextEncoder))?;
        // declare.declare(stringify!(format))?;
        declare.declare(cstr!("default").to_bytes())?;
        Ok(())
    }

    fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> {
        export_default(ctx, exports, |default| {
            let globals = ctx.globals();

            let encoder: Function = globals.get(stringify!(TextEncoder))?;
            let decoder: Function = globals.get(stringify!(TextDecoder))?;

            default.set(stringify!(TextEncoder), encoder)?;
            default.set(stringify!(TextDecoder), decoder)?;
            // default.set("format", Func::from(format_plain))?;

            Ok(())
        })
    }
}

// impl From<UtilModule> for ModuleInfo<UtilModule> {
//     fn from(val: UtilModule) -> Self {
//         ModuleInfo {
//             name: "util",
//             module: val,
//         }
//     }
// }