This project has moved and is read-only. For the latest updates, please go here.

Object Instance Indexer

Dec 28, 2011 at 4:13 AM

I'm building a Matrix instance object that uses a third-party matrix library. Is there any way to implement an instance indexer as a way of exposing underlying data in an array-like manner?

Something like this:

public class MatrixInstance : ObjectInstance {
        OtherLib.Matrix M;

        public MatrixInstance(ObjectInstance prototype, int rows, int columns) : base(prototype)  {
            M = new OtherLib.Matrix(rows, columns);
        // Indexer
        public double this[int x, int y] {
            get {
                return this.M[x, y];
            set {
                this.M[x, y] = value;

Would allow something like this:

var a = new Matrix(3,2);
a[1][1] = 3.2;

Dec 28, 2011 at 5:52 PM

This should very much be possible. However, you can't (as far as I know) use double (or triple or quadruple+++ for that matter) indexes in javascript as far as I know, so the way to achieve something like this is to make a MatrixRowInstance class (or MatrixColumnInstance depending on what you want returned form the first index). After that it's just a simple matter of implementing a simple index, which I have never done in Jurassic, but you should be able to find out how pretty easily from sourcecode to Array in the jurassic sources.

Dec 29, 2011 at 12:19 PM

^ What he said.

This is not something I support (so it might break in a major revision) but you can do it without hacking the source code by overriding:

        /// <summary>
        /// Gets a descriptor for the property with the given array index.
        /// </summary>
        /// <param name="propertyName"> The array index of the property. </param>
        /// <returns> A property descriptor containing the property value and attributes. </returns>
        /// <remarks> The prototype chain is not searched. </remarks>
        public override PropertyDescriptor GetOwnPropertyDescriptor(uint index)

Similarly, you can override the following method to set an index:

        /// <summary>
        /// Sets the value of the property with the given array index.  If a property with the
        /// given index does not exist, or exists in the prototype chain (and is not a setter) then
        /// a new property is created.
        /// </summary>
        /// <param name="index"> The array index of the property to set. </param>
        /// <param name="value"> The value to set the property to.  This must be a javascript
        /// primitive (double, string, etc) or a class derived from <see cref="ObjectInstance"/>. </param>
        /// <param name="throwOnError"> <c>true</c> to throw an exception if the property could not
        /// be set.  This can happen if the property is read-only or if the object is sealed. </param>
        public override void SetPropertyValue(uint index, object value, bool throwOnError)