* perf(javascript): trim per-evaluation work in the JavaScript evaluator A Jint engine is built for every expression evaluation, so anything done during setup is paid for on every evaluation. Four pieces of that work are avoidable: * The three `IObjectConverter` implementations are stateless but were allocated fresh for every engine. They are now shared static instances. * Every prepared-script cache lookup — including hits — computed a SHA-256 hash of the expression text, base64-encoded it and concatenated a prefix, purely to build the cache key. Using a dedicated key type instead keeps the entries distinct from other users of the shared cache while letting the expression itself be the key, so a hit is a dictionary lookup. Looking the entry up directly rather than through `GetOrCreate` also keeps the factory closure off the hit path. * `ObjectConverterHelper.ConvertToJsObject` built an explicit `PropertyDescriptor` per property and called `DefineOwnProperty`. `CreateDataProperty` is public, produces exactly the same writable/enumerable/configurable descriptor, and is the engine's fast path for it. * The variable write-back resolved the workflow input names — walking the whole activity execution context ancestor chain — before checking whether there was anything to write back. Only variables the expression actually referenced are copied into the engine, so for the common case of an expression that never mentions `variables.` the container is empty and all of that work is wasted. The input names are also now looked up through a set rather than a list. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0179sA2T7HuRfRfSc2JirFik * test(javascript): pin the parse-failure test to the same exception every time Calling `ThrowsAnyAsync<Exception>` twice only proved that both evaluations threw something, which is exactly the assertion a poisoned cache would still satisfy: had the failed preparation left a null or half-built entry behind, the second evaluation would have failed too, just with a different exception. The test now captures both exceptions and asserts they are the same type with the same message, so "keeps reporting the same parse failure" is what is actually checked. The message is stable to compare: it is `Could not prepare script: Unexpected end of input (1:9)`, and since both evaluations run the identical script literal the position is identical as well. No file or path detail is involved. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0179sA2T7HuRfRfSc2JirFik --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
53 lines
2.3 KiB
C#
53 lines
2.3 KiB
C#
using Elsa.Expressions.JavaScript.Contracts;
|
|
using Elsa.Expressions.Models;
|
|
using Elsa.Testing.Shared;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace Elsa.JavaScript.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// Verifies the behaviour of the prepared-script cache.
|
|
/// </summary>
|
|
public class ScriptCacheTests
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly IJavaScriptEvaluator _evaluator;
|
|
|
|
public ScriptCacheTests(ITestOutputHelper testOutputHelper)
|
|
{
|
|
_serviceProvider = new TestApplicationBuilder(testOutputHelper).Build();
|
|
_evaluator = _serviceProvider.GetRequiredService<IJavaScriptEvaluator>();
|
|
}
|
|
|
|
[Fact(DisplayName = "Repeated evaluation of the same expression produces the same result")]
|
|
public async Task RepeatedEvaluationIsStable()
|
|
{
|
|
Assert.Equal("3", await EvaluateAsync("return '' + (1 + 2);"));
|
|
Assert.Equal("3", await EvaluateAsync("return '' + (1 + 2);"));
|
|
Assert.Equal("7", await EvaluateAsync("return '' + (3 + 4);"));
|
|
Assert.Equal("3", await EvaluateAsync("return '' + (1 + 2);"));
|
|
}
|
|
|
|
[Fact(DisplayName = "An expression that fails to parse reports the failure on every evaluation")]
|
|
public async Task ExpressionThatFailsToParseKeepsFailing()
|
|
{
|
|
var first = await Assert.ThrowsAnyAsync<Exception>(() => EvaluateAsync("return ("));
|
|
var second = await Assert.ThrowsAnyAsync<Exception>(() => EvaluateAsync("return ("));
|
|
|
|
// Every evaluation has to report the same parse failure. Merely throwing twice would also be satisfied
|
|
// by a cache that stored a null or half-built entry for the failed preparation, because the second
|
|
// evaluation would then fail too — just with a different exception. Comparing the two exceptions rules
|
|
// that out. The script is identical on both evaluations, so any position the message carries is too.
|
|
Assert.IsType(first.GetType(), second);
|
|
Assert.Equal(first.Message, second.Message);
|
|
}
|
|
|
|
private async Task<string?> EvaluateAsync(string script)
|
|
{
|
|
var context = new ExpressionExecutionContext(_serviceProvider, new());
|
|
return (string?)await _evaluator.EvaluateAsync(script, typeof(string), context);
|
|
}
|
|
}
|