1616
1717@dataclass
1818class TypeSpec :
19+ size : int
1920 # Type used within Godot `extension_api.json`
2021 gdapi_type : str
2122 # Type used for PEP 484 Python typing
@@ -58,11 +59,9 @@ def __post_init__(self):
5859
5960
6061TYPES_DB : Dict [str , TypeSpec ] = {
61- "Variant" : TypeSpec (
62- gdapi_type = "Variant" , c_type = "CVariant" , cy_type = "object" , py_type = "GDAny" , is_builtin = True
63- ),
6462 # Types marked as `meta` are used in the classes method args/return types
6563 "meta:int8" : TypeSpec (
64+ size = 1 ,
6665 gdapi_type = "int8" ,
6766 c_type = "int8_t" ,
6867 cy_type = "int8_t" ,
@@ -72,6 +71,7 @@ def __post_init__(self):
7271 is_stack_only = True ,
7372 ),
7473 "meta:int16" : TypeSpec (
74+ size = 2 ,
7575 gdapi_type = "int16" ,
7676 c_type = "int16_t" ,
7777 cy_type = "int16_t" ,
@@ -81,6 +81,7 @@ def __post_init__(self):
8181 is_stack_only = True ,
8282 ),
8383 "meta:int32" : TypeSpec (
84+ size = 4 ,
8485 gdapi_type = "int32" ,
8586 c_type = "int32_t" ,
8687 cy_type = "int32_t" ,
@@ -90,6 +91,7 @@ def __post_init__(self):
9091 is_stack_only = True ,
9192 ),
9293 "meta:int64" : TypeSpec (
94+ size = 8 ,
9395 gdapi_type = "int64" ,
9496 c_type = "int64_t" ,
9597 cy_type = "int64_t" ,
@@ -99,6 +101,7 @@ def __post_init__(self):
99101 is_stack_only = True ,
100102 ),
101103 "meta:uint8" : TypeSpec (
104+ size = 1 ,
102105 gdapi_type = "uint8" ,
103106 c_type = "uint8_t" ,
104107 cy_type = "uint8_t" ,
@@ -108,6 +111,7 @@ def __post_init__(self):
108111 is_stack_only = True ,
109112 ),
110113 "meta:uint16" : TypeSpec (
114+ size = 2 ,
111115 gdapi_type = "uint16" ,
112116 c_type = "uint16_t" ,
113117 cy_type = "uint16_t" ,
@@ -117,6 +121,7 @@ def __post_init__(self):
117121 is_stack_only = True ,
118122 ),
119123 "meta:uint32" : TypeSpec (
124+ size = 4 ,
120125 gdapi_type = "uint32" ,
121126 c_type = "uint32_t" ,
122127 cy_type = "uint32_t" ,
@@ -126,6 +131,7 @@ def __post_init__(self):
126131 is_stack_only = True ,
127132 ),
128133 "meta:uint64" : TypeSpec (
134+ size = 8 ,
129135 gdapi_type = "uint64" ,
130136 c_type = "uint64_t" ,
131137 cy_type = "uint64_t" ,
@@ -136,6 +142,7 @@ def __post_init__(self):
136142 ),
137143 # int is always 8bytes long
138144 "int" : TypeSpec (
145+ size = 8 ,
139146 gdapi_type = "int" ,
140147 c_type = "uint64_t" ,
141148 cy_type = "uint64_t" ,
@@ -145,6 +152,7 @@ def __post_init__(self):
145152 is_stack_only = True ,
146153 ),
147154 "meta:float" : TypeSpec (
155+ size = 4 ,
148156 gdapi_type = "float" ,
149157 c_type = "float" ,
150158 cy_type = "float" ,
@@ -154,6 +162,7 @@ def __post_init__(self):
154162 is_stack_only = True ,
155163 ),
156164 "meta:double" : TypeSpec (
165+ size = 8 ,
157166 gdapi_type = "double" ,
158167 c_type = "double" ,
159168 cy_type = "double" ,
@@ -166,6 +175,17 @@ def __post_init__(self):
166175}
167176
168177
178+ def register_variant_in_types_db (variant_size : int ) -> None :
179+ TYPES_DB ["Variant" ] = TypeSpec (
180+ size = variant_size ,
181+ gdapi_type = "Variant" ,
182+ c_type = "CVariant" ,
183+ cy_type = "object" ,
184+ py_type = "GDAny" ,
185+ is_builtin = True ,
186+ )
187+
188+
169189def register_builtins_in_types_db (builtins : Iterable ["BuiltinSpec" ]) -> None :
170190 for spec in builtins :
171191 if spec .name == "Nil" :
@@ -175,6 +195,7 @@ def register_builtins_in_types_db(builtins: Iterable["BuiltinSpec"]) -> None:
175195 continue
176196 elif spec .name == "bool" :
177197 ts = TypeSpec (
198+ size = spec .size ,
178199 gdapi_type = spec .original_name ,
179200 py_type = "bool" ,
180201 # Cython provide a `bint` type for boolean, however it is defined in C as
@@ -199,6 +220,7 @@ def register_builtins_in_types_db(builtins: Iterable["BuiltinSpec"]) -> None:
199220 ts = replace (TYPES_DB [f"meta:double" ], gdapi_type = spec .original_name )
200221 else :
201222 ts = TypeSpec (
223+ size = spec .size ,
202224 gdapi_type = spec .original_name ,
203225 py_type = spec .name ,
204226 c_type = f"C{ spec .name } " ,
@@ -213,15 +235,19 @@ def register_builtins_in_types_db(builtins: Iterable["BuiltinSpec"]) -> None:
213235def register_classes_in_types_db (classes : Iterable ["ClassSpec" ]) -> None :
214236 for spec in classes :
215237 ts = TypeSpec (
238+ # Class instance is always manipulated as a pointer,
239+ # hence `size` field is never supposed to be used here
240+ size = 0 , # Dummy value
216241 gdapi_type = spec .original_name ,
217242 py_type = spec .name ,
218- c_type = "Object " ,
219- cy_type = "Object " ,
243+ c_type = "GDNativeObjectPtr " ,
244+ cy_type = "GDNativeObjectPtr " ,
220245 variant_type_name = "GDNATIVE_VARIANT_TYPE_OBJECT" ,
221246 )
222247 TYPES_DB [ts .gdapi_type ] = ts
223248 for e in spec .enums :
224249 ts = TypeSpec (
250+ size = 4 ,
225251 gdapi_type = f"enum::{ spec .original_name } .{ e .original_name } " ,
226252 py_type = f"{ spec .name } .{ e .name } " ,
227253 c_type = "int" ,
@@ -237,6 +263,7 @@ def register_classes_in_types_db(classes: Iterable["ClassSpec"]) -> None:
237263def register_global_enums_in_types_db (enums : Iterable ["GlobalEnumSpec" ]) -> None :
238264 for spec in enums :
239265 ts = TypeSpec (
266+ size = 4 ,
240267 gdapi_type = f"enum::{ spec .original_name } " ,
241268 py_type = spec .name ,
242269 c_type = "int" ,
@@ -269,7 +296,10 @@ def resolve(self) -> TypeSpec:
269296 return
270297
271298 def __getattr__ (self , name : str ):
272- return getattr (self .resolve (), name )
299+ try :
300+ return getattr (self .resolve (), name )
301+ except AttributeError as exc :
302+ raise RuntimeError (f"Error in TypeSpec accessing: { exc } " ) from exc
273303
274304
275305# ValueInUse is only used to create function argument's default value,
0 commit comments