Hi,
I am having an issue preserving objects type when enumerating a Vector (in jscript).
Code below describes the use-case :
- Create a custom object type
- Create a new object from that type and add it to a vector (and a map to compare)
- Check its type (obj instanceof CustomObjectType)
The third operation works fine when checked on :
- The object itself (still on the stack)
- The object in the vector when accessed from its index (vector[index])
- The object in the map when enumerated (kinda similar to vector juste above since enumerator gives the keys, but the object is still accessed by the key "index")
BUT it is not working when accessed through enumeration of the vector.
Code :
function OnInit(initData)
{
initData.name = "TestEnumVector";
initData.version = "1.0";
initData.copyright = "(c) 2024 Stephane";
initData.desc = "";
initData.default_enable = true;
initData.min_version = "13.0";
}
// Called to add commands to Opus
function OnAddCommands(addCmdData)
{
var cmd = addCmdData.AddCommand();
cmd.name = "TestEnumVector";
cmd.method = "OnTestEnumVector";
cmd.desc = "";
cmd.label = "TestEnumVector";
cmd.template = "";
cmd.hide = false;
cmd.icon = "script";
}
MyObject = function(str) {
this.name = str;
this.sayHi = function() { DOpus.Output("Hello, my name is " + this.name); };
}
function OnTestEnumVector(scriptCmdData)
{
var obj = new MyObject("what ?");
var v = DOpus.Create.Vector();
var m = DOpus.Create.Map();
v.push_back(obj);
m("key") = obj;
DOpus.Output("standalone obj is MyObject ? : " + (obj instanceof MyObject) );
obj.sayHi();
DOpus.Output("obj in Vector [index access] is MyObject ? : " + (v[0] instanceof MyObject) );
v[0].sayHi();
for (var e = new Enumerator(v); !e.atEnd(); e.moveNext()) {
DOpus.Output("obj in Vector [enumerator access] is MyObject ? : " + (e.item() instanceof MyObject) );
e.item().sayHi();
}
DOpus.Output("obj in Map [key access] is MyObject ? : " + (m("key") instanceof MyObject) );
m("key").sayHi();
for (var e = new Enumerator(m); !e.atEnd(); e.moveNext()) {
var key = e.item();
var value = m(key);
DOpus.Output("obj in Vector [enumerator access] is MyObject ? : " + (value instanceof MyObject) );
value.sayHi();
}
}
Output goes as this :
07/07/2024 19:26 TestEnumVector: standalone obj is MyObject ? : true
07/07/2024 19:26 TestEnumVector: Hello, my name is what ?
07/07/2024 19:26 TestEnumVector: obj in Vector [index access] is MyObject ? : true
07/07/2024 19:26 TestEnumVector: Hello, my name is what ?
07/07/2024 19:26 TestEnumVector: obj in Vector [enumerator access] is MyObject ? : false
07/07/2024 19:26 TestEnumVector: Hello, my name is what ?
07/07/2024 19:26 TestEnumVector: obj in Map [key access] is MyObject ? : true
07/07/2024 19:26 TestEnumVector: Hello, my name is what ?
07/07/2024 19:26 TestEnumVector: obj in Vector [enumerator access] is MyObject ? : true
07/07/2024 19:26 TestEnumVector: Hello, my name is what ?
Is this some kind of bug or should I just change route and access the vector values through index "enumeration" (for (var i=0; i<v.length; ...) to avoid this ?
Thanks.