Skip to content

Commit

Permalink
Adding Js::Utf8String as a string type which maintains a UTF8 buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
MSLaguana committed Jul 25, 2018
1 parent f8154dd commit cd74d97
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/Runtime/Library/Chakra.Runtime.Library.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
<ClInclude Include="SparseArraySegment.h" />
<ClInclude Include="SubString.h" />
<ClInclude Include="UriHelper.h" />
<ClInclude Include="Utf8String.h" />
<ClInclude Include="WabtInterface.h" />
<ClInclude Include="WasmLibrary.h" />
<ClInclude Include="WebAssembly.h" />
Expand Down
1 change: 1 addition & 0 deletions lib/Runtime/Library/Chakra.Runtime.Library.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
<ClInclude Include="JsBuiltIn\JsBuiltIn.js.nojit.bc.32b.h" />
<ClInclude Include="JsBuiltIn\JsBuiltIn.js.nojit.bc.64b.h" />
<ClInclude Include="PropertyRecordUsageCache.h" />
<ClInclude Include="Utf8String.h" />
<ClInclude Include="..\LibraryFunction.h" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion lib/Runtime/Library/JavascriptString.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace Js
void FinishCopy(__inout_xcount(m_charLength) char16 *const buffer, StringCopyInfoStack &nestedStringTreeCopyInfos);

public:
virtual int GetRandomAccessItemsFromConcatString(Js::JavascriptString * const *& items) const { return -1; }
virtual int GetRandomAccessItemsFromConcatString(_Out_ Js::JavascriptString * const *& items) const { items = nullptr; return -1; }
virtual bool IsTree() const { return false; }

virtual BOOL SetItem(uint32 index, Var value, PropertyOperationFlags propertyOperationFlags) override;
Expand Down
1 change: 1 addition & 0 deletions lib/Runtime/Library/RuntimeLibraryPch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Library/SingleCharString.h"
#include "Library/SubString.h"
#include "Library/BufferStringBuilder.h"
#include "Library/Utf8String.h"

#include "Library/BoundFunction.h"
#include "Library/JavascriptGeneratorFunction.h"
Expand Down
96 changes: 96 additions & 0 deletions lib/Runtime/Library/Utf8String.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once

#include "Codex/Utf8Codex.h"

namespace Js
{
class Utf8String : public JavascriptString
{
private:
typedef struct {
FieldNoBarrier(size_t) length;
Field(char*) buffer;
} PrefixedUtf8String;
Field(PrefixedUtf8String*) utf8String;

protected:
DEFINE_VTABLE_CTOR(Utf8String, JavascriptString);

private:

void SetUtf8Buffer(_In_reads_(utf8Length) char* buffer, size_t utf8Length)
{
this->utf8String = RecyclerNew(this->GetRecycler(), PrefixedUtf8String);
this->utf8String->length = utf8Length;
this->utf8String->buffer = buffer;
}

public:

Utf8String(_In_ JavascriptString* originalString, _In_reads_(utf8Length) char* buffer, size_t utf8Length, _In_ StaticType* type) :
JavascriptString(type),
utf8String(nullptr)
{
SetUtf8Buffer(buffer, utf8Length);

this->SetLength(originalString->GetLength());
this->SetBuffer(originalString->UnsafeGetBuffer());
}

size_t Utf8Length() const
{
return this->utf8String->length;
}

const char* Utf8Buffer() const
{
return this->utf8String->buffer;
}

const char16* GetSz() override
{
if (this->IsFinalized())
{
return this->UnsafeGetBuffer();
}

// TODO: This is currently wrong in the presence of unmatched surrogate pairs.
// They will be converted to a 3 byte replacement character in utf8, and then
// converted back into a 1 utf16 character 0xfffd representation of that, not the original
// utf16 surrogate pair character
char16* buffer = RecyclerNewArrayLeaf(this->GetRecycler(), char16, this->GetLength() + 1);

// Fetching the char* from the Field(char*) first so we can then cast to LPCUTF8
const char* bufferStart = this->utf8String->buffer;
LPCUTF8 start = reinterpret_cast<LPCUTF8>(bufferStart);
utf8::DecodeUnitsIntoAndNullTerminateNoAdvance(buffer, start, start + this->utf8String->length);

buffer[this->GetLength()] = 0;

this->SetBuffer(buffer);
return this->UnsafeGetBuffer();
}

static bool Is(RecyclableObject* obj)
{
return VirtualTableInfo<Js::Utf8String>::HasVirtualTable(obj);
}

template <typename StringType>
static Utf8String * ConvertString(StringType * originalString, _In_reads_(utf8Length) char* buffer, size_t utf8Length)
{
VirtualTableInfo<Utf8String>::SetVirtualTable(originalString);
Utf8String * convertedString = (Utf8String *)originalString;

convertedString->SetUtf8Buffer(buffer, utf8Length);

// length and buffer are preserved from the original string, since that is part of JavascriptString

return convertedString;
}
};
}
2 changes: 2 additions & 0 deletions lib/Runtime/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ enum tagDEBUG_EVENT_INFO_TYPE
#include "Library/PropertyRecordUsageCache.h"
#include "Library/PropertyString.h"
#include "Library/SingleCharString.h"
#include "Library/Utf8String.h"
#include "Library/LazyJSONString.h"

#include "Library/JavascriptTypedNumber.h"
#include "Library/SparseArraySegment.h"
Expand Down

0 comments on commit cd74d97

Please sign in to comment.