@@ -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 {
0 commit comments