diff --git a/kernel/src/handle.rs b/kernel/src/handle.rs index 6d1b7485..b141726c 100644 --- a/kernel/src/handle.rs +++ b/kernel/src/handle.rs @@ -1,6 +1,10 @@ -use crate::arch::hardware_random_u64; -use alloc::vec::Vec; -use core::fmt::{self, Formatter}; +use crate::interp::objects::{Object, OBJECTS}; + +use { + crate::arch::hardware_random_u64, + alloc::vec::Vec, + core::fmt::{self, Formatter}, +}; #[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub struct OSHandle { pub id: u64, @@ -21,19 +25,30 @@ impl OSHandle { pub struct Handle { id: OSHandle, perms: Permissions, + r#ref: usize, } impl Handle { - pub fn new() -> Handle { + pub fn new(r#ref: usize) -> Handle { Handle { id: OSHandle::random_new(), perms: Permissions::new(), + r#ref, } } pub fn as_u64(&self) -> u64 { self.id.id } + + pub fn get FnOnce(Option<&'a mut Object>) -> R>(&self,f: F) -> R{ + let l = OBJECTS; + let mut olock = l.lock(); + let a = olock.get_mut(self.r#ref).and_then(|a|{ + a.as_mut() + }); + return f(a); + } } impl fmt::Display for Handle { diff --git a/kernel/src/interp/host_functions.rs b/kernel/src/interp/host_functions.rs index 401e7b40..f55ea7ed 100644 --- a/kernel/src/interp/host_functions.rs +++ b/kernel/src/interp/host_functions.rs @@ -34,7 +34,7 @@ pub fn host_register_idt_handler( 0 } -use crate::interp::Handle; +use crate::interp::{Handle, objects::{Object, ObjectHeader, ObjectSpecial}}; pub fn host_make_object( mut caller: Caller<'_, HostState>, @@ -54,14 +54,17 @@ pub fn host_make_object( name.push(ch); } trace!("Object Name {}", name); - let hand = Handle::new(); - { - let binding = OBJECTS; - let mut olock = binding.lock(); - let obj = xml::XMLElement::new(name); + let binding = OBJECTS; + let mut olock = binding.lock(); + let hand = Handle::new(olock.len()); + let obj = xml::XMLElement::new(name); - olock.push(Some(obj)) - } + olock.push(Some(Object{ + header: ObjectHeader{ + + }, + special: ObjectSpecial::Xml(obj) + })); caller.data_mut().handles.push(hand); // hand.into() hand.as_u64().try_into().unwrap() diff --git a/kernel/src/interp/mod.rs b/kernel/src/interp/mod.rs index 550cd0be..0193975d 100644 --- a/kernel/src/interp/mod.rs +++ b/kernel/src/interp/mod.rs @@ -1,5 +1,5 @@ mod host_functions; -mod objects; +pub mod objects; use { crate::{ handle::{self, Handle}, diff --git a/kernel/src/interp/objects.rs b/kernel/src/interp/objects.rs index acaf3a2d..3d9c24ec 100644 --- a/kernel/src/interp/objects.rs +++ b/kernel/src/interp/objects.rs @@ -1,9 +1,19 @@ -use alloc::vec; -use alloc::vec::Vec; +use alloc::{vec, vec::Vec}; use spin::{Lazy, Mutex}; -pub type HostObjects = Vec>; +pub struct Object { + pub header: ObjectHeader, + pub special: ObjectSpecial, +} + +pub struct ObjectHeader {} + +pub enum ObjectSpecial { + Xml(xml::XMLElement), +} + +pub type HostObjects = Vec>; pub const OBJECTS: Lazy> = Lazy::new(|| { let mut obj = vec![]; diff --git a/user/ablewasi/src/api.rs b/user/ablewasi/src/api.rs index c44a8db5..2dc7507d 100644 --- a/user/ablewasi/src/api.rs +++ b/user/ablewasi/src/api.rs @@ -13,7 +13,7 @@ impl Drop for Object{ } impl Object{ - fn to_global(self) -> [u64; 4]{ + pub fn to_global(self) -> [u64; 4]{ let b = unsafe { sys::make_global(self.id).into() }; @@ -21,9 +21,16 @@ impl Object{ return b; } - fn from_global(a: [u64; 4]) -> Object{ + pub fn from_global(a: [u64; 4]) -> Object{ return Object{id: unsafe { sys::take_global(a.into()) }}; } + + pub fn new(name: &str) -> Object{ + return Object{id: unsafe { + let a = name.as_ptr(); + sys::create_object(a, name.len() as i32) + }}; + } } \ No newline at end of file