Skip to content

Commit d686375

Browse files
committed
Add JsDeserializeParserState method to deserialize parser state without executing it
1 parent 3cfff2c commit d686375

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

bin/ChakraCore/ChakraCore.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ JsSetHostPromiseRejectionTracker
6868
JsGetProxyProperties
6969
JsSerializeParserState
7070
JsRunScriptWithParserState
71+
JsDeserializeParserState
7172
JsGetPromiseState
7273
JsGetPromiseResult
7374

lib/Jsrt/ChakraCore.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,46 @@ CHAKRA_API
15821582
_In_ JsValueRef parserState,
15831583
_Out_ JsValueRef * result);
15841584

1585+
/// <summary>
1586+
/// Deserializes the cache of initial parser state and (along with the same
1587+
/// script source) returns a function representing that script.
1588+
/// </summary>
1589+
/// <remarks>
1590+
/// <para>
1591+
/// Requires an active script context.
1592+
/// </para>
1593+
/// <para>
1594+
/// Script source can be either JavascriptString or JavascriptExternalArrayBuffer.
1595+
/// In case it is an ExternalArrayBuffer, and the encoding of the buffer is Utf16,
1596+
/// JsParseScriptAttributeArrayBufferIsUtf16Encoded is expected on parseAttributes.
1597+
/// </para>
1598+
/// <para>
1599+
/// Use JavascriptExternalArrayBuffer with Utf8/ASCII script source
1600+
/// for better performance and smaller memory footprint.
1601+
/// </para>
1602+
/// </remarks>
1603+
/// <param name="script">The script to deserialize.</param>
1604+
/// <param name="sourceContext">
1605+
/// A cookie identifying the script that can be used by debuggable script contexts.
1606+
/// </param>
1607+
/// <param name="sourceUrl">The location the script came from</param>
1608+
/// <param name="parseAttributes">Attribute mask for parsing the script</param>
1609+
/// <param name="parserState">
1610+
/// A buffer containing a cache of the parser state generated by <c>JsSerializeParserState</c>.
1611+
/// </param>
1612+
/// <param name="result">A function representing the script. This parameter can be null.</param>
1613+
/// <returns>
1614+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
1615+
/// </returns>
1616+
CHAKRA_API
1617+
JsDeserializeParserState(
1618+
_In_ JsValueRef script,
1619+
_In_ JsSourceContext sourceContext,
1620+
_In_ JsValueRef sourceUrl,
1621+
_In_ JsParseScriptAttributes parseAttributes,
1622+
_In_ JsValueRef parserState,
1623+
_Out_ JsValueRef* result);
1624+
15851625
typedef void (CHAKRA_CALLBACK *JsBeforeSweepCallback)(_In_opt_ void *callbackState);
15861626

15871627
CHAKRA_API

lib/Jsrt/Jsrt.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ CHAKRA_API RunScriptWithParserStateCore(
3030
_In_ WCHAR *url,
3131
_In_ JsParseScriptAttributes parseAttributes,
3232
_In_ JsValueRef parserState,
33+
_In_ bool parseOnly,
3334
_Out_ JsValueRef *result
3435
);
3536

@@ -5370,7 +5371,6 @@ CHAKRA_API JsSerializeParserState(
53705371
return errorCode;
53715372
}
53725373

5373-
53745374
static bool CHAKRA_CALLBACK DummyScriptLoadSourceCallbackForRunScriptWithParserState(
53755375
JsSourceContext sourceContext,
53765376
_Out_ JsValueRef *value,
@@ -5388,6 +5388,7 @@ CHAKRA_API RunScriptWithParserStateCore(
53885388
_In_ WCHAR *url,
53895389
_In_ JsParseScriptAttributes parseAttributes,
53905390
_In_ JsValueRef parserState,
5391+
_In_ bool parseOnly,
53915392
_Out_ JsValueRef *result
53925393
)
53935394
{
@@ -5480,7 +5481,7 @@ CHAKRA_API RunScriptWithParserStateCore(
54805481
return RunSerializedScriptCore(
54815482
dummy, DummyScriptUnloadCallback,
54825483
sourceContext, // use the same user provided sourceContext as scriptLoadSourceContext
5483-
buffer, arrayBuffer, sourceContext, url, dwBgParseCookie, false, true, result, sourceIndex);
5484+
buffer, arrayBuffer, sourceContext, url, dwBgParseCookie, parseOnly, true, result, sourceIndex);
54845485
}
54855486

54865487
CHAKRA_API JsRunScriptWithParserState(
@@ -5495,14 +5496,33 @@ CHAKRA_API JsRunScriptWithParserState(
54955496
if (sourceUrl && Js::VarIs<Js::JavascriptString>(sourceUrl))
54965497
{
54975498
url = const_cast<WCHAR*>(((Js::JavascriptString*)(sourceUrl))->GetSz());
5498-
return RunScriptWithParserStateCore(0, script, sourceContext, url, parseAttributes, parserState, result);
5499+
return RunScriptWithParserStateCore(0, script, sourceContext, url, parseAttributes, parserState, false, result);
54995500
}
55005501
else
55015502
{
55025503
return JsErrorInvalidArgument;
55035504
}
55045505
}
55055506

5507+
CHAKRA_API JsDeserializeParserState(
5508+
_In_ JsValueRef script,
5509+
_In_ JsSourceContext sourceContext,
5510+
_In_ JsValueRef sourceUrl,
5511+
_In_ JsParseScriptAttributes parseAttributes,
5512+
_In_ JsValueRef parserState,
5513+
_Out_ JsValueRef * result)
5514+
{
5515+
WCHAR *url = nullptr;
5516+
if (sourceUrl && Js::VarIs<Js::JavascriptString>(sourceUrl))
5517+
{
5518+
url = const_cast<WCHAR*>(((Js::JavascriptString*)(sourceUrl))->GetSz());
5519+
return RunScriptWithParserStateCore(0, script, sourceContext, url, parseAttributes, parserState, true, result);
5520+
}
5521+
else
5522+
{
5523+
return JsErrorInvalidArgument;
5524+
}
5525+
}
55065526

55075527
CHAKRA_API
55085528
JsExecuteBackgroundParse_Experimental(
@@ -5524,6 +5544,7 @@ JsExecuteBackgroundParse_Experimental(
55245544
url,
55255545
parseAttributes,
55265546
parserState,
5547+
false,
55275548
result
55285549
);
55295550
}

0 commit comments

Comments
 (0)