Skip to content

Commit

Permalink
[Runtime] Prevent double delete when engine shutdown.
Browse files Browse the repository at this point in the history
[VariantUtils] Fix `write_json` bug that returns positive integers when reading negative integers.
[Runtime] Remove `String::operator=(value_type ch)`, since it is not used at all and may cause ambiguity.
  • Loading branch information
JX-Master committed Oct 7, 2024
1 parent 0b60676 commit 1ae6e7e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 24 deletions.
10 changes: 0 additions & 10 deletions Modules/Luna/Runtime/Impl/String.inl
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,6 @@ namespace Luna
return *this;
}
template <typename _Char, typename _Alloc>
inline BasicString<_Char, _Alloc>& BasicString<_Char, _Alloc>::operator=(value_type ch)
{
clear();
reserve(1);
m_allocator_and_buffer.second()[0] = ch;
m_size = 1;
m_allocator_and_buffer.second()[m_size] = (value_type)0;
return *this;
}
template <typename _Char, typename _Alloc>
inline BasicString<_Char, _Alloc>& BasicString<_Char, _Alloc>::operator=(InitializerList<value_type> ilist)
{
clear();
Expand Down
4 changes: 2 additions & 2 deletions Modules/Luna/Runtime/Source/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ namespace Luna
{
if (g_initialized) return true;
OS::init();
stack_allocator_init();
profiler_init();
stack_allocator_init();
error_init();
name_init();
type_registry_init();
Expand Down Expand Up @@ -96,8 +96,8 @@ namespace Luna
type_registry_close();
name_close();
error_close();
profiler_close();
stack_allocator_close();
profiler_close();
OS::close();
g_initialized = false;
}
Expand Down
7 changes: 5 additions & 2 deletions Modules/Luna/Runtime/Source/StackAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ namespace Luna
}
void stack_allocator_close()
{
{
LockGuard guard(g_stack_allocator_ctxs_lock);
g_stack_allocator_ctxs.clear();
g_stack_allocator_ctxs.shrink_to_fit();
}
tls_free(g_stack_allocator_tls);
g_stack_allocator_ctxs.clear();
g_stack_allocator_ctxs.shrink_to_fit();
}
StackAllocatorTLSContext* get_stack_allocator_ctx()
{
Expand Down
5 changes: 0 additions & 5 deletions Modules/Luna/Runtime/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ namespace Luna
//! @par Valid Usage
//! * `s` must points to a valid null-terminated string.
BasicString& operator=(const value_type* s);
//! Assigns the string with the specified character. This operation behaves the same as
//! `assign(1, ch)`.
//! @param[in] ch The character to assign.
//! @return Returns `*this`.
BasicString& operator=(value_type ch);
//! Assigns the string by coping characters from the initializer list.
//! @param[in] ilist The initializer list to copy characters from.
//! @return Returns `*this`.
Expand Down
2 changes: 1 addition & 1 deletion Modules/Luna/VariantUtils/Source/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ namespace Luna
value *= 10;
value += i - '0';
}
return Variant(value);
return Variant(-value);
}
}

Expand Down
4 changes: 0 additions & 4 deletions Tests/RuntimeTest/Source/StringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ namespace Luna
str8 = u8"Sample String";
lutest(!strcmp(str8.c_str(), u8"Sample String"));

// operator=(value_type ch)
str8 = 'c';
lutest(!strcmp(str8.c_str(), u8"c"));

// operator=(InitializerList<value_type> ilist)
str8 = { 'e', 't', 'f' };
lutest(!strcmp(str8.c_str(), u8"etf"));
Expand Down
9 changes: 9 additions & 0 deletions Tests/VariantUtilsTest/Source/JSONTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,14 @@ namespace Luna
Variant& blob_var2 = blob_var2_r.get();
luassert_always(blob_var == blob_var2);
}

{
// Bugfix: reading negative number will result in positive number.
Variant var1((i64)-3);
String s = VariantUtils::write_json(var1);
R<Variant> var2 = VariantUtils::read_json(s.c_str());
luassert_always(succeeded(var2));
luassert_always(var1 == var2.get());
}
}
}

0 comments on commit 1ae6e7e

Please sign in to comment.