python-plugin: RAII (PyOwned/PyBorrowed) + autodecode enum; crate: ny-llvmc --emit exe with NyRT link; tools: build_llvm.sh crate-exe path + crate_exe_smoke; CURRENT_TASK update

This commit is contained in:
Selfhosting Dev
2025-09-18 03:57:25 +09:00
parent 1b12b1eb7d
commit 5d51086530
27 changed files with 2802 additions and 2484 deletions

View File

@ -123,9 +123,9 @@ 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 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<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>,
@ -135,7 +135,9 @@ unsafe impl Sync for NyashTypeBoxFfi {}
use std::ffi::CStr;
extern "C" fn pycompiler_resolve(name: *const std::os::raw::c_char) -> u32 {
if name.is_null() { return 0; }
if name.is_null() {
return 0;
}
let s = unsafe { CStr::from_ptr(name) }.to_string_lossy();
match s.as_ref() {
"birth" => METHOD_BIRTH,
@ -156,52 +158,81 @@ extern "C" fn pycompiler_invoke_id(
match method_id {
METHOD_BIRTH => unsafe {
let mut id_g = NEXT_ID.lock().unwrap();
let id = *id_g; *id_g += 1;
if result_len.is_null() { return NYB_E_SHORT_BUFFER; }
let id = *id_g;
*id_g += 1;
if result_len.is_null() {
return NYB_E_SHORT_BUFFER;
}
let need = 4usize;
if *result_len < need { *result_len = need; return NYB_E_SHORT_BUFFER; }
if *result_len < need {
*result_len = need;
return NYB_E_SHORT_BUFFER;
}
let out = std::slice::from_raw_parts_mut(result, *result_len);
out[0..4].copy_from_slice(&(id as u32).to_le_bytes());
*result_len = need;
NYB_SUCCESS
},
METHOD_COMPILE => unsafe {
let ir = if args.is_null() || args_len < 8 { None } else {
let ir = if args.is_null() || args_len < 8 {
None
} else {
let buf = std::slice::from_raw_parts(args, args_len);
let tag = u16::from_le_bytes([buf[4], buf[5]]);
let len = u16::from_le_bytes([buf[6], buf[7]]) as usize;
if tag == 6 && 8 + len <= buf.len() {
std::str::from_utf8(&buf[8..8+len]).ok().map(|s| s.to_string())
} else { None }
std::str::from_utf8(&buf[8..8 + len])
.ok()
.map(|s| s.to_string())
} else {
None
}
};
let nyash_source = if let Some(s) = ir.or_else(|| std::env::var("NYASH_PY_IR").ok()) {
match serde_json::from_str::<Json>(&s).ok() {
Some(Json::Object(map)) => {
if let Some(Json::String(src)) = map.get("nyash_source") { src.clone() }
else if let Some(module) = map.get("module") {
if let Some(Json::String(src)) = map.get("nyash_source") {
src.clone()
} else if let Some(module) = map.get("module") {
let mut ret_expr = "0".to_string();
if let Some(funcs) = module.get("functions").and_then(|v| v.as_array()) {
if let Some(funcs) = module.get("functions").and_then(|v| v.as_array())
{
if let Some(fun0) = funcs.get(0) {
if let Some(retv) = fun0.get("return_value") {
if retv.is_number() { ret_expr = retv.to_string(); }
else if let Some(s) = retv.as_str() { ret_expr = s.to_string(); }
if retv.is_number() {
ret_expr = retv.to_string();
} else if let Some(s) = retv.as_str() {
ret_expr = s.to_string();
}
}
}
}
format!("static box Generated {\n main() {\n return {}\n }\n}}", ret_expr)
} else { "static box Generated { main() { return 0 } }".to_string() }
format!(
"static box Generated {\n main() {\n return {}\n }\n}}",
ret_expr
)
} else {
"static box Generated { main() { return 0 } }".to_string()
}
}
_ => "static box Generated { main() { return 0 } }".to_string(),
}
} else { "static box Generated { main() { return 0 } }".to_string() };
} else {
"static box Generated { main() { return 0 } }".to_string()
};
let bytes = nyash_source.as_bytes();
if result_len.is_null() { return NYB_E_SHORT_BUFFER; }
if result_len.is_null() {
return NYB_E_SHORT_BUFFER;
}
let need = 4 + bytes.len();
if *result_len < need { *result_len = need; return NYB_E_SHORT_BUFFER; }
if *result_len < need {
*result_len = need;
return NYB_E_SHORT_BUFFER;
}
let out = std::slice::from_raw_parts_mut(result, *result_len);
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);
out[4..4 + bytes.len()].copy_from_slice(bytes);
*result_len = need;
NYB_SUCCESS
},