26,10 → 26,11 |
|
// $Id$ |
|
import java.lang.reflect.Array; |
import java.util.Arrays; |
import net.outlyer.plugins.Functor; |
import net.outlyer.plugins.SandboxAccessor; |
import java.io.File; |
import java.io.FileNotFoundException; |
import java.io.FileReader; |
import javax.script.ScriptException; |
import net.outlyer.plugins.SandboxAccessorImpl; |
|
/** |
* Extra facilities found in other languages or more complete |
36,162 → 37,39 |
* versions of available facilities. |
* Recommended name: lang |
*/ |
public class LanguageExtensions extends LanguageExtensions_V1 |
implements SandboxAccessor { |
|
public static final int revision = 2; |
|
// The array() family is mostly redundant since rhino translates |
// automatically as required from JavaScript arrays to Java arrays |
// but there are some edge-cases in which they might be desirable |
|
public class LanguageExtensions extends SandboxAccessorImpl { |
/** |
* Create an array of the same class as the provided object. |
* @param tplt Template for the array, must be non-null and non-Void |
* @param size Size of the array to create (can be 0) |
* @since {@link #revision} 2 |
* ScriptEngine's eval. |
* The built-in eval() appears to be less-powerful, in general they're |
* equivalent, but if the eval'ed string is obtainer from input |
* <code>eval()</code> appears to ignore it, while |
* <code>LanguageExtensions.eval()</code> does eval it. |
* @param code Code to evaluate |
* @return Eval's result |
*/ |
public <T> T[] array(final T tplt, int size) { |
if (null == tplt || Void.class == tplt.getClass()) { |
return null; |
public Object eval(final String code) { |
try { |
return getSandbox().getCurrentEngine().eval(code); |
} |
return (T[]) Array.newInstance(tplt.getClass(), size); |
} |
|
/** |
* Create an array of the provided class. |
* @param c Class of the array elements, must be non-null and different from Void.class |
* @param size Size of the array (can be 0) |
* @since {@link #revision} 2 |
*/ |
public <T> T[] array(final Class<T> c, int size) { |
return (T[]) Array.newInstance(c, size); |
} |
|
/** |
* Create an array and copy a set of elements to it. |
* Note that a JavaScript array will produce a java.lang.Object array |
* @param size Size of the array to create (if less than |
* <code>initValues.length</code>, will use |
* <code>initValues.length</code> instead) (i.e., use 0 to |
* get the same size as initValues) |
* @param initValues Initial values to put in the array |
* @since {@link #revision} 2 |
*/ |
public <T> T[] array(final T[] initValues, int size) { |
if (null == initValues) { |
return null; |
catch (final ScriptException e) { |
// FIXME: Handle better |
System.err.println("Exception: " + e.getMessage()); |
} |
final T[] a = Arrays.copyOf(initValues, Math.max(size, initValues.length)); |
if (null == a) { |
return null; |
} |
for (int i=0; i<initValues.length; ++i) { |
a[i] = initValues[i]; |
} |
return a; |
return null; |
} |
|
/** |
* Shorthand form, allows getting a Java array from a JavaScript array. |
* @param initValues |
* @since {@link #revision} 2 |
*/ |
public <T> T[] array(final T[] initValues) { |
return array(initValues, initValues.length); |
} |
|
public void var_dump(final Object obj) { |
System.out.println(var_dump_(obj)); |
} |
|
private String var_dump_(final Object obj) { |
final StringBuilder sb = new StringBuilder(); |
if (null == obj) { |
sb.append("null"); |
} |
else if (obj.getClass() == String.class) { |
sb.append("String(").append(((String)obj).length()) |
.append(") \"").append(obj).append("\""); |
} |
else if (Number.class.isInstance(obj)) { |
final Class c = obj.getClass(); |
String v; |
if (c == Double.class) { // Most probable |
v = String.format("double(%f)", (Double)obj); |
public Object include(final String fileName) { // TODO: Support jar URIs |
try { |
return getSandbox().getCurrentEngine().eval(new FileReader(new File(fileName))); |
} |
else if (c == Float.class) { |
v = String.format("float(%f)", (Float)obj); |
catch (final FileNotFoundException e) { |
// FIXME: Handle better |
System.err.println("Failed to read "+fileName); |
} |
else if (c == Long.class) { |
v = String.format("long(%d)", (Long)obj); |
catch (final ScriptException e) { |
// FIXME: Handle better |
System.err.println("Exception: " + e.getMessage()); |
} |
else if (c == Integer.class) { |
v = String.format("int(%d)", (Integer)obj); |
} |
else if (c == Short.class) { |
v = String.format("short(%d)", (Short)obj); |
} |
else if (c == Byte.class) { |
v = String.format("byte(%d)", (Byte)obj); |
} |
else { |
v = String.format("%s(%f)", c.getSimpleName(), |
((Number)obj).doubleValue()); |
} |
sb.append(v); |
return null; |
} |
else if (obj.getClass().isArray()) { |
sb.append("array(").append(java.lang.reflect.Array.getLength(obj)) |
.append(") {\n"); |
/* |
for (final Object elem : java.util.Arrays.asList(obj)) { |
sb.append(var_dump_(elem)); |
} |
*/ |
int len = java.lang.reflect.Array.getLength(obj); |
for (int i=0; i<len; ++i) { |
sb.append(" [").append(i).append("]=>\n ") |
.append(var_dump_(java.lang.reflect.Array.get(obj, i))) |
.append("\n"); |
} |
sb.append("}"); |
} |
else { |
sb.append(obj); |
} |
return sb.toString(); |
} |
|
/** |
* Apply a function to each element of an iterable collection. |
* @see #for_each(Object[], Functor) |
* @param c Collection to which to apply |
* @param f Function to apply |
* @since {@link #revision} 2 |
*/ |
public <T> void for_each(final Iterable<T> c, final Functor<Object, T> f) { |
for (final T elem : c) { |
f.apply(elem); |
} |
} |
|
/** |
* Apply a function to each element of an array |
* A JavaScript function can be used transparently as a functor, e.g.: |
* <br /> |
* <code>var arr = lang.array(new Array(1, 2, 3));<br /> |
* lang.for_each(arr, function(x) { err.println("Element: "+x); });</code>> |
* Will produce: |
* <pre>Element: 1<br />Element: 2<br />Element: 3</pre> |
* @param a Array to which to apply |
* @param f Function to apply |
* @since {@link #revision} 2 |
*/ |
public <T> void for_each(final T[] a, final Functor<Object, T> f) { |
if (null == a || null == f) { |
return; // FIXME: Error handling |
} |
for_each(Arrays.asList(a), f); |
} |
} |