Skip to content

Commit

Permalink
Backport make EsprimaExtensions.TryGetKey more resilient to missing e…
Browse files Browse the repository at this point in the history
…xecution context (#1919)

Co-authored-by: Thrasha <[email protected]>
  • Loading branch information
lahma and Thrasha authored Jul 17, 2024
1 parent 67bfc58 commit 91c2cd4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
14 changes: 14 additions & 0 deletions Jint.Tests/Runtime/EvaluationContextTests.cs
Original file line number Diff line number Diff line change
@@ -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<Jint.Runtime.JavaScriptException>(() => AstExtensions.TryGetComputedPropertyKey(expression, mockedEngine));
}
}
4 changes: 2 additions & 2 deletions Jint/AstExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static JsValue TryGetKey<T>(this T expression, Engine engine, bool reso
return key;
}

private static JsValue TryGetComputedPropertyKey<T>(T expression, Engine engine)
internal static JsValue TryGetComputedPropertyKey<T>(T expression, Engine engine)
where T : Expression
{
if (expression.Type is NodeType.Identifier
Expand All @@ -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!);
}

Expand Down
6 changes: 3 additions & 3 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public sealed partial class Engine : IDisposable
internal readonly Dictionary<string, Type?> TypeCache = new(StringComparer.Ordinal);

// we use registered type reference as prototype if it's known
internal Dictionary<Type,TypeReference>? _typeReferences;
internal Dictionary<Type, TypeReference>? _typeReferences;

// cache for already wrapped CLR objects to keep object identity
internal ConditionalWeakTable<object, ObjectInstance>? _objectWrapperCache;
Expand Down Expand Up @@ -462,7 +462,7 @@ private Engine ScriptEvaluation(ScriptRecord scriptRecord, ParserOptions parserO
// TODO what about callstack and thrown exceptions?
RunAvailableContinuations();

return this;
return this;
}
finally
{
Expand Down Expand Up @@ -1258,7 +1258,7 @@ internal void EvalDeclarationInstantiation(
{
foreach (var name in pointer.Names)
{
privateIdentifiers??= new HashSet<PrivateIdentifier>(PrivateIdentifierNameComparer._instance);
privateIdentifiers ??= new HashSet<PrivateIdentifier>(PrivateIdentifierNameComparer._instance);
privateIdentifiers.Add(name.Key);
}

Expand Down
5 changes: 4 additions & 1 deletion Jint/Native/Function/EvalFunction.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Interpreter/Expressions/JintExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 91c2cd4

Please sign in to comment.