Skip to content

Commit

Permalink
Merge pull request #85975 from Repiteo/c#-null-ptr-exception
Browse files Browse the repository at this point in the history
C#: Implement function to throw on null pointers
  • Loading branch information
YuriSizov committed Dec 19, 2023
2 parents 38d8ca0 + a0db883 commit f478a59
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions modules/mono/editor/bindings_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ StringBuilder &operator<<(StringBuilder &r_sb, const char *p_cstring) {

#define OPEN_BLOCK_L1 INDENT1 OPEN_BLOCK
#define OPEN_BLOCK_L2 INDENT2 OPEN_BLOCK
#define OPEN_BLOCK_L3 INDENT3 OPEN_BLOCK
#define CLOSE_BLOCK_L1 INDENT1 CLOSE_BLOCK
#define CLOSE_BLOCK_L2 INDENT2 CLOSE_BLOCK
#define CLOSE_BLOCK_L3 INDENT3 CLOSE_BLOCK
Expand Down Expand Up @@ -2591,7 +2592,11 @@ Error BindingsGenerator::_generate_cs_native_calls(const InternalCall &p_icall,
// Generate icall function

r_output << MEMBER_BEGIN "internal static unsafe " << (ret_void ? "void" : return_type->c_type_out) << " "
<< icall_method << "(" << c_func_sig.as_string() << ") " OPEN_BLOCK;
<< icall_method << "(" << c_func_sig.as_string() << ")\n" OPEN_BLOCK_L1;

if (!p_icall.is_static) {
r_output << INDENT2 "ExceptionUtils.ThrowIfNullPtr(" CS_PARAM_INSTANCE ");\n";
}

if (!ret_void && (!p_icall.is_vararg || return_type->cname != name_cache.type_Variant)) {
String ptrcall_return_type;
Expand Down Expand Up @@ -2619,11 +2624,6 @@ Error BindingsGenerator::_generate_cs_native_calls(const InternalCall &p_icall,
r_output << ptrcall_return_type << " " C_LOCAL_RET << initialization << ";\n";
}

if (!p_icall.is_static) {
r_output << INDENT2 "if (" CS_PARAM_INSTANCE " == IntPtr.Zero)\n"
<< INDENT3 "throw new ArgumentNullException(nameof(" CS_PARAM_INSTANCE "));\n";
}

String argc_str = itos(p_icall.get_arguments_count());

auto generate_call_and_return_stmts = [&](const char *base_indent) {
Expand Down Expand Up @@ -2714,7 +2714,7 @@ Error BindingsGenerator::_generate_cs_native_calls(const InternalCall &p_icall,

r_output << c_in_statements.as_string();

r_output << INDENT3 "for (int i = 0; i < vararg_length; i++) " OPEN_BLOCK
r_output << INDENT3 "for (int i = 0; i < vararg_length; i++)\n" OPEN_BLOCK_L3
<< INDENT4 "varargs[i] = " << vararg_arg << "[i].NativeVar;\n"
<< INDENT4 C_LOCAL_PTRCALL_ARGS "[" << real_argc_str << " + i] = new IntPtr(&varargs[i]);\n"
<< CLOSE_BLOCK_L3;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;

#nullable enable
Expand Down Expand Up @@ -239,5 +240,13 @@ private unsafe static string GetVariantTypeName(godot_variant* variant)

return variant->Type.ToString();
}

internal static void ThrowIfNullPtr(IntPtr ptr, [CallerArgumentExpression(nameof(ptr))] string? paramName = null)
{
if (ptr == IntPtr.Zero)
{
throw new ArgumentNullException(paramName);
}
}
}
}

0 comments on commit f478a59

Please sign in to comment.