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
376 changes: 243 additions & 133 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ anyhow = { workspace = true }
rustyline = { workspace = true }
toml = { workspace = true }
indexmap = { workspace = true }
bincode = "3.0"
bincode = "1.3.3"
dirs = "6.0"
num_cpus = "1.15"
ureq = { version = "3.4.0" }
ureq = { version = "2.9" }

[build-dependencies]
winres = "0.1.12"
Expand Down
4 changes: 2 additions & 2 deletions compiler/bytecode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ techscript_syntax = { path = "../syntax" }
techscript_ast = { path = "../ast" }
techscript_ir = { path = "../ir" }
serde = { workspace = true }
bincode = "3.0"
bincode = "1.3.3"

[dev-dependencies]
techscript_errors = { path = "../errors" }
Expand All @@ -19,4 +19,4 @@ techscript_parser = { path = "../parser" }
techscript_semantic = { path = "../semantic" }
techscript_optimizer = { path = "../optimizer" }
serde = { workspace = true }
bincode = "3.0"
bincode = "1.3.3"
2 changes: 1 addition & 1 deletion compiler/llvm_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ techscript_common = { path = "../common" }
techscript_ir = { path = "../ir" }
techscript_ast = { path = "../ast" }
techscript_syntax = { path = "../syntax" }
llvm-sys = { version = "221", optional = true }
llvm-sys = { version = "180", optional = true }
serde = { workspace = true }
anyhow = { workspace = true }
thiserror = { workspace = true }
Expand Down
43 changes: 37 additions & 6 deletions compiler/llvm_backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ impl<'a> CodegenEngine<'a> {
}
}

/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn compile_module(&mut self, ir_module: &Module) -> Result<(), String> {
self.global_names.clear();

// 1. Declare globals
for &(ref global_id, ref name, ref ty) in &ir_module.globals {
for (global_id, name, ty) in &ir_module.globals {
let llvm_ty = to_llvm_type(self.ctx.context, ty);
let global_var = LLVMAddGlobal(
self.ctx.module,
Expand Down Expand Up @@ -124,8 +127,6 @@ impl<'a> CodegenEngine<'a> {
let dest_block = self.ctx.get_block(*dest).unwrap();
LLVMBuildBr(self.ctx.builder, dest_block);
}
<
}
TerminatorKind::ConditionalJump {
cond,
then_block,
Expand Down Expand Up @@ -199,6 +200,25 @@ impl<'a> CodegenEngine<'a> {
TerminatorKind::Unreachable => {
LLVMBuildUnreachable(self.ctx.builder);
}
TerminatorKind::Throw(val) => {
let thrown_val = self.codegen_val(val)?;
let boxed_val = self.box_val(thrown_val)?;
let i8_ptr_ty = LLVMPointerType(LLVMInt8TypeInContext(self.ctx.context), 0);
let fn_throw = self.get_or_declare_runtime_fn(
"ts_throw",
LLVMVoidTypeInContext(self.ctx.context),
&[i8_ptr_ty],
);
LLVMBuildCall2(
self.ctx.builder,
LLVMTypeOf(fn_throw),
fn_throw,
[boxed_val].as_mut_ptr(),
1,
c"".as_ptr(),
);
LLVMBuildUnreachable(self.ctx.builder);
}
}
}
}
Expand Down Expand Up @@ -1056,7 +1076,10 @@ impl<'a> CodegenEngine<'a> {
CString::new("cast").unwrap().as_ptr(),
)
}
Op::Try { catch_block, catch_var } => {
Op::Try {
catch_block,
catch_var,
} => {
let i8_ptr_ty = LLVMPointerType(LLVMInt8TypeInContext(context), 0);
let fn_push = self.get_or_declare_runtime_fn("ts_try_push", i8_ptr_ty, &[]);
let buf_ptr = LLVMBuildCall2(
Expand Down Expand Up @@ -1120,7 +1143,11 @@ impl<'a> CodegenEngine<'a> {
LLVMPositionBuilderAtEnd(self.ctx.builder, dispatch_block);

// Clean up the jmp_buf since we arrived here via longjmp and ts_try_pop wasn't called
let fn_free_buf = self.get_or_declare_runtime_fn("ts_try_free", LLVMVoidTypeInContext(context), &[i8_ptr_ty]);
let fn_free_buf = self.get_or_declare_runtime_fn(
"ts_try_free",
LLVMVoidTypeInContext(context),
&[i8_ptr_ty],
);
LLVMBuildCall2(
self.ctx.builder,
LLVMTypeOf(fn_free_buf),
Expand Down Expand Up @@ -1151,7 +1178,11 @@ impl<'a> CodegenEngine<'a> {
return Ok(());
}
Op::EndTry => {
let fn_pop = self.get_or_declare_runtime_fn("ts_try_pop", LLVMVoidTypeInContext(context), &[]);
let fn_pop = self.get_or_declare_runtime_fn(
"ts_try_pop",
LLVMVoidTypeInContext(context),
&[],
);
LLVMBuildCall2(
self.ctx.builder,
LLVMTypeOf(fn_pop),
Expand Down
3 changes: 3 additions & 0 deletions compiler/llvm_backend/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub struct CodegenContext {
}

impl CodegenContext {
/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn new(name: &str) -> Self {
let context = LLVMContextCreate();
let module =
Expand Down
18 changes: 16 additions & 2 deletions compiler/llvm_backend/src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#![cfg(feature = "llvm")]

use llvm_sys::core::*;
use llvm_sys::orc2::lljit::*;
use llvm_sys::orc2::*;
use std::collections::HashMap;
Expand All @@ -23,6 +22,9 @@ pub struct LLVMJitEngine {

impl LLVMJitEngine {
/// Creates a new LLVMJitEngine instance.
/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn new() -> Result<Self, String> {
let mut jit = ptr::null_mut();
let builder = LLVMOrcCreateLLJITBuilder();
Expand All @@ -41,10 +43,13 @@ impl LLVMJitEngine {
}

/// Compiles a TechScript IR Module to JIT memory.
/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn compile(
&mut self,
ir_module: &techscript_ir::Module,
options: &LLVMBackendOptions,
_options: &LLVMBackendOptions,
) -> Result<(), String> {
// 1. Build LLVM IR Module
let mut ctx = CodegenContext::new(&ir_module.name);
Expand Down Expand Up @@ -79,6 +84,9 @@ impl LLVMJitEngine {
}

/// Looks up a function symbol by name.
/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn lookup(&mut self, name: &str) -> Result<u64, String> {
if let Some(&addr) = self.cache.get(name) {
return Ok(addr);
Expand All @@ -96,13 +104,19 @@ impl LLVMJitEngine {
}

/// Executes the JIT-compiled main function and returns its result (if integer).
/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn execute(&mut self, func_name: &str) -> Result<i64, String> {
let addr = self.lookup(func_name)?;
let func: extern "C" fn() -> i64 = std::mem::transmute(addr);
Ok(func())
}

/// Clears the function cache and reloads the engine (for hot reload support).
/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn hot_reload(&mut self) {
self.cache.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/llvm_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl LLVMBackend {
let mut engine = CodegenEngine::new(&mut ctx);
engine
.compile_module(ir_module)
.map_err(|e| LLVMCodegenError::CompilationError(e))?;
.map_err(LLVMCodegenError::CompilationError)?;

let out_str = CString::new(out_path.to_string_lossy().to_string()).unwrap();
let mut err_msg = std::ptr::null_mut();
Expand Down Expand Up @@ -147,7 +147,7 @@ impl LLVMBackend {

engine
.compile_module(ir_module)
.map_err(|e| LLVMCodegenError::CompilationError(e))?;
.map_err(LLVMCodegenError::CompilationError)?;

// 3. Resolve Target Triple & Host CPU Features
let triple_cstr = CString::new(options.target_triple.as_str()).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions compiler/llvm_backend/src/type_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use llvm_sys::prelude::*;
use techscript_ir::types::IRType;

/// Maps a TechScript IRType to its corresponding LLVMTypeRef.
/// # Safety
///
/// Caller must ensure LLVM context is valid.
pub unsafe fn to_llvm_type(context: LLVMContextRef, ty: &IRType) -> LLVMTypeRef {
match ty {
IRType::Void => LLVMVoidTypeInContext(context),
Expand Down
4 changes: 2 additions & 2 deletions runtime/native_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,8 @@ extern "C" {
}

thread_local! {
static TRY_STACK: RefCell<Vec<*mut JmpBuf>> = RefCell::new(Vec::new());
static PENDING_EXCEPTION: RefCell<*mut TsValue> = RefCell::new(std::ptr::null_mut());
static TRY_STACK: RefCell<Vec<*mut JmpBuf>> = const { RefCell::new(Vec::new()) };
static PENDING_EXCEPTION: RefCell<*mut TsValue> = const { RefCell::new(std::ptr::null_mut()) };
}

#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion runtime/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ techscript_runtime = { path = "../runtime" }
techscript_builtins = { path = "../builtins" }
techscript_stdlib = { path = "../../stdlib" }
serde = { workspace = true }
bincode = "3.0"
bincode = "1.3.3"
thiserror = { workspace = true }
indexmap = "2.0"

Expand Down
8 changes: 5 additions & 3 deletions stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ techscript_runtime = { path = "../runtime/runtime" }
serde_json = { workspace = true }
serde = { workspace = true }
indexmap = { version = "2", features = ["serde"] }
ureq = { version = "3.4", features = ["json"] }
ureq = { version = "2.9", features = ["json"] }
toml = { workspace = true }
rusqlite = { version = "0.40", features = ["bundled"] }
md5 = "0.8"
Expand All @@ -23,7 +23,10 @@ flate2 = "1.0"
crc = "3.0"
image = "0.25"
tiny_http = "0.12"
rand = "0.10"
rand = "0.8"
zip = "0.6"
shlex = "1.3"
url = "2.5"
base64 = "0.23"
quick-xml = "0.41"
qrcode = "0.14"
Expand All @@ -36,4 +39,3 @@ uuid = { version = "1", features = ["v4"] }
rustls = { version = "0.23", optional = true }
tokio = { version = "1", features = ["rt", "macros", "sync", "time"], optional = true }
hex = "0.4.3"
<
9 changes: 6 additions & 3 deletions stdlib/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ impl StdlibRegistry {
pub fn zip_dir(src_dir: &str, dst_file: &str) -> std::io::Result<()> {
let file = File::create(dst_file)?;
let mut zip = zip::ZipWriter::new(file);
let options = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Deflated);
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Deflated);

let walkdir = std::fs::read_dir(src_dir)?;
for entry in walkdir {
Expand Down Expand Up @@ -300,7 +300,10 @@ mod tests {

let dest_dir = temp_dir.join("dest");

let result = unzip_archive(corrupted_zip_path.to_str().unwrap(), dest_dir.to_str().unwrap());
let result = unzip_archive(
corrupted_zip_path.to_str().unwrap(),
dest_dir.to_str().unwrap(),
);
assert!(result.is_err());

std::fs::remove_dir_all(&temp_dir).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use aes_gcm::{
Aes256Gcm, Nonce,
};
use bcrypt;
use rand::Rng;
use sha2::Digest;
use std::collections::HashMap;
use rand::Rng;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, error::RuntimeErrorKind, value::RuntimeValue};

Expand Down
6 changes: 4 additions & 2 deletions stdlib/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,16 @@ impl StdlibRegistry {

let parsed = shlex::split(&cmd).ok_or_else(|| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation("Failed to parse command string".to_string()),
RuntimeErrorKind::InvalidOperation(
"Failed to parse command string".to_string(),
),
None,
None,
)
})?;

if parsed.is_empty() {
return Err(RuntimeError::new(
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation("Empty command string".to_string()),
None,
None,
Expand Down
17 changes: 15 additions & 2 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ impl StdlibRegistry {
CONNECTIONS.with(|m| {
let mut map = m.borrow_mut();
if let Some(conn) = map.get_mut(&id) {

conn.execute(&sql, rusqlite::params_from_iter(params))
.map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
})?;
Ok(())
} else {
Err(RuntimeError::new(
Expand Down Expand Up @@ -166,7 +173,13 @@ impl StdlibRegistry {
.map(|i| stmt.column_name(i).unwrap_or("?").to_string())
.collect();


let mut rows = Vec::new();
let row_iter = stmt
.query_map(rusqlite::params_from_iter(params), |row| {
let mut map = IndexMap::new();
for name in &col_names {
map.insert(name.clone(), RuntimeValue::Null);
}
for i in 0..col_count {
let val: String =
row.get::<_, String>(i).unwrap_or_default();
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/web.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::net::{ToSocketAddrs, IpAddr, SocketAddr};
use url::Url;
use ureq::Resolver;
use url::Url;

fn is_safe_ip(ip: &IpAddr) -> bool {
match ip {
Expand Down
6 changes: 1 addition & 5 deletions stdlib/tests/stdlib_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::collections::HashSet;
use std::rc::Rc;
use techscript_runtime::{context::Capability, value::RuntimeValue, RuntimeConfig, RuntimeContext, error::RuntimeErrorKind};
use techscript_runtime::{context::Capability, value::RuntimeValue, RuntimeConfig, RuntimeContext};
use techscript_stdlib::StdlibRegistry;

#[test]
Expand Down Expand Up @@ -1151,7 +1151,3 @@ fn test_ai_generate_text() {
let val = res.unwrap();
assert!(val.as_string().unwrap().contains("Prompt: What is 2+2?"));
}

#[test]

}
2 changes: 1 addition & 1 deletion tools/package-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ serde = { workspace = true }
serde_json = { workspace = true }
toml = "1.1"
anyhow = { workspace = true }
ureq = { version = "3.4" }
ureq = { version = "2.9" }
Loading
Loading