// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Diagnostics; using Acornima; using Acornima.Ast; using Jint; using Jint.Constraints; using Jint.Native; using Jint.Native.Promise; using Jint.Runtime; using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.Scripting.ContentWrapper; using Squidex.Domain.Apps.Core.Scripting.Internal; using Squidex.Infrastructure; using Squidex.Infrastructure.Json.Objects; using Squidex.Infrastructure.Translations; using Squidex.Infrastructure.Validation; namespace Squidex.Domain.Apps.Core.Scripting; internal sealed class JintScript : IScript, IAsyncScript, IDisposable { // The engine is shared between all executions, therefore only one of them can run at a time. private readonly SemaphoreSlim executionGate = new SemaphoreSlim(1); // The engine is also shared with the callbacks of the scheduled tasks, which run on other threads. private readonly SemaphoreSlim engineLock = new SemaphoreSlim(1); // Jint only creates the constraint for a token that can actually be cancelled, therefore the engine is // built with a placeholder that is never cancelled and repointed for each execution. private readonly CancellationTokenSource placeholder = new CancellationTokenSource(); private readonly CancellationConstraint? cancellation; private readonly Engine engine; private readonly Prepared