summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorschinz <schinz@epfl.ch>2005-04-25 15:58:18 +0000
committerschinz <schinz@epfl.ch>2005-04-25 15:58:18 +0000
commitbadd1338a02cf1bf587ca787a52f17c90aa43c89 (patch)
treecd1e88f229f9cf7dd54d82cfefca0c2fb3be5e52 /sources
parent2554f8b5f6c2f3494920a9ddfe69cde13ac9c755 (diff)
downloadscala-badd1338a02cf1bf587ca787a52f17c90aa43c89.tar.gz
scala-badd1338a02cf1bf587ca787a52f17c90aa43c89.tar.bz2
scala-badd1338a02cf1bf587ca787a52f17c90aa43c89.zip
- added allowUnsafeArrays thread-local variable...
- added allowUnsafeArrays thread-local variable in Type, to allow or disallow the creation of "unsafe" arrays by the current thread. Unsafe arrays are defined as those whose type, after erasure, is the same as the erasure of some other array type. (For example, an array of "C with D" is indistinguishable from an array of "C" after erasure, hence unsafe).
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/Type.java18
-rw-r--r--sources/scala/runtime/types/CompoundType.java6
-rw-r--r--sources/scala/runtime/types/SingleType.java6
-rw-r--r--sources/scala/runtime/types/SpecialType.java7
4 files changed, 34 insertions, 3 deletions
diff --git a/sources/scala/Type.java b/sources/scala/Type.java
index 0119d38dba..c150c6ecc5 100644
--- a/sources/scala/Type.java
+++ b/sources/scala/Type.java
@@ -43,6 +43,18 @@ abstract public class Type implements java.io.Serializable {
assert Statistics.incInstances(getClass().getName(), this);
}
+ protected static ThreadLocal unsafeArraysAllowed = new BooleanThreadLocal();
+
+ /*
+ * Allow (or not) the creation of "unsafe" arrays for the current
+ * thread.
+ */
+ public static void allowUnsafeArrays(boolean allow) {
+ unsafeArraysAllowed.set(allow
+ ? java.lang.Boolean.TRUE
+ : java.lang.Boolean.FALSE);
+ }
+
/** @meta method [?T](scala.Int) scala.Array[?T]; */
abstract public Array newArray(int size);
@@ -141,3 +153,9 @@ abstract public class Type implements java.io.Serializable {
return h;
}
}
+
+class BooleanThreadLocal extends ThreadLocal {
+ protected Object initialValue() {
+ return java.lang.Boolean.TRUE;
+ }
+}
diff --git a/sources/scala/runtime/types/CompoundType.java b/sources/scala/runtime/types/CompoundType.java
index 716b00cfe2..4dcb830b0d 100644
--- a/sources/scala/runtime/types/CompoundType.java
+++ b/sources/scala/runtime/types/CompoundType.java
@@ -32,7 +32,11 @@ public class CompoundType extends Type {
}
public Array newArray(int size) {
- throw new Error("cannot create arrays of compound types");
+ if (Type.unsafeArraysAllowed.get() == java.lang.Boolean.TRUE)
+ return (Array)
+ java.lang.reflect.Array.newInstance(components[0].clazz, size);
+ else
+ throw new Error("cannot create arrays of compound types");
}
public Object defaultValue() {
diff --git a/sources/scala/runtime/types/SingleType.java b/sources/scala/runtime/types/SingleType.java
index 635ced6be2..0bd8b398f0 100644
--- a/sources/scala/runtime/types/SingleType.java
+++ b/sources/scala/runtime/types/SingleType.java
@@ -21,7 +21,11 @@ public class SingleType extends Type {
}
public Array newArray(int size) {
- throw new Error("cannot create array of single types");
+ if (Type.unsafeArraysAllowed.get() == java.lang.Boolean.TRUE)
+ return (Array)
+ java.lang.reflect.Array.newInstance(instance.getClass(), size);
+ else
+ throw new Error("cannot create array of single types");
}
public Object defaultValue() {
diff --git a/sources/scala/runtime/types/SpecialType.java b/sources/scala/runtime/types/SpecialType.java
index 5916182fd7..6c2527570b 100644
--- a/sources/scala/runtime/types/SpecialType.java
+++ b/sources/scala/runtime/types/SpecialType.java
@@ -24,7 +24,12 @@ import scala.runtime.RunTime;
abstract public class SpecialType extends Type {
public Array newArray(int size) {
- throw new Error("cannot create arrays of special types");
+ if (Type.unsafeArraysAllowed.get() == java.lang.Boolean.TRUE)
+ return (Array)
+ java.lang.reflect.Array.newInstance(Object.class, size);
+ else
+ throw new Error("cannot create arrays of special type "
+ + "(" + getClass().getName() + ")");
}
public Object defaultValue() {