Skip to content

Commit c5aabcc

Browse files
committed
Implementation of godots GDExtensionScriptInstance
1 parent 13ab375 commit c5aabcc

File tree

5 files changed

+703
-1
lines changed

5 files changed

+703
-1
lines changed

godot-codegen/src/codegen_special_cases.rs

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ const SELECTED_CLASSES: &[&str] = &[
157157
"ResourceLoader",
158158
"RigidBody2D",
159159
"SceneTree",
160+
"Script",
161+
"ScriptLanguage",
160162
"Sprite2D",
161163
"SpriteFrames",
162164
"TextServer",

godot-core/src/builtin/meta/mod.rs

+61
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,64 @@ impl PropertyInfo {
270270
}
271271
}
272272
}
273+
274+
#[derive(Debug)]
275+
pub struct MethodInfo {
276+
pub id: i32,
277+
pub method_name: StringName,
278+
pub class_name: ClassName,
279+
pub return_type: PropertyInfo,
280+
pub arguments: Vec<PropertyInfo>,
281+
pub default_arguments: Vec<Variant>,
282+
pub flags: global::MethodFlags,
283+
}
284+
285+
impl MethodInfo {
286+
/// Converts to the FFI type. Keep this object allocated while using that!
287+
///
288+
/// # Safety
289+
/// This function leaks memory that has to be cleaned up by the caller
290+
/// once it is no longer used. Specifically the `arguments` and
291+
/// `default_arguments` vectors have to be reconstruced from the pointer
292+
/// and length and then dropped / freed.
293+
///
294+
/// Each vector can be reconstructed with `Vec::from_raw_parts` since the
295+
/// pointers where created with `Vec::into_boxed_slice` which gurantees that
296+
/// vector capacity and length are equal.
297+
pub fn method_sys(&self) -> sys::GDExtensionMethodInfo {
298+
use crate::obj::EngineEnum as _;
299+
300+
let argument_count = self.arguments.len() as u32;
301+
let argument_vec = self
302+
.arguments
303+
.iter()
304+
.map(|arg| arg.property_sys())
305+
.collect::<Vec<_>>()
306+
.into_boxed_slice();
307+
308+
// SAFETY: dereferencing the new box pointer is fine as it is guaranteed to not be null
309+
let arguments = unsafe { (*Box::into_raw(argument_vec)).as_mut_ptr() };
310+
311+
let default_argument_count = self.default_arguments.len() as u32;
312+
let default_argument_vec = self
313+
.default_arguments
314+
.iter()
315+
.map(|arg| arg.var_sys())
316+
.collect::<Vec<_>>()
317+
.into_boxed_slice();
318+
319+
// SAFETY: dereferencing the new box pointer is fine as it is guaranteed to not be null
320+
let default_arguments = unsafe { (*Box::into_raw(default_argument_vec)).as_mut_ptr() };
321+
322+
sys::GDExtensionMethodInfo {
323+
id: self.id,
324+
name: self.method_name.string_sys(),
325+
return_value: self.return_type.property_sys(),
326+
argument_count,
327+
arguments,
328+
default_argument_count,
329+
default_arguments,
330+
flags: u32::try_from(self.flags.ord()).expect("flags should be valid"),
331+
}
332+
}
333+
}

godot-core/src/builtin/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub use real_inner::*;
5050
pub use rect2::*;
5151
pub use rect2i::*;
5252
pub use rid::*;
53+
pub use script::*;
5354
pub use string::*;
5455
pub use transform2d::*;
5556
pub use transform3d::*;
@@ -91,6 +92,7 @@ mod quaternion;
9192
mod rect2;
9293
mod rect2i;
9394
mod rid;
95+
mod script;
9496
mod string;
9597
mod transform2d;
9698
mod transform3d;

0 commit comments

Comments
 (0)