Skip to content

Commit

Permalink
Fix process hr-time-res (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
richarddavison authored Feb 14, 2024
1 parent 05dfadf commit e27bf91
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
8 changes: 8 additions & 0 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fn stringify_primitive<'js>(
Type::Undefined => result.push_str(COLOR_BLACK),
Type::Int | Type::Float | Type::Bool => result.push_str(COLOR_YELLOW),
Type::Symbol => result.push_str(COLOR_GREEN),
Type::BigInt => result.push_str(COLOR_YELLOW),
_ => has_color = false,
}
}
Expand All @@ -144,6 +145,12 @@ fn stringify_primitive<'js>(
} else {
"false"
}),
Type::BigInt => {
let mut buffer = itoa::Buffer::new();
let big_int = value.as_big_int().unwrap();
result.push_str(buffer.format(big_int.clone().to_i64()?));
result.push('n');
}
Type::Int => {
let mut buffer = itoa::Buffer::new();
result.push_str(buffer.format(value.as_int().unwrap()))
Expand Down Expand Up @@ -194,6 +201,7 @@ fn is_primitive_like_or_void(typeof_value: Type) -> bool {
| Type::Int
| Type::Float
| Type::String
| Type::BigInt
| Type::Symbol
| Type::Unknown
)
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl StringBuilder {
}
}

pub fn atob<'js>(ctx: Ctx<'js>, encoded_value: String) -> Result<String> {
pub fn atob(ctx: Ctx<'_>, encoded_value: String) -> Result<String> {
let vec = bytes_from_b64(encoded_value.as_bytes()).or_throw(&ctx)?;
Ok(unsafe { String::from_utf8_unchecked(vec) })
}
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ mod xml;
use minimal_tracer::MinimalTracer;
use rquickjs::{AsyncContext, Module};
use std::{
env::{self},
env,
error::Error,
mem::MaybeUninit,
path::{Path, PathBuf},
process::exit,
sync::atomic::Ordering,
Expand All @@ -59,13 +60,17 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
#[global_allocator]
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;

pub static mut STARTED: MaybeUninit<Instant> = MaybeUninit::uninit();

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let now = Instant::now();

unsafe { STARTED.write(Instant::now()) };

MinimalTracer::register()?;
trace!("Started runtime");

let now = Instant::now();

let vm = Vm::new().await?;
trace!("Initialized VM in {}ms", now.elapsed().as_millis());

Expand Down
41 changes: 26 additions & 15 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::{
collections::HashMap,
env,
time::{SystemTime, UNIX_EPOCH},
};
use std::{collections::HashMap, env};

use rquickjs::{
atom::PredefinedAtom, convert::Coerced, function::Constructor, object::Property, prelude::Func,
Ctx, IntoJs, Object, Result, Value,
Array, BigInt, Ctx, Function, IntoJs, Object, Result, Value,
};

use crate::VERSION;
use crate::{STARTED, VERSION};

fn cwd() -> String {
env::current_dir().unwrap().to_string_lossy().to_string()
Expand All @@ -37,11 +33,26 @@ pub fn get_platform() -> &'static str {
platform
}

fn current_time_micros() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_micros() as u64
fn hr_time_big_int(ctx: Ctx<'_>) -> Result<BigInt> {
let started = unsafe { STARTED.assume_init() };
let elapsed = started.elapsed().as_nanos() as u64;

BigInt::from_u64(ctx, elapsed)
}

fn hr_time(ctx: Ctx<'_>) -> Result<Array<'_>> {
let started = unsafe { STARTED.assume_init() };
let elapsed = started.elapsed().as_nanos() as u64;

let seconds = elapsed / 1_000_000_000;
let remaining_nanos = elapsed % 1_000_000_000;

let array = Array::new(ctx)?;

array.set(0, seconds)?;
array.set(1, remaining_nanos)?;

Ok(array)
}

fn exit(code: i32) {
Expand All @@ -64,12 +75,12 @@ pub fn init(ctx: &Ctx<'_>) -> Result<()> {
let process_versions = Object::new(ctx.clone())?;
process_versions.set("llrt", VERSION)?;

let hr_time = Function::new(ctx.clone(), hr_time)?;
hr_time.set("bigint", Func::from(hr_time_big_int))?;

let release = Object::new(ctx.clone())?;
release.prop("name", Property::from("llrt").enumerable())?;

let hr_time = Object::new(ctx.clone())?;
hr_time.set("bigint", Func::from(current_time_micros))?;

let env_map: HashMap<String, String> = env::vars().collect();
let mut args: Vec<String> = env::args().collect();

Expand Down

0 comments on commit e27bf91

Please sign in to comment.