Skip to content
Closed
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
112 changes: 70 additions & 42 deletions stdlib/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,71 @@
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use indexmap::IndexMap;
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};
use techscript_runtime::{error::RuntimeError, value::RuntimeValue, RuntimeContext};

fn sys_os(
_ctx: &mut RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
Ok(RuntimeValue::Str(std::env::consts::OS.to_string()))
}

fn sys_arch(
_ctx: &mut RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
Ok(RuntimeValue::Str(std::env::consts::ARCH.to_string()))
}

fn sys_cpucount(
_ctx: &mut RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let count = std::thread::available_parallelism()
.map(|n| n.get() as i64)
.unwrap_or(4);
Ok(RuntimeValue::Int(count))
}

fn sys_memory(
_ctx: &mut RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
// âš¡ Bolt Performance Optimization:
// Pre-allocating the IndexMap capacity matching the expected number of keys
// avoids intermediate allocations and re-hashing during insertion.
let mut mem_map = IndexMap::with_capacity(2);
// Provide a cross-platform system memory lookup
let total = 16 * 1024 * 1024 * 1024; // 16 GB simulated
let free = 8 * 1024 * 1024 * 1024; // 8 GB simulated

mem_map.insert("total".to_string(), RuntimeValue::Int(total));
mem_map.insert("free".to_string(), RuntimeValue::Int(free));
Ok(RuntimeValue::Map {
entries: Rc::new(std::cell::RefCell::new(mem_map)),
is_const: false,
})
}

fn sys_disk(
_ctx: &mut RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
// âš¡ Bolt Performance Optimization:
// Pre-allocating the IndexMap capacity matching the expected number of keys
// avoids intermediate allocations and re-hashing during insertion.
let mut disk_map = IndexMap::with_capacity(2);
let total = 512 * 1024 * 1024 * 1024; // 512 GB simulated
let free = 256 * 1024 * 1024 * 1024; // 256 GB simulated

disk_map.insert("total".to_string(), RuntimeValue::Int(total));
disk_map.insert("free".to_string(), RuntimeValue::Int(free));
Ok(RuntimeValue::Map {
entries: Rc::new(std::cell::RefCell::new(disk_map)),
is_const: false,
})
}

impl StdlibRegistry {
pub fn register_system(&mut self) {
Expand All @@ -13,7 +77,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "os".to_string(),
arity: 0,
callback: |_ctx, _args| Ok(RuntimeValue::Str(std::env::consts::OS.to_string())),
callback: sys_os,
}),
);

Expand All @@ -22,7 +86,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "arch".to_string(),
arity: 0,
callback: |_ctx, _args| Ok(RuntimeValue::Str(std::env::consts::ARCH.to_string())),
callback: sys_arch,
}),
);

Expand All @@ -31,12 +95,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "cpucount".to_string(),
arity: 0,
callback: |_ctx, _args| {
let count = std::thread::available_parallelism()
.map(|n| n.get() as i64)
.unwrap_or(4);
Ok(RuntimeValue::Int(count))
},
callback: sys_cpucount,
}),
);

Expand All @@ -45,23 +104,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "memory".to_string(),
arity: 0,
callback: |_ctx, _args| {
use indexmap::IndexMap;
// âš¡ Bolt Performance Optimization:
// Pre-allocating the IndexMap capacity matching the expected number of keys
// avoids intermediate allocations and re-hashing during insertion.
let mut mem_map = IndexMap::with_capacity(2);
// Provide a cross-platform system memory lookup
let total = 16 * 1024 * 1024 * 1024; // 16 GB simulated
let free = 8 * 1024 * 1024 * 1024; // 8 GB simulated

mem_map.insert("total".to_string(), RuntimeValue::Int(total));
mem_map.insert("free".to_string(), RuntimeValue::Int(free));
Ok(RuntimeValue::Map {
entries: Rc::new(std::cell::RefCell::new(mem_map)),
is_const: false,
})
},
callback: sys_memory,
}),
);

Expand All @@ -70,22 +113,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "disk".to_string(),
arity: 0,
callback: |_ctx, _args| {
use indexmap::IndexMap;
// âš¡ Bolt Performance Optimization:
// Pre-allocating the IndexMap capacity matching the expected number of keys
// avoids intermediate allocations and re-hashing during insertion.
let mut disk_map = IndexMap::with_capacity(2);
let total = 512 * 1024 * 1024 * 1024; // 512 GB simulated
let free = 256 * 1024 * 1024 * 1024; // 256 GB simulated

disk_map.insert("total".to_string(), RuntimeValue::Int(total));
disk_map.insert("free".to_string(), RuntimeValue::Int(free));
Ok(RuntimeValue::Map {
entries: Rc::new(std::cell::RefCell::new(disk_map)),
is_const: false,
})
},
callback: sys_disk,
}),
);

Expand Down
Loading