# HG changeset patch # User ellis # Date 1683506546 14400 # Node ID bebb76da449c9b428e94fe2acb3b4f72ed84d37f # Parent 315fedf35bc7c8962ec4b9b8425def084a27e0e0 tests and stuff diff -r 315fedf35bc7 -r bebb76da449c cfg.lisp diff -r 315fedf35bc7 -r bebb76da449c demo.asd --- a/demo.asd Sun May 07 18:06:13 2023 -0400 +++ b/demo.asd Sun May 07 20:42:26 2023 -0400 @@ -22,6 +22,7 @@ :components ((:file "pkg") (:file "ffi") (:file "tk") + (:file "cfg") (:file "db") (:file "ui") (:file "demo")) diff -r 315fedf35bc7 -r bebb76da449c ffi.lisp --- a/ffi.lisp Sun May 07 18:06:13 2023 -0400 +++ b/ffi.lisp Sun May 07 20:42:26 2023 -0400 @@ -1,7 +1,7 @@ (in-package :demo) -(define-foreign-library demo_ffi +(define-foreign-library demo (:win32 (:default "demo")) (t (:default "libdemo"))) -;; (use-foreign-library "./target/release/libdemo_ffi.dylib") +(use-foreign-library "./target/release/libdemo.dylib") diff -r 315fedf35bc7 -r bebb76da449c lib.rs --- a/lib.rs Sun May 07 18:06:13 2023 -0400 +++ b/lib.rs Sun May 07 20:42:26 2023 -0400 @@ -1,17 +1,17 @@ //! demo/lib.rs --- generated by DEMO:RS-MACROEXPAND -extern crate obj; extern crate libc; +extern crate obj; //pub use fig::*; -use obj::{Objective,Service,CustomService}; +use libc::{c_char, size_t}; +use obj::{CustomService, Objective, Service}; use std::ffi::{CStr, CString}; use std::slice; -use libc::{c_char, size_t}; #[no_mangle] pub unsafe extern "C" fn free_service(ptr: *mut Service) { - if ptr.is_null() { - return; - } - let _ = Box::from_raw(ptr); + if ptr.is_null() { + return; + } + let _ = Box::from_raw(ptr); } #[no_mangle] pub unsafe extern "C" fn service_from_string(ptr: *const c_char) -> *mut Service { @@ -23,49 +23,53 @@ pub unsafe extern "C" fn service_from_json_string(ptr: *const c_char) -> *mut Service { assert!(!ptr.is_null()); let s = CStr::from_ptr(ptr); - Box::into_raw(Box::new(Service::from_json_str(&s.to_str().unwrap()).unwrap())) + Box::into_raw(Box::new( + Service::from_json_str(&s.to_str().unwrap()).unwrap(), + )) } #[no_mangle] pub unsafe extern "C" fn service_to_json_string(ptr: *const Service) -> *mut c_char { - let p = &*ptr; - let x = p.to_json_string().unwrap(); - CString::new(x.as_str().as_bytes()).unwrap().into_raw() + let p = &*ptr; + let x = p.to_json_string().unwrap(); + CString::new(x.as_str().as_bytes()).unwrap().into_raw() } #[no_mangle] pub unsafe extern "C" fn service_from_ron_string(ptr: *const c_char) -> *mut Service { assert!(!ptr.is_null()); let s = CStr::from_ptr(ptr); - Box::into_raw(Box::new(Service::from_ron_str(&s.to_str().unwrap()).unwrap())) + Box::into_raw(Box::new( + Service::from_ron_str(&s.to_str().unwrap()).unwrap(), + )) } #[no_mangle] pub unsafe extern "C" fn service_to_ron_string(ptr: *const Service) -> *mut c_char { - let p = &*ptr; - let x = p.to_ron_string().unwrap(); - CString::new(x.as_str().as_bytes()).unwrap().into_raw() + let p = &*ptr; + let x = p.to_ron_string().unwrap(); + CString::new(x.as_str().as_bytes()).unwrap().into_raw() } #[no_mangle] pub unsafe extern "C" fn service_decode(ptr: *const u8, len: size_t) -> *mut Service { - Box::into_raw(Box::new(Service::decode(slice::from_raw_parts(ptr, len)).unwrap())) + Box::into_raw(Box::new( + Service::decode(slice::from_raw_parts(ptr, len)).unwrap(), + )) } #[no_mangle] pub unsafe extern "C" fn service_encode(ptr: *const Service) -> *mut u8 { - let p = &*ptr; - let mut x = p.encode().unwrap(); - let r = x.as_mut_ptr(); - std::mem::forget(x); - r + let p = &*ptr; + let mut x = p.encode().unwrap(); + let r = x.as_mut_ptr(); + std::mem::forget(x); + r } #[no_mangle] pub unsafe extern "C" fn free_custom_service(ptr: *mut CustomService) { - if ptr.is_null() { - return; - } - let _ = Box::from_raw(ptr); + if ptr.is_null() { + return; + } + let _ = Box::from_raw(ptr); } #[no_mangle] -pub unsafe extern "C" fn custom_service_from_string( - ptr: *const c_char, -) -> *mut CustomService { +pub unsafe extern "C" fn custom_service_from_string(ptr: *const c_char) -> *mut CustomService { assert!(!ptr.is_null()); let p = CStr::from_ptr(ptr).to_str().unwrap(); Box::into_raw(Box::new(p.into())) @@ -74,45 +78,41 @@ pub unsafe extern "C" fn custom_service_from_json_string(ptr: *const c_char) -> *mut CustomService { assert!(!ptr.is_null()); let s = CStr::from_ptr(ptr); - Box::into_raw(Box::new(CustomService::from_json_str(&s.to_str().unwrap()).unwrap()))} -#[no_mangle] -pub unsafe extern "C" fn custom_service_to_json_string( - ptr: *const CustomService, -) -> *mut c_char { - let p = &*ptr; - let x = p.to_json_string().unwrap(); - CString::new(x.as_str().as_bytes()).unwrap().into_raw() + Box::into_raw(Box::new( + CustomService::from_json_str(&s.to_str().unwrap()).unwrap(), + )) } #[no_mangle] -pub unsafe extern "C" fn custom_service_from_ron_string( - ptr: *const c_char, -) -> *mut CustomService { +pub unsafe extern "C" fn custom_service_to_json_string(ptr: *const CustomService) -> *mut c_char { + let p = &*ptr; + let x = p.to_json_string().unwrap(); + CString::new(x.as_str().as_bytes()).unwrap().into_raw() +} +#[no_mangle] +pub unsafe extern "C" fn custom_service_from_ron_string(ptr: *const c_char) -> *mut CustomService { assert!(!ptr.is_null()); let s = CStr::from_ptr(ptr); - Box::into_raw(Box::new(CustomService::from_ron_str(&s.to_str().unwrap()).unwrap())) + Box::into_raw(Box::new( + CustomService::from_ron_str(&s.to_str().unwrap()).unwrap(), + )) } #[no_mangle] -pub unsafe extern "C" fn custom_service_to_ron_string( - ptr: *const CustomService, -) -> *mut c_char { - let p = &*ptr; - let x = p.to_ron_string().unwrap(); - CString::new(x.as_str().as_bytes()).unwrap().into_raw() +pub unsafe extern "C" fn custom_service_to_ron_string(ptr: *const CustomService) -> *mut c_char { + let p = &*ptr; + let x = p.to_ron_string().unwrap(); + CString::new(x.as_str().as_bytes()).unwrap().into_raw() } #[no_mangle] -pub unsafe extern "C" fn custom_service_decode( - ptr: *const u8, - len: size_t, -) -> *mut CustomService { - Box::into_raw( - Box::new(CustomService::decode(slice::from_raw_parts(ptr, len)).unwrap()), - ) +pub unsafe extern "C" fn custom_service_decode(ptr: *const u8, len: size_t) -> *mut CustomService { + Box::into_raw(Box::new( + CustomService::decode(slice::from_raw_parts(ptr, len)).unwrap(), + )) } #[no_mangle] pub unsafe extern "C" fn custom_service_encode(ptr: *const CustomService) -> *mut u8 { - let p = &*ptr; - let mut x = p.encode().unwrap(); - let r = x.as_mut_ptr(); - std::mem::forget(x); - r + let p = &*ptr; + let mut x = p.encode().unwrap(); + let r = x.as_mut_ptr(); + std::mem::forget(x); + r } diff -r 315fedf35bc7 -r bebb76da449c pkg.lisp --- a/pkg.lisp Sun May 07 18:06:13 2023 -0400 +++ b/pkg.lisp Sun May 07 20:42:26 2023 -0400 @@ -49,12 +49,18 @@ (:export #:on-new-window #:start-ui) ;; tk.lisp - (:export #:random-id - #:scan-dir - #:mkstr - #:symb - #:sbq-reader - #:find-rust-dll) + (:export + #:*cargo-target* + #:*rs-macros* + #:random-id + #:scan-dir + #:mkstr + #:symb + #:sbq-reader + #:rs-find-dll + #:rs-defmacro + #:rs-macroexpand-1 + #:rs-macroexpand) ;; ffi.lisp ;; (:export) ) diff -r 315fedf35bc7 -r bebb76da449c readme.org --- a/readme.org Sun May 07 18:06:13 2023 -0400 +++ b/readme.org Sun May 07 20:42:26 2023 -0400 @@ -5,6 +5,8 @@ * Run * Play * Config -* COMMENT tasks -** TODO proc_macros +* tasks ** TODO ros scripts +** TODO rs-macroexpand +- rs-gen-file rs-defmacro rs-macros +- rs-macroexpand-1 diff -r 315fedf35bc7 -r bebb76da449c scripts/build.ros --- a/scripts/build.ros Sun May 07 18:06:13 2023 -0400 +++ b/scripts/build.ros Sun May 07 20:42:26 2023 -0400 @@ -14,7 +14,10 @@ (defun main (&rest argv) (declare (ignorable argv)) + (write-line "> cargo build --release") + (uiop:run-program "cargo build --release") + (wrie-line " built rust libs and bindings") (asdf:load-asd "cl-demo.asd") - (asdf:load-system "cl-demo") - (asdf:make :cl-demo)) + (asdf:load-system "demo") + (asdf:make :demo)) ;;; vim: set ft=lisp lisp: diff -r 315fedf35bc7 -r bebb76da449c scripts/clean.ros --- a/scripts/clean.ros Sun May 07 18:06:13 2023 -0400 +++ b/scripts/clean.ros Sun May 07 20:42:26 2023 -0400 @@ -16,5 +16,8 @@ (in-package :ros.script.clean.3891893753) (defun main (&rest argv) - (declare (ignorable argv))) + (declare (ignorable argv)) + (write-line "> cargo clean") + (uiop:run-program "cargo clean") + (write-line " cleaned rust crates")) ;;; vim: set ft=lisp lisp: diff -r 315fedf35bc7 -r bebb76da449c scripts/demo.ros --- a/scripts/demo.ros Sun May 07 18:06:13 2023 -0400 +++ b/scripts/demo.ros Sun May 07 20:42:26 2023 -0400 @@ -9,4 +9,4 @@ (defun main (&rest argv) (declare (ignorable argv)) - (format t "hello world")) + (demo:main)) diff -r 315fedf35bc7 -r bebb76da449c scripts/fmt.ros --- a/scripts/fmt.ros Sun May 07 18:06:13 2023 -0400 +++ b/scripts/fmt.ros Sun May 07 20:42:26 2023 -0400 @@ -14,5 +14,7 @@ (defun main (&rest argv) (declare (ignorable argv)) - (uiop:run-program "cargo fmt")) + (write-line "> cargo fmt") + (uiop:run-program "cargo fmt") + (write-line " formatted rust code")) ;;; vim: set ft=lisp lisp: diff -r 315fedf35bc7 -r bebb76da449c scripts/test.ros --- a/scripts/test.ros Sun May 07 18:06:13 2023 -0400 +++ b/scripts/test.ros Sun May 07 20:42:26 2023 -0400 @@ -9,4 +9,6 @@ (defun main (&rest argv) (declare (ignorable argv)) - (format t "hello world")) + (write-line "> cargo test") + (uiop:run-program "cargo test") + (write-line " tested rust crates")) diff -r 315fedf35bc7 -r bebb76da449c tests/demo_test.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/demo_test.c Sun May 07 20:42:26 2023 -0400 @@ -0,0 +1,8 @@ +#include +#include "demo.h" + +int main() { + Service *srv = service_from_string("weather"); + printf!("%s\n",service_to_json_str(srv)); + free_service(srv); +} diff -r 315fedf35bc7 -r bebb76da449c tests/demo_test.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/demo_test.py Sun May 07 20:42:26 2023 -0400 @@ -0,0 +1,1 @@ +from _demo import lib diff -r 315fedf35bc7 -r bebb76da449c tk.lisp --- a/tk.lisp Sun May 07 18:06:13 2023 -0400 +++ b/tk.lisp Sun May 07 20:42:26 2023 -0400 @@ -2,6 +2,8 @@ (defvar *cargo-target* #P"/Users/ellis/dev/otom8/demo/target/") +(defvar *rs-macros* nil) + (defmacro rs-find-dll (name &optional debug) "Find the rust dll specified by NAME." (cond @@ -13,7 +15,12 @@ (uiop:run-program `("cargo" "build" ,(unless debug "--release")) :output t) `,(find-rust-dll name debug))))) -(defmacro rs-macroexpand (env &body body) +(defmacro rs-defmacro (args &body body) + "Define a macro which expands to a string of Rust code.") + +(defun rs-macroexpand-1 (form &optional env)) + +(defun rs-macroexpand (env &rest body) "Cbindgen is quite the menace and really doesn't like our macros used to generate C FFI bindings. To compensate for this, we use a tool called cargo-expand by the most excellent dtolnay which expands Rust