sebastienros / jint

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

Incorrect C# code not throwing an exception #1897

Closed danemorgridge closed 1 month ago

danemorgridge commented 1 month ago

I'm using 3.1.3, but I also tried with the current main branch and it didn't fix it.

I'm using Jint to write some scripts for a game I'm working on and it's not throwing an exception if there's an issue on the C# side of the script. Here is a block of code that illustrates what I'm trying to do:

`using Jint; using TestJSEngine;

var c = new Character(); c.Name = "Test"; c.MaxHealth = 40; c.CurrentHealth = 15;

var script = @"c.CurrrentHealth += 30; if(c.CurrentHealth > c.MaxHealth) { c.CurrentHealth = c.MaxHealth; } log(c.Name + ' is healed for 30'); ";

try { var engine = new Engine() .SetValue("log", new Action(Console.WriteLine)) .SetValue("c", c) .Execute(script);
} catch (Exception ex) { var msg = ex.Message; }

Console.WriteLine($"{ c.CurrentHealth } - {c.MaxHealth}");`

I would expect this to throw an error since the first line has the C# property spelled incorrectly. The script continues to run and the log function is called as I would expect it to. When the C# object is spelled correctly, the script works perfectly. I have a QA process, but it would be really nice if this kind of error can be caught and thrown at execution time. I played with the CLR execution stuff, but couldn't' get it to fail.

lahma commented 1 month ago

Javascript is a dynamic language, so it isn't so picky about type shapes. So you would like to get runtime errors if unknown properties are being accessed under interop?