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

Commented Unassigned: GlobalMember property not found (regression) [57]

0
0
when upgrading from 5.3.10 to 5.4, my unit tests fail when trying to find a globalmember property, in a particular case. Some unit tests are better than a long speech so here are the tests (only the NOK fails, with error : __ReferenceError: A is not defined__) :

```
class host1
{
public string H { get; set; }
public PropertyBag A { get; set; }
public host1() { A = new PropertyBag(); A.Add("FOUR", 4); }
}

class host2
{
public string W { get; set; }
public PropertyBag B { get; set; }
public host2() { B = new PropertyBag(); B.Add("EIGHT", 8); }
}

[Test]
public void Test_engine_global_objects_NOK()
{
var engine = new V8ScriptEngine("xscript");

engine.AddHostObject("", HostItemFlags.GlobalMembers, new host1());
engine.AddHostObject("", HostItemFlags.GlobalMembers, new host2());

Assert.AreEqual(4, engine.Evaluate("A.FOUR"));
Assert.AreEqual(8, engine.Evaluate("B.EIGHT"));
}

[Test]
public void Test_engine_global_objects_A()
{
var engine = new V8ScriptEngine("xscript");
engine.AddHostObject("", HostItemFlags.GlobalMembers, new host1());
Assert.AreEqual(4, engine.Evaluate("A.FOUR"));
}
[Test]
public void Test_engine_global_objects_B()
{
var engine = new V8ScriptEngine("xscript");
engine.AddHostObject("", HostItemFlags.GlobalMembers, new host2());
Assert.AreEqual(8, engine.Evaluate("B.EIGHT"));
}

[Test]
public void Test_engine_normal_objects()
{
var engine = new V8ScriptEngine("xscript");

engine.AddHostObject("f", new host1().A);
engine.AddHostObject("e", new host2().B);
Assert.AreEqual(4, engine.Evaluate("f.FOUR"));
Assert.AreEqual(8, engine.Evaluate("e.EIGHT"));
}

[Test]
public void Test_engine_simple_global_objects()
{
var engine = new V8ScriptEngine("xscript");

engine.AddHostObject("", HostItemFlags.GlobalMembers, new host1());
engine.AddHostObject("", HostItemFlags.GlobalMembers, new host2());

engine.Execute("H = 'hello'");
engine.Execute("W = 'world'");
Assert.AreEqual("hello", engine.Evaluate("H"));
Assert.AreEqual("world", engine.Evaluate("W"));
}

```
Comments: Oooouch.. I really didn't expected that. (BTW when reading the clearscript code I thought using same names was specifically supported). The intent behind such code was to run specific scripts with particular scopes. We have a global scope, and specific scopes depending on the scripts being run. Imagine this example : ``` C# engine.AddHostObject("", HostItemFlags.GlobalMembers, new global_context()); engine.AddHostObject("", HostItemFlags.GlobalMembers, new kitchen_context()); ``` I can run the following js code (kitchen_context has a Shutter and Oven property, global_context has a Sensor property) : ``` js Oven.Start(); if (Sensor.GetHumidity() > 0.70) { Shutter.Open(0.5); } ``` And with another scope we can achieve this : (bathroom_context has a HVAC property) ``` C# otherengine.AddHostObject("", HostItemFlags.GlobalMembers, new global_context()); otherengine.AddHostObject("", HostItemFlags.GlobalMembers, new bathroom_context()); ``` I can run the following js code : ``` js if (Sensor.GetHumidity() > 0.70) { HVAC.Blow(); } ``` I guess we are stuck with 5.3.10 now... (or maybe some sort of proxy object could merge the two globalmembers contexts ? or maybe you could still support this :-p)

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles





Latest Images