diff --git a/stdlib/src/sys.rs b/stdlib/src/sys.rs index 42d264b3..00a45f14 100644 --- a/stdlib/src/sys.rs +++ b/stdlib/src/sys.rs @@ -1,14 +1,87 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry}; -use indexmap::IndexMap; -use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; use techscript_runtime::{ - context::Capability, + context::{Capability, RuntimeContext}, error::{RuntimeError, RuntimeErrorKind}, value::RuntimeValue, }; +fn check_fs_capability(ctx: &RuntimeContext) -> Result<(), RuntimeError> { + if !ctx.config.capabilities.contains(&Capability::FileSystem) { + return Err(RuntimeError::new( + RuntimeErrorKind::InvalidOperation( + "Security policy violation: FileSystem capability is denied".to_string(), + ), + None, + None, + )); + } + Ok(()) +} + +fn sys_read_file( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { + check_fs_capability(ctx)?; + let path = args[0].try_into_string()?; + let content = std::fs::read_to_string(path).map_err(|e| { + RuntimeError::new( + RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)), + None, + None, + ) + })?; + Ok(RuntimeValue::Str(content)) +} + +fn sys_write_file( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { + check_fs_capability(ctx)?; + let path = args[0].try_into_string()?; + let content = args[1].try_into_string()?; + std::fs::write(path, content).map_err(|e| { + RuntimeError::new( + RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)), + None, + None, + ) + })?; + Ok(RuntimeValue::Null) +} + +fn sys_exists( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { + check_fs_capability(ctx)?; + let path = args[0].try_into_string()?; + Ok(RuntimeValue::Bool(std::path::Path::new(&path).exists())) +} + +fn sys_now( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + let start = std::time::SystemTime::now(); + let since_the_epoch = start + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default(); + Ok(RuntimeValue::Float(since_the_epoch.as_secs_f64())) +} + +fn sys_sleep( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { + let ms = args[0].try_into_int()?; + std::thread::sleep(std::time::Duration::from_millis(ms as u64)); + Ok(RuntimeValue::Null) +} + impl StdlibRegistry { pub fn register_sys(&mut self) { let mut exports: HashMap> = @@ -19,27 +92,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "read_file".to_string(), arity: 1, - callback: |ctx, args| { - if !ctx.config.capabilities.contains(&Capability::FileSystem) { - return Err(RuntimeError::new( - RuntimeErrorKind::InvalidOperation( - "Security policy violation: FileSystem capability is denied" - .to_string(), - ), - None, - None, - )); - } - let path = args[0].try_into_string()?; - let content = std::fs::read_to_string(path).map_err(|e| { - RuntimeError::new( - RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)), - None, - None, - ) - })?; - Ok(RuntimeValue::Str(content)) - }, + callback: sys_read_file, }), ); @@ -48,28 +101,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "write_file".to_string(), arity: 2, - callback: |ctx, args| { - if !ctx.config.capabilities.contains(&Capability::FileSystem) { - return Err(RuntimeError::new( - RuntimeErrorKind::InvalidOperation( - "Security policy violation: FileSystem capability is denied" - .to_string(), - ), - None, - None, - )); - } - let path = args[0].try_into_string()?; - let content = args[1].try_into_string()?; - std::fs::write(path, content).map_err(|e| { - RuntimeError::new( - RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)), - None, - None, - ) - })?; - Ok(RuntimeValue::Null) - }, + callback: sys_write_file, }), ); @@ -78,20 +110,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "exists".to_string(), arity: 1, - callback: |ctx, args| { - if !ctx.config.capabilities.contains(&Capability::FileSystem) { - return Err(RuntimeError::new( - RuntimeErrorKind::InvalidOperation( - "Security policy violation: FileSystem capability is denied" - .to_string(), - ), - None, - None, - )); - } - let path = args[0].try_into_string()?; - Ok(RuntimeValue::Bool(std::path::Path::new(&path).exists())) - }, + callback: sys_exists, }), ); @@ -112,13 +131,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "now".to_string(), arity: 0, - callback: |_ctx, _args| { - let start = std::time::SystemTime::now(); - let since_the_epoch = start - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default(); - Ok(RuntimeValue::Float(since_the_epoch.as_secs_f64())) - }, + callback: sys_now, }), ); @@ -127,11 +140,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "sleep".to_string(), arity: 1, - callback: |_ctx, args| { - let ms = args[0].try_into_int()?; - std::thread::sleep(std::time::Duration::from_millis(ms as u64)); - Ok(RuntimeValue::Null) - }, + callback: sys_sleep, }), ); diff --git a/test_output.log b/test_output.log new file mode 100644 index 00000000..d8364f42 --- /dev/null +++ b/test_output.log @@ -0,0 +1,384 @@ + Downloading crates ... + Downloaded plotters-svg v0.3.7 + Downloaded same-file v1.0.6 + Downloaded plotters-backend v0.3.7 + Downloaded alloca v0.4.0 + Downloaded ciborium v0.2.2 + Downloaded ciborium-ll v0.2.2 + Downloaded walkdir v2.5.0 + Downloaded tinytemplate v1.2.1 + Downloaded page_size v0.6.0 + Downloaded itertools v0.13.0 + Downloaded oorandom v11.1.5 + Downloaded criterion-plot v0.8.2 + Downloaded ciborium-io v0.2.2 + Downloaded criterion v0.8.2 + Downloaded regex v1.13.1 + Downloaded plotters v0.3.7 + Downloaded cast v0.3.0 + Downloaded anes v0.1.6 + Compiling unicode-ident v1.0.24 + Compiling cfg-if v1.0.4 + Compiling serde_core v1.0.229 + Compiling serde v1.0.229 + Compiling libc v0.2.189 + Compiling proc-macro2 v1.0.107 + Compiling memchr v2.8.3 + Compiling quote v1.0.47 + Compiling syn v3.0.3 + Compiling syn v2.0.119 + Compiling typenum v1.20.1 + Compiling rand_core v0.10.1 + Compiling zeroize v1.9.0 + Compiling shlex v2.0.1 + Compiling cc v1.4.3 + Compiling getrandom v0.4.3 + Compiling hybrid-array v0.4.14 + Compiling log v0.4.33 + Compiling simd-adler32 v0.3.10 + Compiling crossbeam-utils v0.8.22 + Compiling crypto-common v0.2.2 + Compiling bitflags v2.13.1 + Compiling adler2 v2.0.1 + Compiling itoa v1.0.18 + Compiling num-traits v0.2.19 + Compiling stable_deref_trait v1.2.1 + Compiling crc32fast v1.5.0 + Compiling winnow v1.0.4 + Compiling pin-project-lite v0.2.17 + Compiling miniz_oxide v0.9.1 + Compiling serde_derive v1.0.229 + Compiling strum_macros v0.28.0 + Compiling block-buffer v0.12.1 + Compiling zlib-rs v0.6.7 + Compiling strum v0.28.0 + Compiling equivalent v1.0.2 + Compiling zmij v1.0.23 + Compiling serde_json v1.0.151 + Compiling synstructure v0.13.2 + Compiling futures-core v0.3.34 + Compiling cmov v0.5.4 + Compiling zerofrom-derive v0.1.7 + Compiling ctutils v0.4.2 + Compiling crossbeam-epoch v0.9.20 + Compiling techscript_common v2.0.0 (/app/compiler/common) + Compiling smallvec v1.15.2 + Compiling flate2 v1.1.10 + Compiling techscript_syntax v2.0.0 (/app/compiler/syntax) + Compiling techscript_errors v2.0.0 (/app/compiler/errors) + Compiling zerofrom v0.1.8 + Compiling yoke-derive v0.8.2 + Compiling block-padding v0.4.2 + Compiling either v1.17.0 + Compiling inout v0.2.2 + Compiling crossbeam-deque v0.8.7 + Compiling enumflags2_derive v0.7.12 + Compiling cpufeatures v0.3.0 + Compiling rayon-core v1.13.0 + Compiling zerocopy-derive v0.8.56 + Compiling zerovec-derive v0.11.5 + Compiling yoke v0.8.3 + Compiling futures-io v0.3.34 + Compiling parking v2.2.1 + Compiling once_cell v1.21.4 + Compiling anyhow v1.0.104 + Compiling cipher v0.5.2 + Compiling displaydoc v0.2.7 + Compiling foldhash v0.2.0 + Compiling zerovec v0.11.7 + Compiling const-oid v0.10.2 + Compiling zerocopy v0.8.56 + Compiling digest v0.11.3 + Compiling hashbrown v0.17.1 + Compiling rayon v1.12.0 + Compiling techscript_ast v2.0.0 (/app/compiler/ast) + Compiling ring v0.17.14 + Compiling bytes v1.12.1 + Compiling indexmap v2.14.2 + Compiling toml_parser v1.1.3+spec-1.1.0 + Compiling percent-encoding v2.3.2 + Compiling arrayvec v0.7.8 + Compiling slab v0.4.12 + Compiling toml_edit v0.25.13+spec-1.1.0 + Compiling tinystr v0.8.4 + Compiling enumflags2 v0.7.12 + Compiling equator-macro v0.4.2 + Compiling half v2.7.1 + Compiling thiserror-impl v2.0.21 + Compiling rustls-pki-types v1.15.1 + Compiling getrandom v0.2.17 + Compiling encoding_rs v0.8.35 + Compiling litemap v0.8.3 + Compiling writeable v0.6.4 + Compiling cpubits v0.1.1 + Compiling untrusted v0.9.0 + Compiling fastrand v2.5.0 + Compiling linux-raw-sys v0.12.1 + Compiling tinyvec_macros v0.1.1 + Compiling tinyvec v1.12.0 + Compiling thiserror v2.0.21 + Compiling icu_locale_core v2.3.0 + Compiling equator v0.4.2 + Compiling rustix v1.1.4 + Compiling proc-macro-crate v3.5.0 + Compiling httparse v1.10.1 + Compiling libm v0.2.16 + Compiling zerotrie v0.2.5 + Compiling potential_utf v0.1.6 + Compiling zstd-sys v2.0.16+zstd.1.5.7 + Compiling event-listener v5.4.2 + Compiling zvariant_utils v4.2.0 + Compiling num-integer v0.1.47 + Compiling concurrent-queue v2.5.0 + Compiling errno v0.3.14 + Compiling base64 v0.23.1 + Compiling utf8_iter v1.0.4 + Compiling subtle v2.6.1 + Compiling icu_collections v2.3.0 + Compiling proc-macro2-diagnostics v0.10.1 + Compiling rustls v0.23.45 + Compiling signal-hook-registry v1.4.8 + Compiling num-bigint v0.4.8 + Compiling zvariant_derive v5.15.0 + Compiling event-listener-strategy v0.5.4 + Compiling icu_provider v2.3.0 + Compiling rustls-webpki v0.103.14 + Compiling aligned-vec v0.6.4 + Compiling fst v0.4.7 + Compiling futures-lite v2.6.1 + Compiling http v1.5.0 + Compiling serde_spanned v1.1.1 + Compiling toml_datetime v1.1.1+spec-1.1.0 + Compiling nom v8.0.0 + Compiling toml_writer v1.1.2+spec-1.1.0 + Compiling endi v1.1.1 + Compiling alloc-no-stdlib v2.0.4 + Compiling toml v1.1.6+spec-1.1.0 + Compiling alloc-stdlib v0.2.4 + Compiling ouroboros_macro v0.18.5 + Compiling ureq-proto v0.6.4 + Compiling v_frame v0.3.9 + Compiling pathfinder_simd v0.5.6 + Compiling num-rational v0.4.2 + Compiling icu_properties_data v2.3.0 + Compiling icu_normalizer_data v2.3.0 + Compiling aes v0.9.2 + Compiling webpki-roots v1.0.9 + Compiling sha2 v0.11.0 + Compiling as-slice v0.2.1 + Compiling static_assertions v1.1.0 + Compiling aliasable v0.1.3 + Compiling bytemuck v1.25.2 + Compiling utf8-zero v0.8.1 + Compiling ouroboros v0.18.5 + Compiling aligned v0.4.3 + Compiling icu_normalizer v2.3.0 + Compiling icu_properties v2.3.0 + Compiling pathfinder_geometry v0.5.1 + Compiling unicode-canonical-combining-class v1.0.0 + Compiling unicode-joining-type v1.0.0 + Compiling brotli-decompressor v5.0.3 + Compiling unicode-general-category v1.1.0 + Compiling ureq v3.4.2 + Compiling hyphenation_commons v0.8.4 + Compiling polling v3.11.0 + Compiling bincode v1.3.3 + Compiling zcheapstr v1.1.0 + Compiling arg_enum_proc_macro v0.3.4 + Compiling profiling-procmacros v1.0.18 + Compiling no_std_io2 v0.9.4 + Compiling async-task v4.7.1 + Compiling y4m v0.8.0 + Compiling zstd-safe v7.2.4 + Compiling ucd-trie v0.1.7 + Compiling num-conv v0.2.2 + Compiling glyph-names v0.2.0 + Compiling quick-error v2.0.1 + Compiling deranged v0.5.8 + Compiling rustc-hash v2.1.3 + Compiling powerfmt v0.2.0 + Compiling time-core v0.1.9 + Compiling allsorts-azul v0.17.2 + Compiling hyphenation v0.8.4 + Compiling av-scenechange v0.14.1 + Compiling time v0.3.55 + Compiling bitstream-io v4.10.0 + Compiling profiling v1.0.18 + Compiling zvariant v5.15.0 + Compiling async-io v2.6.0 + Compiling idna_adapter v1.2.2 + Compiling generic-array v0.14.7 + Compiling num-complex v0.4.6 + Compiling av1-grain v0.2.5 + Compiling async-channel v2.5.0 + Compiling unicode-normalization v0.1.25 + Compiling maybe-rayon v0.1.1 + Compiling hmac v0.13.0 + Compiling tracing-core v0.1.36 + Compiling itertools v0.14.0 + Compiling num-derive v0.4.2 + Compiling tracing-attributes v0.1.31 + Compiling serde_repr v0.1.21 + Compiling miniz_oxide v0.8.9 + Compiling raw-cpuid v11.6.0 + Compiling mmapio v0.9.1 + Compiling simd_helpers v0.1.0 + Compiling unicode-bidi v0.3.18 + Compiling pulp-wasm-simd-flag v0.1.1 + Compiling libbz2-rs-sys v0.2.5 + Compiling bumpalo v3.20.3 + Compiling new_debug_unreachable v1.0.6 + Compiling reborrow v0.5.5 + Compiling azul-simplecss v0.2.1 + Compiling imgref v1.12.2 + Compiling atomic-waker v1.1.2 + Compiling zune-core v0.5.3 + Compiling weezl v0.1.12 + Compiling xmlparser v0.13.6 + Compiling pulp v0.22.3 + Compiling zune-jpeg v0.5.15 + Compiling piper v0.2.5 + Compiling rav1e v0.8.1 + Compiling loop9 v0.1.5 + Compiling azul-css v0.0.16 + Compiling zopfli v0.8.3 + Compiling bzip2 v0.6.1 + Compiling rust-fontconfig v5.0.0 + Compiling libsqlite3-sys v0.38.2 + Compiling tracing v0.1.44 + Compiling pbkdf2 v0.13.0 + Compiling idna v1.1.0 + Compiling async-signal v0.2.14 + Compiling zbus_names v4.3.4 + Compiling slotmap v1.1.1 + Compiling zstd v0.13.3 + Compiling lzma-rust2 v0.16.5 + Compiling async-lock v3.4.2 + Compiling avif-serialize v0.8.9 + Compiling form_urlencoded v1.2.2 + Compiling sha1 v0.11.0 + Compiling chacha20 v0.10.1 + Compiling universal-hash v0.6.1 + Compiling async-trait v0.1.92 + Compiling zune-inflate v0.2.54 + Compiling fdeflate v0.3.7 + Compiling lebe v0.5.3 + Compiling grid v1.0.1 + Compiling fax v0.2.7 + Compiling unicode-segmentation v1.13.3 + Compiling color_quant v1.1.0 + Compiling unicode-properties v0.1.4 + Compiling ppmd-rust v1.4.0 + Compiling byteorder v1.5.0 + Compiling typed-path v0.12.3 + Compiling rgb v0.8.53 + Compiling byteorder-lite v0.1.0 + Compiling hex v0.4.3 + Compiling constant_time_eq v0.4.2 + Compiling deflate64 v0.1.12 + Compiling bit_field v0.10.3 + Compiling pxfm v0.1.30 + Compiling gl-context-loader v0.1.10 + Compiling azul-core v0.0.16 + Compiling zip v8.6.0 + Compiling exr v1.74.2 + Compiling moxcms v0.8.1 + Compiling image-webp v0.2.4 + Compiling ravif v0.13.0 + Compiling stringprep v0.1.5 + Compiling gif v0.14.2 + Compiling tiff v0.11.3 + Compiling taffy v0.10.1 + Compiling png v0.18.1 + Compiling polyval v0.7.3 + Compiling rand v0.10.2 + Compiling url v2.5.8 + Compiling async-process v2.5.0 + Compiling zbus_macros v5.19.0 + Compiling blocking v1.6.2 + Compiling block-buffer v0.10.4 + Compiling crypto-common v0.1.7 + Compiling async-executor v1.14.0 + Compiling qoi v0.4.1 + Compiling async-broadcast v0.7.2 + Compiling md-5 v0.11.0 + Compiling cbc v0.2.1 + Compiling ecb v0.2.0 + Compiling ordered-stream v0.2.0 + Compiling async-recursion v1.1.1 + Compiling uuid v1.26.1 + Compiling roxmltree v0.21.1 + Compiling regex-lite v0.1.9 + Compiling iana-time-zone v0.1.65 + Compiling weezl v0.2.1 + Compiling lru v0.18.2 + Compiling rangemap v1.8.0 + Compiling debug_unsafe v0.1.4 + Compiling xmlwriter v0.1.0 + Compiling base64 v0.22.1 + Compiling lopdf v0.44.0 + Compiling azul-layout v0.0.16 + Compiling atoi_simd v0.18.1 + Compiling chrono v0.4.45 + Compiling zbus v5.19.0 + Compiling image v0.25.10 + Compiling digest v0.10.7 + Compiling ghash v0.6.0 + Compiling blowfish v0.10.0 + Compiling xattr v1.6.1 + Compiling techscript_runtime v2.0.0 (/app/runtime/runtime) + Compiling codepage v0.1.2 + Compiling quick-xml v0.41.0 + Compiling hashlink v0.12.1 + Compiling ctr v0.10.1 + Compiling aead v0.6.1 + Compiling filetime v0.2.29 + Compiling csv-core v0.1.13 + Compiling fallible-iterator v0.3.0 + Compiling crc-catalog v2.5.0 + Compiling cpufeatures v0.2.17 + Compiling ascii v1.1.0 + Compiling fallible-streaming-iterator v0.1.9 + Compiling chunked_transfer v1.5.0 + Compiling httpdate v1.0.3 + Compiling fast-float2 v0.2.4 + Compiling ryu v1.0.23 + Compiling calamine v0.36.1 + Compiling csv v1.4.0 + Compiling tiny_http v0.12.0 + Compiling rusqlite v0.40.2 + Compiling sha-1 v0.10.1 + Compiling crc v3.4.0 + Compiling tar v0.4.46 + Compiling aes-gcm v0.11.1 + Compiling bcrypt v0.19.3 + Compiling notify-rust v4.18.1 + Compiling qrcode v0.14.1 + Compiling quick-xml v0.42.0 + Compiling md5 v0.8.1 + Compiling logos-codegen v0.16.1 + Compiling techscript_module_resolver v2.0.0 (/app/compiler/module_resolver) + Compiling techscript_parser v2.0.0 (/app/compiler/parser) + Compiling utf8parse v0.2.2 + Compiling anstyle-parse v1.0.0 + Compiling is_terminal_polyfill v1.70.2 + Compiling colorchoice v1.0.5 + Compiling anstyle v1.0.14 + Compiling anstyle-query v1.1.5 + Compiling anstream v1.0.0 + Compiling logos-derive v0.16.1 + Compiling strsim v0.11.1 + Compiling logos v0.16.1 + Compiling clap_lex v1.1.0 + Compiling techscript_lexer v2.0.0 (/app/compiler/lexer) + Compiling clap_builder v4.6.7 + Compiling clap_derive v4.6.7 + Compiling bincode v3.0.0 +error: https://xkcd.com/2347/ + --> /home/jules/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bincode-3.0.0/src/lib.rs:1:1 + | +1 | compile_error!("https://xkcd.com/2347/"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: could not compile `bincode` (lib) due to 1 previous error +warning: build failed, waiting for other jobs to finish...