Skip to content

Commit 30d9652

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

File tree

9 files changed

+1143
-2
lines changed

9 files changed

+1143
-2
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

+60
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,63 @@ 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+
/// The struct returned by this function contains pointers into the fields of `self`. `self` should therefore not be dropped while the
289+
/// [`sys::GDExtensionMethodInfo`] is still in use.
290+
///
291+
/// This function also leaks memory that has to be cleaned up by the caller once it is no longer used. Specifically the `arguments` and
292+
/// `default_arguments` vectors have to be reconstruced from the pointer and length and then dropped / freed.
293+
///
294+
/// Each vector can be reconstructed with `Vec::from_raw_parts` since the pointers where created with `Vec::into_boxed_slice` which
295+
/// gurantees that vector capacity and length are equal.
296+
pub fn method_sys(&self) -> sys::GDExtensionMethodInfo {
297+
use crate::obj::EngineEnum as _;
298+
299+
let argument_count = self.arguments.len() as u32;
300+
let argument_vec = self
301+
.arguments
302+
.iter()
303+
.map(|arg| arg.property_sys())
304+
.collect::<Vec<_>>()
305+
.into_boxed_slice();
306+
307+
// SAFETY: dereferencing the new box pointer is fine as it is guaranteed to not be null
308+
let arguments = unsafe { (*Box::into_raw(argument_vec)).as_mut_ptr() };
309+
310+
let default_argument_count = self.default_arguments.len() as u32;
311+
let default_argument_vec = self
312+
.default_arguments
313+
.iter()
314+
.map(|arg| arg.var_sys())
315+
.collect::<Vec<_>>()
316+
.into_boxed_slice();
317+
318+
// SAFETY: dereferencing the new box pointer is fine as it is guaranteed to not be null
319+
let default_arguments = unsafe { (*Box::into_raw(default_argument_vec)).as_mut_ptr() };
320+
321+
sys::GDExtensionMethodInfo {
322+
id: self.id,
323+
name: self.method_name.string_sys(),
324+
return_value: self.return_type.property_sys(),
325+
argument_count,
326+
arguments,
327+
default_argument_count,
328+
default_arguments,
329+
flags: u32::try_from(self.flags.ord()).expect("flags should be valid"),
330+
}
331+
}
332+
}

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)