Skip to content

Commit a46b8e1

Browse files
authored
Merge pull request godotengine#29140 from neikeq/to_string_impl
C#: Implement ScriptInstance::to_string
2 parents 9738ed5 + 04ebf29 commit a46b8e1

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

modules/mono/csharp_script.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,34 @@ void CSharpInstance::_call_notification(int p_notification) {
18551855
}
18561856
}
18571857

1858+
String CSharpInstance::to_string(bool *r_valid) {
1859+
MonoObject *mono_object = get_mono_object();
1860+
1861+
if (mono_object == NULL) {
1862+
if (r_valid)
1863+
*r_valid = false;
1864+
return String();
1865+
}
1866+
1867+
MonoException *exc = NULL;
1868+
MonoString *result = GDMonoUtils::object_to_string(mono_object, &exc);
1869+
1870+
if (exc) {
1871+
GDMonoUtils::set_pending_exception(exc);
1872+
if (r_valid)
1873+
*r_valid = false;
1874+
return String();
1875+
}
1876+
1877+
if (result == NULL) {
1878+
if (r_valid)
1879+
*r_valid = false;
1880+
return String();
1881+
}
1882+
1883+
return GDMonoMarshal::mono_string_to_godot(result);
1884+
}
1885+
18581886
Ref<Script> CSharpInstance::get_script() const {
18591887

18601888
return script;

modules/mono/csharp_script.h

+2
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class CSharpInstance : public ScriptInstance {
261261
virtual void notification(int p_notification);
262262
void _call_notification(int p_notification);
263263

264+
virtual String to_string(bool *r_valid);
265+
264266
virtual Ref<Script> get_script() const;
265267

266268
virtual ScriptLanguage *get_language();

modules/mono/editor/bindings_generator.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -2300,9 +2300,14 @@ void BindingsGenerator::_populate_object_type_interfaces() {
23002300
if (method_info.name.empty())
23012301
continue;
23022302

2303+
String cname = method_info.name;
2304+
2305+
if (blacklisted_methods.find(itype.cname) && blacklisted_methods[itype.cname].find(cname))
2306+
continue;
2307+
23032308
MethodInterface imethod;
23042309
imethod.name = method_info.name;
2305-
imethod.cname = imethod.name;
2310+
imethod.cname = cname;
23062311

23072312
if (method_info.flags & METHOD_FLAG_VIRTUAL)
23082313
imethod.is_virtual = true;
@@ -2975,6 +2980,13 @@ void BindingsGenerator::_populate_global_constants() {
29752980
}
29762981
}
29772982

2983+
void BindingsGenerator::_initialize_blacklisted_methods() {
2984+
2985+
blacklisted_methods["Object"].push_back("to_string"); // there is already ToString
2986+
blacklisted_methods["Object"].push_back("_to_string"); // override ToString instead
2987+
blacklisted_methods["Object"].push_back("_init"); // never called in C# (TODO: implement it)
2988+
}
2989+
29782990
void BindingsGenerator::_log(const char *p_format, ...) {
29792991

29802992
if (log_print_enabled) {
@@ -2992,6 +3004,8 @@ void BindingsGenerator::_initialize() {
29923004

29933005
enum_types.clear();
29943006

3007+
_initialize_blacklisted_methods();
3008+
29953009
_populate_object_type_interfaces();
29963010
_populate_builtin_type_interfaces();
29973011

modules/mono/editor/bindings_generator.h

+4
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ class BindingsGenerator {
491491
List<InternalCall> core_custom_icalls;
492492
List<InternalCall> editor_custom_icalls;
493493

494+
Map<StringName, List<StringName> > blacklisted_methods;
495+
496+
void _initialize_blacklisted_methods();
497+
494498
struct NameCache {
495499
StringName type_void;
496500
StringName type_Array;

0 commit comments

Comments
 (0)