This project is read-only.

SetAccessor for array?

Jan 30, 2015 at 4:17 PM
There is no overload for
void SetAccessor(string name, V8NativeObjectPropertyGetter getter, V8NativeObjectPropertySetter setter, V8PropertyAttributes attributes = V8PropertyAttributes.None, V8AccessControl access = V8AccessControl.Default);
I wanna to make an array,
when I try to access the array element like below
array[2]
A .NET method is fired like :
InternalHandle ArrayGetter(InternalHandle _this, int idx)
{
}
Is this possible?
Jan 30, 2015 at 4:38 PM
Edited Jan 30, 2015 at 4:38 PM
If there's no overload, it's most likely because Google V8 doesn't support it. ;). V8.Net can only do what V8 allows.

You have to understand JS a bit more. Numbers are just properties in bracket form. You can put them on objects as well as arrays. You might even be able to add an accessor for the property "1", for example. If you want to intercept ALL properties, use an ObjectTemplate (but it won't be an array).
Jan 31, 2015 at 6:39 AM
I tried with following code and it works.
Although it is not a real Array object, but looks the same on usage.
_children = _engine.CreateObject();
this.SetProperty("children", _children);
if (_treeNode.Children != null)
{
    for (int i = 0; i < _treeNode.Children.Count; i++)
    {
        _children.SetAccessor(i.ToString(), ChildGetter, null, V8PropertyAttributes.Locked, V8AccessControl.AllCanRead);
    }
    _children.SetProperty("length", _treeNode.Children.Count);
}


private InternalHandle ChildGetter(InternalHandle _this, string propertyName)
{
    int index;
    if (propertyName == null ||
    !int.TryParse(propertyName, out index) ||
    index < 0 ||
    index >= _treeNode.Children.Count)
    {
    return null;
    }

    NamespaceNodeObject ns;
    if (_childrenMap.TryGetValue(index, out ns))
    return ns;
    ns = NamespaceNodeObject.New( _engine, _treeNode.Children[index], _workItem);
    _childrenMap[index] = ns;
    return ns;
}