@@ -196,97 +196,62 @@ HANDLE GetFileHandle(LPCWSTR filename)
196
196
return GetStdHandle (STD_OUTPUT_HANDLE);
197
197
}
198
198
199
- HRESULT CreateLibraryByteCodeHeader (LPCSTR contentsRaw, JsFinalizeCallback contentsRawFinalizeCallback, DWORD lengthBytes, LPCWSTR bcFullPath, LPCSTR libraryNameNarrow )
199
+ HRESULT CreateLibraryByteCode ( const char * contentsRaw )
200
200
{
201
- HANDLE bcFileHandle = nullptr ;
202
201
JsValueRef bufferVal;
203
202
BYTE *bcBuffer = nullptr ;
204
203
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
242
223
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));
244
227
245
228
// 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 */" );
251
230
252
231
for (unsigned int i = 0 ; i < bcBufferSize; i++)
253
232
{
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
261
235
if (i < bcBufferSize - 1 )
262
236
{
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 (" ," );
266
238
}
267
239
268
240
// Add a line break every 16 scratches, primarily so the compiler doesn't complain about the string being too long.
269
241
// Also, won't add for the last scratch
270
242
if (i % 16 == 15 && i < bcBufferSize - 1 )
271
243
{
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 );
275
245
}
276
246
}
277
- outputStr = " };\n\n " ;
278
- IfFalseGo (WriteFile (bcFileHandle, outputStr, (DWORD)strlen (outputStr), &written, nullptr ));
247
+ print (" };\n\n " );
279
248
280
- outputStr = " } \n " ;
281
- IfFalseGo ( WriteFile (bcFileHandle, outputStr, (DWORD) strlen (outputStr), &written, nullptr ));
249
+ # undef print
250
+ # undef print_format
282
251
283
- ErrorRunFinalize:
284
- Error:
285
- if (bcFileHandle != nullptr )
286
- {
287
- CloseHandle (bcFileHandle);
288
- }
252
+ hr = S_OK;
289
253
254
+ Error:
290
255
return hr;
291
256
}
292
257
@@ -933,19 +898,7 @@ HRESULT ExecuteTest(const char* fileName)
933
898
len = strlen (fullPath);
934
899
if (HostConfigFlags::flags.GenerateLibraryByteCodeHeaderIsEnabled )
935
900
{
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));
949
902
}
950
903
else if (HostConfigFlags::flags.SerializedIsEnabled )
951
904
{
0 commit comments