From 91c2cd4ccccecc66837436badb09d4ed2a3bf73d Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Wed, 17 Jul 2024 21:03:45 +0300 Subject: [PATCH] Backport make EsprimaExtensions.TryGetKey more resilient to missing execution context (#1919) Co-authored-by: Thrasha <112633551+Thrasha@users.noreply.github.com> --- Jint.Tests/Runtime/EvaluationContextTests.cs | 14 ++++++++++++++ Jint/AstExtensions.cs | 4 ++-- Jint/Engine.cs | 6 +++--- Jint/Native/Function/EvalFunction.cs | 5 ++++- .../Interpreter/Expressions/JintExpression.cs | 2 +- 5 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 Jint.Tests/Runtime/EvaluationContextTests.cs diff --git a/Jint.Tests/Runtime/EvaluationContextTests.cs b/Jint.Tests/Runtime/EvaluationContextTests.cs new file mode 100644 index 0000000000..1cdb4f3f97 --- /dev/null +++ b/Jint.Tests/Runtime/EvaluationContextTests.cs @@ -0,0 +1,14 @@ +namespace Jint.Tests.Runtime; + +public class EvaluationContextTests +{ + [Fact] + public void ShouldThrowJavaScriptException() + { + var mockedEngine = new Engine(); + + Expression expression = new Identifier(NodeType.MemberExpression.ToString()); + + Assert.Throws(() => AstExtensions.TryGetComputedPropertyKey(expression, mockedEngine)); + } +} diff --git a/Jint/AstExtensions.cs b/Jint/AstExtensions.cs index 6fd3a5894a..773e186bd9 100644 --- a/Jint/AstExtensions.cs +++ b/Jint/AstExtensions.cs @@ -58,7 +58,7 @@ internal static JsValue TryGetKey(this T expression, Engine engine, bool reso return key; } - private static JsValue TryGetComputedPropertyKey(T expression, Engine engine) + internal static JsValue TryGetComputedPropertyKey(T expression, Engine engine) where T : Expression { if (expression.Type is NodeType.Identifier @@ -75,7 +75,7 @@ or NodeType.FunctionExpression or NodeType.YieldExpression or NodeType.TemplateLiteral) { - var context = engine._activeEvaluationContext; + var context = engine._activeEvaluationContext ?? new EvaluationContext(engine); return JintExpression.Build(expression).GetValue(context!); } diff --git a/Jint/Engine.cs b/Jint/Engine.cs index 378d9c4889..1bee5f8eba 100644 --- a/Jint/Engine.cs +++ b/Jint/Engine.cs @@ -61,7 +61,7 @@ public sealed partial class Engine : IDisposable internal readonly Dictionary TypeCache = new(StringComparer.Ordinal); // we use registered type reference as prototype if it's known - internal Dictionary? _typeReferences; + internal Dictionary? _typeReferences; // cache for already wrapped CLR objects to keep object identity internal ConditionalWeakTable? _objectWrapperCache; @@ -462,7 +462,7 @@ private Engine ScriptEvaluation(ScriptRecord scriptRecord, ParserOptions parserO // TODO what about callstack and thrown exceptions? RunAvailableContinuations(); - return this; + return this; } finally { @@ -1258,7 +1258,7 @@ internal void EvalDeclarationInstantiation( { foreach (var name in pointer.Names) { - privateIdentifiers??= new HashSet(PrivateIdentifierNameComparer._instance); + privateIdentifiers ??= new HashSet(PrivateIdentifierNameComparer._instance); privateIdentifiers.Add(name.Key); } diff --git a/Jint/Native/Function/EvalFunction.cs b/Jint/Native/Function/EvalFunction.cs index cd62f9f1d1..580769ef93 100644 --- a/Jint/Native/Function/EvalFunction.cs +++ b/Jint/Native/Function/EvalFunction.cs @@ -1,6 +1,7 @@ using Jint.Runtime; using Jint.Runtime.Descriptors; using Jint.Runtime.Environments; +using Jint.Runtime.Interpreter; using Jint.Runtime.Interpreter.Statements; using Environment = Jint.Runtime.Environments.Environment; @@ -172,7 +173,9 @@ internal JsValue PerformEval(JsValue x, Realm callerRealm, bool strictCaller, bo Engine.EvalDeclarationInstantiation(script, varEnv, lexEnv, privateEnv, strictEval); var statement = new JintScript(script); - var result = statement.Execute(_engine._activeEvaluationContext!); + var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine); + var result = statement.Execute(context); + var value = result.GetValueOrDefault(); if (result.Type == CompletionType.Throw) diff --git a/Jint/Runtime/Interpreter/Expressions/JintExpression.cs b/Jint/Runtime/Interpreter/Expressions/JintExpression.cs index c3f051ded1..f7714b4136 100644 --- a/Jint/Runtime/Interpreter/Expressions/JintExpression.cs +++ b/Jint/Runtime/Interpreter/Expressions/JintExpression.cs @@ -132,7 +132,7 @@ protected internal static JintExpression Build(Expression expression) : new JintMemberExpression((MemberExpression) ((ChainExpression) expression).Expression), NodeType.AwaitExpression => new JintAwaitExpression((AwaitExpression) expression), NodeType.YieldExpression => new JintYieldExpression((YieldExpression) expression), - _ => null + _ => null }; if (result is null)