diff --git a/Cargo.toml b/Cargo.toml index 011ed45e..01e7577f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -237,6 +237,9 @@ members = [ "crates/*", "plugins/*", ] +exclude = [ + "plugins/nyash-file", +] [profile.release] # 最適化設定 diff --git a/nyash.toml b/nyash.toml index e0834b3c..0e0bbc1e 100644 --- a/nyash.toml +++ b/nyash.toml @@ -186,6 +186,41 @@ close = { method_id = 3 } recvTimeout = { method_id = 4 } fini = { method_id = 4294967295 } +[libraries."libnyash_encoding_plugin.so"] +boxes = ["EncodingBox"] +path = "plugins/nyash-encoding-plugin/target/release/libnyash_encoding_plugin.so" + +[libraries."libnyash_encoding_plugin.so".EncodingBox] +type_id = 53 +abi_version = 1 +singleton = false + +[libraries."libnyash_encoding_plugin.so".EncodingBox.methods] +birth = { method_id = 0 } +toUtf8Bytes = { method_id = 1 } +fromUtf8Bytes = { method_id = 2 } +base64Encode = { method_id = 3 } +base64Decode = { method_id = 4 } +hexEncode = { method_id = 5 } +hexDecode = { method_id = 6 } +fini = { method_id = 4294967295 } + +[libraries."libnyash_toml_plugin.so"] +boxes = ["TOMLBox"] +path = "plugins/nyash-toml-plugin/target/release/libnyash_toml_plugin.so" + +[libraries."libnyash_toml_plugin.so".TOMLBox] +type_id = 54 +abi_version = 1 +singleton = false + +[libraries."libnyash_toml_plugin.so".TOMLBox.methods] +birth = { method_id = 0 } +parse = { method_id = 1 } +get = { method_id = 2 } +toJson = { method_id = 3 } +fini = { method_id = 4294967295 } + # Python (v2 TypeBox) plugins [libraries."libnyash_python_plugin.so"] boxes = ["PyRuntimeBox", "PyObjectBox"] diff --git a/plugins/nyash-array-plugin/src/lib.rs b/plugins/nyash-array-plugin/src/lib.rs index 2dff45b0..b2498357 100644 --- a/plugins/nyash-array-plugin/src/lib.rs +++ b/plugins/nyash-array-plugin/src/lib.rs @@ -39,142 +39,7 @@ static INSTANCES: Lazy>> = Lazy::new(|| Mutex::new(HashMap::new())); static INSTANCE_COUNTER: AtomicU32 = AtomicU32::new(1); -#[no_mangle] -pub extern "C" fn nyash_plugin_abi() -> u32 { - 1 -} - -#[no_mangle] -pub extern "C" fn nyash_plugin_init() -> i32 { - NYB_SUCCESS -} - -#[no_mangle] -pub extern "C" fn nyash_plugin_invoke( - type_id: u32, - method_id: u32, - instance_id: u32, - args: *const u8, - args_len: usize, - result: *mut u8, - result_len: *mut usize, -) -> i32 { - if type_id != TYPE_ID_ARRAY { - return NYB_E_INVALID_TYPE; - } - - unsafe { - match method_id { - METHOD_BIRTH => { - if result_len.is_null() { - return NYB_E_INVALID_ARGS; - } - if preflight(result, result_len, 4) { - return NYB_E_SHORT_BUFFER; - } - let id = INSTANCE_COUNTER.fetch_add(1, Ordering::Relaxed); - if let Ok(mut map) = INSTANCES.lock() { - map.insert(id, ArrayInstance { data: Vec::new() }); - } else { - return NYB_E_PLUGIN_ERROR; - } - let bytes = id.to_le_bytes(); - std::ptr::copy_nonoverlapping(bytes.as_ptr(), result, 4); - *result_len = 4; - NYB_SUCCESS - } - METHOD_FINI => { - if let Ok(mut map) = INSTANCES.lock() { - map.remove(&instance_id); - NYB_SUCCESS - } else { - NYB_E_PLUGIN_ERROR - } - } - METHOD_LENGTH => { - if let Ok(map) = INSTANCES.lock() { - if let Some(inst) = map.get(&instance_id) { - return write_tlv_i64(inst.data.len() as i64, result, result_len); - } else { - return NYB_E_INVALID_HANDLE; - } - } else { - return NYB_E_PLUGIN_ERROR; - } - } - METHOD_GET => { - let idx = match read_arg_i64(args, args_len, 0) { - Some(v) => v, - None => return NYB_E_INVALID_ARGS, - }; - if idx < 0 { - return NYB_E_INVALID_ARGS; - } - if let Ok(map) = INSTANCES.lock() { - if let Some(inst) = map.get(&instance_id) { - let i = idx as usize; - if i >= inst.data.len() { - return NYB_E_INVALID_ARGS; - } - return write_tlv_i64(inst.data[i], result, result_len); - } else { - return NYB_E_INVALID_HANDLE; - } - } else { - return NYB_E_PLUGIN_ERROR; - } - } - METHOD_PUSH => { - let val = match read_arg_i64(args, args_len, 0) { - Some(v) => v, - None => return NYB_E_INVALID_ARGS, - }; - if let Ok(mut map) = INSTANCES.lock() { - if let Some(inst) = map.get_mut(&instance_id) { - inst.data.push(val); - return write_tlv_i64(inst.data.len() as i64, result, result_len); - } else { - return NYB_E_INVALID_HANDLE; - } - } else { - return NYB_E_PLUGIN_ERROR; - } - } - METHOD_SET => { - let idx = match read_arg_i64(args, args_len, 0) { - Some(v) => v, - None => return NYB_E_INVALID_ARGS, - }; - let val = match read_arg_i64(args, args_len, 1) { - Some(v) => v, - None => return NYB_E_INVALID_ARGS, - }; - if idx < 0 { - return NYB_E_INVALID_ARGS; - } - if let Ok(mut map) = INSTANCES.lock() { - if let Some(inst) = map.get_mut(&instance_id) { - let i = idx as usize; - let len = inst.data.len(); - if i < len { - inst.data[i] = val; - } else if i == len { - inst.data.push(val); - } else { - return NYB_E_INVALID_ARGS; - } - return write_tlv_i64(inst.data.len() as i64, result, result_len); - } else { - return NYB_E_INVALID_HANDLE; - } - } else { - return NYB_E_PLUGIN_ERROR; - } - } - _ => NYB_E_INVALID_METHOD, - } - } -} +// legacy v1 entry points removed // ===== TypeBox FFI (resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-console-plugin/src/lib.rs b/plugins/nyash-console-plugin/src/lib.rs index 8e7e9551..f4ffc3af 100644 --- a/plugins/nyash-console-plugin/src/lib.rs +++ b/plugins/nyash-console-plugin/src/lib.rs @@ -189,6 +189,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { NYB_SUCCESS } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -236,6 +237,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TypeBox FFI (resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-counter-plugin/src/lib.rs b/plugins/nyash-counter-plugin/src/lib.rs index 84b56d5a..1c40c83b 100644 --- a/plugins/nyash-counter-plugin/src/lib.rs +++ b/plugins/nyash-counter-plugin/src/lib.rs @@ -45,6 +45,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { NYB_SUCCESS } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -123,6 +124,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TLV helpers ===== fn write_tlv_result(payloads: &[(u8, &[u8])], result: *mut u8, result_len: *mut usize) -> i32 { diff --git a/plugins/nyash-egui-plugin/src/lib.rs b/plugins/nyash-egui-plugin/src/lib.rs index d48d2774..3f81d2ae 100644 --- a/plugins/nyash-egui-plugin/src/lib.rs +++ b/plugins/nyash-egui-plugin/src/lib.rs @@ -122,6 +122,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -241,6 +242,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TLV helpers (version=1) ===== fn write_tlv_result(payloads: &[(u8, &[u8])], result: *mut u8, result_len: *mut usize) -> i32 { diff --git a/plugins/nyash-encoding-plugin/src/lib.rs b/plugins/nyash-encoding-plugin/src/lib.rs index a14ea3cd..fc3084b6 100644 --- a/plugins/nyash-encoding-plugin/src/lib.rs +++ b/plugins/nyash-encoding-plugin/src/lib.rs @@ -42,6 +42,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -148,6 +149,101 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ + +// ===== TypeBox ABI v2 (resolve/invoke_id) ===== +#[repr(C)] +pub struct NyashTypeBoxFfi { + pub abi_tag: u32, // 'TYBX' + pub version: u16, // 1 + pub struct_size: u16, // sizeof(NyashTypeBoxFfi) + pub name: *const std::os::raw::c_char, + pub resolve: Option u32>, + pub invoke_id: Option i32>, + pub capabilities: u64, +} +unsafe impl Sync for NyashTypeBoxFfi {} + +use std::ffi::CStr; +extern "C" fn encoding_resolve(name: *const std::os::raw::c_char) -> u32 { + if name.is_null() { return 0; } + let s = unsafe { CStr::from_ptr(name) }.to_string_lossy(); + match s.as_ref() { + "toUtf8Bytes" => M_TO_UTF8_BYTES, + "fromUtf8Bytes" => M_FROM_UTF8_BYTES, + "base64Encode" => M_BASE64_ENC, + "base64Decode" => M_BASE64_DEC, + "hexEncode" => M_HEX_ENC, + "hexDecode" => M_HEX_DEC, + "birth" => M_BIRTH, + "fini" => M_FINI, + _ => 0, + } +} + +extern "C" fn encoding_invoke_id( + instance_id: u32, + method_id: u32, + args: *const u8, + args_len: usize, + result: *mut u8, + result_len: *mut usize, +) -> i32 { + unsafe { + match method_id { + M_BIRTH => { + if result_len.is_null() { return E_ARGS; } + if preflight(result, result_len, 4) { return E_SHORT; } + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + if let Ok(mut m) = INST.lock() { m.insert(id, EncInstance); } else { return E_PLUGIN; } + let b = id.to_le_bytes(); + std::ptr::copy_nonoverlapping(b.as_ptr(), result, 4); + *result_len = 4; OK + } + M_FINI => { if let Ok(mut m) = INST.lock() { m.remove(&instance_id); OK } else { E_PLUGIN } } + M_TO_UTF8_BYTES => { + let s = match read_arg_string(args, args_len, 0) { Some(v) => v, None => return E_ARGS }; + write_tlv_bytes(s.as_bytes(), result, result_len) + } + M_FROM_UTF8_BYTES => { + let bytes = match read_arg_bytes(args, args_len, 0) { Some(v) => v, None => return E_ARGS }; + match String::from_utf8(bytes) { Ok(s) => write_tlv_string(&s, result, result_len), Err(_) => write_tlv_string("", result, result_len) } + } + M_BASE64_ENC => { + if let Some(b) = read_arg_bytes(args, args_len, 0) { let s = base64::encode(b); return write_tlv_string(&s, result, result_len); } + let s = match read_arg_string(args, args_len, 0) { Some(v) => v, None => return E_ARGS }; + let enc = base64::encode(s.as_bytes()); + write_tlv_string(&enc, result, result_len) + } + M_BASE64_DEC => { + let s = match read_arg_string(args, args_len, 0) { Some(v) => v, None => return E_ARGS }; + match base64::decode(s.as_bytes()) { Ok(b) => write_tlv_bytes(&b, result, result_len), Err(_) => write_tlv_bytes(&[], result, result_len) } + } + M_HEX_ENC => { + if let Some(b) = read_arg_bytes(args, args_len, 0) { let s = hex::encode(b); return write_tlv_string(&s, result, result_len); } + let s = match read_arg_string(args, args_len, 0) { Some(v) => v, None => return E_ARGS }; + let enc = hex::encode(s.as_bytes()); + write_tlv_string(&enc, result, result_len) + } + M_HEX_DEC => { + let s = match read_arg_string(args, args_len, 0) { Some(v) => v, None => return E_ARGS }; + match hex::decode(s.as_bytes()) { Ok(b) => write_tlv_bytes(&b, result, result_len), Err(_) => write_tlv_bytes(&[], result, result_len) } + } + _ => E_METHOD, + } + } +} + +#[no_mangle] +pub static nyash_typebox_EncodingBox: NyashTypeBoxFfi = NyashTypeBoxFfi { + abi_tag: 0x54594258, + version: 1, + struct_size: std::mem::size_of::() as u16, + name: b"EncodingBox\0".as_ptr() as *const std::os::raw::c_char, + resolve: Some(encoding_resolve), + invoke_id: Some(encoding_invoke_id), + capabilities: 0, +}; fn preflight(result: *mut u8, result_len: *mut usize, needed: usize) -> bool { unsafe { diff --git a/plugins/nyash-filebox-plugin/src/lib.rs b/plugins/nyash-filebox-plugin/src/lib.rs index b98c6b7e..20197360 100644 --- a/plugins/nyash-filebox-plugin/src/lib.rs +++ b/plugins/nyash-filebox-plugin/src/lib.rs @@ -85,6 +85,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { } /// Method invocation - 仮実装 +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( _type_id: u32, @@ -397,6 +398,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== Helpers ===== diff --git a/plugins/nyash-integer-plugin/src/lib.rs b/plugins/nyash-integer-plugin/src/lib.rs index c3e8a963..cafde800 100644 --- a/plugins/nyash-integer-plugin/src/lib.rs +++ b/plugins/nyash-integer-plugin/src/lib.rs @@ -45,6 +45,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -119,6 +120,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TypeBox FFI (resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-map-plugin/src/lib.rs b/plugins/nyash-map-plugin/src/lib.rs index 91929e26..c34f13dd 100644 --- a/plugins/nyash-map-plugin/src/lib.rs +++ b/plugins/nyash-map-plugin/src/lib.rs @@ -58,6 +58,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { NYB_SUCCESS } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -405,6 +406,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ---- Nyash TypeBox (FFI minimal PoC) ---- #[repr(C)] diff --git a/plugins/nyash-math-plugin/src/lib.rs b/plugins/nyash-math-plugin/src/lib.rs index f39910f0..a533dbdd 100644 --- a/plugins/nyash-math-plugin/src/lib.rs +++ b/plugins/nyash-math-plugin/src/lib.rs @@ -91,6 +91,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -117,6 +118,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TypeBox ABI v2 (resolve/invoke_id per box) ===== #[repr(C)] diff --git a/plugins/nyash-net-plugin/src/lib.rs b/plugins/nyash-net-plugin/src/lib.rs index b08ade80..d9b78f20 100644 --- a/plugins/nyash-net-plugin/src/lib.rs +++ b/plugins/nyash-net-plugin/src/lib.rs @@ -181,6 +181,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -212,6 +213,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TypeBox ABI v2 (per-Box resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-path-plugin/src/lib.rs b/plugins/nyash-path-plugin/src/lib.rs index 1ad78871..078c292a 100644 --- a/plugins/nyash-path-plugin/src/lib.rs +++ b/plugins/nyash-path-plugin/src/lib.rs @@ -32,16 +32,12 @@ struct PathInstance; // stateless static INST: Lazy>> = Lazy::new(|| Mutex::new(HashMap::new())); static NEXT_ID: AtomicU32 = AtomicU32::new(1); +// legacy v1 entry points removed +/* #[no_mangle] -pub extern "C" fn nyash_plugin_abi() -> u32 { - 1 -} - +pub extern "C" fn nyash_plugin_abi() -> u32 { 1 } #[no_mangle] -pub extern "C" fn nyash_plugin_init() -> i32 { - OK -} - +pub extern "C" fn nyash_plugin_init() -> i32 { OK } #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -152,6 +148,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TypeBox ABI (resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-python-compiler-plugin/src/lib.rs b/plugins/nyash-python-compiler-plugin/src/lib.rs index 5ce7a0e1..29667eae 100644 --- a/plugins/nyash-python-compiler-plugin/src/lib.rs +++ b/plugins/nyash-python-compiler-plugin/src/lib.rs @@ -23,6 +23,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { NYB_SUCCESS } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -125,6 +126,7 @@ pub extern "C" fn nyash_plugin_invoke( _ => NYB_E_INVALID_METHOD, } } +*/ // ===== TypeBox ABI v2 (resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-python-parser-plugin/src/lib.rs b/plugins/nyash-python-parser-plugin/src/lib.rs index 975c3766..b4280877 100644 --- a/plugins/nyash-python-parser-plugin/src/lib.rs +++ b/plugins/nyash-python-parser-plugin/src/lib.rs @@ -18,113 +18,7 @@ struct ParseCounts { unsupported: usize, } -/// FFI: プラグインABIバージョン -#[no_mangle] -pub extern "C" fn nyash_plugin_abi_version() -> u32 { - 1 -} - -/// FFI: プラグイン初期化 -#[no_mangle] -pub extern "C" fn nyash_plugin_init() -> i32 { - // Python初期化は pyo3 の auto-initialize が処理 - 0 -} - -/// FFI: プラグインメソッド呼び出し(BID形式) -#[no_mangle] -pub extern "C" fn nyash_plugin_invoke( - type_id: u32, - method_id: u32, - _instance_id: u32, - args: *const u8, - args_len: usize, - result: *mut u8, - result_len: *mut usize, -) -> i32 { - const TYPE_ID_PARSER: u32 = 60; - const METHOD_BIRTH: u32 = 0; - const METHOD_PARSE: u32 = 1; - const METHOD_FINI: u32 = u32::MAX; - - if type_id != TYPE_ID_PARSER { - return -3; // NYB_E_INVALID_METHOD - } - - match method_id { - METHOD_BIRTH => { - // インスタンスIDを返す - unsafe { - let instance_id = 1u32; // 簡易実装 - if *result_len < 4 { - *result_len = 4; - return -1; // NYB_E_SHORT_BUFFER - } - let out = std::slice::from_raw_parts_mut(result, *result_len); - out[0..4].copy_from_slice(&instance_id.to_le_bytes()); - *result_len = 4; - } - 0 - } - METHOD_PARSE => { - // 引数からコードを取得(TLV形式の文字列を期待) - let code = unsafe { - if args.is_null() || args_len < 4 { - // 引数なしの場合は環境変数から取得 - std::env::var("NYASH_PY_CODE") - .unwrap_or_else(|_| "def main():\n return 0".to_string()) - } else { - // TLVデコード(簡易版) - let buf = std::slice::from_raw_parts(args, args_len); - if args_len >= 8 { - let tag = u16::from_le_bytes([buf[0], buf[1]]); - let len = u16::from_le_bytes([buf[2], buf[3]]) as usize; - if tag == 6 && 4 + len <= args_len { - match std::str::from_utf8(&buf[4..4 + len]) { - Ok(s) => s.to_string(), - Err(_) => std::env::var("NYASH_PY_CODE") - .unwrap_or_else(|_| "def main():\n return 0".to_string()), - } - } else { - std::env::var("NYASH_PY_CODE") - .unwrap_or_else(|_| "def main():\n return 0".to_string()) - } - } else { - std::env::var("NYASH_PY_CODE") - .unwrap_or_else(|_| "def main():\n return 0".to_string()) - } - } - }; - - // パース実行 - let parse_result = Python::with_gil(|py| parse_python_code(py, &code)); - - // JSONにシリアライズ - match serde_json::to_string(&parse_result) { - Ok(json) => { - unsafe { - let bytes = json.as_bytes(); - let need = 4 + bytes.len(); - if *result_len < need { - *result_len = need; - return -1; // NYB_E_SHORT_BUFFER - } - let out = std::slice::from_raw_parts_mut(result, *result_len); - // TLVエンコード(tag=6:string) - out[0..2].copy_from_slice(&6u16.to_le_bytes()); - out[2..4].copy_from_slice(&(bytes.len() as u16).to_le_bytes()); - out[4..4 + bytes.len()].copy_from_slice(bytes); - *result_len = need; - } - 0 - } - Err(_) => -4, // エラー - } - } - METHOD_FINI => 0, - _ => -3, // NYB_E_INVALID_METHOD - } -} +/* legacy v1 entries removed: nyash_plugin_abi_version/nyash_plugin_init/nyash_plugin_invoke */ /// FFI: Pythonコードをパース #[no_mangle] diff --git a/plugins/nyash-python-plugin/src/lib.rs b/plugins/nyash-python-plugin/src/lib.rs index bb028f69..7858c3e6 100644 --- a/plugins/nyash-python-plugin/src/lib.rs +++ b/plugins/nyash-python-plugin/src/lib.rs @@ -353,6 +353,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { NYB_SUCCESS } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -373,6 +374,7 @@ pub extern "C" fn nyash_plugin_invoke( _ => NYB_E_INVALID_TYPE, } } +*/ fn handle_py_runtime( method_id: u32, diff --git a/plugins/nyash-regex-plugin/src/lib.rs b/plugins/nyash-regex-plugin/src/lib.rs index 49ef557d..907e8bc9 100644 --- a/plugins/nyash-regex-plugin/src/lib.rs +++ b/plugins/nyash-regex-plugin/src/lib.rs @@ -46,6 +46,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -210,6 +211,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TypeBox ABI v2 (resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-string-plugin/src/lib.rs b/plugins/nyash-string-plugin/src/lib.rs index 75e8cd4c..166d2c44 100644 --- a/plugins/nyash-string-plugin/src/lib.rs +++ b/plugins/nyash-string-plugin/src/lib.rs @@ -46,6 +46,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -203,6 +204,7 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ // ===== TypeBox FFI (resolve/invoke_id) ===== #[repr(C)] diff --git a/plugins/nyash-toml-plugin/src/lib.rs b/plugins/nyash-toml-plugin/src/lib.rs index 5f16ad27..b2910341 100644 --- a/plugins/nyash-toml-plugin/src/lib.rs +++ b/plugins/nyash-toml-plugin/src/lib.rs @@ -40,6 +40,7 @@ pub extern "C" fn nyash_plugin_init() -> i32 { OK } +/* legacy v1 entry removed #[no_mangle] pub extern "C" fn nyash_plugin_invoke( type_id: u32, @@ -153,6 +154,103 @@ pub extern "C" fn nyash_plugin_invoke( } } } +*/ + +// ===== TypeBox ABI v2 (resolve/invoke_id) ===== +#[repr(C)] +pub struct NyashTypeBoxFfi { + pub abi_tag: u32, // 'TYBX' + pub version: u16, // 1 + pub struct_size: u16, // sizeof(NyashTypeBoxFfi) + pub name: *const std::os::raw::c_char, + pub resolve: Option u32>, + pub invoke_id: Option i32>, + pub capabilities: u64, +} +unsafe impl Sync for NyashTypeBoxFfi {} + +use std::ffi::CStr; +extern "C" fn toml_resolve(name: *const std::os::raw::c_char) -> u32 { + if name.is_null() { return 0; } + let s = unsafe { CStr::from_ptr(name) }.to_string_lossy(); + match s.as_ref() { + "parse" => M_PARSE, + "get" => M_GET, + "toJson" | "toJSON" => M_TO_JSON, + "birth" => M_BIRTH, + "fini" => M_FINI, + _ => 0, + } +} + +extern "C" fn toml_invoke_id( + instance_id: u32, + method_id: u32, + args: *const u8, + args_len: usize, + result: *mut u8, + result_len: *mut usize, +) -> i32 { + unsafe { + match method_id { + M_BIRTH => { + if result_len.is_null() { return E_ARGS; } + if preflight(result, result_len, 4) { return E_SHORT; } + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + if let Ok(mut m) = INST.lock() { m.insert(id, TomlInstance { value: None }); } else { return E_PLUGIN; } + let b = id.to_le_bytes(); + std::ptr::copy_nonoverlapping(b.as_ptr(), result, 4); + *result_len = 4; OK + } + M_FINI => { if let Ok(mut m) = INST.lock() { m.remove(&instance_id); OK } else { E_PLUGIN } } + M_PARSE => { + let text = match read_arg_string(args, args_len, 0) { Some(s) => s, None => return E_ARGS }; + if let Ok(mut m) = INST.lock() { + if let Some(inst) = m.get_mut(&instance_id) { + inst.value = toml::from_str::(&text).ok(); + return write_tlv_bool(inst.value.is_some(), result, result_len); + } else { return E_HANDLE; } + } else { return E_PLUGIN; } + } + M_GET => { + let path = match read_arg_string(args, args_len, 0) { Some(s) => s, None => return E_ARGS }; + if let Ok(m) = INST.lock() { + if let Some(inst) = m.get(&instance_id) { + let mut cur = match &inst.value { Some(v) => v, None => { return write_tlv_string("", result, result_len); } }; + if !path.is_empty() { + for seg in path.split('.') { + match cur.get(seg) { Some(v) => cur = v, None => { return write_tlv_string("", result, result_len); } } + } + } + return write_tlv_string(&cur.to_string(), result, result_len); + } else { return E_HANDLE; } + } else { return E_PLUGIN; } + } + M_TO_JSON => { + if let Ok(m) = INST.lock() { + if let Some(inst) = m.get(&instance_id) { + if let Some(v) = &inst.value { + if let Ok(s) = serde_json::to_string(v) { return write_tlv_string(&s, result, result_len); } + } + return write_tlv_string("{}", result, result_len); + } else { return E_HANDLE; } + } else { return E_PLUGIN; } + } + _ => E_METHOD, + } + } +} + +#[no_mangle] +pub static nyash_typebox_TOMLBox: NyashTypeBoxFfi = NyashTypeBoxFfi { + abi_tag: 0x54594258, + version: 1, + struct_size: std::mem::size_of::() as u16, + name: b"TOMLBox\0".as_ptr() as *const std::os::raw::c_char, + resolve: Some(toml_resolve), + invoke_id: Some(toml_invoke_id), + capabilities: 0, +}; fn toml_to_json(v: &toml::Value) -> serde_json::Value { match v {