Repository: awslabs/llrt
File: llrt_modules/src/modules/perf_hooks.rs (Rust)

Code before:
    let time_origin = TIME_ORIGIN.load(Ordering::Relaxed) as f64;

    time_origin / 1e6
}

fn now() -> f64 {
    let now = Utc::now().timestamp_nanos_opt().unwrap_or_default() as f64;
    let started = TIME_ORIGIN.load(Ordering::Relaxed) as f64;

    (now - started) / 1e6
}

fn to_json(ctx: Ctx<'_>) -> Result<Object<'_>> {
    let obj = Object::new(ctx.clone())?;

    obj.set("timeOrigin", get_time_origin())?;

    Ok(obj)
}

pub fn new_performance(ctx: Ctx<'_>) -> Result<Object<'_>> {
    if TIME_ORIGIN.load(Ordering::Relaxed) == 0 {
        let time_origin = Utc::now().timestamp_nanos_opt().unwrap_or_default() as usize;
        TIME_ORIGIN.store(time_origin, Ordering::Relaxed)
    }

    let performance = Object::new(ctx)?;
    performance.set("timeOrigin", get_time_origin())?;
    performance.set("now", Func::from(now))?;
    performance.set(PredefinedAtom::ToJSON, Func::from(to_json))?;
    Ok(performance)
}

pub struct PerfHooksModule;

impl ModuleDef for PerfHooksModule {
    fn declare(declare: &Declarations<'_>) -> Result<()> {
        declare.declare("performance")?;
        declare.declare("default")?;
        Ok(())
    }

    fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> {
        export_default(ctx, exports, |default| {
            let performance = new_performance(ctx.clone())?;
            default.set("performance", performance)?;
            Ok(())
        })
    }
}


Review comment (refactor):
Move this out and create an "init" function that sets time origin if not already set. Then call this `init` function from vm.rs

Code after:
    let time_origin = TIME_ORIGIN.load(Ordering::Relaxed) as f64;

    time_origin / 1e6
}

fn now() -> f64 {
    let now = Utc::now().timestamp_nanos_opt().unwrap_or_default() as f64;
    let started = TIME_ORIGIN.load(Ordering::Relaxed) as f64;

    (now - started) / 1e6
}

fn to_json(ctx: Ctx<'_>) -> Result<Object<'_>> {
    let obj = Object::new(ctx.clone())?;

    obj.set("timeOrigin", get_time_origin())?;

    Ok(obj)
}

// For accuracy reasons, this function should be executed when the vm is initialized
pub fn init() {
    if TIME_ORIGIN.load(Ordering::Relaxed) == 0 {
        let time_origin = Utc::now().timestamp_nanos_opt().unwrap_or_default() as usize;
        TIME_ORIGIN.store(time_origin, Ordering::Relaxed)
    }
}

pub(crate) fn new_performance(ctx: Ctx<'_>) -> Result<Object<'_>> {
    let global = ctx.globals();
    global.get("performance").or_else(|_| {
        let performance = Object::new(ctx)?;
        performance.set("timeOrigin", get_time_origin())?;
        performance.set("now", Func::from(now))?;
        performance.set(PredefinedAtom::ToJSON, Func::from(to_json))?;
        global.set("performance", performance)?;
        global.get("performance")
    })
}

pub struct PerfHooksModule;

impl ModuleDef for PerfHooksModule {
    fn declare(declare: &Declarations<'_>) -> Result<()> {
        declare.declare("performance")?;
        declare.declare("default")?;
        Ok(())
    }

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