Skip to content

Commit b1e8735

Browse files
committed
New Bytecode regen process:
1. Use python on windows and macOS/Linux 2. Suport multiple files of BuiltIns 3. Move file structure from ch.cpp into python 4. Delete defunct .cmd and .ps1 scripts 5. Delete defunct tests 6. Add testing via new python script and wire into CI 7. Add a noJit build on macOS to ensure noJit bytecode is tested
1 parent e367380 commit b1e8735

14 files changed

+375
-599
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
test/**/*.js -crlf
44
test/es6/HTMLComments.js binary diff=cpp
55
*.wasm binary
6+
lib/**/*.js eol=lf diff=cpp
67
*.cpp text eol=lf diff=cpp
78
*.h text eol=lf diff=cpp
89
*.inl text eol=lf diff=cpp

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ if(NO_JIT_SH)
466466
unset(NO_JIT_SH CACHE) # don't cache
467467
unset(BuildJIT CACHE) # also clear it just in case
468468
add_definitions(-DDISABLE_JIT=1)
469+
elseif(DISABLE_JIT)
470+
set(BuildJIT 0)
471+
add_definitions(-DDISABLE_JIT=1)
469472
else()
470473
set(BuildJIT 1)
471474
endif()

RegenAllByteCode.cmd

-84
This file was deleted.

RegenAllByteCodeNoBuild.cmd

-36
This file was deleted.

azure-pipelines.yml

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ jobs:
5050
build_type: 'Release'
5151
do_test: false
5252
libtype_flag: ''
53+
OSX.noJit:
54+
image_name: 'macOS-latest'
55+
deps: 'brew install ninja icu4c'
56+
build_type: 'Debug'
57+
do_test: true
58+
libtype_flag: '-DSTATIC_LIBRARY=ON -DDISABLE_JIT=1'
5359
OSX.Debug:
5460
image_name: 'macOS-latest'
5561
deps: 'brew install ninja icu4c'

bin/ch/ch.cpp

+34-81
Original file line numberDiff line numberDiff line change
@@ -196,97 +196,62 @@ HANDLE GetFileHandle(LPCWSTR filename)
196196
return GetStdHandle(STD_OUTPUT_HANDLE);
197197
}
198198

199-
HRESULT CreateLibraryByteCodeHeader(LPCSTR contentsRaw, JsFinalizeCallback contentsRawFinalizeCallback, DWORD lengthBytes, LPCWSTR bcFullPath, LPCSTR libraryNameNarrow)
199+
HRESULT CreateLibraryByteCode(const char* contentsRaw)
200200
{
201-
HANDLE bcFileHandle = nullptr;
202201
JsValueRef bufferVal;
203202
BYTE *bcBuffer = nullptr;
204203
unsigned int bcBufferSize = 0;
205-
DWORD written;
206-
// For validating the header file against the library file
207-
auto outputStr =
208-
"//-------------------------------------------------------------------------------------------------------\n"
209-
"// Copyright (C) Microsoft. All rights reserved.\n"
210-
"// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.\n"
211-
"//-------------------------------------------------------------------------------------------------------\n"
212-
"#if 0\n";
213-
214-
std::string normalizedContentStr;
215-
char* nextToken = nullptr;
216-
char* token = strtok_s((char*)contentsRaw, "\r", &nextToken);
217-
while (token)
218-
{
219-
normalizedContentStr.append(token);
220-
token = strtok_s(nullptr, "\r", &nextToken);
221-
}
222-
// We no longer need contentsRaw, so call the finalizer for it if one was provided
223-
if (contentsRawFinalizeCallback != nullptr)
224-
{
225-
contentsRawFinalizeCallback((void*)contentsRaw);
226-
}
227-
228-
const char* normalizedContent = normalizedContentStr.c_str();
229-
// We still need contentsRaw after this, so pass a null finalizeCallback into it
230-
HRESULT hr = GetSerializedBuffer(normalizedContent, nullptr, &bufferVal);
231-
232-
IfFailedGoLabel((hr), ErrorRunFinalize);
233-
234-
IfJsrtErrorHRLabel(ChakraRTInterface::JsGetArrayBufferStorage(bufferVal, &bcBuffer, &bcBufferSize), ErrorRunFinalize);
235-
236-
bcFileHandle = GetFileHandle(bcFullPath);
237-
IfFalseGo(bcFileHandle != INVALID_HANDLE_VALUE && bcFileHandle != nullptr);
238-
239-
IfFalseGoLabel(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr), ErrorRunFinalize);
240-
IfFalseGoLabel(WriteFile(bcFileHandle, normalizedContent, (DWORD)normalizedContentStr.size(), &written, nullptr), ErrorRunFinalize);
241-
outputStr = "\n#endif\n";
204+
HRESULT hr = E_FAIL;
205+
206+
// Windows can't do the below with printf - so use windows API on windows but printf on posix
207+
#ifdef _WIN32
208+
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
209+
DWORD written = 0;
210+
#define print_format(format, element, size) \
211+
{ \
212+
auto scratchLen = size; \
213+
char scratch[size]; \
214+
int len = _snprintf_s(scratch, scratchLen, _countof(scratch), format, element); \
215+
IfFalseGo(WriteFile(out, scratch, (DWORD)(len), &written, nullptr)); \
216+
}
217+
#define print(text) \
218+
WriteFile(out, text, (DWORD)strlen(text), &written, nullptr);
219+
#else
220+
#define print_format(format, element, size) printf(format, element)
221+
#define print printf
222+
#endif
242223

243-
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
224+
// Generate the bytecode, free the original buffer then retrieve the generated bytecode
225+
IfFailGo(GetSerializedBuffer(contentsRaw, WScriptJsrt::FinalizeFree, &bufferVal));
226+
IfFailGo(ChakraRTInterface::JsGetArrayBufferStorage(bufferVal, &bcBuffer, &bcBufferSize));
244227

245228
// Write out the bytecode
246-
outputStr = "namespace Js\n{\n const char Library_Bytecode_";
247-
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
248-
IfFalseGo(WriteFile(bcFileHandle, libraryNameNarrow, (DWORD)strlen(libraryNameNarrow), &written, nullptr));
249-
outputStr = "[] = {\n/* 00000000 */";
250-
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
229+
print("[] = {\n/* 00000000 */");
251230

252231
for (unsigned int i = 0; i < bcBufferSize; i++)
253232
{
254-
char scratch[6];
255-
auto scratchLen = sizeof(scratch);
256-
int num = _snprintf_s(scratch, scratchLen, _countof(scratch), " 0x%02X", bcBuffer[i]);
257-
Assert(num == 5);
258-
IfFalseGo(WriteFile(bcFileHandle, scratch, (DWORD)(scratchLen - 1), &written, nullptr));
259-
260-
// Add a comma and a space if this is not the last item
233+
print_format(" 0x%02X", bcBuffer[i], 6);
234+
// Add a comma if this is not the last item
261235
if (i < bcBufferSize - 1)
262236
{
263-
char commaSpace[2];
264-
_snprintf_s(commaSpace, sizeof(commaSpace), _countof(commaSpace), ","); // close quote, new line, offset and open quote
265-
IfFalseGo(WriteFile(bcFileHandle, commaSpace, (DWORD)strlen(commaSpace), &written, nullptr));
237+
print(",");
266238
}
267239

268240
// Add a line break every 16 scratches, primarily so the compiler doesn't complain about the string being too long.
269241
// Also, won't add for the last scratch
270242
if (i % 16 == 15 && i < bcBufferSize - 1)
271243
{
272-
char offset[17];
273-
int actualLen = _snprintf_s(offset, sizeof(offset), _countof(offset), "\n/* %08X */", i + 1); // close quote, new line, offset and open quote
274-
IfFalseGo(WriteFile(bcFileHandle, offset, actualLen, &written, nullptr));
244+
print_format("\n/* %08X */", i + 1, 17);
275245
}
276246
}
277-
outputStr = "};\n\n";
278-
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
247+
print("};\n\n");
279248

280-
outputStr = "}\n";
281-
IfFalseGo(WriteFile(bcFileHandle, outputStr, (DWORD)strlen(outputStr), &written, nullptr));
249+
#undef print
250+
#undef print_format
282251

283-
ErrorRunFinalize:
284-
Error:
285-
if (bcFileHandle != nullptr)
286-
{
287-
CloseHandle(bcFileHandle);
288-
}
252+
hr = S_OK;
289253

254+
Error:
290255
return hr;
291256
}
292257

@@ -933,19 +898,7 @@ HRESULT ExecuteTest(const char* fileName)
933898
len = strlen(fullPath);
934899
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeaderIsEnabled)
935900
{
936-
937-
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeader != nullptr)
938-
{
939-
if (wcslen(HostConfigFlags::flags.GenerateLibraryByteCodeHeader) == 0)
940-
{
941-
HostConfigFlags::flags.GenerateLibraryByteCodeHeader = nullptr;
942-
}
943-
}
944-
CHAR libraryName[_MAX_PATH];
945-
CHAR ext[_MAX_EXT];
946-
_splitpath_s(fullPath, NULL, 0, NULL, 0, libraryName, _countof(libraryName), ext, _countof(ext));
947-
948-
IfFailGo(CreateLibraryByteCodeHeader(fileContents, WScriptJsrt::FinalizeFree, lengthBytes, HostConfigFlags::flags.GenerateLibraryByteCodeHeader, libraryName));
901+
IfFailGo(CreateLibraryByteCode(fileContents));
949902
}
950903
else if (HostConfigFlags::flags.SerializedIsEnabled)
951904
{

lib/Runtime/Library/InJavascript/GenByteCode.cmd

-85
This file was deleted.

0 commit comments

Comments
 (0)