-
I'm trying to create Javascript objects in C# and use them as arguments when using Invoke(myObject), and I noticed that objectInstance.ToString() throws an exception: var engine = new Engine();
var instance = new ObjectInstance(engine);
Console.WriteLine(instance.ToString()); // raises JavaScriptException Is this a bug or do I need to use a different class to create Javascript objects in C#? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
There problem here is that you create a plain Use the way how engine constructs the final object (preferred): using System;
using Jint;
using Jint.Runtime;
var engine = new Engine();
var instance = engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
Console.WriteLine(instance.ToString()); Or set prototype manually: using System;
using Jint;
using Jint.Native.Object;
var engine = new Engine();
var instance = new ObjectInstance(engine);
instance.SetPrototypeOf(engine.Realm.Intrinsics.Object.PrototypeObject);
Console.WriteLine(instance.ToString()); |
Beta Was this translation helpful? Give feedback.
There problem here is that you create a plain
ObjectInstance
without a prototype chain. Prototype contains needed guidance how to handle common tasks, it also gives the JS functionality you are expected to find from any JS object. So two alternatives:Use the way how engine constructs the final object (preferred):
Or set prototype manually: