Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 141 additions & 122 deletions stdlib/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,142 @@ use techscript_runtime::{
value::RuntimeValue,
};

pub fn env_get(
ctx: &mut techscript_runtime::context::RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied".to_string(),
),
None,
None,
));
}
let key = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};
let val = std::env::var(&key).unwrap_or_default();
Ok(RuntimeValue::Str(val))
}

pub fn env_set(
ctx: &mut techscript_runtime::context::RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied".to_string(),
),
None,
None,
));
}
let key = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};
let val = match &args[1] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};
std::env::set_var(&key, &val);
Ok(RuntimeValue::Null)
}

pub fn env_all(
ctx: &mut techscript_runtime::context::RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied".to_string(),
),
None,
None,
));
}
let mut entries = indexmap::IndexMap::new();
for (k, v) in std::env::vars() {
entries.insert(k, RuntimeValue::Str(v));
}
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(entries)),
is_const: true,
})
}

pub fn env_args(
ctx: &mut techscript_runtime::context::RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied".to_string(),
),
None,
None,
));
}
let r_args: Vec<RuntimeValue> = std::env::args().map(RuntimeValue::Str).collect();
Ok(RuntimeValue::List {
items: Rc::new(RefCell::new(r_args)),
is_const: false,
})
}

pub fn env_current_dir(
ctx: &mut techscript_runtime::context::RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied".to_string(),
),
None,
None,
));
}
let p = std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string();
Ok(RuntimeValue::Str(p))
}

impl StdlibRegistry {
pub fn register_env(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
Expand All @@ -18,33 +154,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "get".to_string(),
arity: 1,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied"
.to_string(),
),
None,
None,
));
}
let key = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};
let val = std::env::var(&key).unwrap_or_default();
Ok(RuntimeValue::Str(val))
},
callback: env_get,
}),
);

Expand All @@ -53,46 +163,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "set".to_string(),
arity: 2,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied"
.to_string(),
),
None,
None,
));
}
let key = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};
let val = match &args[1] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};
std::env::set_var(&key, &val);
Ok(RuntimeValue::Null)
},
callback: env_set,
}),
);

Expand All @@ -101,26 +172,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "all".to_string(),
arity: 0,
callback: |ctx, _args| {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied"
.to_string(),
),
None,
None,
));
}
let mut entries = indexmap::IndexMap::new();
for (k, v) in std::env::vars() {
entries.insert(k, RuntimeValue::Str(v));
}
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(entries)),
is_const: true,
})
},
callback: env_all,
}),
);

Expand All @@ -129,24 +181,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "args".to_string(),
arity: 0,
callback: |ctx, _args| {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied"
.to_string(),
),
None,
None,
));
}
let r_args: Vec<RuntimeValue> =
std::env::args().map(RuntimeValue::Str).collect();
Ok(RuntimeValue::List {
items: Rc::new(RefCell::new(r_args)),
is_const: false,
})
},
callback: env_args,
}),
);

Expand All @@ -155,23 +190,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "current_dir".to_string(),
arity: 0,
callback: |ctx, _args| {
if !ctx.config.capabilities.contains(&Capability::Environment) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Environment capability is denied"
.to_string(),
),
None,
None,
));
}
let p = std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string();
Ok(RuntimeValue::Str(p))
},
callback: env_current_dir,
}),
);

Expand Down
Loading
Loading