Skip to content

Commit

Permalink
[MERGE #5808 @boingoing] Add JsDeserializeParserState method to deser…
Browse files Browse the repository at this point in the history
…ialize parser state without executing it

Merge pull request #5808 from boingoing:add_JsDeserializeParserState
  • Loading branch information
boingoing committed Feb 8, 2019
2 parents 3cfff2c + d686375 commit 9c289b8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions bin/ChakraCore/ChakraCore.def
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ JsSetHostPromiseRejectionTracker
JsGetProxyProperties
JsSerializeParserState
JsRunScriptWithParserState
JsDeserializeParserState
JsGetPromiseState
JsGetPromiseResult

Expand Down
40 changes: 40 additions & 0 deletions lib/Jsrt/ChakraCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,46 @@ CHAKRA_API
_In_ JsValueRef parserState,
_Out_ JsValueRef * result);

/// <summary>
/// Deserializes the cache of initial parser state and (along with the same
/// script source) returns a function representing that script.
/// </summary>
/// <remarks>
/// <para>
/// Requires an active script context.
/// </para>
/// <para>
/// Script source can be either JavascriptString or JavascriptExternalArrayBuffer.
/// In case it is an ExternalArrayBuffer, and the encoding of the buffer is Utf16,
/// JsParseScriptAttributeArrayBufferIsUtf16Encoded is expected on parseAttributes.
/// </para>
/// <para>
/// Use JavascriptExternalArrayBuffer with Utf8/ASCII script source
/// for better performance and smaller memory footprint.
/// </para>
/// </remarks>
/// <param name="script">The script to deserialize.</param>
/// <param name="sourceContext">
/// A cookie identifying the script that can be used by debuggable script contexts.
/// </param>
/// <param name="sourceUrl">The location the script came from</param>
/// <param name="parseAttributes">Attribute mask for parsing the script</param>
/// <param name="parserState">
/// A buffer containing a cache of the parser state generated by <c>JsSerializeParserState</c>.
/// </param>
/// <param name="result">A function representing the script. This parameter can be null.</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
/// </returns>
CHAKRA_API
JsDeserializeParserState(
_In_ JsValueRef script,
_In_ JsSourceContext sourceContext,
_In_ JsValueRef sourceUrl,
_In_ JsParseScriptAttributes parseAttributes,
_In_ JsValueRef parserState,
_Out_ JsValueRef* result);

typedef void (CHAKRA_CALLBACK *JsBeforeSweepCallback)(_In_opt_ void *callbackState);

CHAKRA_API
Expand Down
27 changes: 24 additions & 3 deletions lib/Jsrt/Jsrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CHAKRA_API RunScriptWithParserStateCore(
_In_ WCHAR *url,
_In_ JsParseScriptAttributes parseAttributes,
_In_ JsValueRef parserState,
_In_ bool parseOnly,
_Out_ JsValueRef *result
);

Expand Down Expand Up @@ -5370,7 +5371,6 @@ CHAKRA_API JsSerializeParserState(
return errorCode;
}


static bool CHAKRA_CALLBACK DummyScriptLoadSourceCallbackForRunScriptWithParserState(
JsSourceContext sourceContext,
_Out_ JsValueRef *value,
Expand All @@ -5388,6 +5388,7 @@ CHAKRA_API RunScriptWithParserStateCore(
_In_ WCHAR *url,
_In_ JsParseScriptAttributes parseAttributes,
_In_ JsValueRef parserState,
_In_ bool parseOnly,
_Out_ JsValueRef *result
)
{
Expand Down Expand Up @@ -5480,7 +5481,7 @@ CHAKRA_API RunScriptWithParserStateCore(
return RunSerializedScriptCore(
dummy, DummyScriptUnloadCallback,
sourceContext, // use the same user provided sourceContext as scriptLoadSourceContext
buffer, arrayBuffer, sourceContext, url, dwBgParseCookie, false, true, result, sourceIndex);
buffer, arrayBuffer, sourceContext, url, dwBgParseCookie, parseOnly, true, result, sourceIndex);
}

CHAKRA_API JsRunScriptWithParserState(
Expand All @@ -5495,14 +5496,33 @@ CHAKRA_API JsRunScriptWithParserState(
if (sourceUrl && Js::VarIs<Js::JavascriptString>(sourceUrl))
{
url = const_cast<WCHAR*>(((Js::JavascriptString*)(sourceUrl))->GetSz());
return RunScriptWithParserStateCore(0, script, sourceContext, url, parseAttributes, parserState, result);
return RunScriptWithParserStateCore(0, script, sourceContext, url, parseAttributes, parserState, false, result);
}
else
{
return JsErrorInvalidArgument;
}
}

CHAKRA_API JsDeserializeParserState(
_In_ JsValueRef script,
_In_ JsSourceContext sourceContext,
_In_ JsValueRef sourceUrl,
_In_ JsParseScriptAttributes parseAttributes,
_In_ JsValueRef parserState,
_Out_ JsValueRef * result)
{
WCHAR *url = nullptr;
if (sourceUrl && Js::VarIs<Js::JavascriptString>(sourceUrl))
{
url = const_cast<WCHAR*>(((Js::JavascriptString*)(sourceUrl))->GetSz());
return RunScriptWithParserStateCore(0, script, sourceContext, url, parseAttributes, parserState, true, result);
}
else
{
return JsErrorInvalidArgument;
}
}

CHAKRA_API
JsExecuteBackgroundParse_Experimental(
Expand All @@ -5524,6 +5544,7 @@ JsExecuteBackgroundParse_Experimental(
url,
parseAttributes,
parserState,
false,
result
);
}
Expand Down

0 comments on commit 9c289b8

Please sign in to comment.