Skip to content

Commit 496ec1b

Browse files
committed
add a version of Utf8Helper conversion that uses a function to allocate.
Remove nonsense with global allocator for utf8 conversion in WabtInterface
1 parent 0433005 commit 496ec1b

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

lib/Common/Codex/Utf8Helper.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace utf8
1313
/// using Allocator.
1414
/// The returned string is null terminated.
1515
///
16-
template <class Allocator>
17-
HRESULT WideStringToNarrow(_In_ LPCWSTR sourceString, size_t sourceCount, _Out_ LPSTR* destStringPtr, _Out_ size_t* destCount)
16+
template <typename AllocatorFunction>
17+
HRESULT WideStringToNarrow(_In_ AllocatorFunction allocator, _In_ LPCWSTR sourceString, size_t sourceCount, _Out_ LPSTR* destStringPtr, _Out_ size_t* destCount)
1818
{
1919
size_t cchSourceString = sourceCount;
2020

@@ -31,7 +31,7 @@ namespace utf8
3131
return E_OUTOFMEMORY;
3232
}
3333

34-
utf8char_t* destString = (utf8char_t*)Allocator::allocate(cbDestString);
34+
utf8char_t* destString = (utf8char_t*)allocator(cbDestString);
3535
if (destString == nullptr)
3636
{
3737
return E_OUTOFMEMORY;
@@ -45,14 +45,20 @@ namespace utf8
4545
return S_OK;
4646
}
4747

48+
template <class Allocator>
49+
HRESULT WideStringToNarrow(_In_ LPCWSTR sourceString, size_t sourceCount, _Out_ LPSTR* destStringPtr, _Out_ size_t* destCount)
50+
{
51+
return WideStringToNarrow(Allocator::allocate, sourceString, sourceCount, destStringPtr, destCount);
52+
}
53+
4854
///
4955
/// Use the codex library to encode a UTF8 string to UTF16.
5056
/// The caller is responsible for freeing the memory, which is allocated
5157
/// using Allocator.
5258
/// The returned string is null terminated.
5359
///
54-
template <class Allocator>
55-
HRESULT NarrowStringToWide(_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount)
60+
template <typename AllocatorFunction>
61+
HRESULT NarrowStringToWide(_In_ AllocatorFunction allocator,_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount)
5662
{
5763
size_t cbSourceString = sourceCount;
5864
charcount_t cchDestString = utf8::ByteIndexIntoCharacterIndex((LPCUTF8) sourceString, cbSourceString);
@@ -64,14 +70,14 @@ namespace utf8
6470
return E_OUTOFMEMORY;
6571
}
6672

67-
WCHAR* destString = (WCHAR*)Allocator::allocate(cbDestString);
73+
WCHAR* destString = (WCHAR*)allocator(cbDestString);
6874
if (destString == nullptr)
6975
{
7076
return E_OUTOFMEMORY;
7177
}
7278

7379
// Some node tests depend on the utf8 decoder not swallowing invalid unicode characters
74-
// instead of replacing them with the "replacement" chracter. Pass a flag to our
80+
// instead of replacing them with the "replacement" character. Pass a flag to our
7581
// decoder to require such behavior
7682
utf8::DecodeUnitsIntoAndNullTerminateNoAdvance(destString, (LPCUTF8) sourceString, (LPCUTF8) sourceString + cbSourceString, DecodeOptions::doAllowInvalidWCHARs);
7783
Assert(destString[cchDestString] == 0);
@@ -81,6 +87,12 @@ namespace utf8
8187
return S_OK;
8288
}
8389

90+
template <class Allocator>
91+
HRESULT NarrowStringToWide(_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount)
92+
{
93+
return NarrowStringToWide(Allocator::allocate, sourceString, sourceCount, destStringPtr, destCount);
94+
}
95+
8496
class malloc_allocator
8597
{
8698
public:

lib/Runtime/Library/WabtInterface.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
#include "Codex/Utf8Helper.h"
1919
using namespace wabt;
2020

21-
ArenaAllocator* tempArenaPtr;
22-
struct utf8Allocator
23-
{
24-
static byte* allocate(size_t size)
25-
{
26-
return AnewArray(tempArenaPtr, byte, size);
27-
}
28-
};
29-
3021
struct MemoryWriterContext
3122
{
3223
MemoryWriter* writer;
@@ -105,8 +96,8 @@ Js::Var Js::WabtInterface::EntryConvertWast2Wasm(RecyclableObject* function, Cal
10596
size_t origSize = string->GetLength();
10697
size_t wastSize;
10798
char* wastBuffer = nullptr;
108-
tempArenaPtr = &arena;
109-
utf8::WideStringToNarrow<utf8Allocator>(str, origSize, &wastBuffer, &wastSize);
99+
auto allocator = [&arena](size_t size) {return (utf8char_t*)AnewArray(&arena, byte, size);};
100+
utf8::WideStringToNarrow(allocator, str, origSize, &wastBuffer, &wastSize);
110101

111102
AstLexer* lexer = new_ast_buffer_lexer("", wastBuffer, wastSize);
112103
struct AutoCleanLexer

0 commit comments

Comments
 (0)