diff --git a/CMakeLists.txt b/CMakeLists.txt index fbfca89..7d93022 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(fun VERSION 0.38.0 LANGUAGES C) +project(fun VERSION 0.38.1 LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/cmake/Targets.cmake b/cmake/Targets.cmake index c1db251..dbdb7da 100644 --- a/cmake/Targets.cmake +++ b/cmake/Targets.cmake @@ -90,6 +90,16 @@ if(FUN_WITH_TCLTK) target_compile_definitions(fun_core PUBLIC FUN_WITH_TCLTK=1) endif() +# Workaround: provide a dummy rust_eh_personality to satisfy linker when using +# no_std Rust staticlib with panic abort (some toolchains still reference it). +# If needed, we could add a C shim here, but the Rust crate now defines +# rust_eh_personality itself, so no extra C sources are required. +if(FUN_WITH_RUST) + if(TARGET fun) + target_sources(fun PRIVATE ${CMAKE_SOURCE_DIR}/src/rust/eh_personality.c) + endif() +endif() + # Special case for libxml2 system include path if enabled if(FUN_WITH_XML2) if(EXISTS "/usr/include/libxml2") diff --git a/examples/extra/http_server_test.fun b/examples/blocking/http_server_test.fun similarity index 100% rename from examples/extra/http_server_test.fun rename to examples/blocking/http_server_test.fun diff --git a/examples/extra/pcsc_demo.fun b/examples/extra/pcsc_demo.fun index 850ace6..47d1572 100755 --- a/examples/extra/pcsc_demo.fun +++ b/examples/extra/pcsc_demo.fun @@ -33,9 +33,9 @@ if len(readers) > 0 if h != 0 // Sample APDU: SELECT MF (00 A4 00 00 02 3F 00) - resp = pc.transmit_hex(h, "00A40000023F00") - // Print hex data and SW - print("resp.data_hex=" + resp["data_hex"]) + // Use the safe convenience transmitter which returns a map {data, sw1, sw2, code} + resp = pc.transmit("00A40000023F00") + print("resp.data.len=" + to_string(len(resp["data"])) + " bytes") print("resp.sw1=" + to_string(resp["sw1"]) + " sw2=" + to_string(resp["sw2"]) + " code=" + to_string(resp["code"])) _ = pc.disconnect(h) diff --git a/examples/rust_hello.fun b/examples/rust_hello.fun index 5cd23d2..e5455db 100755 --- a/examples/rust_hello.fun +++ b/examples/rust_hello.fun @@ -30,3 +30,7 @@ */ print(rust_hello()) + +/* Expected output: +Hello from Rust ops! +*/ diff --git a/examples/rust_hello_args.fun b/examples/rust_hello_args.fun new file mode 100755 index 0000000..9fd2e70 --- /dev/null +++ b/examples/rust_hello_args.fun @@ -0,0 +1,37 @@ +#!/usr/bin/env fun + +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2025 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-01-28 + */ + +/* + * Rust-backed opcode demo with argument: rust_hello_args(STRING) + * + * Build instructions: + * - Rust integration is optional. Enable via: + * cmake -S . -B build_debug -DFUN_WITH_RUST=ON + * - Then build: + * cmake --build build_debug --target fun + * + * Run: + * build_debug/fun examples/rust_hello_args.fun + * + * Expected output (with FUN_WITH_RUST=ON): + * Hello from Fun to Rust! + * + * Note: + * The opcode prints from Rust and returns Nil, so there's nothing to print afterwards. + */ + +rust_hello_args("Hello from Fun to Rust!") + +/* Expected output: +Hello from Fun to Rust! +*/ diff --git a/lib/io/pcsc.fun b/lib/io/pcsc.fun index feb9263..14cc764 100644 --- a/lib/io/pcsc.fun +++ b/lib/io/pcsc.fun @@ -17,11 +17,9 @@ include -/* +// Unified PCSC class API class PCSC() - // ---- hex helpers moved to lib/hex.fun ---- - - // ---- PCSC wrappers ---- + // ---- Context management ---- fun establish(this) // returns ctx id (>0) or 0 return pcsc_establish() @@ -36,6 +34,16 @@ class PCSC() return [] return readers + // Convenience: get readers without managing ctx externally + fun get_readers(this) + ctx = pcsc_establish() + readers = pcsc_list_readers(ctx) + _ = pcsc_release(ctx) + if readers == nil + return [] + return readers + + // ---- Connection management ---- fun connect(this, ctx, reader_name) // returns handle id (>0) or 0 return pcsc_connect(ctx, to_string(reader_name)) @@ -44,6 +52,7 @@ class PCSC() // returns 1 on success or 0 return pcsc_disconnect(handle) + // ---- Transmit helpers ---- // Transmit raw APDU bytes (array of numbers 0..255). Returns map {"data":[], "sw1":n, "sw2":n, "code":n} fun transmit_bytes(this, handle, bytes) res = pcsc_transmit(handle, bytes) @@ -77,22 +86,12 @@ class PCSC() m["sw2"] = res["sw2"] m["code"] = res["code"] return m -*/ -// This class is in a very early stage of development. -class PCSC() - fun get_readers(this) - ctx = pcsc_establish() - readers = pcsc_list_readers(ctx) - _ = pcsc_release(ctx) - return readers - - // Transmit a raw hex APDU to the first available reader and print the result map + // Convenience: Transmit a raw hex APDU to the first available reader and return the result map fun transmit(this, hex_apdu) ctx = pcsc_establish() readers = pcsc_list_readers(ctx) if (readers == nil) - print([]) _ = pcsc_release(ctx) // return a default result map instead of 0, so callers can index fields safely m = {} @@ -102,7 +101,6 @@ class PCSC() m["code"] = -2 return m if (len(readers) == 0) - print([]) _ = pcsc_release(ctx) m = {} m["data"] = [] @@ -110,50 +108,19 @@ class PCSC() m["sw2"] = -1 m["code"] = -2 return m - // Actually selecting the second reader hardcoded. + // Select the second reader (index 1) as previously used in examples handle = pcsc_connect(ctx, readers[1]) - apdu = this.hex_to_bytes(hex_apdu) - res = pcsc_transmit(handle, apdu) + apdu_bytes = hex_to_bytes(hex_apdu) + res = pcsc_transmit(handle, apdu_bytes) _ = pcsc_disconnect(handle) _ = pcsc_release(ctx) + if res == nil + m = {} + m["data"] = [] + m["sw1"] = -1 + m["sw2"] = -1 + m["code"] = -2 + return m + if res["data"] == nil + res["data"] = [] return res - - fun hex_digit(this, ch) - m = {} - m["0"] = 0 - m["1"] = 1 - m["2"] = 2 - m["3"] = 3 - m["4"] = 4 - m["5"] = 5 - m["6"] = 6 - m["7"] = 7 - m["8"] = 8 - m["9"] = 9 - m["a"] = 10 - m["A"] = 10 - m["b"] = 11 - m["B"] = 11 - m["c"] = 12 - m["C"] = 12 - m["d"] = 13 - m["D"] = 13 - m["e"] = 14 - m["E"] = 14 - m["f"] = 15 - m["F"] = 15 - v = m[ch] - if v == nil - return 0 - return v - - fun hex_to_bytes(this, hex) - s = to_string(hex) - out = [] - number i = 0 - number n = len(s) - while i + 1 < n - number b = this.hex_digit(substr(s, i, 1)) * 16 + this.hex_digit(substr(s, i + 1, 1)) - push(out, b) - i = i + 2 - return out diff --git a/make b/make index b0d587a..18c5969 100755 --- a/make +++ b/make @@ -88,6 +88,27 @@ elif [ "$target" = "repl" ]; then && cmake -S . -B build \ -DFUN_WITH_REPL=ON \ && cmake --build build --target fun +elif [ "$target" = "rust_all" ]; then + rm -rf build && rm -rf src/rust/target \ + && cmake -S . -B build \ + -DFUN_WITH_PCSC=ON \ + -DFUN_WITH_REPL=ON \ + -DFUN_WITH_LIBSQL=ON \ + -DFUN_WITH_SQLITE=ON \ + -DFUN_WITH_CURL=ON \ + -DFUN_WITH_PCRE2=ON \ + -DFUN_WITH_XML2=ON \ + -DFUN_WITH_JSON=ON \ + -DFUN_WITH_TCLTK=ON \ + -DFUN_WITH_INI=ON \ + -DFUN_WITH_RUST=ON \ + -DFUN_DEBUG=OFF \ + && cmake --build build --target fun +elif [ "$target" = "rust_minimal" ]; then + rm -rf build && rm -rf src/rust/target \ + && cmake -S . -B build \ + -DFUN_WITH_RUST=ON \ + && cmake --build build --target fun else echo "Build target $target not found... aborting!"; echo "Available targets:"; @@ -100,5 +121,7 @@ else echo " - minimal"; echo " - musl"; echo " - repl"; + echo " - rust_all"; + echo " - rust_minimal"; fi diff --git a/src/bytecode.c b/src/bytecode.c index 58b62a8..82d8ca1 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -207,6 +207,7 @@ static const char *opcode_name(OpCode op) { case OP_FMIN: return "FMIN"; case OP_FMAX: return "FMAX"; case OP_RUST_HELLO: return "RUST_HELLO"; + case OP_RUST_HELLO_ARGS: return "RUST_HELLO_ARGS"; default: return "???"; } } diff --git a/src/bytecode.h b/src/bytecode.h index 0cb4908..81c12c1 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -265,6 +265,7 @@ typedef enum { // Rust FFI demo opcode(s) OP_RUST_HELLO, // pushes string returned from Rust (hello world) + OP_RUST_HELLO_ARGS, // pops message string; prints it via Rust; pushes Nil /* Notcurses TUI (optional) */ OP_NC_INIT, // initializes Notcurses; returns 1 on success, 0 on failure diff --git a/src/parser.c b/src/parser.c index 78da293..bef13c9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -793,6 +793,14 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) free(name); return 1; } + if (strcmp(name, "rust_hello_args") == 0) { + (*pos)++; /* '(' */ + if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "rust_hello_args expects (message:string)"); free(name); return 0; } + if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after rust_hello_args arg"); free(name); return 0; } + bytecode_add_instruction(bc, OP_RUST_HELLO_ARGS, 0); + free(name); + return 1; + } if (strcmp(name, "os_list_dir") == 0) { (*pos)++; /* '(' */ if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "os_list_dir expects (path)"); free(name); return 0; } diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml index a6c7500..1080d9f 100644 --- a/src/rust/Cargo.toml +++ b/src/rust/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" [lib] crate-type = ["staticlib"] +[profile.dev] +panic = "abort" + [profile.release] panic = "abort" # Optimize for minimal size diff --git a/src/rust/eh_personality.c b/src/rust/eh_personality.c new file mode 100644 index 0000000..666aec0 --- /dev/null +++ b/src/rust/eh_personality.c @@ -0,0 +1,2 @@ +/* Workaround for linking Rust staticlib in no_std/abort mode on some toolchains */ +void rust_eh_personality(void) {} diff --git a/src/rust/src/lib.rs b/src/rust/src/lib.rs index 12121ed..115785b 100644 --- a/src/rust/src/lib.rs +++ b/src/rust/src/lib.rs @@ -11,8 +11,8 @@ #![no_std] -#[repr(C)] -pub struct Vm; +// Use an FFI-safe opaque handle for the VM pointer +pub type Vm = core::ffi::c_void; extern "C" { fn vm_pop_i64(vm: *mut Vm) -> i64; @@ -37,6 +37,23 @@ pub extern "C" fn fun_rust_get_string() -> *const core::ffi::c_char { b"Hello from Rust ops!\0".as_ptr() as *const _ } +// Print a C string via libc printf to stdout (acceptable under no_std via extern) +#[no_mangle] +pub extern "C" fn fun_rust_print_string(msg: *const core::ffi::c_char) -> i32 { + unsafe { + extern "C" { + fn printf(fmt: *const core::ffi::c_char, ...) -> i32; + } + let fmt = b"%s\n\0".as_ptr() as *const core::ffi::c_char; + printf(fmt, msg); + } + 0 +} + +// Provide personality symbol to satisfy some linkers in no_std builds +#[no_mangle] +pub extern "C" fn rust_eh_personality() {} + // Minimal panic handler for no_std; abort behavior requested via Cargo profile #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { diff --git a/src/vm.h b/src/vm.h index 0e1d528..e746a96 100644 --- a/src/vm.h +++ b/src/vm.h @@ -56,7 +56,7 @@ static const char *opcode_names[] = { "TRY_PUSH","TRY_POP","THROW", "FMIN","FMAX", /* Rust FFI demo */ - "RUST_HELLO", + "RUST_HELLO","RUST_HELLO_ARGS", /* Notcurses TUI (optional) */ "NC_INIT","NC_SHUTDOWN","NC_CLEAR","NC_DRAW_TEXT","NC_GETCH" }; @@ -157,5 +157,7 @@ int fun_op_radd(VM *vm); /* Example Rust function returning a demo C string (null-terminated). */ const char *fun_rust_get_string(void); +/* Rust function that prints a passed C string, returns 0 on success. */ +int fun_rust_print_string(const char *msg); #endif diff --git a/src/vm/rust/hello.c b/src/vm/rust/hello.c index 709d8c6..6878c46 100644 --- a/src/vm/rust/hello.c +++ b/src/vm/rust/hello.c @@ -25,3 +25,29 @@ case OP_RUST_HELLO: { #endif break; } + +/* + * Rust FFI demo opcode with argument: OP_RUST_HELLO_ARGS + * Pops a string from the stack and asks Rust to print it. Pushes Nil. + */ +case OP_RUST_HELLO_ARGS: { +#ifdef FUN_WITH_RUST + Value vmsg = pop_value(vm); + char *msg = value_to_string_alloc(&vmsg); + free_value(vmsg); + if (msg) { + fun_rust_print_string(msg); + free(msg); + } else { + fun_rust_print_string(""); + } + push_value(vm, make_nil()); +#else + /* Still pop and free the arg to keep stack sane */ + Value vmsg = pop_value(vm); + free_value(vmsg); + vm_raise_error(vm, "RUST_HELLO_ARGS requires FUN_WITH_RUST=ON at build time"); + push_value(vm, make_nil()); +#endif + break; +}