Skip to content

Commit 3b0f405

Browse files
committed
All tests passing in rust loader.
1 parent b1575a9 commit 3b0f405

File tree

12 files changed

+112
-81
lines changed

12 files changed

+112
-81
lines changed

source/loaders/rs_loader/rust/compiler/src/api/class.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub struct ClassInterface {
2929

3030
#[no_mangle]
3131
extern "C" fn class_singleton_create(_klass: OpaqueType, _class_impl: OpaqueType) -> c_int {
32-
println!("create class");
3332
0
3433
}
3534
#[no_mangle]
@@ -41,7 +40,6 @@ extern "C" fn class_singleton_constructor(
4140
class_args: OpaqueTypeList,
4241
size: usize,
4342
) -> OpaqueType {
44-
println!("invoke class constructor");
4543
unsafe {
4644
let class_impl_ptr = class_impl as *mut class::Class;
4745
let class = Box::from_raw(class_impl_ptr);
@@ -63,7 +61,7 @@ extern "C" fn class_singleton_static_set(
6361
_accessor: OpaqueType,
6462
_value: OpaqueType,
6563
) -> c_int {
66-
println!("class static set");
64+
eprintln!("Rust Loader: Class static set not implemented");
6765
0
6866
}
6967

@@ -73,7 +71,7 @@ extern "C" fn class_singleton_static_get(
7371
_class_impl: OpaqueType,
7472
_accessor: OpaqueType,
7573
) -> OpaqueType {
76-
println!("class static get");
74+
eprintln!("Rust Loader: Class static get not implemented");
7775
0 as OpaqueType
7876
}
7977

@@ -85,7 +83,6 @@ extern "C" fn class_singleton_static_invoke(
8583
args_p: OpaqueTypeList,
8684
size: usize,
8785
) -> OpaqueType {
88-
println!("class static invoke");
8986
let ret = unsafe {
9087
let class_impl_ptr = class_impl as *mut class::Class;
9188
let class = Box::from_raw(class_impl_ptr);
@@ -113,19 +110,20 @@ extern "C" fn class_singleton_static_await(
113110
_args_p: OpaqueTypeList,
114111
_size: usize,
115112
) -> OpaqueType {
116-
println!("class static await");
113+
eprintln!("Rust Loader: Class static await not implemented");
117114
0 as OpaqueType
118115
}
119116

120117
#[no_mangle]
121118
extern "C" fn class_singleton_destroy(_klass: OpaqueType, class_impl: OpaqueType) {
122-
if !class_impl.is_null() {
123-
unsafe {
124-
let class = Box::from_raw(class_impl as *mut class::Class);
125-
drop(class);
119+
if !rs_loader_destroyed() {
120+
if !class_impl.is_null() {
121+
unsafe {
122+
let class = Box::from_raw(class_impl as *mut class::Class);
123+
drop(class);
124+
}
126125
}
127126
}
128-
println!("class destroy");
129127
}
130128

131129
#[no_mangle]
@@ -164,10 +162,9 @@ pub fn register_class(class_registration: ClassRegistration) {
164162
class_info,
165163
} = class_registration.class_create;
166164
let name = CString::new(name).expect("Failed to convert function name to C string");
167-
// dbg!(&class_info);
168165
let class = unsafe { class_create(name.as_ptr(), 0, class_impl, singleton) };
169166

170-
// register ctor:
167+
// Register ctor
171168
if let Some(constructor) = class_info.constructor {
172169
let ctor = unsafe { constructor_create(constructor.args.len(), 0) };
173170
for (idx, arg) in constructor.args.iter().enumerate() {
@@ -188,9 +185,10 @@ pub fn register_class(class_registration: ClassRegistration) {
188185
unsafe { class_register_constructor(class, ctor) };
189186
} else {
190187
// TODO: add default constructor
191-
println!("should add default constructor");
188+
eprintln!("Rust Loader: Class default constructor not implemented");
192189
}
193-
// register attrs
190+
191+
// Register attrs
194192
for attr in class_info.attributes.iter() {
195193
let name =
196194
CString::new(attr.name.clone()).expect("Failed to convert function name to C string");
@@ -208,11 +206,13 @@ pub fn register_class(class_registration: ClassRegistration) {
208206
};
209207
unsafe { class_register_attribute(class, attribute) };
210208
}
211-
// we don't have static attributes in rust for now.
209+
210+
// TODO: We don't have static attributes in rust for now.
212211
// for attr in class_info.static_attributes.iter() {
213212
// let static_attribute = unsafe { attribute_create(class, name, ty, null, 0, null) };
214213
// unsafe { class_register_static_attribute(class, static_attribute) };
215214
// }
215+
216216
for method in class_info.methods.iter() {
217217
let name =
218218
CString::new(method.name.clone()).expect("Failed to convert function name to C string");

source/loaders/rs_loader/rust/compiler/src/api/function.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@ extern "C" fn function_singleton_await(
5050
_reject: extern "C" fn(OpaqueType, OpaqueType) -> OpaqueType,
5151
_data: OpaqueType,
5252
) -> OpaqueType {
53-
println!("rs_loader: await function");
53+
eprintln!("Rust Loader: Function await not implemented");
5454
0 as OpaqueType
5555
}
5656

57+
5758
#[no_mangle]
5859
extern "C" fn function_singleton_destroy(_func: OpaqueType, func_impl: OpaqueType) {
59-
if !func_impl.is_null() {
60-
unsafe {
61-
let func_ptr = Box::from_raw(func_impl as *mut class::Function);
62-
drop(func_ptr);
60+
if !rs_loader_destroyed() {
61+
if !func_impl.is_null() {
62+
unsafe {
63+
let func_ptr = Box::from_raw(func_impl as *mut class::Function);
64+
drop(func_ptr);
65+
}
6366
}
6467
}
6568
}

source/loaders/rs_loader/rust/compiler/src/api/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ extern "C" {
7373
fn loader_impl_type(loader_impl: OpaqueType, name: *const c_char) -> OpaqueType;
7474

7575
fn scope_define(scope: OpaqueType, key: *mut c_char, value: OpaqueType) -> c_int;
76-
// REFLECT_API klass class_create(const char *name, enum accessor_type_id accessor, class_impl impl, class_impl_interface_singleton singleton);
7776
fn class_create(
7877
name: *const c_char,
7978
accessor_type_id: c_int,
@@ -82,7 +81,6 @@ extern "C" {
8281
) -> OpaqueType;
8382
fn value_create_class(class: OpaqueType) -> OpaqueType;
8483
fn class_name(class: OpaqueType) -> *mut c_char;
85-
// constructor constructor_create(size_t count, enum class_visibility_id visibility);
8684
fn constructor_create(count: usize, visibility: c_int) -> OpaqueType;
8785
fn constructor_set(ctor: OpaqueType, index: usize, name: *const c_char, t: OpaqueType);
8886
fn class_register_constructor(class: OpaqueType, ctor: OpaqueType) -> c_int;
@@ -118,6 +116,8 @@ extern "C" {
118116
class: OpaqueType,
119117
) -> OpaqueType;
120118

119+
fn metacall_loader(tag: *const c_char) -> OpaqueType;
120+
fn loader_is_destroyed(loader_impl: OpaqueType) -> i32;
121121
}
122122

123123
pub fn get_loader_lifecycle_state(loader_impl: OpaqueType) -> *mut LoaderLifecycleState {
@@ -127,12 +127,22 @@ pub fn get_loader_lifecycle_state(loader_impl: OpaqueType) -> *mut LoaderLifecyc
127127
loader_lifecycle_state
128128
}
129129

130+
static mut RS_LOADER_PTR: *mut c_void = std::ptr::null_mut();
131+
130132
pub fn loader_lifecycle_register(loader_impl: OpaqueType) {
131-
unsafe { loader_initialization_register(loader_impl) };
133+
const TAG: *const c_char = "rs\0".as_ptr() as *const c_char;
134+
unsafe {
135+
loader_initialization_register(loader_impl);
136+
137+
// Get rust loader pointer
138+
if RS_LOADER_PTR.is_null() {
139+
RS_LOADER_PTR = metacall_loader(TAG);
140+
}
141+
}
132142
}
133143

134144
pub fn loader_lifecycle_unload_children(loader_impl: OpaqueType) {
135-
unsafe { loader_unload_children(loader_impl) };
145+
unsafe { loader_unload_children(loader_impl); }
136146
}
137147

138148
pub enum PrimitiveMetacallProtocolTypes {
@@ -170,3 +180,9 @@ pub fn define_type(
170180
loader_impl_type_define(loader_impl, type_name(t), t)
171181
};
172182
}
183+
184+
pub fn rs_loader_destroyed() -> bool {
185+
unsafe {
186+
loader_is_destroyed(RS_LOADER_PTR) == 0
187+
}
188+
}

source/loaders/rs_loader/rust/compiler/src/api/object.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub struct Object {
2323

2424
#[no_mangle]
2525
extern "C" fn object_singleton_create(_object: OpaqueType, _object_impl: OpaqueType) -> c_int {
26-
println!("object create");
2726
0
2827
}
2928

@@ -41,7 +40,6 @@ extern "C" fn object_singleton_set(
4140
let name = CStr::from_ptr(get_attr_name(accessor))
4241
.to_str()
4342
.expect("Unable to get attr name");
44-
println!("object set attr: {}", name);
4543
obj.instance.set_attr(name, value, &class);
4644

4745
std::mem::forget(class);
@@ -64,7 +62,6 @@ extern "C" fn object_singleton_get(
6462
let name = CStr::from_ptr(get_attr_name(accessor))
6563
.to_str()
6664
.expect("Unable to get attr name");
67-
println!("object get attr: {}", name);
6865
let ret = obj.instance.get_attr(name, &class);
6966

7067
std::mem::forget(class);
@@ -95,7 +92,6 @@ extern "C" fn object_singleton_method_invoke(
9592
let name = CStr::from_ptr(method_name(method))
9693
.to_str()
9794
.expect("Unable to get method name");
98-
println!("object invoke: {}", name);
9995
let ret = obj.instance.call(name, args, &class);
10096

10197
std::mem::forget(class);
@@ -118,23 +114,25 @@ extern "C" fn object_singleton_method_await(
118114
_args_p: OpaqueTypeList,
119115
_size: usize,
120116
) -> OpaqueType {
121-
println!("object await");
117+
eprintln!("Rust Loader: Object await not implemented");
122118
0 as OpaqueType
123119
}
124120
#[no_mangle]
125-
extern "C" fn object_singleton_destructor(_object: OpaqueType, object_impl: OpaqueType) -> c_int {
126-
if !object_impl.is_null() {
127-
unsafe {
128-
let object = Box::from_raw(object_impl as *mut Object);
129-
drop(object);
130-
}
131-
}
132-
println!("destruct object");
121+
extern "C" fn object_singleton_destructor(_object: OpaqueType, _object_impl: OpaqueType) -> c_int {
122+
eprintln!("Rust Loader: Object destructor not implemented");
133123
0
134124
}
125+
135126
#[no_mangle]
136-
extern "C" fn object_singleton_destroy(_object: OpaqueType, _object_impl: OpaqueType) {
137-
println!("destroy object");
127+
extern "C" fn object_singleton_destroy(_object: OpaqueType, object_impl: OpaqueType) {
128+
if !rs_loader_destroyed() {
129+
if !object_impl.is_null() {
130+
unsafe {
131+
let object = Box::from_raw(object_impl as *mut Object);
132+
drop(object);
133+
}
134+
}
135+
}
138136
}
139137

140138
#[no_mangle]

source/loaders/rs_loader/rust/compiler/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl Drop for DynlinkLibrary {
286286
fn drop(&mut self) {
287287
unsafe {
288288
if !self.instance.is_null() {
289-
// Call the C function to unload the dynamic library
289+
// Unload the dynamic library
290290
dynlink_unload(self.instance);
291291

292292
// Set the instance to null for safety

source/loaders/rs_loader/rust/compiler/src/middle.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn handle_ty(ty: &TyS) -> FunctionParameter {
4848
if let GenericArgKind::Type(ty) = gen_arg.unpack() {
4949
result.generic.push(handle_ty(ty));
5050
} else {
51-
println!("expect generic arg, get nothing");
51+
eprintln!("Rust Loader: Expect generic arg, get nothing");
5252
}
5353
}
5454
"std::collections::HashMap" => {
@@ -57,13 +57,13 @@ pub fn handle_ty(ty: &TyS) -> FunctionParameter {
5757
if let GenericArgKind::Type(ty) = key.unpack() {
5858
result.generic.push(handle_ty(ty));
5959
} else {
60-
println!("expect key, get nothing");
60+
eprintln!("Rust Loader: Expect key, get nothing");
6161
}
6262
let value = gen[1];
6363
if let GenericArgKind::Type(ty) = value.unpack() {
6464
result.generic.push(handle_ty(ty));
6565
} else {
66-
println!("expect value, get nothing");
66+
eprintln!("Rust Loader: Expect value, get nothing");
6767
}
6868
}
6969
"std::string::String" => result.ty = FunctionType::String,

source/loaders/rs_loader/rust/compiler/src/wrapper/class.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ impl ToMetaResult for char {
509509

510510
impl ToMetaResult for usize {
511511
fn to_meta_result(self) -> Result<MetacallValue> {
512-
println!("get usize: {self}");
513512
// FIXME: convert usize to i32?
514513
Ok(unsafe { metacall_value_create_int(self as i32) })
515514
}
@@ -697,7 +696,7 @@ macro_rules! convert_to {
697696
Ok(metacall_value_to_double($val) as $t)
698697
}
699698
Err(_) => {
700-
println!("receive id: {}, should be [2-6]", id);
699+
eprintln!("Rust Loader: Return type with id #{} is not implemented, ", id);
701700
panic!("received mismatch type");
702701
}
703702
}
@@ -809,7 +808,6 @@ where
809808
// .iter()
810809
// .map(|p| metacall_value_to_int(*p))
811810
// .collect::<Vec<i32>>();
812-
// println!("{:?}", vec);
813811
// vec
814812
// })
815813
// }

0 commit comments

Comments
 (0)