chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt
This commit is contained in:
@ -13,13 +13,23 @@ thread_local! {
|
||||
static CURRENT_VM: std::cell::Cell<*mut crate::backend::vm::VM> = std::cell::Cell::new(std::ptr::null_mut());
|
||||
}
|
||||
|
||||
pub fn set_current_vm(ptr: *mut crate::backend::vm::VM) { CURRENT_VM.with(|c| c.set(ptr)); }
|
||||
pub fn clear_current_vm() { CURRENT_VM.with(|c| c.set(std::ptr::null_mut())); }
|
||||
pub fn set_current_vm(ptr: *mut crate::backend::vm::VM) {
|
||||
CURRENT_VM.with(|c| c.set(ptr));
|
||||
}
|
||||
pub fn clear_current_vm() {
|
||||
CURRENT_VM.with(|c| c.set(std::ptr::null_mut()));
|
||||
}
|
||||
fn with_current_vm_mut<F, R>(f: F) -> Option<R>
|
||||
where F: FnOnce(&mut crate::backend::vm::VM) -> R {
|
||||
where
|
||||
F: FnOnce(&mut crate::backend::vm::VM) -> R,
|
||||
{
|
||||
CURRENT_VM.with(|c| {
|
||||
let p = c.get();
|
||||
if p.is_null() { None } else { Some(unsafe { f(&mut *p) }) }
|
||||
if p.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(unsafe { f(&mut *p) })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -45,35 +55,59 @@ fn tlv_encode_one(val: &crate::backend::vm::VMValue) -> Vec<u8> {
|
||||
fn vmvalue_from_tlv(tag: u8, payload: &[u8]) -> Option<crate::backend::vm::VMValue> {
|
||||
use crate::runtime::plugin_ffi_common as tlv;
|
||||
match tag {
|
||||
1 => Some(crate::backend::vm::VMValue::Bool(tlv::decode::bool(payload).unwrap_or(false))),
|
||||
1 => Some(crate::backend::vm::VMValue::Bool(
|
||||
tlv::decode::bool(payload).unwrap_or(false),
|
||||
)),
|
||||
2 => tlv::decode::i32(payload).map(|v| crate::backend::vm::VMValue::Integer(v as i64)),
|
||||
3 => {
|
||||
if payload.len()==8 { let mut b=[0u8;8]; b.copy_from_slice(payload); Some(crate::backend::vm::VMValue::Integer(i64::from_le_bytes(b))) } else { None }
|
||||
if payload.len() == 8 {
|
||||
let mut b = [0u8; 8];
|
||||
b.copy_from_slice(payload);
|
||||
Some(crate::backend::vm::VMValue::Integer(i64::from_le_bytes(b)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
5 => tlv::decode::f64(payload).map(crate::backend::vm::VMValue::Float),
|
||||
6|7 => Some(crate::backend::vm::VMValue::String(tlv::decode::string(payload))),
|
||||
6 | 7 => Some(crate::backend::vm::VMValue::String(tlv::decode::string(
|
||||
payload,
|
||||
))),
|
||||
8 => {
|
||||
// PluginHandle(type_id, instance_id) → reconstruct PluginBoxV2 (when plugins enabled)
|
||||
if let Some((type_id, instance_id)) = tlv::decode::plugin_handle(payload) {
|
||||
if let Some(arc) = plugin_box_from_handle(type_id, instance_id) { return Some(crate::backend::vm::VMValue::BoxRef(arc)); }
|
||||
if let Some(arc) = plugin_box_from_handle(type_id, instance_id) {
|
||||
return Some(crate::backend::vm::VMValue::BoxRef(arc));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
9 => {
|
||||
if let Some(h) = tlv::decode::u64(payload) { crate::runtime::host_handles::get(h).map(crate::backend::vm::VMValue::BoxRef) } else { None }
|
||||
if let Some(h) = tlv::decode::u64(payload) {
|
||||
crate::runtime::host_handles::get(h).map(crate::backend::vm::VMValue::BoxRef)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn slice_from_raw<'a>(ptr: *const u8, len: usize) -> &'a [u8] { std::slice::from_raw_parts(ptr, len) }
|
||||
unsafe fn slice_from_raw_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut [u8] { std::slice::from_raw_parts_mut(ptr, len) }
|
||||
unsafe fn slice_from_raw<'a>(ptr: *const u8, len: usize) -> &'a [u8] {
|
||||
std::slice::from_raw_parts(ptr, len)
|
||||
}
|
||||
unsafe fn slice_from_raw_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut [u8] {
|
||||
std::slice::from_raw_parts_mut(ptr, len)
|
||||
}
|
||||
|
||||
fn encode_out(out_ptr: *mut u8, out_len: *mut usize, buf: &[u8]) -> i32 {
|
||||
unsafe {
|
||||
if out_ptr.is_null() || out_len.is_null() { return -2; }
|
||||
if out_ptr.is_null() || out_len.is_null() {
|
||||
return -2;
|
||||
}
|
||||
let cap = *out_len;
|
||||
if cap < buf.len() { return -3; }
|
||||
if cap < buf.len() {
|
||||
return -3;
|
||||
}
|
||||
let out = slice_from_raw_mut(out_ptr, cap);
|
||||
out[..buf.len()].copy_from_slice(buf);
|
||||
*out_len = buf.len();
|
||||
@ -82,62 +116,115 @@ fn encode_out(out_ptr: *mut u8, out_len: *mut usize, buf: &[u8]) -> i32 {
|
||||
}
|
||||
|
||||
#[cfg_attr(all(not(test), feature = "c-abi-export"), no_mangle)]
|
||||
pub extern "C" fn nyrt_host_call_name(handle: u64, method_ptr: *const u8, method_len: usize,
|
||||
args_ptr: *const u8, args_len: usize,
|
||||
out_ptr: *mut u8, out_len: *mut usize) -> i32 {
|
||||
pub extern "C" fn nyrt_host_call_name(
|
||||
handle: u64,
|
||||
method_ptr: *const u8,
|
||||
method_len: usize,
|
||||
args_ptr: *const u8,
|
||||
args_len: usize,
|
||||
out_ptr: *mut u8,
|
||||
out_len: *mut usize,
|
||||
) -> i32 {
|
||||
// Resolve receiver
|
||||
let recv_arc = match crate::runtime::host_handles::get(handle) { Some(a) => a, None => return -1 };
|
||||
let method = unsafe { std::str::from_utf8(slice_from_raw(method_ptr, method_len)).unwrap_or("") }.to_string();
|
||||
let recv_arc = match crate::runtime::host_handles::get(handle) {
|
||||
Some(a) => a,
|
||||
None => return -1,
|
||||
};
|
||||
let method =
|
||||
unsafe { std::str::from_utf8(slice_from_raw(method_ptr, method_len)).unwrap_or("") }
|
||||
.to_string();
|
||||
// Parse TLV args (header + entries)
|
||||
let mut argv: Vec<crate::backend::vm::VMValue> = Vec::new();
|
||||
if args_ptr.is_null() || args_len < 4 { /* no args */ } else {
|
||||
if args_ptr.is_null() || args_len < 4 { /* no args */
|
||||
} else {
|
||||
let buf = unsafe { slice_from_raw(args_ptr, args_len) };
|
||||
// Iterate entries
|
||||
let mut off = 4usize;
|
||||
while buf.len() >= off + 4 {
|
||||
let tag = buf[off]; let _rsv = buf[off+1]; let sz = u16::from_le_bytes([buf[off+2], buf[off+3]]) as usize;
|
||||
if buf.len() < off + 4 + sz { break; }
|
||||
let payload = &buf[off+4..off+4+sz];
|
||||
if let Some(v) = vmvalue_from_tlv(tag, payload) { argv.push(v); }
|
||||
let tag = buf[off];
|
||||
let _rsv = buf[off + 1];
|
||||
let sz = u16::from_le_bytes([buf[off + 2], buf[off + 3]]) as usize;
|
||||
if buf.len() < off + 4 + sz {
|
||||
break;
|
||||
}
|
||||
let payload = &buf[off + 4..off + 4 + sz];
|
||||
if let Some(v) = vmvalue_from_tlv(tag, payload) {
|
||||
argv.push(v);
|
||||
}
|
||||
off += 4 + sz;
|
||||
}
|
||||
}
|
||||
// Dispatch minimal supported methods
|
||||
// InstanceBox getField/setField
|
||||
if let Some(inst) = recv_arc.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
|
||||
if let Some(inst) = recv_arc
|
||||
.as_any()
|
||||
.downcast_ref::<crate::instance_v2::InstanceBox>()
|
||||
{
|
||||
match method.as_str() {
|
||||
"getField" if argv.len() >= 1 => {
|
||||
let field = match &argv[0] { crate::backend::vm::VMValue::String(s) => s.clone(), v => v.to_string() };
|
||||
let out = inst.get_field_unified(&field).map(|nv| match nv {
|
||||
crate::value::NyashValue::Integer(i) => crate::backend::vm::VMValue::Integer(i),
|
||||
crate::value::NyashValue::Float(f) => crate::backend::vm::VMValue::Float(f),
|
||||
crate::value::NyashValue::Bool(b) => crate::backend::vm::VMValue::Bool(b),
|
||||
crate::value::NyashValue::String(s) => crate::backend::vm::VMValue::String(s),
|
||||
crate::value::NyashValue::Void | crate::value::NyashValue::Null => crate::backend::vm::VMValue::String("".to_string()),
|
||||
crate::value::NyashValue::Box(b) => {
|
||||
if let Ok(g) = b.lock() { crate::backend::vm::VMValue::BoxRef(std::sync::Arc::from(g.share_box())) } else { crate::backend::vm::VMValue::String("".to_string()) }
|
||||
}
|
||||
_ => crate::backend::vm::VMValue::String("".to_string()),
|
||||
}).unwrap_or(crate::backend::vm::VMValue::String("".to_string()));
|
||||
let field = match &argv[0] {
|
||||
crate::backend::vm::VMValue::String(s) => s.clone(),
|
||||
v => v.to_string(),
|
||||
};
|
||||
let out = inst
|
||||
.get_field_unified(&field)
|
||||
.map(|nv| match nv {
|
||||
crate::value::NyashValue::Integer(i) => {
|
||||
crate::backend::vm::VMValue::Integer(i)
|
||||
}
|
||||
crate::value::NyashValue::Float(f) => crate::backend::vm::VMValue::Float(f),
|
||||
crate::value::NyashValue::Bool(b) => crate::backend::vm::VMValue::Bool(b),
|
||||
crate::value::NyashValue::String(s) => {
|
||||
crate::backend::vm::VMValue::String(s)
|
||||
}
|
||||
crate::value::NyashValue::Void | crate::value::NyashValue::Null => {
|
||||
crate::backend::vm::VMValue::String("".to_string())
|
||||
}
|
||||
crate::value::NyashValue::Box(b) => {
|
||||
if let Ok(g) = b.lock() {
|
||||
crate::backend::vm::VMValue::BoxRef(std::sync::Arc::from(
|
||||
g.share_box(),
|
||||
))
|
||||
} else {
|
||||
crate::backend::vm::VMValue::String("".to_string())
|
||||
}
|
||||
}
|
||||
_ => crate::backend::vm::VMValue::String("".to_string()),
|
||||
})
|
||||
.unwrap_or(crate::backend::vm::VMValue::String("".to_string()));
|
||||
let buf = tlv_encode_one(&out);
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
"setField" if argv.len() >= 2 => {
|
||||
let field = match &argv[0] { crate::backend::vm::VMValue::String(s) => s.clone(), v => v.to_string() };
|
||||
let field = match &argv[0] {
|
||||
crate::backend::vm::VMValue::String(s) => s.clone(),
|
||||
v => v.to_string(),
|
||||
};
|
||||
// Barrier: use current VM runtime if available
|
||||
let _ = with_current_vm_mut(|vm| {
|
||||
crate::backend::gc_helpers::gc_write_barrier_site(vm.runtime_ref(), "HostAPI.setField");
|
||||
crate::backend::gc_helpers::gc_write_barrier_site(
|
||||
vm.runtime_ref(),
|
||||
"HostAPI.setField",
|
||||
);
|
||||
});
|
||||
// Accept primitives only for now
|
||||
let nv_opt = match argv[1].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Some(crate::value::NyashValue::Integer(i)),
|
||||
crate::backend::vm::VMValue::Float(f) => Some(crate::value::NyashValue::Float(f)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Some(crate::value::NyashValue::Integer(i))
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Some(crate::value::NyashValue::Float(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => Some(crate::value::NyashValue::Bool(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Some(crate::value::NyashValue::String(s)),
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Some(crate::value::NyashValue::String(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(_) => None,
|
||||
_ => None,
|
||||
};
|
||||
if let Some(nv) = nv_opt { let _ = inst.set_field_unified(field, nv); }
|
||||
if let Some(nv) = nv_opt {
|
||||
let _ = inst.set_field_unified(field, nv);
|
||||
}
|
||||
let buf = tlv_encode_one(&crate::backend::vm::VMValue::Bool(true));
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
@ -145,27 +232,47 @@ pub extern "C" fn nyrt_host_call_name(handle: u64, method_ptr: *const u8, method
|
||||
}
|
||||
}
|
||||
// ArrayBox get/set
|
||||
if let Some(arr) = recv_arc.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
if let Some(arr) = recv_arc
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
match method.as_str() {
|
||||
"get" if argv.len() >= 1 => {
|
||||
let idx = match argv[0].clone() { crate::backend::vm::VMValue::Integer(i) => i, v => v.to_string().parse::<i64>().unwrap_or(0) };
|
||||
let idx = match argv[0].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => i,
|
||||
v => v.to_string().parse::<i64>().unwrap_or(0),
|
||||
};
|
||||
let out = arr.get(Box::new(crate::box_trait::IntegerBox::new(idx)));
|
||||
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
|
||||
let buf = tlv_encode_one(&vmv);
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
"set" if argv.len() >= 2 => {
|
||||
let idx = match argv[0].clone() { crate::backend::vm::VMValue::Integer(i) => i, v => v.to_string().parse::<i64>().unwrap_or(0) };
|
||||
let idx = match argv[0].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => i,
|
||||
v => v.to_string().parse::<i64>().unwrap_or(0),
|
||||
};
|
||||
let vb = match argv[1].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Box::new(crate::box_trait::IntegerBox::new(i)) as Box<dyn NyashBox>,
|
||||
crate::backend::vm::VMValue::Float(f) => Box::new(crate::boxes::math_box::FloatBox::new(f)),
|
||||
crate::backend::vm::VMValue::Bool(b) => Box::new(crate::box_trait::BoolBox::new(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Box::new(crate::box_trait::StringBox::new(s)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Box::new(crate::box_trait::IntegerBox::new(i)) as Box<dyn NyashBox>
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Box::new(crate::boxes::math_box::FloatBox::new(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => {
|
||||
Box::new(crate::box_trait::BoolBox::new(b))
|
||||
}
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Box::new(crate::box_trait::StringBox::new(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(b) => b.share_box(),
|
||||
_ => Box::new(crate::box_trait::VoidBox::new()),
|
||||
};
|
||||
let _ = with_current_vm_mut(|vm| {
|
||||
crate::backend::gc_helpers::gc_write_barrier_site(vm.runtime_ref(), "HostAPI.Array.set");
|
||||
crate::backend::gc_helpers::gc_write_barrier_site(
|
||||
vm.runtime_ref(),
|
||||
"HostAPI.Array.set",
|
||||
);
|
||||
});
|
||||
let out = arr.set(Box::new(crate::box_trait::IntegerBox::new(idx)), vb);
|
||||
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
|
||||
@ -188,7 +295,12 @@ fn plugin_box_from_handle(type_id: u32, instance_id: u32) -> Option<std::sync::A
|
||||
Some(std::sync::Arc::from(bx))
|
||||
}
|
||||
#[cfg(any(not(feature = "plugins"), target_arch = "wasm32"))]
|
||||
fn plugin_box_from_handle(_type_id: u32, _instance_id: u32) -> Option<std::sync::Arc<dyn NyashBox>> { None }
|
||||
fn plugin_box_from_handle(
|
||||
_type_id: u32,
|
||||
_instance_id: u32,
|
||||
) -> Option<std::sync::Arc<dyn NyashBox>> {
|
||||
None
|
||||
}
|
||||
|
||||
// ---- by-slot variant (selector_id: u64) ----
|
||||
// Minimal slot mapping (subject to consolidation with TypeRegistry):
|
||||
@ -206,66 +318,125 @@ fn plugin_box_from_handle(_type_id: u32, _instance_id: u32) -> Option<std::sync:
|
||||
// 204: MapBox.set(key:any, value:any) -> any
|
||||
// 300: StringBox.len() -> i64
|
||||
#[cfg_attr(all(not(test), feature = "c-abi-export"), no_mangle)]
|
||||
pub extern "C" fn nyrt_host_call_slot(handle: u64, selector_id: u64,
|
||||
args_ptr: *const u8, args_len: usize,
|
||||
out_ptr: *mut u8, out_len: *mut usize) -> i32 {
|
||||
let recv_arc = match crate::runtime::host_handles::get(handle) { Some(a) => a, None => return -1 };
|
||||
pub extern "C" fn nyrt_host_call_slot(
|
||||
handle: u64,
|
||||
selector_id: u64,
|
||||
args_ptr: *const u8,
|
||||
args_len: usize,
|
||||
out_ptr: *mut u8,
|
||||
out_len: *mut usize,
|
||||
) -> i32 {
|
||||
let recv_arc = match crate::runtime::host_handles::get(handle) {
|
||||
Some(a) => a,
|
||||
None => return -1,
|
||||
};
|
||||
// Parse TLV args
|
||||
let mut argv: Vec<crate::backend::vm::VMValue> = Vec::new();
|
||||
if !args_ptr.is_null() && args_len >= 4 {
|
||||
let buf = unsafe { slice_from_raw(args_ptr, args_len) };
|
||||
let mut off = 4usize;
|
||||
while buf.len() >= off + 4 {
|
||||
let tag = buf[off]; let sz = u16::from_le_bytes([buf[off+2], buf[off+3]]) as usize;
|
||||
if buf.len() < off + 4 + sz { break; }
|
||||
let payload = &buf[off+4..off+4+sz];
|
||||
if let Some(v) = vmvalue_from_tlv(tag, payload) { argv.push(v); }
|
||||
let tag = buf[off];
|
||||
let sz = u16::from_le_bytes([buf[off + 2], buf[off + 3]]) as usize;
|
||||
if buf.len() < off + 4 + sz {
|
||||
break;
|
||||
}
|
||||
let payload = &buf[off + 4..off + 4 + sz];
|
||||
if let Some(v) = vmvalue_from_tlv(tag, payload) {
|
||||
argv.push(v);
|
||||
}
|
||||
off += 4 + sz;
|
||||
}
|
||||
}
|
||||
|
||||
match selector_id {
|
||||
1 | 2 | 3 | 4 => {
|
||||
if let Some(inst) = recv_arc.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
|
||||
if let Some(inst) = recv_arc
|
||||
.as_any()
|
||||
.downcast_ref::<crate::instance_v2::InstanceBox>()
|
||||
{
|
||||
if selector_id == 1 {
|
||||
// getField(name)
|
||||
if argv.len() >= 1 {
|
||||
let field = match &argv[0] { crate::backend::vm::VMValue::String(s) => s.clone(), v => v.to_string() };
|
||||
let out = inst.get_field_unified(&field).map(|nv| match nv {
|
||||
crate::value::NyashValue::Integer(i) => crate::backend::vm::VMValue::Integer(i),
|
||||
crate::value::NyashValue::Float(f) => crate::backend::vm::VMValue::Float(f),
|
||||
crate::value::NyashValue::Bool(b) => crate::backend::vm::VMValue::Bool(b),
|
||||
crate::value::NyashValue::String(s) => crate::backend::vm::VMValue::String(s),
|
||||
crate::value::NyashValue::Void | crate::value::NyashValue::Null => crate::backend::vm::VMValue::String("".to_string()),
|
||||
crate::value::NyashValue::Box(b) => {
|
||||
if let Ok(g) = b.lock() { crate::backend::vm::VMValue::BoxRef(std::sync::Arc::from(g.share_box())) } else { crate::backend::vm::VMValue::String("".to_string()) }
|
||||
}
|
||||
_ => crate::backend::vm::VMValue::String("".to_string()),
|
||||
}).unwrap_or(crate::backend::vm::VMValue::String("".to_string()));
|
||||
let field = match &argv[0] {
|
||||
crate::backend::vm::VMValue::String(s) => s.clone(),
|
||||
v => v.to_string(),
|
||||
};
|
||||
let out = inst
|
||||
.get_field_unified(&field)
|
||||
.map(|nv| match nv {
|
||||
crate::value::NyashValue::Integer(i) => {
|
||||
crate::backend::vm::VMValue::Integer(i)
|
||||
}
|
||||
crate::value::NyashValue::Float(f) => {
|
||||
crate::backend::vm::VMValue::Float(f)
|
||||
}
|
||||
crate::value::NyashValue::Bool(b) => {
|
||||
crate::backend::vm::VMValue::Bool(b)
|
||||
}
|
||||
crate::value::NyashValue::String(s) => {
|
||||
crate::backend::vm::VMValue::String(s)
|
||||
}
|
||||
crate::value::NyashValue::Void | crate::value::NyashValue::Null => {
|
||||
crate::backend::vm::VMValue::String("".to_string())
|
||||
}
|
||||
crate::value::NyashValue::Box(b) => {
|
||||
if let Ok(g) = b.lock() {
|
||||
crate::backend::vm::VMValue::BoxRef(std::sync::Arc::from(
|
||||
g.share_box(),
|
||||
))
|
||||
} else {
|
||||
crate::backend::vm::VMValue::String("".to_string())
|
||||
}
|
||||
}
|
||||
_ => crate::backend::vm::VMValue::String("".to_string()),
|
||||
})
|
||||
.unwrap_or(crate::backend::vm::VMValue::String("".to_string()));
|
||||
let buf = tlv_encode_one(&out);
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
} else if selector_id == 2 {
|
||||
// setField(name, value)
|
||||
if argv.len() >= 2 {
|
||||
let field = match &argv[0] { crate::backend::vm::VMValue::String(s) => s.clone(), v => v.to_string() };
|
||||
let _ = with_current_vm_mut(|vm| { crate::backend::gc_helpers::gc_write_barrier_site(vm.runtime_ref(), "HostAPI.setField"); });
|
||||
let field = match &argv[0] {
|
||||
crate::backend::vm::VMValue::String(s) => s.clone(),
|
||||
v => v.to_string(),
|
||||
};
|
||||
let _ = with_current_vm_mut(|vm| {
|
||||
crate::backend::gc_helpers::gc_write_barrier_site(
|
||||
vm.runtime_ref(),
|
||||
"HostAPI.setField",
|
||||
);
|
||||
});
|
||||
let nv_opt = match argv[1].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Some(crate::value::NyashValue::Integer(i)),
|
||||
crate::backend::vm::VMValue::Float(f) => Some(crate::value::NyashValue::Float(f)),
|
||||
crate::backend::vm::VMValue::Bool(b) => Some(crate::value::NyashValue::Bool(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Some(crate::value::NyashValue::String(s)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Some(crate::value::NyashValue::Integer(i))
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Some(crate::value::NyashValue::Float(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => {
|
||||
Some(crate::value::NyashValue::Bool(b))
|
||||
}
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Some(crate::value::NyashValue::String(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(_) => None,
|
||||
_ => None,
|
||||
};
|
||||
if let Some(nv) = nv_opt { let _ = inst.set_field_unified(field, nv); }
|
||||
if let Some(nv) = nv_opt {
|
||||
let _ = inst.set_field_unified(field, nv);
|
||||
}
|
||||
let buf = tlv_encode_one(&crate::backend::vm::VMValue::Bool(true));
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
} else if selector_id == 3 {
|
||||
// has(name)
|
||||
if argv.len() >= 1 {
|
||||
let field = match &argv[0] { crate::backend::vm::VMValue::String(s) => s.clone(), v => v.to_string() };
|
||||
let field = match &argv[0] {
|
||||
crate::backend::vm::VMValue::String(s) => s.clone(),
|
||||
v => v.to_string(),
|
||||
};
|
||||
let has = inst.get_field_unified(&field).is_some();
|
||||
let buf = tlv_encode_one(&crate::backend::vm::VMValue::Bool(has));
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
@ -279,36 +450,62 @@ pub extern "C" fn nyrt_host_call_slot(handle: u64, selector_id: u64,
|
||||
}
|
||||
}
|
||||
100 | 101 | 102 => {
|
||||
if let Some(arr) = recv_arc.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
if let Some(arr) = recv_arc
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
match selector_id {
|
||||
100 => { // get(index)
|
||||
100 => {
|
||||
// get(index)
|
||||
if argv.len() >= 1 {
|
||||
let idx = match argv[0].clone() { crate::backend::vm::VMValue::Integer(i) => i, v => v.to_string().parse::<i64>().unwrap_or(0) };
|
||||
let idx = match argv[0].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => i,
|
||||
v => v.to_string().parse::<i64>().unwrap_or(0),
|
||||
};
|
||||
let out = arr.get(Box::new(crate::box_trait::IntegerBox::new(idx)));
|
||||
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
|
||||
let buf = tlv_encode_one(&vmv);
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
}
|
||||
101 => { // set(index, value)
|
||||
101 => {
|
||||
// set(index, value)
|
||||
if argv.len() >= 2 {
|
||||
let idx = match argv[0].clone() { crate::backend::vm::VMValue::Integer(i) => i, v => v.to_string().parse::<i64>().unwrap_or(0) };
|
||||
let idx = match argv[0].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => i,
|
||||
v => v.to_string().parse::<i64>().unwrap_or(0),
|
||||
};
|
||||
let vb = match argv[1].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Box::new(crate::box_trait::IntegerBox::new(i)) as Box<dyn NyashBox>,
|
||||
crate::backend::vm::VMValue::Float(f) => Box::new(crate::boxes::math_box::FloatBox::new(f)),
|
||||
crate::backend::vm::VMValue::Bool(b) => Box::new(crate::box_trait::BoolBox::new(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Box::new(crate::box_trait::StringBox::new(s)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Box::new(crate::box_trait::IntegerBox::new(i))
|
||||
as Box<dyn NyashBox>
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Box::new(crate::boxes::math_box::FloatBox::new(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => {
|
||||
Box::new(crate::box_trait::BoolBox::new(b))
|
||||
}
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Box::new(crate::box_trait::StringBox::new(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(b) => b.share_box(),
|
||||
_ => Box::new(crate::box_trait::VoidBox::new()),
|
||||
};
|
||||
let _ = with_current_vm_mut(|vm| { crate::backend::gc_helpers::gc_write_barrier_site(vm.runtime_ref(), "HostAPI.Array.set"); });
|
||||
let _ = with_current_vm_mut(|vm| {
|
||||
crate::backend::gc_helpers::gc_write_barrier_site(
|
||||
vm.runtime_ref(),
|
||||
"HostAPI.Array.set",
|
||||
);
|
||||
});
|
||||
let out = arr.set(Box::new(crate::box_trait::IntegerBox::new(idx)), vb);
|
||||
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
|
||||
let buf = tlv_encode_one(&vmv);
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
}
|
||||
102 => { // len()
|
||||
102 => {
|
||||
// len()
|
||||
let len = arr.len();
|
||||
let buf = tlv_encode_one(&crate::backend::vm::VMValue::Integer(len as i64));
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
@ -318,7 +515,10 @@ pub extern "C" fn nyrt_host_call_slot(handle: u64, selector_id: u64,
|
||||
}
|
||||
}
|
||||
200 | 201 | 202 | 203 | 204 => {
|
||||
if let Some(map) = recv_arc.as_any().downcast_ref::<crate::boxes::map_box::MapBox>() {
|
||||
if let Some(map) = recv_arc
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::map_box::MapBox>()
|
||||
{
|
||||
match selector_id {
|
||||
200 | 201 => {
|
||||
let out = map.size();
|
||||
@ -326,16 +526,27 @@ pub extern "C" fn nyrt_host_call_slot(handle: u64, selector_id: u64,
|
||||
let buf = tlv_encode_one(&vmv);
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
202 => { // has(key)
|
||||
202 => {
|
||||
// has(key)
|
||||
if argv.len() >= 1 {
|
||||
let key_box: Box<dyn NyashBox> = match argv[0].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Box::new(crate::box_trait::IntegerBox::new(i)),
|
||||
crate::backend::vm::VMValue::Float(f) => Box::new(crate::boxes::math_box::FloatBox::new(f)),
|
||||
crate::backend::vm::VMValue::Bool(b) => Box::new(crate::box_trait::BoolBox::new(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Box::new(crate::box_trait::StringBox::new(s)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Box::new(crate::box_trait::IntegerBox::new(i))
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Box::new(crate::boxes::math_box::FloatBox::new(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => {
|
||||
Box::new(crate::box_trait::BoolBox::new(b))
|
||||
}
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Box::new(crate::box_trait::StringBox::new(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(b) => b.share_box(),
|
||||
crate::backend::vm::VMValue::Future(fu) => Box::new(fu),
|
||||
crate::backend::vm::VMValue::Void => Box::new(crate::box_trait::VoidBox::new()),
|
||||
crate::backend::vm::VMValue::Void => {
|
||||
Box::new(crate::box_trait::VoidBox::new())
|
||||
}
|
||||
};
|
||||
let out = map.has(key_box);
|
||||
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
|
||||
@ -343,16 +554,27 @@ pub extern "C" fn nyrt_host_call_slot(handle: u64, selector_id: u64,
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
}
|
||||
203 => { // get(key)
|
||||
203 => {
|
||||
// get(key)
|
||||
if argv.len() >= 1 {
|
||||
let key_box: Box<dyn NyashBox> = match argv[0].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Box::new(crate::box_trait::IntegerBox::new(i)),
|
||||
crate::backend::vm::VMValue::Float(f) => Box::new(crate::boxes::math_box::FloatBox::new(f)),
|
||||
crate::backend::vm::VMValue::Bool(b) => Box::new(crate::box_trait::BoolBox::new(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Box::new(crate::box_trait::StringBox::new(s)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Box::new(crate::box_trait::IntegerBox::new(i))
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Box::new(crate::boxes::math_box::FloatBox::new(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => {
|
||||
Box::new(crate::box_trait::BoolBox::new(b))
|
||||
}
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Box::new(crate::box_trait::StringBox::new(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(b) => b.share_box(),
|
||||
crate::backend::vm::VMValue::Future(fu) => Box::new(fu),
|
||||
crate::backend::vm::VMValue::Void => Box::new(crate::box_trait::VoidBox::new()),
|
||||
crate::backend::vm::VMValue::Void => {
|
||||
Box::new(crate::box_trait::VoidBox::new())
|
||||
}
|
||||
};
|
||||
let out = map.get(key_box);
|
||||
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
|
||||
@ -360,25 +582,46 @@ pub extern "C" fn nyrt_host_call_slot(handle: u64, selector_id: u64,
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
}
|
||||
}
|
||||
204 => { // set(key, value)
|
||||
204 => {
|
||||
// set(key, value)
|
||||
if argv.len() >= 2 {
|
||||
let key_box: Box<dyn NyashBox> = match argv[0].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Box::new(crate::box_trait::IntegerBox::new(i)),
|
||||
crate::backend::vm::VMValue::Float(f) => Box::new(crate::boxes::math_box::FloatBox::new(f)),
|
||||
crate::backend::vm::VMValue::Bool(b) => Box::new(crate::box_trait::BoolBox::new(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Box::new(crate::box_trait::StringBox::new(s)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Box::new(crate::box_trait::IntegerBox::new(i))
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Box::new(crate::boxes::math_box::FloatBox::new(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => {
|
||||
Box::new(crate::box_trait::BoolBox::new(b))
|
||||
}
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Box::new(crate::box_trait::StringBox::new(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(b) => b.share_box(),
|
||||
crate::backend::vm::VMValue::Future(fu) => Box::new(fu),
|
||||
crate::backend::vm::VMValue::Void => Box::new(crate::box_trait::VoidBox::new()),
|
||||
crate::backend::vm::VMValue::Void => {
|
||||
Box::new(crate::box_trait::VoidBox::new())
|
||||
}
|
||||
};
|
||||
let val_box: Box<dyn NyashBox> = match argv[1].clone() {
|
||||
crate::backend::vm::VMValue::Integer(i) => Box::new(crate::box_trait::IntegerBox::new(i)),
|
||||
crate::backend::vm::VMValue::Float(f) => Box::new(crate::boxes::math_box::FloatBox::new(f)),
|
||||
crate::backend::vm::VMValue::Bool(b) => Box::new(crate::box_trait::BoolBox::new(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Box::new(crate::box_trait::StringBox::new(s)),
|
||||
crate::backend::vm::VMValue::Integer(i) => {
|
||||
Box::new(crate::box_trait::IntegerBox::new(i))
|
||||
}
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Box::new(crate::boxes::math_box::FloatBox::new(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => {
|
||||
Box::new(crate::box_trait::BoolBox::new(b))
|
||||
}
|
||||
crate::backend::vm::VMValue::String(s) => {
|
||||
Box::new(crate::box_trait::StringBox::new(s))
|
||||
}
|
||||
crate::backend::vm::VMValue::BoxRef(b) => b.share_box(),
|
||||
crate::backend::vm::VMValue::Future(fu) => Box::new(fu),
|
||||
crate::backend::vm::VMValue::Void => Box::new(crate::box_trait::VoidBox::new()),
|
||||
crate::backend::vm::VMValue::Void => {
|
||||
Box::new(crate::box_trait::VoidBox::new())
|
||||
}
|
||||
};
|
||||
let out = map.set(key_box, val_box);
|
||||
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
|
||||
@ -391,7 +634,10 @@ pub extern "C" fn nyrt_host_call_slot(handle: u64, selector_id: u64,
|
||||
}
|
||||
}
|
||||
300 => {
|
||||
if let Some(sb) = recv_arc.as_any().downcast_ref::<crate::box_trait::StringBox>() {
|
||||
if let Some(sb) = recv_arc
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::StringBox>()
|
||||
{
|
||||
let out = crate::backend::vm::VMValue::Integer(sb.value.len() as i64);
|
||||
let buf = tlv_encode_one(&out);
|
||||
return encode_out(out_ptr, out_len, &buf);
|
||||
|
||||
Reference in New Issue
Block a user