Skip to content

Commit

Permalink
Backport make EsprimaExtensions.TryGetKey[ more resilient to missing …
Browse files Browse the repository at this point in the history
…execution context
  • Loading branch information
Thrasha authored and lahma committed Jul 17, 2024
1 parent 67bfc58 commit 20a9f99
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 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));

Check failure on line 12 in Jint.Tests/Runtime/EvaluationContextTests.cs

View workflow job for this annotation

GitHub Actions / windows

Jint.Tests.Runtime.EvaluationContextTests.ShouldThrowJavaScriptException

Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(Jint.Runtime.JavaScriptException) Actual: typeof(System.NullReferenceException) ---- System.NullReferenceException : Object reference not set to an instance of an object.

Check failure on line 12 in Jint.Tests/Runtime/EvaluationContextTests.cs

View workflow job for this annotation

GitHub Actions / macos

Jint.Tests.Runtime.EvaluationContextTests.ShouldThrowJavaScriptException

Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(Jint.Runtime.JavaScriptException) Actual: typeof(System.NullReferenceException) ---- System.NullReferenceException : Object reference not set to an instance of an object.

Check failure on line 12 in Jint.Tests/Runtime/EvaluationContextTests.cs

View workflow job for this annotation

GitHub Actions / linux

Jint.Tests.Runtime.EvaluationContextTests.ShouldThrowJavaScriptException

Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(Jint.Runtime.JavaScriptException) Actual: typeof(System.NullReferenceException) ---- System.NullReferenceException : Object reference not set to an instance of an object.
}

Check failure on line 13 in Jint.Tests/Runtime/EvaluationContextTests.cs

View workflow job for this annotation

GitHub Actions / windows

Jint.Tests.Runtime.EvaluationContextTests.ShouldThrowJavaScriptException

Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(Jint.Runtime.JavaScriptException) Actual: typeof(System.NullReferenceException) ---- System.NullReferenceException : Object reference not set to an instance of an object.
}
2 changes: 1 addition & 1 deletion 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 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 20a9f99

Please sign in to comment.