This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for external arrays on each ScriptableObject via a new me…
…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
- Loading branch information
Gregory Brail
committed
Jul 16, 2014
1 parent
6952f5a
commit ff25391
Showing
12 changed files
with
810 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.mozilla.javascript.arrays; | ||
|
||
import org.mozilla.javascript.ScriptRuntime; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* The abstract base class for the different types of external arrays. Users must construct one of | ||
* the subclasses before passing it to "ScriptableObject.setExternalArray()". | ||
*/ | ||
|
||
public abstract class ExternalArray | ||
implements Serializable | ||
{ | ||
/** | ||
* Return the length of the array. | ||
*/ | ||
public abstract int getLength(); | ||
|
||
/** | ||
* Return true if the given index is in range. Normally users should not need to use this. | ||
*/ | ||
public boolean inRange(int index) { | ||
return ((index < getLength()) && (index >= 0)); | ||
} | ||
|
||
/** | ||
* Return the element at the specified index as an Object. | ||
*/ | ||
public Object get(int index) { | ||
return getElement(index); | ||
} | ||
|
||
/** | ||
* Set the element at the specified index. | ||
*/ | ||
public void put(int index, Object value) { | ||
try { | ||
putElement(index, value); | ||
} catch (ClassCastException cce) { | ||
throw ScriptRuntime.typeError("Invalid object type for external array"); | ||
} | ||
} | ||
|
||
/** | ||
* Copy numeric ids representing the property IDs of each array element to the specified array. | ||
* Used internally when iterating over the properties of the object. | ||
*/ | ||
public int copyIds(Object[] ids) { | ||
int i; | ||
for (i = 0; i < getLength(); i++) { | ||
ids[i] = Integer.valueOf(i); | ||
} | ||
return i; | ||
} | ||
|
||
/** | ||
* Return the value of the element in a form that works for a script. Caller will check bounds -- | ||
* don't do that. | ||
*/ | ||
protected abstract Object getElement(int index); | ||
|
||
/** | ||
* Set the value of the element. Don't check bounds. Blind casting is OK -- caller will handle | ||
* ClassCastException -- don't add an instanceof check. | ||
*/ | ||
protected abstract void putElement(int index, Object value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.arrays; | ||
|
||
/** | ||
* An implementation of the external array using an array of bytes. From a JavaScript perspective, | ||
* only "number" types may be set in the array. Valid values are between -128 and 127, inclusive. | ||
*/ | ||
|
||
public class ExternalByteArray | ||
extends ExternalArray | ||
{ | ||
private static final long serialVersionUID = 5377484970217959212L; | ||
|
||
private final byte[] array; | ||
|
||
public ExternalByteArray(byte[] array) { | ||
this.array = array; | ||
} | ||
|
||
public byte[] getArray() { | ||
return array; | ||
} | ||
|
||
protected Object getElement(int index) { | ||
return Integer.valueOf(array[index]); | ||
} | ||
|
||
protected void putElement(int index, Object value) { | ||
Number num = (Number)value; | ||
array[index] = num.byteValue(); | ||
} | ||
|
||
public int getLength() { | ||
return array.length; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/org/mozilla/javascript/arrays/ExternalClampedByteArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.arrays; | ||
|
||
/** | ||
* An implementation of the external array using an array of bytes. From a JavaScript perspective, | ||
* only "number" types may be set in the array. Valid values are between 0 and 255, inclusive. Any values out | ||
* of that range will be "clamped", meaning that values smaller than 0 will be set to 0 and larger than 255 will | ||
* be set to 255. | ||
*/ | ||
|
||
public class ExternalClampedByteArray | ||
extends ExternalArray | ||
{ | ||
private static final long serialVersionUID = -1883561335812409618L; | ||
|
||
private final byte[] array; | ||
|
||
public ExternalClampedByteArray(byte[] array) { | ||
this.array = array; | ||
} | ||
|
||
public byte[] getArray() { | ||
return array; | ||
} | ||
|
||
protected Object getElement(int index) { | ||
return array[index] & 0xff; | ||
} | ||
|
||
protected void putElement(int index, Object value) { | ||
int val = ((Number)value).intValue(); | ||
if (val < 0) { | ||
val = 0; | ||
} else if (val > 255) { | ||
val = 255; | ||
} | ||
array[index] = (byte)(val & 0xff); | ||
} | ||
|
||
public int getLength() { | ||
return array.length; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/org/mozilla/javascript/arrays/ExternalDoubleArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.arrays; | ||
|
||
/** | ||
* An implementation of the external array using an array of "double"s. Only "number" types may be set in | ||
* the array. | ||
*/ | ||
|
||
public class ExternalDoubleArray | ||
extends ExternalArray | ||
{ | ||
private static final long serialVersionUID = -773914084068347275L; | ||
|
||
private final double[] array; | ||
|
||
public ExternalDoubleArray(double[] array) { | ||
this.array = array; | ||
} | ||
|
||
public double[] getArray() { | ||
return array; | ||
} | ||
|
||
protected Object getElement(int index) { | ||
return array[index]; | ||
} | ||
|
||
protected void putElement(int index, Object value) { | ||
double val = ((Number)value).doubleValue(); | ||
array[index] = val; | ||
} | ||
|
||
public int getLength() { | ||
return array.length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.arrays; | ||
|
||
/** | ||
* An implementation of the external array using an array of "float"s. Only "number" types may be set in | ||
* the array. | ||
*/ | ||
|
||
public class ExternalFloatArray | ||
extends ExternalArray | ||
{ | ||
private static final long serialVersionUID = 3786769656861013570L; | ||
|
||
private final float[] array; | ||
|
||
public ExternalFloatArray(float[] array) { | ||
this.array = array; | ||
} | ||
|
||
public float[] getArray() { | ||
return array; | ||
} | ||
|
||
protected Object getElement(int index) { | ||
return array[index]; | ||
} | ||
|
||
protected void putElement(int index, Object value) { | ||
float val = ((Number)value).floatValue(); | ||
array[index] = val; | ||
} | ||
|
||
public int getLength() { | ||
return array.length; | ||
} | ||
} |
Oops, something went wrong.