diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs index ca82fc159a..dd38aa7fc5 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs @@ -63,7 +63,7 @@ public async Task ExecuteAsync(ScriptVars vars, string script, Script context.Complete(JsonMapper.Map(value)); })); - var result = Execute(context.Engine, script); + var result = Execute(context, script); return await context.CompleteAsync() ?? JsonMapper.Map(result); } @@ -109,7 +109,7 @@ public async Task TransformAsync(DataScriptVars vars, string script } })); - Execute(context.Engine, script); + Execute(context, script); return await context.CompleteAsync() ?? vars.Data!; } @@ -131,7 +131,7 @@ public JsonValue Execute(ScriptVars vars, string script, ScriptOptions options = .Extend(vars, options) .Extend(extensions); - var result = Execute(context.Engine, script); + var result = Execute(context, script); return JsonMapper.Map(result); } @@ -179,14 +179,11 @@ private ScriptExecutionContext CreateEngine(ScriptOptions options, Cancell return new ScriptExecutionContext(engine, ct); } - private JsValue Execute(Engine engine, string script) + private JsValue Execute(ScriptExecutionContext context, string script) { - var program = parser.Parse(script); + var parsed = parser.Parse(script); - lock (engine) - { - return engine.Evaluate(program); - } + return context.Evaluate(parsed); } private static Exception MapException(Exception inner) diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ScriptExecutionContext.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ScriptExecutionContext.cs index 1fac1b854d..66454bbdf8 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ScriptExecutionContext.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ScriptExecutionContext.cs @@ -5,7 +5,9 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using Esprima.Ast; using Jint; +using Jint.Native; using Squidex.Infrastructure.Tasks; namespace Squidex.Domain.Apps.Core.Scripting; @@ -19,6 +21,8 @@ protected ScriptExecutionContext(Engine engine) Engine = engine; } + public abstract JsValue Evaluate(Script program); + public abstract void Schedule(Func action); } @@ -51,6 +55,14 @@ public void Complete(T value) tcs.TrySetResult(value); } + public override JsValue Evaluate(Script script) + { + lock (Engine) + { + return Engine.Evaluate(script); + } + } + public override void Schedule(Func action) { if (IsCompleted)