Eval function in non strict mode do not create variables in global (surround) context

using System;

namespace ToRemObjects
{
class Program
{
static void Main(string[] args)
{
using (var esc = new RemObjects.Script.EcmaScriptComponent())
{
try
{
Console.WriteLine(“Create variable 1: {0}”, esc.RunFunction(“eval”, “globalVarible1 = 100; globalVarible1”));
Console.WriteLine(“Read variable 1: {0}”, esc.RunFunction(“eval”, “globalVarible1”));// OK
}
catch (Exception ex)
{
Console.WriteLine(“Read variable 1: {0}”, ex.Message);
}
try
{
Console.WriteLine(“Create variable2: {0}”, esc.RunFunction(“eval”, “var globalVarible2 = 100; globalVarible2”));
Console.WriteLine(“Read variable2: {0}”, esc.RunFunction(“eval”, “globalVarible2”));//??? “ReferenceError: globalVarible2 is not defined”
}
catch (Exception ex)
{
Console.WriteLine(“Read variable2: {0}”, ex.Message);
}

            try
            {
                Console.WriteLine("Create variable3: {0}", esc.RunFunction("eval", "'use strict'; var globalVarible3 = 100; globalVarible3"));
                Console.WriteLine("Read variable3: {0}", esc.RunFunction("eval", "globalVarible3"));//OK, ex = "ReferenceError: globalVarible is not defined"
            }
            catch (Exception ex)
            {
                Console.WriteLine("Read variable3: {0}", ex.Message);
            }
            try
            {
                Console.WriteLine("Create variable4: {0}", esc.RunFunction("eval", "'use strict'; globalVarible4 = 100; globalVarible4"));//OK, TypeError: Cannot call globalVarible4 on undefined
                Console.WriteLine("Read variable4: {0}", esc.RunFunction("eval", "globalVarible4"));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Read variable4: {0}", ex.Message);
            }
            Console.ReadLine();
        }
    }
}

}

Hello.

Thank you for the report. I have reproduced the problem. Corresponding issue is registered as #60328.

Thanks.

What is going on with this

Essentially, the RunFunction code creates it’s own scope and doesn’t really allow what you want to do (eval is a special function). What you can try is accessing the engine.GlobalObject and call NotStrictGlobalEval on it. engine.GlobalObject.ExecutionContext can be the first parameter, engine.GlobalObject the second and the actual code in the last params array.

Thanks