Skip to content

Commit ebdab39

Browse files
TitanNanoBromeonlilizoey
committed
Implementation of godots GDExtensionScriptInstance
Co-authored-by: Jan Haller <[email protected]> Co-authored-by: Lili Zoey <[email protected]>
1 parent 05a1b09 commit ebdab39

File tree

9 files changed

+1216
-2
lines changed

9 files changed

+1216
-2
lines changed

godot-codegen/src/codegen_special_cases.rs

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ const SELECTED_CLASSES: &[&str] = &[
157157
"ResourceLoader",
158158
"RigidBody2D",
159159
"SceneTree",
160+
"Script",
161+
"ScriptExtension",
162+
"ScriptLanguage",
160163
"Sprite2D",
161164
"SpriteFrames",
162165
"TextServer",

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

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

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)