Skip to content

Commit 40b53e1

Browse files
author
Jianchun Xu
committed
[MERGE #779] xplat: build lib/Runtime/Library
Merge pull request #779 from jianchun:lib This change enables building `lib/Runtime/Library`. - Added a few unimplemented stubs into `CommonPal.h`. - Replaced a bunch of `__try/__finally(__leave)` with `TryFinally`. - `JavascriptBuiltInFunctions.cpp`: Added `BUILTIN_TEMPLATE` to work with all TypedArray types as they are template specializations and need `template<>`. - `TypedArray.cpp`: Needed to move `Is`/`FromVar` specializations forward before being used. 2 files excluded from build: - JavascriptErrorDebug.cpp: using interfaces - MathLibrary.cpp: using intrinsics conflicting with stdlib/math.h
2 parents b27dd9f + 13f6e3b commit 40b53e1

39 files changed

+897
-833
lines changed

lib/Common/CommonPal.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ inline T InterlockedDecrement(
274274
return __sync_sub_and_fetch(Addend, T(1));
275275
}
276276

277+
inline __int64 _abs64(__int64 n)
278+
{
279+
return n < 0 ? -n : n;
280+
}
277281

278282
// xplat-todo: implement these for JIT and Concurrent/Partial GC
279283
uintptr_t _beginthreadex(
@@ -298,10 +302,18 @@ int GetCurrentThreadStackBounds(char** stackBase, char** stackEnd);
298302
errno_t rand_s(unsigned int* randomValue);
299303
errno_t __cdecl _ultow_s(unsigned _Value, WCHAR *_Dst, size_t _SizeInWords, int _Radix);
300304
errno_t __cdecl _ui64tow_s(unsigned __int64 _Value, WCHAR *_Dst, size_t _SizeInWords, int _Radix);
305+
void __cdecl qsort_s(void *base, size_t num, size_t width,
306+
int (__cdecl *compare )(void *, const void *, const void *),
307+
void * context
308+
);
309+
char16* __cdecl wmemset(char16* wcs, char16 wc, size_t n);
310+
DWORD __cdecl CharLowerBuffW(const char16* lpsz, DWORD cchLength);
311+
DWORD __cdecl CharUpperBuffW(const char16* lpsz, DWORD cchLength);
301312

302313
#define MAXUINT32 ((uint32_t)~((uint32_t)0))
303314
#define MAXINT32 ((int32_t)(MAXUINT32 >> 1))
304315
#define BYTE_MAX 0xff
316+
#define USHORT_MAX 0xffff
305317

306318
#ifdef UNICODE
307319
#define StringCchPrintf StringCchPrintfW
@@ -394,4 +406,3 @@ void TryFinally(const TryFunc& tryFunc, const FinallyFunc& finallyFunc)
394406

395407
__inline
396408
HRESULT ULongMult(ULONG ulMultiplicand, ULONG ulMultiplier, ULONG* pulResult);
397-

lib/Common/DataStructures/WeakReferenceDictionary.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ namespace JsUtil
2121
public IWeakReferenceDictionary
2222
{
2323
typedef BaseDictionary<TKey, RecyclerWeakReference<TValue>*, RecyclerNonLeafAllocator, SizePolicy, Comparer, WeakRefValueDictionaryEntry> Base;
24-
24+
typedef typename Base::EntryType EntryType;
25+
2526
public:
2627
WeakReferenceDictionary(Recycler* recycler, int capacity = 0):
27-
BaseDictionary(recycler, capacity)
28+
Base(recycler, capacity)
2829
{
2930
Assert(reinterpret_cast<void*>(this) == reinterpret_cast<void*>((IWeakReferenceDictionary*) this));
3031
}

lib/Common/Memory/AutoPtr.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ class AutoBSTR : public BasePtr<OLECHAR>
145145
};
146146

147147
template <typename T>
148-
class AutoDiscardPTR : public BasePtr < T >
148+
class AutoDiscardPTR : public BasePtr<T>
149149
{
150150
public:
151-
AutoDiscardPTR(T * ptr) : BasePtr(ptr) {}
151+
AutoDiscardPTR(T * ptr) : BasePtr<T>(ptr) {}
152152
~AutoDiscardPTR()
153153
{
154154
Clear();
@@ -164,10 +164,10 @@ class AutoDiscardPTR : public BasePtr < T >
164164
private:
165165
void Clear()
166166
{
167-
if (ptr != nullptr)
167+
if (this->ptr != nullptr)
168168
{
169-
ptr->Discard();
170-
ptr = nullptr;
169+
this->ptr->Discard();
170+
this->ptr = nullptr;
171171
}
172172
}
173173
};

lib/Runtime/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ include_directories(
66
$(CHAKRA_RUNTIME_SOURCE_DIR)/../Backend
77
$(CHAKRA_RUNTIME_SOURCE_DIR)/../Parser
88
$(CHAKRA_RUNTIME_SOURCE_DIR)/ByteCode
9+
$(CHAKRA_RUNTIME_SOURCE_DIR)/Math
910
)
1011

1112
add_subdirectory (Base)
1213
add_subdirectory (ByteCode)
1314
add_subdirectory (Language)
15+
add_subdirectory (Library)
1416
add_subdirectory (Math)
1517
add_subdirectory (Types)

lib/Runtime/Library/BufferStringBuilder.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ namespace Js
1212
Assert( dst != nullptr);
1313
Assert( ptr != nullptr);
1414

15-
if( cchDst < cch )
16-
{
17-
Throw::FatalInternalError();
18-
}
19-
20-
wmemcpy_s(dst, cchDst, ptr, cch);
15+
js_wmemcpy_s(dst, cchDst, ptr, cch);
2116

2217
cchDst -= cch;
2318
return dst + cch;

lib/Runtime/Library/BufferStringBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace Js
5353
const char16* suffix, charcount_t cchSuffix);
5454

5555
protected:
56-
DEFINE_VTABLE_CTOR(BufferStringBuilder::WritableString, JavascriptString);
56+
DEFINE_VTABLE_CTOR(WritableString, JavascriptString);
5757
DECLARE_CONCRETE_STRING_CLASS;
5858

5959
private:

lib/Runtime/Library/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ add_library (Chakra.Runtime.Library
3636
JavascriptDate.cpp
3737
JavascriptEnumeratorIterator.cpp
3838
JavascriptError.cpp
39-
JavascriptErrorDebug.cpp
39+
# JavascriptErrorDebug.cpp
4040
JavascriptExternalFunction.cpp
4141
JavascriptFunction.cpp
4242
JavascriptGenerator.cpp
@@ -79,7 +79,7 @@ add_library (Chakra.Runtime.Library
7979
JavascriptWeakMap.cpp
8080
JavascriptWeakSet.cpp
8181
LiteralString.cpp
82-
MathLibrary.cpp
82+
# MathLibrary.cpp
8383
ModuleRoot.cpp
8484
NullEnumerator.cpp
8585
ObjectPrototypeObject.cpp

lib/Runtime/Library/DataView.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ namespace Js
174174

175175
DataView* dataView = DataView::FromVar(args[0]);
176176
uint32 offset = JavascriptConversion::ToUInt32(args[1], scriptContext);
177-
return dataView->GetValue<int8>(offset, _u("DataView.prototype.GetInt8"), FALSE);
177+
return dataView->template GetValue<int8>(offset, _u("DataView.prototype.GetInt8"), FALSE);
178178
}
179179

180180
Var DataView::EntryGetUint8(RecyclableObject* function, CallInfo callInfo, ...)
@@ -253,7 +253,7 @@ namespace Js
253253

254254
DataView* dataView = DataView::FromVar(args[0]);
255255
uint32 offset = JavascriptConversion::ToUInt32(args[1], scriptContext);
256-
return dataView->GetValue<uint16>(offset, _u("DataView.prototype.GetUint16"), isLittleEndian);
256+
return dataView->template GetValue<uint16>(offset, _u("DataView.prototype.GetUint16"), isLittleEndian);
257257
}
258258

259259
Var DataView::EntryGetUint32(RecyclableObject* function, CallInfo callInfo, ...)
@@ -680,30 +680,30 @@ namespace Js
680680
}
681681

682682
#ifdef _M_ARM
683-
// Provide template specialization (only) for memory access at unaligned float/double address which causes data alignment exception otherwise.
684-
template<>
685-
Var DataView::GetValueWithCheck<float>(uint32 byteOffset, char16 *funcName, BOOL isLittleEndian = FALSE)
686-
{
687-
return this->GetValueWithCheck<float, float UNALIGNED*>(byteOffset, isLittleEndian, funcName);
688-
}
683+
// Provide template specialization (only) for memory access at unaligned float/double address which causes data alignment exception otherwise.
684+
template<>
685+
Var DataView::GetValueWithCheck<float>(uint32 byteOffset, const char16 *funcName, BOOL isLittleEndian = FALSE)
686+
{
687+
return this->GetValueWithCheck<float, float UNALIGNED*>(byteOffset, isLittleEndian, funcName);
688+
}
689689

690-
template<>
691-
Var DataView::GetValueWithCheck<double>(uint32 byteOffset, char16 *funcName, BOOL isLittleEndian = FALSE)
692-
{
693-
return this->GetValueWithCheck<double, double UNALIGNED*>(byteOffset, isLittleEndian, funcName);
694-
}
690+
template<>
691+
Var DataView::GetValueWithCheck<double>(uint32 byteOffset, const char16 *funcName, BOOL isLittleEndian = FALSE)
692+
{
693+
return this->GetValueWithCheck<double, double UNALIGNED*>(byteOffset, isLittleEndian, funcName);
694+
}
695695

696-
template<>
697-
void DataView::SetValue<float>(uint32 byteOffset, float value, char16 *funcName, BOOL isLittleEndian = FALSE)
698-
{
699-
this->SetValue<float, float UNALIGNED*>(byteOffset, value, isLittleEndian, funcName);
700-
}
696+
template<>
697+
void DataView::SetValue<float>(uint32 byteOffset, float value, const char16 *funcName, BOOL isLittleEndian = FALSE)
698+
{
699+
this->SetValue<float, float UNALIGNED*>(byteOffset, value, isLittleEndian, funcName);
700+
}
701701

702-
template<>
703-
void DataView::SetValue<double>(uint32 byteOffset, double value, char16 *funcName, BOOL isLittleEndian = FALSE)
704-
{
705-
this->SetValue<double, double UNALIGNED*>(byteOffset, value, isLittleEndian, funcName);
706-
}
702+
template<>
703+
void DataView::SetValue<double>(uint32 byteOffset, double value, const char16 *funcName, BOOL isLittleEndian = FALSE)
704+
{
705+
this->SetValue<double, double UNALIGNED*>(byteOffset, value, isLittleEndian, funcName);
706+
}
707707
#endif
708708

709709
}

lib/Runtime/Library/DataView.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace Js
9292
template<> void SwapRoutine(double* input, double* dest) {*((uint64*)dest) = RtlUlonglongByteSwap(*((uint64*)input)); }
9393

9494
template<typename TypeName>
95-
Var GetValue(uint32 byteOffset, char16* funcName, BOOL isLittleEndian = FALSE)
95+
Var GetValue(uint32 byteOffset, const char16* funcName, BOOL isLittleEndian = FALSE)
9696
{
9797
ScriptContext* scriptContext = GetScriptContext();
9898
if (this->GetArrayBuffer()->IsDetached())
@@ -120,13 +120,13 @@ namespace Js
120120
}
121121

122122
template<typename TypeName>
123-
inline Var GetValueWithCheck(uint32 byteOffset, char16* funcName, BOOL isLittleEndian = FALSE)
123+
inline Var GetValueWithCheck(uint32 byteOffset, const char16* funcName, BOOL isLittleEndian = FALSE)
124124
{
125125
return GetValueWithCheck<TypeName, TypeName*>(byteOffset, isLittleEndian, funcName);
126126
}
127127

128128
template<typename TypeName, typename PointerAccessTypeName>
129-
Var GetValueWithCheck(uint32 byteOffset, BOOL isLittleEndian, char16* funcName)
129+
Var GetValueWithCheck(uint32 byteOffset, BOOL isLittleEndian, const char16* funcName)
130130
{
131131
ScriptContext* scriptContext = GetScriptContext();
132132
if (this->GetArrayBuffer()->IsDetached())
@@ -154,13 +154,13 @@ namespace Js
154154
}
155155

156156
template<typename TypeName>
157-
inline void SetValue(uint32 byteOffset, TypeName value, char16 *funcName, BOOL isLittleEndian = FALSE)
157+
inline void SetValue(uint32 byteOffset, TypeName value, const char16 *funcName, BOOL isLittleEndian = FALSE)
158158
{
159159
SetValue<TypeName, TypeName*>(byteOffset, value, isLittleEndian, funcName);
160160
}
161161

162162
template<typename TypeName, typename PointerAccessTypeName>
163-
void SetValue(uint32 byteOffset, TypeName value, BOOL isLittleEndian, char16 *funcName)
163+
void SetValue(uint32 byteOffset, TypeName value, BOOL isLittleEndian, const char16 *funcName)
164164
{
165165
ScriptContext* scriptContext = GetScriptContext();
166166
if (this->GetArrayBuffer()->IsDetached())
@@ -188,10 +188,10 @@ namespace Js
188188
#ifdef _M_ARM
189189
// For ARM, memory access for float/double address causes data alignment exception if the address is not aligned.
190190
// Provide template specialization (only) for these scenarios.
191-
template<> Var GetValueWithCheck<float>(uint32 byteOffset, char16 *funcName, BOOL isLittleEndian /* = FALSE */);
192-
template<> Var GetValueWithCheck<double>(uint32 byteOffset, char16 *funcName, BOOL isLittleEndian /* = FALSE */);
193-
template<> void SetValue<float>(uint32 byteOffset, float value, char16 *funcName, BOOL isLittleEndian /* = FALSE */);
194-
template<> void SetValue<double>(uint32 byteOffset, double value, char16 *funcName, BOOL isLittleEndian /* = FALSE */);
191+
template<> Var GetValueWithCheck<float>(uint32 byteOffset, const char16 *funcName, BOOL isLittleEndian /* = FALSE */);
192+
template<> Var GetValueWithCheck<double>(uint32 byteOffset, const char16 *funcName, BOOL isLittleEndian /* = FALSE */);
193+
template<> void SetValue<float>(uint32 byteOffset, float value, const char16 *funcName, BOOL isLittleEndian /* = FALSE */);
194+
template<> void SetValue<double>(uint32 byteOffset, double value, const char16 *funcName, BOOL isLittleEndian /* = FALSE */);
195195
#endif
196196

197197
uint32 byteOffset;

lib/Runtime/Library/DateImplementation.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
44
//-------------------------------------------------------------------------------------------------------
55
#include "RuntimeLibraryPch.h"
6-
#include <time.h>
76
#include "DateImplementationData.h"
87

98
#include "CharClassifier.h"
@@ -32,7 +31,7 @@ namespace Js {
3231

3332
struct SZS
3433
{
35-
char16 *psz; // string
34+
const char16 *psz; // string
3635
short cch; // length of string
3736
short szst; // type of entry
3837
long lwVal; // value
@@ -369,6 +368,7 @@ namespace Js {
369368
return DateImplementation::GetDateDefaultString(&ymd, &tzd, 0, scriptContext);
370369
}
371370

371+
#ifdef ENABLE_GLOBALIZATION
372372
JavascriptString*
373373
DateImplementation::GetDateDefaultString(Js::YMD *pymd, TZD *ptzd,DateTimeFlag noDateTime,ScriptContext* scriptContext)
374374
{
@@ -378,6 +378,7 @@ namespace Js {
378378
return CompoundString::NewWithCharCapacity(capacity, scriptContext->GetLibrary());
379379
});
380380
}
381+
#endif // ENABLE_GLOBALIZATION
381382

382383
JavascriptString*
383384
DateImplementation::GetDateGmtString(Js::YMD *pymd,ScriptContext* scriptContext)
@@ -434,6 +435,7 @@ namespace Js {
434435
return bs;
435436
}
436437

438+
#ifdef ENABLE_GLOBALIZATION
437439
JavascriptString*
438440
DateImplementation::GetDateLocaleString(Js::YMD *pymd, TZD *ptzd, DateTimeFlag noDateTime,ScriptContext* scriptContext)
439441
{
@@ -565,6 +567,7 @@ namespace Js {
565567

566568
return bs;
567569
}
570+
#endif // ENABLE_GLOBALIZATION
568571

569572
double
570573
DateImplementation::GetDateData(DateData dd, bool fUtc, ScriptContext* scriptContext)

0 commit comments

Comments
 (0)