Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Implement function to throw on null pointers #85975

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading