Skip to content

Commit b728ee4

Browse files
committed
Make LuaString.ToString() return null for null pointer
1 parent 0b91a28 commit b728ee4

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

src/Laylua/Library/Entities/Reference/LuaReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public override string ToString()
200200
{
201201
PushValue(this);
202202
var L = Lua.GetStatePointer();
203-
return luaL_tostring(L, -1).ToString();
203+
return luaL_tostring(L, -1).ToString() ?? "<invalid>";
204204
}
205205
}
206206

src/Laylua/Library/LuaException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal static unsafe bool TryGetError(lua_State* L, out (string? Message, Exce
7272

7373
if (type == LuaType.String)
7474
{
75-
error = (lua_tostring(L, -1).ToString().Replace("\r", "").Replace("\n", ""), Exception: null);
75+
error = (lua_tostring(L, -1).ToString()?.Replace("\r", "").Replace("\n", ""), Exception: null);
7676
return true;
7777
}
7878

src/Laylua/Library/Marshaler/DefaultLuaMarshaler.TryGetValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public override bool TryGetValue<T>(LuaThread lua, int stackIndex, out T? obj)
234234
var nativeStringValue = lua_tostring(L, stackIndex);
235235
if (clrType == typeof(string) || clrType == typeof(object))
236236
{
237-
obj = (T) (object) nativeStringValue.ToString();
237+
obj = (T?) (object?) nativeStringValue.ToString();
238238
return true;
239239
}
240240

src/Laylua/Moon/Native/LuaString.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ public int GetChars(Span<char> destination)
8181
/// Returns a <see cref="string"/> created from this <see cref="LuaString"/>.
8282
/// </summary>
8383
/// <returns> The created <see cref="string"/>. </returns>
84-
public override string ToString()
84+
public override string? ToString()
8585
{
8686
return Pointer != null
8787
? Encoding.UTF8.GetString(Pointer, (int) Length)
88-
: "<invalid string pointer>";
88+
: null;
8989
}
9090

9191
/// <inheritdoc/>

0 commit comments

Comments
 (0)