sebastienros / jint

Javascript Interpreter for .NET
BSD 2-Clause "Simplified" License
4.04k stars 558 forks source link

get rid of 'reference not set to an instance of an object' error, which appears when context is null #1917

Closed Thrasha closed 2 months ago

Thrasha commented 2 months ago

getting weird error when execute ts script in jint: Method CalculateLoanConditions has thrown an exception. System.NullReferenceException: Object reference not set to an instance of an object. at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context) at Jint.EsprimaExtensions.TryGetKey[T](T expression, Engine engine, Boolean resolveComputed) at Jint.Runtime.Interpreter.Expressions.JintObjectExpression.BuildObjectNormal(EvaluationContext context) at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context) at Jint.Runtime.Interpreter.Expressions.JintCallExpression.ArgumentListEvaluation(EvaluationContext context) at Jint.Runtime.Interpreter.Expressions.JintCallExpression.EvaluateInternal(EvaluationContext context) at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context) at Jint.Runtime.Interpreter.Statements.JintReturnStatement.ExecuteInternal(EvaluationContext context) at Jint.Native.Function.ScriptFunction.Call(JsValue thisObject, JsValue[] arguments) at Jint.Runtime.Interpreter.Expressions.JintCallExpression.EvaluateInternal(EvaluationContext context) at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context) at Jint.Runtime.Interpreter.Statements.JintReturnStatement.ExecuteInternal(EvaluationContext context) at Jint.Native.Function.ScriptFunction.Call(JsValue thisObject, JsValue[] arguments)

https://stackoverflow.com/questions/78326684/error-object-reference-not-set-to-an-instance-of-an-object-after-upgrade-jint

after debugging jint, I've found out that for my scripts context is null. I substitute null to new EvaluationContext() and it started to work well

lahma commented 2 months ago

Can you add a test case showing the original problem?

Thrasha commented 2 months ago

I will try to reproduce it wia unit test, however, I'm not positive with that. The script, generated by state machine is huge and complex, and I couldn't determine which part of script has context null when I was debugging jint part. Give me a day, I'll try to find something

Thrasha commented 2 months ago

Hi @lahma. Unfortunately, it is impossible to write a unit test due to protection level of _activeEvaluationContext. What else can I do to make this fix merged?

lahma commented 2 months ago

Jint.Tests projects should be able to see internals?

Thrasha commented 2 months ago

It does see, however, we have to change private method TryGetComputedPropertyKey to internal. If yes, this following test throws JS error after my fix and 'Object reference not set to an instance of an object' before my fix