Skip to content

Commit

Permalink
Use a proxy setter to force process.env values to be strings
Browse files Browse the repository at this point in the history
  • Loading branch information
richarddavison committed Nov 27, 2023
1 parent 1ad0898 commit 4ff4d3f
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

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

fn cwd() -> String {
env::current_dir().unwrap().to_string_lossy().to_string()
Expand Down Expand Up @@ -41,6 +44,15 @@ fn exit(code: i32) {
std::process::exit(code)
}

fn env_proxy_setter<'js>(
target: Object<'js>,
prop: Value<'js>,
value: Coerced<String>,
) -> Result<bool> {
target.set(prop, value.to_string())?;
Ok(true)
}

pub fn init(ctx: &Ctx<'_>) -> Result<()> {
let globals = ctx.globals();

Expand All @@ -62,12 +74,16 @@ pub fn init(ctx: &Ctx<'_>) -> Result<()> {
}
}

process.set("env", env_map)?;
let proxy_ctor = globals.get::<_, Constructor>(PredefinedAtom::Proxy)?;

let env_obj = env_map.into_js(ctx)?;
let env_proxy_cfg = Object::new(ctx.clone())?;
env_proxy_cfg.set(PredefinedAtom::Setter, Func::from(env_proxy_setter))?;
let env_proxy = proxy_ctor.construct::<_, Value>((env_obj, env_proxy_cfg))?;

process.set("env", env_proxy)?;
process.set("cwd", Func::from(cwd))?;
process.set(
"argv0",
args.clone().first().map(|s| s.clone()).unwrap_or_default(),
)?;
process.set("argv0", args.clone().first().cloned().unwrap_or_default())?;
process.set("id", std::process::id())?;
process.set("argv", args)?;
process.set("platform", get_platform())?;
Expand Down

0 comments on commit 4ff4d3f

Please sign in to comment.