Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Implement external array storage #17

Open
gbrail opened this issue Jul 16, 2014 · 0 comments
Open

Implement external array storage #17

gbrail opened this issue Jul 16, 2014 · 0 comments

Comments

@gbrail
Copy link

gbrail commented Jul 16, 2014

This is another requirement coming to us from the world of Node.js -- the need to allocate "external storage" that is specified as a separate JavaScript array which replaces the various slots inside an object with the array data.

We will add the following methods to ScriptableObject:

void setExternalArray(ExternalArray array);
ExternalArray getExternalArray();
boolean hasExternalArray();

The external array is represented as an abstract class, which has the following subclasses (to match the array types supported by Rhino):

ExternalByteArray
ExternalUnsignedByteArray
ExternalClampedByteArray
ExternalShortArray
ExternalUnsignedShortArray
ExternalIntArray
ExternalUnsignedIntArray
ExternalFloatArray
ExternalDoubleArray

each takes an appropriate Java array type and implements JavaScript getters and setters to support signed or unsigned values, as appropriate.

The "clamped" array, like the V8 equivalent, coerces bytes inserted to the range [0,255].

Once an array is set, all indexes between 0 and the array size are handled directly by the array.

Access to other object properties, and array indices higher than the array size, behave as normal.

The array may be set at any time, or removed by setting it to null. However, if it is set after data has already been loaded, then the result will be undefined.

For instance, assume that an object "o" is in scope, that the Java code already set it up by calling "setExternalArray()" with the type "ExternalIntArray" of size 10, and that each element is initialized to 0:

assert(o[0] === 0);
o[9] = 99;
assert(o[0] === 99);
assert(!o[10]);
o[10] = 88;
assert(o[10] === 88);
o.foo = 'bar';
assert(o.foo === 'bar');
gbrail pushed a commit that referenced this issue Jul 16, 2014
…thod:

  ScriptableObject.setExternalArray()
and the new "ExternalArray" class and its subclasses.
This feature lets you set the contents of the "array" portion of an
object to a Java array of various types. This makes it easier to
write code that efficiently shares code between Java and Node.js
See here for more:
  #17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant