Created
August 30, 2016 17:49
-
-
Save mattpodwysocki/9b738b7ef2ff123cc622b50db291eeba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JsErrorCode LoadByteCode(const wchar_t* szPath, BYTE** pData) | |
{ | |
FILE *file; | |
*pData = nullptr; | |
if (_wfopen_s(&file, szPath, L"rb")) | |
{ | |
return JsErrorInvalidArgument; | |
} | |
unsigned int current = ftell(file); | |
fseek(file, 0, SEEK_END); | |
unsigned int end = ftell(file); | |
fseek(file, current, SEEK_SET); | |
unsigned int lengthBytes = end - current; | |
*pData = new BYTE[lengthBytes]; | |
fread(*pData, sizeof(BYTE), lengthBytes, file); | |
if (fclose(file)) | |
{ | |
return JsErrorFatal; | |
} | |
return JsNoError; | |
} | |
JsErrorCode LoadFileContents(const wchar_t* szPath, wchar_t** pszData) | |
{ | |
FILE *file; | |
*pszData = nullptr; | |
if (_wfopen_s(&file, szPath, L"rb")) | |
{ | |
return JsErrorInvalidArgument; | |
} | |
unsigned int current = ftell(file); | |
fseek(file, 0, SEEK_END); | |
unsigned int end = ftell(file); | |
fseek(file, current, SEEK_SET); | |
unsigned int lengthBytes = end - current; | |
char *rawBytes = (char *)calloc(lengthBytes + 1, sizeof(char)); | |
if (rawBytes == nullptr) | |
{ | |
return JsErrorFatal; | |
} | |
fread(rawBytes, sizeof(char), lengthBytes, file); | |
if (fclose(file)) | |
{ | |
return JsErrorFatal; | |
} | |
*pszData = (wchar_t *)calloc(lengthBytes + 1, sizeof(wchar_t)); | |
if (*pszData == nullptr) | |
{ | |
free(rawBytes); | |
return JsErrorFatal; | |
} | |
if (MultiByteToWideChar(CP_UTF8, 0, rawBytes, lengthBytes + 1, *pszData, lengthBytes + 1) == 0) | |
{ | |
free(*pszData); | |
free(rawBytes); | |
return JsErrorFatal; | |
} | |
return JsNoError; | |
} | |
bool CompareLastWrite(const wchar_t* szPath1, const wchar_t* szPath2) | |
{ | |
HANDLE hPath1, hPath2; | |
FILETIME ftPath1Create, ftPath1Access, ftPath1Write; | |
FILETIME ftPath2Create, ftPath2Access, ftPath2Write; | |
hPath1 = CreateFile2(szPath1, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr); | |
if (hPath1 == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND) { | |
return false; | |
} | |
hPath2 = CreateFile2(szPath2, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr); | |
GetFileTime(hPath1, &ftPath1Create, &ftPath1Access, &ftPath1Write); | |
GetFileTime(hPath2, &ftPath2Create, &ftPath2Access, &ftPath2Write); | |
CloseHandle(hPath1); | |
CloseHandle(hPath2); | |
return CompareFileTime(&ftPath1Write, &ftPath2Write) == 1; | |
} | |
JsErrorCode ChakraHost::RunSerializedScriptFromFile(const wchar_t* szPath, const wchar_t* szSerializedPath, const wchar_t* szSourceUri, JsValueRef* result) | |
{ | |
JsErrorCode status = JsNoError; | |
ULONG bufferSize = 0L; | |
BYTE* buffer = nullptr; | |
wchar_t* szScriptBuffer = nullptr; | |
IfFailCleanup(LoadFileContents(szPath, &szScriptBuffer)); | |
if (!CompareLastWrite(szSerializedPath, szPath)) | |
{ | |
IfFailCleanup(JsSerializeScript(szScriptBuffer, buffer, &bufferSize)); | |
buffer = new BYTE[bufferSize]; | |
IfFailCleanup(JsSerializeScript(szScriptBuffer, buffer, &bufferSize)); | |
FILE* file; | |
_wfopen_s(&file, szSerializedPath, L"wb"); | |
fwrite(buffer, sizeof(BYTE), bufferSize, file); | |
fclose(file); | |
} | |
else | |
{ | |
IfFailCleanup(LoadByteCode(szSerializedPath, &buffer)); | |
} | |
IfFailCleanup(JsRunSerializedScript(szScriptBuffer, buffer, currentSourceContext++, szSourceUri, result)); | |
cleanup: | |
if (buffer != nullptr) | |
{ | |
delete[] buffer; | |
} | |
if (szScriptBuffer != nullptr) | |
{ | |
free(szScriptBuffer); | |
} | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment