6
6
7
7
//! Generates a file for each Godot class
8
8
9
- use proc_macro2:: TokenStream ;
9
+ use proc_macro2:: { Literal , TokenStream } ;
10
10
use quote:: { format_ident, quote} ;
11
11
use std:: path:: { Path , PathBuf } ;
12
12
13
13
use crate :: api_parser:: * ;
14
- use crate :: util:: { c_str , ident, safe_ident, strlit, to_module_name, to_rust_type} ;
14
+ use crate :: util:: { ident, safe_ident, strlit, to_module_name, to_rust_type} ;
15
15
use crate :: { special_cases, util, Context , GeneratedClass , GeneratedModule , RustTy } ;
16
16
17
17
pub ( crate ) fn generate_class_files (
@@ -59,7 +59,7 @@ pub(crate) fn generate_class_files(
59
59
out_files. push ( out_path) ;
60
60
}
61
61
62
- fn make_constructor ( class : & Class , ctx : & Context , class_name_cstr : TokenStream ) -> TokenStream {
62
+ fn make_constructor ( class : & Class , ctx : & Context , class_name_str : & Literal ) -> TokenStream {
63
63
if ctx. is_singleton ( & class. name ) {
64
64
// Note: we cannot return &'static mut Self, as this would be very easy to mutably alias.
65
65
// &'static Self would be possible, but we would lose the whole mutability information (even if that
@@ -69,7 +69,8 @@ fn make_constructor(class: &Class, ctx: &Context, class_name_cstr: TokenStream)
69
69
quote ! {
70
70
pub fn singleton( ) -> Gd <Self > {
71
71
unsafe {
72
- let object_ptr = sys:: interface_fn!( global_get_singleton) ( #class_name_cstr) ;
72
+ let class_name = StringName :: from( #class_name_str) ;
73
+ let object_ptr = sys:: interface_fn!( global_get_singleton) ( class_name. string_sys( ) ) ;
73
74
Gd :: from_obj_sys( object_ptr)
74
75
}
75
76
}
@@ -82,7 +83,8 @@ fn make_constructor(class: &Class, ctx: &Context, class_name_cstr: TokenStream)
82
83
quote ! {
83
84
pub fn new( ) -> Gd <Self > {
84
85
unsafe {
85
- let object_ptr = sys:: interface_fn!( classdb_construct_object) ( #class_name_cstr) ;
86
+ let class_name = StringName :: from( #class_name_str) ;
87
+ let object_ptr = sys:: interface_fn!( classdb_construct_object) ( class_name. string_sys( ) ) ;
86
88
//let instance = Self { object_ptr };
87
89
Gd :: from_obj_sys( object_ptr)
88
90
}
@@ -94,7 +96,8 @@ fn make_constructor(class: &Class, ctx: &Context, class_name_cstr: TokenStream)
94
96
#[ must_use]
95
97
pub fn new_alloc( ) -> Gd <Self > {
96
98
unsafe {
97
- let object_ptr = sys:: interface_fn!( classdb_construct_object) ( #class_name_cstr) ;
99
+ let class_name = StringName :: from( #class_name_str) ;
100
+ let object_ptr = sys:: interface_fn!( classdb_construct_object) ( class_name. string_sys( ) ) ;
98
101
Gd :: from_obj_sys( object_ptr)
99
102
}
100
103
}
@@ -114,9 +117,8 @@ fn make_class(class: &Class, ctx: &mut Context) -> GeneratedClass {
114
117
115
118
let name = ident ( & class. name ) ;
116
119
let name_str = strlit ( & class. name ) ;
117
- let name_cstr = c_str ( & class. name ) ;
118
120
119
- let constructor = make_constructor ( class, ctx, name_cstr ) ;
121
+ let constructor = make_constructor ( class, ctx, & name_str ) ;
120
122
121
123
let methods = make_methods ( & class. methods , & class. name , ctx) ;
122
124
let enums = make_enums ( & class. enums , & class. name , ctx) ;
@@ -356,7 +358,7 @@ fn make_method_definition(method: &Method, class_name: &str, ctx: &mut Context)
356
358
let is_varcall = method. is_vararg ;
357
359
let ( params, arg_exprs) = make_params ( & method. arguments , is_varcall, ctx) ;
358
360
359
- let method_name = special_cases:: maybe_renamed ( class_name, & method. name ) ;
361
+ let method_name_str = special_cases:: maybe_renamed ( class_name, & method. name ) ;
360
362
/*if method.map_args(|args| args.is_empty()) {
361
363
// Getters (i.e. 0 arguments) will be stripped of their `get_` prefix, to conform to Rust convention
362
364
if let Some(remainder) = method_name.strip_prefix("get_") {
@@ -367,10 +369,7 @@ fn make_method_definition(method: &Method, class_name: &str, ctx: &mut Context)
367
369
}
368
370
}
369
371
}*/
370
- let method_name = safe_ident ( method_name) ;
371
-
372
- let c_method_name = c_str ( & method. name ) ;
373
- let c_class_name = c_str ( class_name) ;
372
+ let method_name = safe_ident ( method_name_str) ;
374
373
let hash = method. hash ;
375
374
376
375
// TODO &mut safety
@@ -393,7 +392,13 @@ fn make_method_definition(method: &Method, class_name: &str, ctx: &mut Context)
393
392
quote ! {
394
393
#vis fn #method_name( #receiver #( , #params ) * , varargs: & [ Variant ] ) #return_decl {
395
394
unsafe {
396
- let method_bind = sys:: interface_fn!( classdb_get_method_bind) ( #c_class_name, #c_method_name, #hash) ;
395
+ let class_name = StringName :: from( #class_name) ;
396
+ let method_name = StringName :: from( #method_name_str) ;
397
+ let method_bind = sys:: interface_fn!( classdb_get_method_bind) (
398
+ class_name. string_sys( ) ,
399
+ method_name. string_sys( ) ,
400
+ #hash
401
+ ) ;
397
402
let call_fn = sys:: interface_fn!( object_method_bind_call) ;
398
403
399
404
let explicit_args = [
@@ -414,7 +419,13 @@ fn make_method_definition(method: &Method, class_name: &str, ctx: &mut Context)
414
419
quote ! {
415
420
#vis fn #method_name( #receiver, #( #params ) , * ) #return_decl {
416
421
unsafe {
417
- let method_bind = sys:: interface_fn!( classdb_get_method_bind) ( #c_class_name, #c_method_name, #hash) ;
422
+ let class_name = StringName :: from( #class_name) ;
423
+ let method_name = StringName :: from( #method_name_str) ;
424
+ let method_bind = sys:: interface_fn!( classdb_get_method_bind) (
425
+ class_name. string_sys( ) ,
426
+ method_name. string_sys( ) ,
427
+ #hash
428
+ ) ;
418
429
let call_fn = sys:: interface_fn!( object_method_bind_ptrcall) ;
419
430
420
431
let args = [
@@ -441,16 +452,17 @@ pub(crate) fn make_function_definition(
441
452
let is_vararg = function. is_vararg ;
442
453
let ( params, arg_exprs) = make_params ( & function. arguments , is_vararg, ctx) ;
443
454
444
- let function_name = safe_ident ( & function. name ) ;
445
- let c_function_name = c_str ( & function . name ) ;
455
+ let function_name_str = & function. name ;
456
+ let function_name = safe_ident ( function_name_str ) ;
446
457
let hash = function. hash ;
447
458
448
459
let ( return_decl, call) = make_utility_return ( & function. return_type , ctx) ;
449
460
450
461
quote ! {
451
462
pub fn #function_name( #( #params ) , * ) #return_decl {
452
463
let result = unsafe {
453
- let call_fn = sys:: interface_fn!( variant_get_ptr_utility_function) ( #c_function_name, #hash) ;
464
+ let function_name = StringName :: from( #function_name_str) ;
465
+ let call_fn = sys:: interface_fn!( variant_get_ptr_utility_function) ( function_name. string_sys( ) , #hash) ;
454
466
let call_fn = call_fn. unwrap_unchecked( ) ;
455
467
456
468
let args = [
0 commit comments