Skip to content

Commit 56e7249

Browse files
committed
misc: format
1 parent 99c9a48 commit 56e7249

File tree

8 files changed

+51
-118
lines changed

8 files changed

+51
-118
lines changed

cpp/include/mun/runtime_capi.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ typedef struct MunStructInfo {
7373
MunStructMemoryKind memory_kind;
7474
} MunStructInfo;
7575

76+
/**
77+
* Represents an array declaration.
78+
*/
79+
typedef struct MunArrayInfo {
80+
const struct MunTypeInfo *element_type;
81+
} MunArrayInfo;
82+
7683
/**
7784
* Contains data specific to a group of types that illicit the same characteristics.
7885
*/
@@ -89,6 +96,10 @@ enum MunTypeInfoData_Tag
8996
* Struct types (i.e. record, tuple, or unit structs)
9097
*/
9198
Struct,
99+
/**
100+
* Array types
101+
*/
102+
Array,
92103
};
93104
#ifndef __cplusplus
94105
typedef uint8_t MunTypeInfoData_Tag;
@@ -100,6 +111,10 @@ typedef union MunTypeInfoData {
100111
MunTypeInfoData_Tag struct_tag;
101112
struct MunStructInfo struct_;
102113
};
114+
struct {
115+
MunTypeInfoData_Tag array_tag;
116+
struct MunArrayInfo array;
117+
};
103118
} MunTypeInfoData;
104119

105120
/**

crates/mun_codegen/src/ir/body.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,11 @@ impl<'db, 'ink, 't> BodyIrGenerator<'db, 'ink, 't> {
14401440
.builder
14411441
.build_call(
14421442
new_array_fn_ptr,
1443-
&[type_info_ptr.into(), length_value.into(), allocator_handle.into()],
1443+
&[
1444+
type_info_ptr.into(),
1445+
length_value.into(),
1446+
allocator_handle.into(),
1447+
],
14441448
"new_array",
14451449
)
14461450
.try_as_basic_value()

crates/mun_hir/src/ty.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ impl Ty {
255255
pub fn equals_ctor(&self, other: &Ty) -> bool {
256256
match (self.interned(), other.interned()) {
257257
(TyKind::Struct(s1), TyKind::Struct(s2)) => s1 == s2,
258-
(TyKind::Tuple(_, substs1), TyKind::Tuple(_, substs2)) => {
259-
substs1 == substs2
260-
}
258+
(TyKind::Tuple(_, substs1), TyKind::Tuple(_, substs2)) => substs1 == substs2,
261259
(TyKind::Array(_), TyKind::Array(_)) => true,
262260
(TyKind::Float(f1), TyKind::Float(f2)) => f1 == f2,
263261
(TyKind::Int(i1), TyKind::Int(i2)) => i1 == i2,

crates/mun_runtime/src/adt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::garbage_collector::{GcPtr, GcRootPtr, UnsafeTypeInfo};
22
use crate::{
33
marshal::Marshal,
4-
reflection::{
5-
equals_argument_type, ArgumentReflection, ReturnTypeReflection,
6-
},
4+
reflection::{equals_argument_type, ArgumentReflection, ReturnTypeReflection},
75
GarbageCollector, Runtime,
86
};
97
use abi::TypeInfo;

crates/mun_runtime/src/array.rs

Lines changed: 13 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
use crate::garbage_collector::{GcPtr, GcRootPtr};
2-
use crate::{ArgumentReflection, Marshal, ReturnTypeReflection, Runtime, UnsafeTypeInfo};
2+
use crate::{ArgumentReflection, GarbageCollector, Marshal, ReturnTypeReflection, Runtime};
33
use abi::TypeInfo;
44
use memory::gc::{GcRuntime, HasIndirectionPtr};
55
use memory::{ArrayMemoryLayout, ArrayType, CompositeType};
66
use once_cell::sync::OnceCell;
7-
use std::cell::{Ref, RefCell};
8-
use std::marker::{PhantomData, PhantomPinned};
9-
use std::mem::MaybeUninit;
10-
use std::pin::Pin;
7+
use std::marker::PhantomData;
118
use std::ptr::NonNull;
12-
use std::rc::Rc;
139
use std::sync::Arc;
1410

1511
/// Represents a Mun array pointer.
@@ -56,8 +52,8 @@ impl<'array, T: Marshal<'array> + 'array> ArrayRef<'array, T> {
5652
}
5753

5854
/// Roots the `ArrayRef`.
59-
pub fn root(self, runtime: Rc<RefCell<Runtime>>) -> RootedArray<T> {
60-
RootedArray::new(&self.runtime.gc, runtime, self.raw)
55+
pub fn root(self) -> RootedArray<T> {
56+
RootedArray::new(&self.runtime.gc, self.raw)
6157
}
6258

6359
/// Returns the number of elements stored in the array
@@ -181,100 +177,28 @@ impl<'a, T: Marshal<'a> + 'a> Marshal<'a> for ArrayRef<'a, T> {
181177
#[derive(Clone)]
182178
pub struct RootedArray<T> {
183179
handle: GcRootPtr,
184-
runtime: Rc<RefCell<Runtime>>,
185180
_data: PhantomData<T>,
186181
}
187182

188183
impl<T> RootedArray<T> {
189184
/// Creates a `RootedArray` that wraps a raw Mun struct.
190-
fn new<G: GcRuntime<UnsafeTypeInfo>>(
191-
gc: &Arc<G>,
192-
runtime: Rc<RefCell<Runtime>>,
193-
raw: RawArray,
194-
) -> Self {
195-
let handle = {
196-
let runtime_ref = runtime.borrow();
197-
// Safety: The type returned from `ptr_type` is guaranteed to live at least as long as
198-
// `Runtime` does not change. As we hold a shared reference to `Runtime`, this is safe.
199-
assert!(unsafe { gc.ptr_type(raw.0).into_inner().as_ref().data.is_array() });
200-
201-
GcRootPtr::new(&runtime_ref.gc, raw.0)
202-
};
203-
185+
fn new(gc: &Arc<GarbageCollector>, raw: RawArray) -> Self {
186+
// Safety: The type returned from `ptr_type` is guaranteed to live at least as long as
187+
// `Runtime` does not change. As we hold a shared reference to `Runtime`, this is safe.
188+
assert!(unsafe { gc.ptr_type(raw.0).into_inner().as_ref().data.is_array() });
204189
Self {
205-
handle,
206-
runtime,
190+
handle: GcRootPtr::new(gc, raw.0),
207191
_data: Default::default(),
208192
}
209193
}
210194

211-
/// Converts the `RootedArray` into a `ArrayRef`, using an external shared reference to a
195+
/// Converts the `RootedArray` into an `ArrayRef<T>`, using an external shared reference to a
212196
/// `Runtime`.
213-
///
214-
/// # Safety
215-
///
216-
/// The `RootedArray` should have been allocated by the `Runtime`.
217-
pub unsafe fn as_ref<'r>(&self, runtime: &'r Runtime) -> ArrayRef<'r, T>
197+
pub fn as_ref<'r>(&self, runtime: &'r Runtime) -> ArrayRef<'r, T>
218198
where
219-
T: Marshal<'r>,
220-
T: 'r,
199+
T: Marshal<'r> + 'r,
221200
{
201+
assert_eq!(Arc::as_ptr(&runtime.gc), self.handle.runtime().as_ptr());
222202
ArrayRef::new(RawArray(self.handle.handle()), runtime)
223203
}
224-
225-
/// Converts the `RootedArray` to a pinned `RootedArrayRef` that can be used just like a
226-
/// `ArrayRef`.
227-
pub fn by_ref<'r>(&'r self) -> Pin<Box<RootedArrayRef<'r, T>>>
228-
where
229-
T: Marshal<'r>,
230-
T: 'r,
231-
{
232-
RootedArrayRef::new(RawArray(self.handle.handle()), self.borrow_runtime())
233-
}
234-
235-
/// Borrows the struct's runtime.
236-
pub fn borrow_runtime(&self) -> Ref<Runtime> {
237-
self.runtime.borrow()
238-
}
239-
}
240-
241-
/// Type-agnostic wrapper for safely obtaining a `ArrayRef` from a `RootedArray`.
242-
pub struct RootedArrayRef<'s, T> {
243-
runtime: Ref<'s, Runtime>,
244-
array_ref: MaybeUninit<ArrayRef<'s, T>>,
245-
_pin: PhantomPinned,
246-
}
247-
248-
impl<'s, T: Marshal<'s> + 's> RootedArrayRef<'s, T> {
249-
fn new(raw: RawArray, runtime: Ref<'s, Runtime>) -> Pin<Box<Self>> {
250-
let array_ref = RootedArrayRef {
251-
runtime,
252-
array_ref: MaybeUninit::uninit(),
253-
_pin: PhantomPinned,
254-
};
255-
let mut boxed = Box::pin(array_ref);
256-
257-
let runtime = NonNull::from(&boxed.runtime);
258-
259-
// Safety: Modifying a field doesn't move the whole struct
260-
unsafe {
261-
let array_ref = ArrayRef::new(raw, &*runtime.as_ptr());
262-
let mut_ref: Pin<&mut Self> = Pin::as_mut(&mut boxed);
263-
Pin::get_unchecked_mut(mut_ref)
264-
.array_ref
265-
.as_mut_ptr()
266-
.write(array_ref);
267-
}
268-
269-
boxed
270-
}
271-
}
272-
273-
impl<'s, T> std::ops::Deref for RootedArrayRef<'s, T> {
274-
type Target = ArrayRef<'s, T>;
275-
276-
fn deref(&self) -> &Self::Target {
277-
// Safety: We always guarantee to set the `array_ref` upon construction.
278-
unsafe { &*self.array_ref.as_ptr() }
279-
}
280204
}

crates/mun_runtime/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,8 @@ impl Runtime {
655655
// Validate the return type
656656
match if let Some(return_type) = function_info.prototype.signature.return_type() {
657657
if !ReturnType::equals_type(return_type) {
658-
Err((
659-
return_type.name(),
660-
ReturnType::type_name(),
661-
))
662-
}
663-
else {
658+
Err((return_type.name(), ReturnType::type_name()))
659+
} else {
664660
Ok(())
665661
}
666662
} else if <() as ReturnTypeReflection>::type_guid() != ReturnType::type_guid() {

crates/mun_runtime/tests/arrays.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ fn arrays_as_argument() {
5757
)
5858
.expect("Failed to build test driver");
5959

60-
let runtime = driver.runtime();
61-
let runtime_ref = runtime.borrow();
62-
let result: ArrayRef<'_, i32> = invoke_fn!(runtime_ref, "generate").unwrap();
60+
let result: ArrayRef<'_, i32> = driver.runtime.invoke("generate", ()).unwrap();
6361

6462
assert_eq!(result.len(), 5);
6563
assert!(result.capacity() >= 5);
6664
assert_eq!(result.iter().collect::<Vec<_>>(), vec![5, 4, 3, 2, 1]);
6765

68-
let result_array: ArrayRef<'_, i32> =
69-
invoke_fn!(runtime_ref, "add_one", result.clone(), result.len()).unwrap();
66+
let result_array: ArrayRef<'_, i32> = driver
67+
.runtime
68+
.invoke("add_one", (result.clone(), result.len()))
69+
.unwrap();
7070

7171
assert_eq!(result_array.len(), 5);
7272
assert!(result_array.capacity() >= 5);
@@ -84,16 +84,12 @@ fn root_array() {
8484
.expect("Failed to build test driver");
8585

8686
let result = {
87-
let runtime = driver.runtime();
88-
let runtime_ref = runtime.borrow();
89-
let array: ArrayRef<i32> = invoke_fn!(runtime_ref, "main").unwrap();
90-
array.root(driver.runtime())
87+
let array: ArrayRef<i32> = driver.runtime.invoke("main", ()).unwrap();
88+
array.root()
9189
};
9290

93-
assert_eq!(result.by_ref().len(), 5);
94-
assert!(result.by_ref().capacity() >= 5);
95-
assert_eq!(
96-
result.by_ref().iter().collect::<Vec<_>>(),
97-
vec![5, 4, 3, 2, 1]
98-
);
91+
let result = result.as_ref(&driver.runtime);
92+
assert_eq!(result.len(), 5);
93+
assert!(result.capacity() >= 5);
94+
assert_eq!(result.iter().collect::<Vec<_>>(), vec![5, 4, 3, 2, 1]);
9995
}

crates/mun_runtime/tests/runtime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ fn arrays_are_collected() {
3030
.expect("Failed to build test driver");
3131

3232
assert_eq!(driver.runtime.gc_collect(), false);
33-
let _: () = driver.runtime.invoke("main", ())
33+
let _: () = driver
34+
.runtime
35+
.invoke("main", ())
3436
.expect("error invoking main function");
3537
assert_eq!(driver.runtime.gc_collect(), true);
3638
assert_eq!(driver.runtime.gc_collect(), false);

0 commit comments

Comments
 (0)