Skip to content

Commit eb0dcbe

Browse files
author
Thomas Moore
committed
Implement JsExecuteBackgroundParse
This change enables execution of background-parsed script. It is built upon the serialization and parser state JSRT APIs. With this change, there are several memory leaks that will be addressed in a subsequent commit.
1 parent f113fa7 commit eb0dcbe

File tree

8 files changed

+268
-91
lines changed

8 files changed

+268
-91
lines changed

bin/ChakraCore/ChakraCore.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ JsSerializeParserState
7070
JsRunScriptWithParserState
7171
JsGetPromiseState
7272
JsGetPromiseResult
73+
74+
JsQueueBackgroundParse
75+
JsDiscardBackgroundParse
76+
JsExecuteBackgroundParse

bin/ch/ChakraRtInterface.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ bool ChakraRTInterface::LoadChakraDll(ArgInfo* argInfo, HINSTANCE *outLibrary)
160160
m_jsApiHooks.pfJsrtSerializeParserState = (JsAPIHooks::JsrtSerializeParserState)GetChakraCoreSymbol(library, "JsSerializeParserState");
161161
m_jsApiHooks.pfJsrtRunScriptWithParserState = (JsAPIHooks::JsrtRunScriptWithParserState)GetChakraCoreSymbol(library, "JsRunScriptWithParserState");
162162

163+
m_jsApiHooks.pfJsrtQueueBackgroundParse = (JsAPIHooks::JsrtQueueBackgroundParse)GetChakraCoreSymbol(library, "JsQueueBackgroundParse");
164+
m_jsApiHooks.pfJsrtDiscardBackgroundParse = (JsAPIHooks::JsrtDiscardBackgroundParse)GetChakraCoreSymbol(library, "JsDiscardBackgroundParse");
165+
m_jsApiHooks.pfJsrtExecuteBackgroundParse = (JsAPIHooks::JsrtExecuteBackgroundParse)GetChakraCoreSymbol(library, "JsExecuteBackgroundParse");
166+
167+
168+
163169
m_jsApiHooks.pfJsrtTTDCreateRecordRuntime = (JsAPIHooks::JsrtTTDCreateRecordRuntimePtr)GetChakraCoreSymbol(library, "JsTTDCreateRecordRuntime");
164170
m_jsApiHooks.pfJsrtTTDCreateReplayRuntime = (JsAPIHooks::JsrtTTDCreateReplayRuntimePtr)GetChakraCoreSymbol(library, "JsTTDCreateReplayRuntime");
165171
m_jsApiHooks.pfJsrtTTDCreateContext = (JsAPIHooks::JsrtTTDCreateContextPtr)GetChakraCoreSymbol(library, "JsTTDCreateContext");

bin/ch/ChakraRtInterface.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ struct JsAPIHooks
9292

9393
typedef JsErrorCode(WINAPI *JsrtSerializeParserState)(JsValueRef script, JsValueRef *buffer, JsParseScriptAttributes parseAttributes);
9494
typedef JsErrorCode(WINAPI *JsrtRunScriptWithParserState)(JsValueRef script, JsSourceContext sourceContext, JsValueRef sourceUrl, JsParseScriptAttributes parseAttributes, JsValueRef parserState, JsValueRef *result);
95+
96+
typedef JsErrorCode(WINAPI *JsrtQueueBackgroundParse)(JsScriptContents* contents, DWORD* dwBgParseCookie);
97+
typedef JsErrorCode(WINAPI *JsrtDiscardBackgroundParse)(DWORD dwBgParseCookie, void* buffer, bool* callerOwnsBuffer);
98+
typedef JsErrorCode(WINAPI *JsrtExecuteBackgroundParse)(DWORD dwBgParseCookie, JsValueRef script, JsSourceContext sourceContext, WCHAR *url, JsParseScriptAttributes parseAttributes, JsValueRef parserState, JsValueRef *result);
9599

96100
typedef JsErrorCode(WINAPI *JsrtTTDCreateRecordRuntimePtr)(JsRuntimeAttributes attributes, bool enableDebugging, size_t snapInterval, size_t snapHistoryLength, TTDOpenResourceStreamCallback openResourceStream, JsTTDWriteBytesToStreamCallback writeBytesToStream, JsTTDFlushAndCloseStreamCallback flushAndCloseStream, JsThreadServiceCallback threadService, JsRuntimeHandle *runtime);
97101
typedef JsErrorCode(WINAPI *JsrtTTDCreateReplayRuntimePtr)(JsRuntimeAttributes attributes, const char* infoUri, size_t infoUriCount, bool enableDebugging, TTDOpenResourceStreamCallback openResourceStream, JsTTDReadBytesFromStreamCallback readBytesFromStream, JsTTDFlushAndCloseStreamCallback flushAndCloseStream, JsThreadServiceCallback threadService, JsRuntimeHandle *runtime);
@@ -195,6 +199,10 @@ struct JsAPIHooks
195199
JsrtSerializeParserState pfJsrtSerializeParserState;
196200
JsrtRunScriptWithParserState pfJsrtRunScriptWithParserState;
197201

202+
JsrtQueueBackgroundParse pfJsrtQueueBackgroundParse;
203+
JsrtDiscardBackgroundParse pfJsrtDiscardBackgroundParse;
204+
JsrtExecuteBackgroundParse pfJsrtExecuteBackgroundParse;
205+
198206
JsrtTTDCreateRecordRuntimePtr pfJsrtTTDCreateRecordRuntime;
199207
JsrtTTDCreateReplayRuntimePtr pfJsrtTTDCreateReplayRuntime;
200208
JsrtTTDCreateContextPtr pfJsrtTTDCreateContext;
@@ -430,6 +438,10 @@ class ChakraRTInterface
430438

431439
static JsErrorCode WINAPI JsSerializeParserState(JsValueRef script, JsValueRef *buffer, JsParseScriptAttributes parseAttributes) { return HOOK_JS_API(SerializeParserState(script, buffer, parseAttributes)); }
432440
static JsErrorCode WINAPI JsRunScriptWithParserState(JsValueRef script, JsSourceContext sourceContext, JsValueRef sourceUrl, JsParseScriptAttributes parseAttributes, JsValueRef parserState, JsValueRef * result) { return HOOK_JS_API(RunScriptWithParserState(script, sourceContext, sourceUrl, parseAttributes, parserState, result)); }
441+
442+
static JsErrorCode WINAPI JsQueueBackgroundParse(JsScriptContents* contents, DWORD* dwBgParseCookie) { return HOOK_JS_API(QueueBackgroundParse(contents, dwBgParseCookie)); }
443+
static JsErrorCode WINAPI JsDiscardBackgroundParse(DWORD dwBgParseCookie, void* buffer, bool* callerOwnsBuffer) { return HOOK_JS_API(DiscardBackgroundParse(dwBgParseCookie, buffer, callerOwnsBuffer)); }
444+
static JsErrorCode WINAPI JsExecuteBackgroundParse(DWORD dwBgParseCookie, JsValueRef script, JsSourceContext sourceContext, WCHAR *url, JsParseScriptAttributes parseAttributes, JsValueRef parserState, JsValueRef *result) { return HOOK_JS_API(ExecuteBackgroundParse(dwBgParseCookie, script, sourceContext, url, parseAttributes, parserState, result)); }
433445
};
434446

435447
class AutoRestoreContext

bin/ch/ch.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,49 @@ HRESULT CreateAndRunSerializedScript(const char* fileName, LPCSTR fileContents,
755755
return hr;
756756
}
757757

758+
// Use the asynchronous BGParse JSRT APIs in a synchronous call
759+
HRESULT RunBgParseSync(LPCSTR fileContents, UINT lengthBytes, const char* fileName)
760+
{
761+
JsValueRef scriptSource;
762+
JsErrorCode e = (ChakraRTInterface::JsCreateExternalArrayBuffer((void*)fileContents,
763+
(unsigned int)lengthBytes,
764+
nullptr, (void*)fileContents, &scriptSource));
765+
766+
// What's the preferred way of doing this?
767+
WCHAR fileNameWide[MAX_PATH] = { 0 };
768+
size_t fileNameLength = strlen(fileName);
769+
for (int i = 0; i < fileNameLength; i++)
770+
{
771+
fileNameWide[i] = fileName[i];
772+
}
773+
774+
775+
JsScriptContents scriptContents = { 0 };
776+
scriptContents.container = (LPVOID)fileContents;
777+
scriptContents.containerType = JsScriptContainerType::HeapAllocatedBuffer;
778+
scriptContents.encodingType = JsScriptEncodingType::Utf8;
779+
scriptContents.contentLengthInBytes = lengthBytes;
780+
scriptContents.fullPath = fileNameWide;
781+
782+
DWORD cookie = 0;
783+
e = ChakraRTInterface::JsQueueBackgroundParse(&scriptContents, &cookie);
784+
Assert(e == JsErrorCode::JsNoError);
785+
786+
JsValueRef bgResult = nullptr;
787+
e = ChakraRTInterface::JsExecuteBackgroundParse(
788+
cookie,
789+
scriptSource,
790+
WScriptJsrt::GetNextSourceContext(),
791+
(WCHAR*)scriptContents.fullPath,
792+
JsParseScriptAttributes::JsParseScriptAttributeNone,
793+
nullptr,//_In_ JsValueRef parserState,
794+
&bgResult
795+
);
796+
Assert(e == JsErrorCode::JsNoError);
797+
798+
return S_OK;
799+
}
800+
758801
HRESULT ExecuteTest(const char* fileName)
759802
{
760803
HRESULT hr = S_OK;
@@ -914,7 +957,7 @@ HRESULT ExecuteTest(const char* fileName)
914957
}
915958
else if (HostConfigFlags::flags.ExecuteWithBgParse)
916959
{
917-
// Run multiple scripts with BGParse
960+
RunBgParseSync(fileContents, lengthBytes, fileName);
918961
}
919962
else
920963
{

lib/Jsrt/ChakraCommon.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2441,7 +2441,13 @@ typedef unsigned short uint16_t;
24412441
JsDiscardBackgroundParse(DWORD dwBgParseCookie, void* buffer, bool* callerOwnsBuffer);
24422442

24432443
CHAKRA_API
2444-
JsExecuteBackgroundParse(DWORD dwBgParseCookie, JsContextRef context, DWORD_PTR dwSourceContext, DWORD dwFlags, VARIANT* pvarResult, EXCEPINFO* pexcepinfo);
2444+
JsExecuteBackgroundParse(_In_ DWORD dwBgParseCookie,
2445+
_In_ JsValueRef script,
2446+
_In_ JsSourceContext sourceContext,
2447+
_In_ WCHAR *url,
2448+
_In_ JsParseScriptAttributes parseAttributes,
2449+
_In_ JsValueRef parserState,
2450+
_Out_ JsValueRef *result);
24452451

24462452

24472453
#ifdef _WIN32

0 commit comments

Comments
 (0)