Skip to content

Commit fc2e06c

Browse files
authored
Merge pull request #180 from toasteater/fix/null-new-pointer
Fix null function pointer due to renaming of "new"
2 parents d59a5dc + ab1eb86 commit fc2e06c

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

bindings_generator/src/api.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,27 @@ pub struct GodotMethod {
105105
pub arguments: Vec<GodotArgument>,
106106
}
107107

108+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
109+
pub struct MethodName<'a> {
110+
pub rust_name: &'a str,
111+
pub original_name: &'a str,
112+
}
113+
108114
impl GodotMethod {
109-
pub fn get_name(&self) -> &str {
115+
pub fn get_name(&self) -> MethodName {
110116
// GDScript and NativeScript have ::new methods but we want to reserve
111117
// the name for the constructors.
112118
if &self.name == "new" {
113-
return "_new";
119+
return MethodName {
120+
rust_name: "_new",
121+
original_name: "new",
122+
};
114123
}
115124

116-
&self.name
125+
MethodName {
126+
rust_name: &self.name,
127+
original_name: &self.name,
128+
}
117129
}
118130

119131
pub fn get_return_type(&self) -> Ty {

bindings_generator/src/methods.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct {name}MethodTable {{
2727
)?;
2828

2929
for method in &class.methods {
30-
let method_name = method.get_name();
30+
let MethodName { rust_name: method_name, .. } = method.get_name();
3131
if method_name == "free" {
3232
continue;
3333
}
@@ -49,14 +49,14 @@ impl {name}MethodTable {{
4949
name = class.name
5050
)?;
5151
for method in &class.methods {
52-
let method_name = method.get_name();
52+
let MethodName { rust_name: method_name, .. } = method.get_name();
5353
if method_name == "free" {
5454
continue;
5555
}
5656
writeln!(
5757
output,
5858
" {}: 0 as *mut sys::godot_method_bind,",
59-
method.get_name()
59+
method_name,
6060
)?;
6161
}
6262
let lookup_name: String = if has_underscore {
@@ -97,14 +97,15 @@ impl {name}MethodTable {{
9797
lookup_name = lookup_name,
9898
)?;
9999
for method in &class.methods {
100-
let method_name = method.get_name();
100+
let MethodName { rust_name: method_name, original_name } = method.get_name();
101101
if method_name == "free" {
102102
continue;
103103
}
104104

105105
writeln!(output,
106-
r#" table.{method_name} = (gd_api.godot_method_bind_get_method)(class_name, "{method_name}\0".as_ptr() as *const c_char );"#,
107-
method_name = method_name
106+
r#" table.{method_name} = (gd_api.godot_method_bind_get_method)(class_name, "{original_name}\0".as_ptr() as *const c_char );"#,
107+
method_name = method_name,
108+
original_name = original_name,
108109
)?;
109110
}
110111

@@ -124,7 +125,7 @@ pub fn generate_method_impl(
124125
class: &GodotClass,
125126
method: &GodotMethod,
126127
) -> GeneratorResult {
127-
let method_name = method.get_name();
128+
let MethodName { rust_name: method_name, .. } = method.get_name();
128129

129130
if skip_method(&method_name) {
130131
return Ok(());
@@ -259,7 +260,7 @@ pub fn generate_methods(
259260
) -> GeneratorResult {
260261
if let Some(class) = api.find_class(class_name) {
261262
'method: for method in &class.methods {
262-
let method_name = method.get_name();
263+
let MethodName { rust_name: method_name, .. } = method.get_name();
263264

264265
if skip_method(&method_name) {
265266
continue;

test/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub extern "C" fn run_tests(
2222
status &= gdnative::test_vector3_variants();
2323

2424
status &= test_constructor();
25+
status &= test_underscore_method_binding();
2526

2627
gdnative::Variant::from_bool(status).forget()
2728
}
@@ -44,6 +45,21 @@ fn test_constructor() -> bool {
4445
return true;
4546
}
4647

48+
fn test_underscore_method_binding() -> bool {
49+
println!(" -- test_underscore_method_binding");
50+
51+
let ok = std::panic::catch_unwind(|| {
52+
let table = gdnative::NativeScriptMethodTable::get(get_api());
53+
assert_ne!(0, table._new as usize);
54+
}).is_ok();
55+
56+
if !ok {
57+
godot_error!(" !! Test test_underscore_method_binding failed");
58+
}
59+
60+
ok
61+
}
62+
4763
godot_gdnative_init!();
4864
godot_nativescript_init!();
4965
godot_gdnative_terminate!();

0 commit comments

Comments
 (0)