sebastienros / jint

Javascript Interpreter for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Engine is trying to convert a "method name" to Int

Chacalico opened this issue · comments

v3.0.0

I'm using the Jint engine with Unity, and previous versions worked fine. However, after a couple of months, I decided to update the engine to version 3.0.0. Now, something really weird is happening. I tried to download the source code and made some adjustments to maybe fix it, and I found out that the issue occurs in the following JavaScript code snippet:

function update() {
    if (!actor.character.isBusy && !actor.character.isInteracting) {
       // BUG HAPPENS HERE
        var currentAmmoId = gear.GetAmmunitionAt(actor.ammunitionIndex % 4);
        // Check if actor's current ammo ID is different from local
        if (currentAmmoId != ammoId) {
            ammoId = currentAmmoId;

            // Destroy current quiver
            if (quiver)
                Destroy(quiver);
            quiver = null;
        }
    }
}

The corresponding C# method is written as follows:

public int GetAmmunitionAt(int index) => this[(ActorEquipmentSlot)(12 + index)].id;

The change I made was in the Jint.Runtime.Interop.DefaultTypeConverter.TryConvert method. To debug the problem, I imported Unity libraries into the Jint project so that I could use UnityEngine.Debug as follows:

            if (type.IsEnum)
            {
                try
                {
                    var integer = System.Convert.ChangeType(value, intType, formatProvider);
                    if (integer == null)
                    {
                        ExceptionHelper.ThrowArgumentOutOfRangeException();
                    }
                    converted = Enum.ToObject(type, integer);
                    return true;
                }
                catch (System.Exception)
                {
                    UnityEngine.Debug.Log($"Unable to convert {value} ({value.GetType().FullName}) to {intType}");
                    throw;
                }
            }

The console leaves me with the following error message:

imagem_2024-02-04_021658340

trace:

System.FormatException: Input string was not in a correct format.
  at System.Number.ThrowOverflowOrFormatException (System.Boolean overflow, System.String overflowResourceKey) [0x0001a] in <17d9ce77f27a4bd2afb5ba32c9bea976>:0 
  at System.Number.ParseInt32 (System.ReadOnlySpan`1[T] value, System.Globalization.NumberStyles styles, System.Globalization.NumberFormatInfo info) [0x00016] in <17d9ce77f27a4bd2afb5ba32c9bea976>:0 
  at System.Int32.Parse (System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider) [0x0001d] in <17d9ce77f27a4bd2afb5ba32c9bea976>:0 
  at System.Convert.ToInt32 (System.String value, System.IFormatProvider provider) [0x00005] in <17d9ce77f27a4bd2afb5ba32c9bea976>:0 
  at System.String.System.IConvertible.ToInt32 (System.IFormatProvider provider) [0x00000] in <17d9ce77f27a4bd2afb5ba32c9bea976>:0 
  at System.Convert.ChangeType (System.Object value, System.Type conversionType, System.IFormatProvider provider) [0x000e0] in <17d9ce77f27a4bd2afb5ba32c9bea976>:0 
  at Jint.Runtime.Interop.DefaultTypeConverter.TryConvert (System.Object value, System.Type type, System.IFormatProvider formatProvider, System.Boolean propagateException, System.Object& converted, System.String& problemMessage) [0x000b8] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\DefaultTypeConverter.cs:94 
  at Jint.Runtime.Interop.DefaultTypeConverter.TryConvert (System.Object value, System.Type type, System.IFormatProvider formatProvider, System.Object& converted) [0x00001] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\DefaultTypeConverter.cs:51 
  at Jint.Runtime.Interop.Reflection.IndexerAccessor+<>c__DisplayClass7_0.<TryFindIndexer>g__ComposeIndexerFactory|0 (System.Reflection.PropertyInfo candidate, System.Type paramType) [0x00038] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\Reflection\IndexerAccessor.cs:62 
  at Jint.Runtime.Interop.Reflection.IndexerAccessor.TryFindIndexer (Jint.Engine engine, System.Type targetType, System.String propertyName, Jint.Runtime.Interop.Reflection.IndexerAccessor& indexerAccessor, System.Reflection.PropertyInfo& indexer) [0x00157] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\Reflection\IndexerAccessor.cs:116 
  at Jint.Runtime.Interop.TypeResolver.ResolvePropertyDescriptorFactory (Jint.Engine engine, System.Type type, System.String memberName, System.Boolean mustBeReadable, System.Boolean mustBeWritable) [0x00010] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\TypeResolver.cs:84 
  at Jint.Runtime.Interop.TypeResolver.GetAccessor (Jint.Engine engine, System.Type type, System.String member, System.Boolean mustBeReadable, System.Boolean mustBeWritable, System.Func`1[TResult] accessorFactory) [0x00029] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\TypeResolver.cs:62 
  at Jint.Runtime.Interop.ObjectWrapper.GetOwnProperty (Jint.Native.JsValue property, System.Boolean mustBeReadable, System.Boolean mustBeWritable) [0x00164] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\ObjectWrapper.cs:249 
  at Jint.Runtime.Interop.ObjectWrapper.Get (Jint.Native.JsValue property, Jint.Native.JsValue receiver) [0x00056] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interop\ObjectWrapper.cs:119 
  at Jint.Engine.GetValue (Jint.Runtime.Reference reference, System.Boolean returnReferenceToPool) [0x000de] in G:\Downloads\jint-3.0.0\Jint\Engine.cs:576 
  at Jint.Engine.GetValue (System.Object value, System.Boolean returnReferenceToPool) [0x00038] in G:\Downloads\jint-3.0.0\Jint\Engine.cs:536 
  at Jint.Runtime.Interpreter.Expressions.JintCallExpression.EvaluateInternal (Jint.Runtime.Interpreter.EvaluationContext context) [0x000ad] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\Expressions\JintCallExpression.cs:108 
  at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue (Jint.Runtime.Interpreter.EvaluationContext context) [0x00001] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\Expressions\JintExpression.cs:27 
  at Jint.Runtime.Interpreter.Expressions.JintAssignmentExpression+SimpleAssignmentExpression.AssignToIdentifier (Jint.Runtime.Interpreter.EvaluationContext context, Jint.Runtime.Interpreter.Expressions.JintIdentifierExpression left, Jint.Runtime.Interpreter.Expressions.JintExpression right, System.Boolean hasEvalOrArguments) [0x00066] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\Expressions\JintAssignmentExpression.cs:442 
  at Jint.Runtime.Interpreter.Statements.JintVariableDeclaration.ExecuteInternal (Jint.Runtime.Interpreter.EvaluationContext context) [0x0013c] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\Statements\JintVariableDeclaration.cs:101 
  at Jint.Runtime.Interpreter.JintStatementList.Execute (Jint.Runtime.Interpreter.EvaluationContext context) [0x00178] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\JintStatementList.cs:122 
  at Jint.Runtime.Interpreter.Statements.JintBlockStatement.ExecuteBlock (Jint.Runtime.Interpreter.EvaluationContext context) [0x00093] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\Statements\JintBlockStatement.cs:57 
  at Jint.Runtime.Interpreter.Statements.JintIfStatement.ExecuteInternal (Jint.Runtime.Interpreter.EvaluationContext context) [0x00022] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\Statements\JintIfStatement.cs:29 
  at Jint.Runtime.Interpreter.JintStatementList.Execute (Jint.Runtime.Interpreter.EvaluationContext context) [0x00178] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\JintStatementList.cs:122 
  at Jint.Runtime.Interpreter.JintFunctionDefinition.EvaluateBody (Jint.Runtime.Interpreter.EvaluationContext context, Jint.Native.Function.Function functionObject, Jint.Native.JsValue[] argumentsList) [0x0021c] in G:\Downloads\jint-3.0.0\Jint\Runtime\Interpreter\JintFunctionDefinition.cs:90 
  at Jint.Native.Function.ScriptFunction.Call (Jint.Native.JsValue thisObject, Jint.Native.JsValue[] arguments) [0x00093] in G:\Downloads\jint-3.0.0\Jint\Native\Function\ScriptFunction.cs:79 
  at Jint.Engine+<>c__DisplayClass108_0.<Invoke>g__DoInvoke|0 () [0x0009d] in G:\Downloads\jint-3.0.0\Jint\Engine.cs:776 
  at Jint.Engine.ExecuteWithConstraints[T] (System.Boolean strict, System.Func`1[TResult] callback) [0x0003a] in G:\Downloads\jint-3.0.0\Jint\Engine.cs:810 
  at Jint.Engine.Invoke (Jint.Native.JsValue value, System.Object thisObj, System.Object[] arguments) [0x00053] in G:\Downloads\jint-3.0.0\Jint\Engine.cs:796 
  at Jint.Engine.Invoke (System.String propertyName, System.Object thisObj, System.Object[] arguments) [0x00009] in G:\Downloads\jint-3.0.0\Jint\Engine.cs:730 
  at Jint.Engine.Invoke (System.String propertyName, System.Object[] arguments) [0x00001] in G:\Downloads\jint-3.0.0\Jint\Engine.cs:716 
  at Chacalico.ActorEngine.ActorWeaponController.Update () [0x00000] in C:\Projetos\Osiris Protocol\Assets\Scripts\Core\Character\CharacterWeaponController.cs:80 
UnityEngine.Debug:LogError (object,UnityEngine.Object)
Chacalico.ActorEngine.ActorWeaponController:Update () (at Assets/Scripts/Core/Character/CharacterWeaponController.cs:84)

I'm not sure if I'm doing something wrong, so let me know if I should provide more information.

Unfortunately pretty hard to to diagnose or fix without an isolated test case. If you can write a failing test case with minimal constructs that you are using we might have a better chance.

Going to close this in the near future unless there's a sensible repro via test case.

Still missing a test case show closing.