private RavenJObject ApplySingleScript(RavenJObject doc, ScriptedPatchRequest patch, int size, string docId)
{
JintEngine jintEngine;
try
{
jintEngine = scriptsCache.CheckoutScript(CreateEngine, patch);
}
catch (NotSupportedException e)
{
throw new ParseException("Could not parse script", e);
}
catch (JintException e)
{
throw new ParseException("Could not parse script", e);
}
catch (Exception e)
{
throw new ParseException("Could not parse: " + Environment.NewLine + patch.Script, e);
}
loadDocumentStatic = loadDocument;
try
{
CustomizeEngine(jintEngine);
jintEngine.SetFunction("PutDocument", ((Action<string, JsObject, JsObject>) (PutDocument)));
jintEngine.SetParameter("__document_id", docId);
foreach (var kvp in patch.Values)
{
var token = kvp.Value as RavenJToken;
if (token != null)
{
jintEngine.SetParameter(kvp.Key, ToJsInstance(jintEngine.Global, token));
}
else
{
var rjt = RavenJToken.FromObject(kvp.Value);
var jsInstance = ToJsInstance(jintEngine.Global, rjt);
jintEngine.SetParameter(kvp.Key, jsInstance);
}
}
var jsObject = ToJsObject(jintEngine.Global, doc);
jintEngine.ResetSteps();
if (size != 0)
{
jintEngine.SetMaxSteps(maxSteps + (size*additionalStepsPerSize));
}
jintEngine.CallFunction("ExecutePatchScript", jsObject);
foreach (var kvp in patch.Values)
{
jintEngine.RemoveParameter(kvp.Key);
}
jintEngine.RemoveParameter("__document_id");
RemoveEngineCustomizations(jintEngine);
OutputLog(jintEngine);
scriptsCache.CheckinScript(patch, jintEngine);
return ConvertReturnValue(jsObject);
}
catch (ConcurrencyException)
{
throw;
}
catch (Exception errorEx)
{
OutputLog(jintEngine);
var errorMsg = "Unable to execute JavaScript: " + Environment.NewLine + patch.Script;
var error = errorEx as JsException;
if (error != null)
errorMsg += Environment.NewLine + "Error: " + Environment.NewLine + string.Join(Environment.NewLine, error.Value);
if (Debug.Count != 0)
errorMsg += Environment.NewLine + "Debug information: " + Environment.NewLine +
string.Join(Environment.NewLine, Debug);
throw new InvalidOperationException(errorMsg, errorEx);
}
finally
{
loadDocumentStatic = null;
}
}