gui: add EguiBox TypeBox plugin (Windows egui stub)\n\n- plugins: add nyash-egui-plugin with TypeBox (resolve/invoke_id), Windows path for real window via eframe; stub on other OS\n- apps: add apps/egui-hello sample (open→uiLabel→run→close)\n- loader: improve Windows DLL resolution (target triples: x86_64/aarch64 msvc) and lib→dll mapping\n- tests: expand TypeBox vs TLV diff tests up to FileBox; all green\n- docs: update CURRENT_TASK checklist (diff tests completed)\n- config: nyash.toml add EguiBox (type_id=70), plugin registry and methods
This commit is contained in:
@ -4,6 +4,8 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, atomic::{AtomicU32, Ordering}};
|
||||
use std::os::raw::c_char;
|
||||
use std::ffi::CStr;
|
||||
|
||||
// ===== Error Codes (aligned with existing plugins) =====
|
||||
const NYB_SUCCESS: i32 = 0;
|
||||
@ -120,6 +122,68 @@ pub extern "C" fn nyash_plugin_invoke(
|
||||
}
|
||||
}
|
||||
|
||||
// ===== TypeBox FFI (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 c_char, // C string
|
||||
pub resolve: Option<extern "C" fn(*const c_char) -> u32>,
|
||||
pub invoke_id: Option<extern "C" fn(u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32>,
|
||||
pub capabilities: u64,
|
||||
}
|
||||
unsafe impl Sync for NyashTypeBoxFfi {}
|
||||
|
||||
extern "C" fn array_resolve(name: *const c_char) -> u32 {
|
||||
if name.is_null() { return 0; }
|
||||
let s = unsafe { CStr::from_ptr(name) }.to_string_lossy();
|
||||
match s.as_ref() {
|
||||
"len" | "length" => METHOD_LENGTH,
|
||||
"get" => METHOD_GET,
|
||||
"set" => METHOD_SET,
|
||||
"push" => METHOD_PUSH,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn array_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 {
|
||||
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_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; }
|
||||
}
|
||||
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; }
|
||||
}
|
||||
_ => NYB_E_INVALID_METHOD,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub static nyash_typebox_ArrayBox: NyashTypeBoxFfi = NyashTypeBoxFfi {
|
||||
abi_tag: 0x54594258, // 'TYBX'
|
||||
version: 1,
|
||||
struct_size: std::mem::size_of::<NyashTypeBoxFfi>() as u16,
|
||||
name: b"ArrayBox\0".as_ptr() as *const c_char,
|
||||
resolve: Some(array_resolve),
|
||||
invoke_id: Some(array_invoke_id),
|
||||
capabilities: 0,
|
||||
};
|
||||
|
||||
// ===== Minimal TLV helpers (compatible with host expectations) =====
|
||||
fn preflight(result: *mut u8, result_len: *mut usize, needed: usize) -> bool {
|
||||
unsafe {
|
||||
|
||||
@ -150,3 +150,54 @@ pub extern "C" fn nyash_plugin_invoke(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== TypeBox FFI (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 c_char, // C string
|
||||
pub resolve: Option<extern "C" fn(*const c_char) -> u32>,
|
||||
pub invoke_id: Option<extern "C" fn(u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32>,
|
||||
pub capabilities: u64,
|
||||
}
|
||||
unsafe impl Sync for NyashTypeBoxFfi {}
|
||||
|
||||
extern "C" fn console_resolve(name: *const c_char) -> u32 {
|
||||
if name.is_null() { return 0; }
|
||||
let s = unsafe { CStr::from_ptr(name) }.to_string_lossy();
|
||||
match s.as_ref() {
|
||||
"log" => METHOD_LOG,
|
||||
"println" => METHOD_PRINTLN,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn console_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 {
|
||||
METHOD_LOG | METHOD_PRINTLN => {
|
||||
let slice = std::slice::from_raw_parts(args, args_len);
|
||||
let s = match parse_first_string(slice) {
|
||||
Ok(s) => s,
|
||||
Err(_) => format_first_any(slice).unwrap_or_else(|| "".to_string()),
|
||||
};
|
||||
if method_id == METHOD_LOG { print!("{}", s); } else { println!("{}", s); }
|
||||
return write_tlv_void(result, result_len);
|
||||
}
|
||||
_ => NYB_E_INVALID_METHOD,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub static nyash_typebox_ConsoleBox: NyashTypeBoxFfi = NyashTypeBoxFfi {
|
||||
abi_tag: 0x54594258, // 'TYBX'
|
||||
version: 1,
|
||||
struct_size: std::mem::size_of::<NyashTypeBoxFfi>() as u16,
|
||||
name: b"ConsoleBox\0".as_ptr() as *const c_char,
|
||||
resolve: Some(console_resolve),
|
||||
invoke_id: Some(console_invoke_id),
|
||||
capabilities: 0,
|
||||
};
|
||||
|
||||
22
plugins/nyash-egui-plugin/Cargo.toml
Normal file
22
plugins/nyash-egui-plugin/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "nyash-egui-plugin"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
once_cell = "1.21"
|
||||
crossbeam-channel = "0.5"
|
||||
cfg-if = "1.0"
|
||||
|
||||
# Optional: real GUI on Windows behind a feature in follow-ups
|
||||
[features]
|
||||
default = []
|
||||
with-egui = ["eframe", "egui"]
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
eframe = { version = "0.27", optional = true }
|
||||
egui = { version = "0.27", optional = true }
|
||||
|
||||
242
plugins/nyash-egui-plugin/src/lib.rs
Normal file
242
plugins/nyash-egui-plugin/src/lib.rs
Normal file
@ -0,0 +1,242 @@
|
||||
//! Nyash EguiBox Plugin (TypeBox ABI skeleton)
|
||||
//! - Provides a minimal window/UI placeholder via Nyash ABI
|
||||
//! - Windows GUI integration (egui/eframe) can be enabled later via `with-egui` feature
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{collections::HashMap, sync::{Mutex, atomic::{AtomicU32, Ordering}}};
|
||||
|
||||
// ===== Error/Status codes (BID-FFI v1 aligned) =====
|
||||
const OK: i32 = 0;
|
||||
const E_SHORT: i32 = -1;
|
||||
const E_TYPE: i32 = -2;
|
||||
const E_METHOD: i32 = -3;
|
||||
const E_ARGS: i32 = -4;
|
||||
const E_FAIL: i32 = -5;
|
||||
|
||||
// ===== IDs =====
|
||||
const TID_EGUI: u32 = 70; // match nyash.toml [box_types]
|
||||
|
||||
// methods
|
||||
const M_BIRTH: u32 = 0;
|
||||
const M_OPEN: u32 = 1; // open(width:int, height:int, title:str)
|
||||
const M_UI_LABEL: u32 = 2; // uiLabel(text:str)
|
||||
const M_UI_BUTTON: u32 = 3; // uiButton(text:str) -> future: events
|
||||
const M_POLL_EVENT: u32 = 4; // pollEvent() -> Result.Ok(text) / Result.Err("none")
|
||||
const M_RUN: u32 = 5; // run() -> enters loop or no-op
|
||||
const M_CLOSE: u32 = 6; // close()
|
||||
const M_FINI: u32 = u32::MAX;
|
||||
|
||||
#[derive(Default)]
|
||||
struct EguiInstance {
|
||||
width: i32,
|
||||
height: i32,
|
||||
title: String,
|
||||
labels: Vec<String>,
|
||||
}
|
||||
|
||||
static INST: Lazy<Mutex<HashMap<u32, EguiInstance>>> = Lazy::new(|| Mutex::new(HashMap::new()));
|
||||
static NEXT_ID: AtomicU32 = AtomicU32::new(1);
|
||||
|
||||
// ===== TypeBox ABI (resolve/invoke_id) =====
|
||||
#[repr(C)]
|
||||
pub struct NyashTypeBoxFfi {
|
||||
pub abi_tag: u32,
|
||||
pub version: u16,
|
||||
pub struct_size: u16,
|
||||
pub name: *const std::os::raw::c_char,
|
||||
pub resolve: Option<extern "C" fn(*const std::os::raw::c_char) -> u32>,
|
||||
pub invoke_id: Option<extern "C" fn(u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32>,
|
||||
pub capabilities: u64,
|
||||
}
|
||||
|
||||
// This simple POD struct contains raw pointers; mark as Sync for static export
|
||||
unsafe impl Sync for NyashTypeBoxFfi {}
|
||||
|
||||
const ABI_TAG: u32 = 0x58594254; // 'T''Y''B''X' little-endian (TYBX)
|
||||
|
||||
extern "C" fn tb_resolve(name: *const std::os::raw::c_char) -> u32 {
|
||||
unsafe {
|
||||
if name.is_null() { return 0; }
|
||||
let s = std::ffi::CStr::from_ptr(name).to_string_lossy();
|
||||
match s.as_ref() {
|
||||
"birth" => M_BIRTH,
|
||||
"open" => M_OPEN,
|
||||
"uiLabel" => M_UI_LABEL,
|
||||
"uiButton" => M_UI_BUTTON,
|
||||
"pollEvent" => M_POLL_EVENT,
|
||||
"run" => M_RUN,
|
||||
"close" => M_CLOSE,
|
||||
"fini" => M_FINI,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn tb_invoke_id(_method_id: u32, instance_id: u32, args: *const u8, args_len: usize, result: *mut u8, result_len: *mut usize) -> i32 {
|
||||
nyash_plugin_invoke(TID_EGUI, _method_id, instance_id, args, args_len, result, result_len)
|
||||
}
|
||||
|
||||
static TYPE_NAME: &[u8] = b"EguiBox\0";
|
||||
#[no_mangle]
|
||||
pub static nyash_typebox_EguiBox: NyashTypeBoxFfi = NyashTypeBoxFfi {
|
||||
abi_tag: ABI_TAG,
|
||||
version: 1,
|
||||
struct_size: std::mem::size_of::<NyashTypeBoxFfi>() as u16,
|
||||
name: TYPE_NAME.as_ptr() as *const _,
|
||||
resolve: Some(tb_resolve),
|
||||
invoke_id: Some(tb_invoke_id),
|
||||
capabilities: 0,
|
||||
};
|
||||
|
||||
// ===== Plugin entry points =====
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyash_plugin_abi() -> u32 { 1 }
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyash_plugin_init() -> i32 { OK }
|
||||
|
||||
#[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 != TID_EGUI { return E_TYPE; }
|
||||
unsafe {
|
||||
match method_id {
|
||||
M_BIRTH => {
|
||||
let need = 4; // instance_id (u32 LE)
|
||||
if result_len.is_null() { return E_ARGS; }
|
||||
if result.is_null() || *result_len < need { *result_len = need; return E_SHORT; }
|
||||
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
|
||||
if let Ok(mut m) = INST.lock() { m.insert(id, EguiInstance::default()); } else { return E_FAIL; }
|
||||
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_FAIL } }
|
||||
M_OPEN => {
|
||||
let (w, h, title) = match tlv_read_open_args(args, args_len) { Some(v) => v, None => return E_ARGS };
|
||||
if let Ok(mut m) = INST.lock() { if let Some(inst) = m.get_mut(&instance_id) { inst.width=w; inst.height=h; inst.title=title; } else { return E_FAIL; } } else { return E_FAIL; }
|
||||
write_tlv_void(result, result_len)
|
||||
}
|
||||
M_UI_LABEL => {
|
||||
let text = match tlv_read_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.labels.push(text); } else { return E_FAIL; } } else { return E_FAIL; }
|
||||
write_tlv_void(result, result_len)
|
||||
}
|
||||
M_UI_BUTTON => {
|
||||
// For now: stub, accept and return Void
|
||||
if tlv_read_string(args, args_len, 0).is_none() { return E_ARGS; }
|
||||
write_tlv_void(result, result_len)
|
||||
}
|
||||
M_POLL_EVENT => {
|
||||
// Stub: no events yet → return empty string "" (Ok)
|
||||
write_tlv_string("", result, result_len)
|
||||
}
|
||||
M_RUN => {
|
||||
// Windows + with-egui: 実ウィンドウを表示
|
||||
#[cfg(all(windows, feature = "with-egui"))]
|
||||
{
|
||||
if let Ok(m) = INST.lock() {
|
||||
if let Some(inst) = m.get(&instance_id) {
|
||||
winrun::run_window(inst.width, inst.height, &inst.title, inst.labels.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
// それ以外はスタブ
|
||||
write_tlv_void(result, result_len)
|
||||
}
|
||||
M_CLOSE => {
|
||||
// Stub: no-op close
|
||||
write_tlv_void(result, result_len)
|
||||
}
|
||||
_ => E_METHOD,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== TLV helpers (version=1) =====
|
||||
fn write_tlv_result(payloads: &[(u8, &[u8])], result: *mut u8, result_len: *mut usize) -> i32 {
|
||||
if result_len.is_null() { return E_ARGS; }
|
||||
let mut buf: Vec<u8> = Vec::with_capacity(4 + payloads.iter().map(|(_,p)| 4 + p.len()).sum::<usize>());
|
||||
buf.extend_from_slice(&1u16.to_le_bytes());
|
||||
buf.extend_from_slice(&(payloads.len() as u16).to_le_bytes());
|
||||
for (tag, payload) in payloads {
|
||||
buf.push(*tag); buf.push(0); buf.extend_from_slice(&(payload.len() as u16).to_le_bytes()); buf.extend_from_slice(payload);
|
||||
}
|
||||
unsafe {
|
||||
let need = buf.len();
|
||||
if result.is_null() || *result_len < need { *result_len = need; return E_SHORT; }
|
||||
std::ptr::copy_nonoverlapping(buf.as_ptr(), result, need);
|
||||
*result_len = need;
|
||||
}
|
||||
OK
|
||||
}
|
||||
fn write_tlv_void(result: *mut u8, result_len: *mut usize) -> i32 { write_tlv_result(&[(9u8, &[])], result, result_len) }
|
||||
fn write_tlv_string(s: &str, result: *mut u8, result_len: *mut usize) -> i32 { write_tlv_result(&[(6u8, s.as_bytes())], result, result_len) }
|
||||
|
||||
unsafe fn tlv_parse_header(data: *const u8, len: usize) -> Option<(u16,u16,usize)> {
|
||||
if data.is_null() || len < 4 { return None; }
|
||||
let b = std::slice::from_raw_parts(data, len);
|
||||
let ver = u16::from_le_bytes([b[0], b[1]]);
|
||||
let argc = u16::from_le_bytes([b[2], b[3]]);
|
||||
if ver != 1 { return None; }
|
||||
Some((ver, argc, 4))
|
||||
}
|
||||
unsafe fn tlv_read_entry_at(data: *const u8, len: usize, mut pos: usize) -> Option<(u8, usize, usize)> {
|
||||
let b = std::slice::from_raw_parts(data, len);
|
||||
if pos + 4 > len { return None; }
|
||||
let tag = b[pos]; let _ = b[pos+1]; let size = u16::from_le_bytes([b[pos+2], b[pos+3]]) as usize; pos += 4;
|
||||
if pos + size > len { return None; }
|
||||
Some((tag, size, pos))
|
||||
}
|
||||
unsafe fn tlv_read_i64(data: *const u8, len: usize, index: usize) -> Option<i64> {
|
||||
let (_, argc, mut pos) = tlv_parse_header(data, len)?; if argc < (index as u16 + 1) { return None; }
|
||||
for i in 0..=index { let (tag, size, p) = tlv_read_entry_at(data, len, pos)?; if tag == 3 && size == 8 { if i == index { let b = std::slice::from_raw_parts(data.add(p), 8); let mut t=[0u8;8]; t.copy_from_slice(b); return Some(i64::from_le_bytes(t)); } } pos = p + size; }
|
||||
None
|
||||
}
|
||||
unsafe fn tlv_read_string(data: *const u8, len: usize, index: usize) -> Option<String> {
|
||||
let (_, argc, mut pos) = tlv_parse_header(data, len)?; if argc < (index as u16 + 1) { return None; }
|
||||
for i in 0..=index { let (tag, size, p) = tlv_read_entry_at(data, len, pos)?; if tag == 6 || tag == 7 { if i == index { let s = std::slice::from_raw_parts(data.add(p), size); return Some(String::from_utf8_lossy(s).to_string()); } } pos = p + size; }
|
||||
None
|
||||
}
|
||||
unsafe fn tlv_read_open_args(args: *const u8, len: usize) -> Option<(i32,i32,String)> {
|
||||
let w = tlv_read_i64(args, len, 0)? as i32;
|
||||
let h = tlv_read_i64(args, len, 1)? as i32;
|
||||
let t = tlv_read_string(args, len, 2)?;
|
||||
Some((w,h,t))
|
||||
}
|
||||
|
||||
// ===== Windows 実行(with-egui) =====
|
||||
#[cfg(all(windows, feature = "with-egui"))]
|
||||
mod winrun {
|
||||
use super::*;
|
||||
use eframe::egui;
|
||||
|
||||
pub fn run_window(w: i32, h: i32, title: &str, labels: Vec<String>) {
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
.with_inner_size([w.max(100) as f32, h.max(100) as f32])
|
||||
.with_title(title.to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
struct App { labels: Vec<String> }
|
||||
impl eframe::App for App {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
for s in &self.labels { ui.label(s); }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let _ = eframe::run_native(
|
||||
title,
|
||||
options,
|
||||
Box::new(|_cc| Box::new(App { labels })),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,8 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, atomic::{AtomicU32, Ordering}};
|
||||
use std::os::raw::c_char;
|
||||
use std::ffi::CStr;
|
||||
|
||||
// Error codes
|
||||
const OK: i32 = 0;
|
||||
@ -73,6 +75,55 @@ pub extern "C" fn nyash_plugin_invoke(
|
||||
}
|
||||
}
|
||||
|
||||
// ===== TypeBox FFI (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 c_char, // C string
|
||||
pub resolve: Option<extern "C" fn(*const c_char) -> u32>,
|
||||
pub invoke_id: Option<extern "C" fn(u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32>,
|
||||
pub capabilities: u64,
|
||||
}
|
||||
unsafe impl Sync for NyashTypeBoxFfi {}
|
||||
|
||||
extern "C" fn integer_resolve(name: *const c_char) -> u32 {
|
||||
if name.is_null() { return 0; }
|
||||
let s = unsafe { CStr::from_ptr(name) }.to_string_lossy();
|
||||
match s.as_ref() {
|
||||
"get" => M_GET,
|
||||
"set" => M_SET,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn integer_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_GET => {
|
||||
if let Ok(m) = INST.lock() { if let Some(inst) = m.get(&instance_id) { return write_tlv_i64(inst.value, result, result_len); } else { return E_HANDLE; } } else { return E_PLUGIN; }
|
||||
}
|
||||
M_SET => {
|
||||
let v = match read_arg_i64(args, args_len, 0) { Some(v) => v, None => return E_ARGS };
|
||||
if let Ok(mut m) = INST.lock() { if let Some(inst) = m.get_mut(&instance_id) { inst.value = v; return write_tlv_i64(inst.value, result, result_len); } else { return E_HANDLE; } } else { return E_PLUGIN; }
|
||||
}
|
||||
_ => E_METHOD,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub static nyash_typebox_IntegerBox: NyashTypeBoxFfi = NyashTypeBoxFfi {
|
||||
abi_tag: 0x54594258, // 'TYBX'
|
||||
version: 1,
|
||||
struct_size: std::mem::size_of::<NyashTypeBoxFfi>() as u16,
|
||||
name: b"IntegerBox\0".as_ptr() as *const c_char,
|
||||
resolve: Some(integer_resolve),
|
||||
invoke_id: Some(integer_invoke_id),
|
||||
capabilities: 0,
|
||||
};
|
||||
|
||||
fn preflight(result: *mut u8, result_len: *mut usize, needed: usize) -> bool {
|
||||
unsafe { if result_len.is_null() { return false; } if result.is_null() || *result_len < needed { *result_len = needed; return true; } }
|
||||
false
|
||||
|
||||
@ -5,6 +5,8 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, atomic::{AtomicU32, Ordering}};
|
||||
use std::os::raw::c_char;
|
||||
use std::ffi::CStr;
|
||||
|
||||
// Error codes
|
||||
const NYB_SUCCESS: i32 = 0;
|
||||
@ -251,6 +253,143 @@ pub extern "C" fn nyash_plugin_invoke(
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Nyash TypeBox (FFI minimal PoC) ----
|
||||
#[repr(C)]
|
||||
pub struct NyashTypeBoxFfi {
|
||||
pub abi_tag: u32, // 'TYBX'
|
||||
pub version: u16, // 1
|
||||
pub struct_size: u16, // sizeof(NyashTypeBoxFfi)
|
||||
pub name: *const c_char, // C string
|
||||
pub resolve: Option<extern "C" fn(*const c_char) -> u32>,
|
||||
pub invoke_id: Option<extern "C" fn(u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32>,
|
||||
pub capabilities: u64,
|
||||
}
|
||||
unsafe impl Sync for NyashTypeBoxFfi {}
|
||||
|
||||
extern "C" fn mapbox_resolve(name: *const c_char) -> u32 {
|
||||
if name.is_null() { return 0; }
|
||||
let s = unsafe { CStr::from_ptr(name) }.to_string_lossy();
|
||||
match s.as_ref() {
|
||||
"size" | "len" => METHOD_SIZE,
|
||||
"get" => METHOD_GET,
|
||||
"has" => METHOD_HAS,
|
||||
"set" => METHOD_SET,
|
||||
"getS" => METHOD_GET_STR,
|
||||
"hasS" => METHOD_HAS_STR,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn mapbox_invoke_id(instance_id: u32, method_id: u32, args: *const u8, args_len: usize, result: *mut u8, result_len: *mut usize) -> i32 {
|
||||
match method_id {
|
||||
METHOD_SIZE => {
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) {
|
||||
let sz = inst.data_i64.len() + inst.data_str.len();
|
||||
return write_tlv_i64(sz as i64, result, result_len);
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
}
|
||||
METHOD_GET => {
|
||||
if let Some(ik) = read_arg_i64(args, args_len, 0) {
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) {
|
||||
match inst.data_i64.get(&ik).copied() { Some(v) => write_tlv_i64(v, result, result_len), None => NYB_E_INVALID_ARGS }
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
} else if let Some(sk) = read_arg_string(args, args_len, 0) {
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) {
|
||||
match inst.data_str.get(&sk).copied() { Some(v) => write_tlv_i64(v, result, result_len), None => NYB_E_INVALID_ARGS }
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
} else { NYB_E_INVALID_ARGS }
|
||||
}
|
||||
METHOD_HAS => {
|
||||
if let Some(ik) = read_arg_i64(args, args_len, 0) {
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) { write_tlv_bool(inst.data_i64.contains_key(&ik), result, result_len) } else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
} else if let Some(sk) = read_arg_string(args, args_len, 0) {
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) { write_tlv_bool(inst.data_str.contains_key(&sk), result, result_len) } else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
} else { NYB_E_INVALID_ARGS }
|
||||
}
|
||||
METHOD_SET => {
|
||||
// key: i64 or string, value: i64
|
||||
if let Some(val) = read_arg_i64(args, args_len, 1) {
|
||||
if let Some(ik) = read_arg_i64(args, args_len, 0) {
|
||||
if let Ok(mut map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get_mut(&instance_id) {
|
||||
inst.data_i64.insert(ik, val);
|
||||
let sz = inst.data_i64.len() + inst.data_str.len();
|
||||
return write_tlv_i64(sz as i64, result, result_len);
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
} else if let Some(sk) = read_arg_string(args, args_len, 0) {
|
||||
if let Ok(mut map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get_mut(&instance_id) {
|
||||
inst.data_str.insert(sk, val);
|
||||
let sz = inst.data_i64.len() + inst.data_str.len();
|
||||
return write_tlv_i64(sz as i64, result, result_len);
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
} else { NYB_E_INVALID_ARGS }
|
||||
} else { NYB_E_INVALID_ARGS }
|
||||
}
|
||||
METHOD_GET_STR => {
|
||||
let key = match read_arg_string(args, args_len, 0) { Some(s) => s, None => return NYB_E_INVALID_ARGS };
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) {
|
||||
match inst.data_str.get(&key).copied() { Some(v) => write_tlv_i64(v, result, result_len), None => NYB_E_INVALID_ARGS }
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
}
|
||||
METHOD_HAS_STR => {
|
||||
let key = match read_arg_string(args, args_len, 0) { Some(s) => s, None => return NYB_E_INVALID_ARGS };
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) { write_tlv_bool(inst.data_str.contains_key(&key), result, result_len) } else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
}
|
||||
METHOD_KEYS_S => {
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) {
|
||||
let mut keys: Vec<String> = Vec::with_capacity(inst.data_i64.len() + inst.data_str.len());
|
||||
for k in inst.data_i64.keys() { keys.push(k.to_string()); }
|
||||
for k in inst.data_str.keys() { keys.push(k.clone()); }
|
||||
keys.sort();
|
||||
let out = keys.join("\n");
|
||||
return write_tlv_string(&out, result, result_len);
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
}
|
||||
METHOD_VALUES_S => {
|
||||
if let Ok(map) = INSTANCES.lock() {
|
||||
if let Some(inst) = map.get(&instance_id) {
|
||||
let mut vals: Vec<String> = Vec::with_capacity(inst.data_i64.len() + inst.data_str.len());
|
||||
for v in inst.data_i64.values() { vals.push(v.to_string()); }
|
||||
for v in inst.data_str.values() { vals.push(v.to_string()); }
|
||||
let out = vals.join("\n");
|
||||
return write_tlv_string(&out, result, result_len);
|
||||
} else { NYB_E_INVALID_HANDLE }
|
||||
} else { NYB_E_PLUGIN_ERROR }
|
||||
}
|
||||
_ => NYB_E_INVALID_METHOD,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub static nyash_typebox_MapBox: NyashTypeBoxFfi = NyashTypeBoxFfi {
|
||||
abi_tag: 0x54594258, // 'TYBX'
|
||||
version: 1,
|
||||
struct_size: std::mem::size_of::<NyashTypeBoxFfi>() as u16,
|
||||
name: b"MapBox\0".as_ptr() as *const c_char,
|
||||
resolve: Some(mapbox_resolve),
|
||||
invoke_id: Some(mapbox_invoke_id),
|
||||
capabilities: 0,
|
||||
};
|
||||
|
||||
fn escape_json(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len()+8);
|
||||
for ch in s.chars() {
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, atomic::{AtomicU32, Ordering}};
|
||||
use std::os::raw::c_char;
|
||||
use std::ffi::CStr;
|
||||
|
||||
const OK: i32 = 0;
|
||||
const E_SHORT: i32 = -1;
|
||||
@ -115,6 +117,71 @@ pub extern "C" fn nyash_plugin_invoke(
|
||||
}
|
||||
}
|
||||
|
||||
// ===== TypeBox FFI (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 c_char, // C string
|
||||
pub resolve: Option<extern "C" fn(*const c_char) -> u32>,
|
||||
pub invoke_id: Option<extern "C" fn(u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32>,
|
||||
pub capabilities: u64,
|
||||
}
|
||||
unsafe impl Sync for NyashTypeBoxFfi {}
|
||||
|
||||
extern "C" fn string_resolve(name: *const c_char) -> u32 {
|
||||
if name.is_null() { return 0; }
|
||||
let s = unsafe { CStr::from_ptr(name) }.to_string_lossy();
|
||||
match s.as_ref() {
|
||||
"len" | "length" => M_LENGTH,
|
||||
"toUtf8" => M_TO_UTF8,
|
||||
"concat" => M_CONCAT,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn string_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_LENGTH => {
|
||||
if let Ok(m) = INST.lock() { if let Some(inst) = m.get(&instance_id) { return write_tlv_i64(inst.s.len() as i64, result, result_len); } else { return E_HANDLE; } } else { return E_PLUGIN; }
|
||||
}
|
||||
M_TO_UTF8 => {
|
||||
if let Ok(m) = INST.lock() { if let Some(inst) = m.get(&instance_id) { return write_tlv_string(&inst.s, result, result_len); } else { return E_HANDLE; } } else { return E_PLUGIN; }
|
||||
}
|
||||
M_CONCAT => {
|
||||
// support String/Bytes or StringBox handle
|
||||
let (ok, rhs) = if let Some((t, inst)) = read_arg_handle(args, args_len, 0) {
|
||||
if t != TYPE_ID_STRING { return E_TYPE; }
|
||||
if let Ok(m) = INST.lock() { if let Some(s2) = m.get(&inst) { (true, s2.s.clone()) } else { (false, String::new()) } } else { return E_PLUGIN; }
|
||||
} else if let Some(s) = read_arg_string(args, args_len, 0) { (true, s) } else { (false, String::new()) };
|
||||
if !ok { return E_ARGS; }
|
||||
if let Ok(m) = INST.lock() {
|
||||
if let Some(inst) = m.get(&instance_id) {
|
||||
let mut new_s = inst.s.clone(); new_s.push_str(&rhs); drop(m);
|
||||
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
|
||||
if let Ok(mut mm) = INST.lock() { mm.insert(id, StrInstance { s: new_s }); }
|
||||
return write_tlv_handle(TYPE_ID_STRING, id, result, result_len);
|
||||
} else { return E_HANDLE; }
|
||||
} else { return E_PLUGIN; }
|
||||
}
|
||||
_ => E_METHOD,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub static nyash_typebox_StringBox: NyashTypeBoxFfi = NyashTypeBoxFfi {
|
||||
abi_tag: 0x54594258, // 'TYBX'
|
||||
version: 1,
|
||||
struct_size: std::mem::size_of::<NyashTypeBoxFfi>() as u16,
|
||||
name: b"StringBox\0".as_ptr() as *const c_char,
|
||||
resolve: Some(string_resolve),
|
||||
invoke_id: Some(string_invoke_id),
|
||||
capabilities: 0,
|
||||
};
|
||||
|
||||
fn preflight(result: *mut u8, result_len: *mut usize, needed: usize) -> bool {
|
||||
unsafe { if result_len.is_null() { return false; } if result.is_null() || *result_len < needed { *result_len = needed; return true; } }
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user