-
It's not possible to assign values to C# object properties that only have a getter, re-assigning methods is not possible, either. That's good. But if a script tries to do that it's just silently ignored. For example this program: using System;
using Jint;
using Jint.Runtime;
using Jint.Runtime.Interop;
namespace jinttestx
{
public class MyClass
{
public int MyProp => 1;
public void MyMethod(string s) => Console.WriteLine($"From C#: {s}");
}
class Program
{
static void Main(string[] args)
{
Engine engine = new Engine();
engine.SetValue("log", new Action<object>(Console.WriteLine));
engine.SetValue("MyClass", TypeReference.CreateTypeReference(engine, typeof(MyClass)));
try
{
engine.Execute(@"
const mc = new MyClass();
log(mc.MyProp); // Prints 1
mc.MyProp = 2; // Doesn't actually change MyProp to 2 (good!)
log(mc.MyProp); // Prints 1
mc.MyMethod('test'); // Prints From C#: test
mc.MyMethod = s => log(`From JS: ${s}`); // doesn't reassign (good!)
mc.MyMethod('test'); // Prints From C#: test
");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
if (e is JavaScriptException jse)
Console.WriteLine(jse.JavaScriptStackTrace);
}
Console.ReadKey();
}
}
} First it tries to assign the value Is there a way to get Jint to throw an exception if a script tries to do something like that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Everything is always possible and PRs with test cases are appreciated 😉 The thing here is that this follows the JS behavior, JS acts no-op when such assignment doesn't do anything. |
Beta Was this translation helpful? Give feedback.
Everything is always possible and PRs with test cases are appreciated 😉 The thing here is that this follows the JS behavior, JS acts no-op when such assignment doesn't do anything.