Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: redefining toString() - don't know what to expect

0
0
One of my users created a toString() function which miserably failed, always returning [object Undefined].

I created a test, and the unexpected behaviour seems to be triggered by using GlobalMembers.

I do not know exactly what behaviour to expect in the context of clearscript, but the function toString cannot be redefined once the global host object is added.
Is it a bug, is it a design feature ?

Here is the test code :
class foo
        {
            publicstring A { get; set; }
        }

        [Test]
        publicvirtualvoid Test_ToString_v8_base()
        {
            using(var run = new V8ScriptEngine())
            {
                var tsBefore = run.Evaluate("toString()");
                Assert.AreEqual("[object Undefined]", tsBefore);// is it ok ?

                run.Execute(@"function toString(){return 'AAA';}");
                var tsBefore2 = run.Evaluate("toString()");
                Assert.AreEqual("AAA", tsBefore2);

                var foo = new foo();
                run.AddHostObject("foo", HostItemFlags.GlobalMembers, foo);

                var tsAfter = run.Evaluate("toString()");

                run.Execute(@"function toString(){return 'BBB';}");

                var tsAfter2 = run.Evaluate("toString()");
                Assert.AreEqual("BBB", tsAfter2);// NOK: it is still [object Undefined]
                Assert.AreEqual("AAA", tsAfter); // don't know what to expect here..
            }
        }

Viewing all articles
Browse latest Browse all 2297