Skip to content

Commit 9fc00ed

Browse files
committed
Fixed compilation issue in playground and made it refresh during build
1 parent 67c4c17 commit 9fc00ed

15 files changed

Lines changed: 247 additions & 980 deletions

scripts/cn1playground/common/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,26 @@
300300

301301
<build>
302302
<plugins>
303+
<plugin>
304+
<groupId>org.codehaus.mojo</groupId>
305+
<artifactId>exec-maven-plugin</artifactId>
306+
<executions>
307+
<execution>
308+
<id>generate-cn1-access-registry</id>
309+
<phase>generate-sources</phase>
310+
<goals>
311+
<goal>exec</goal>
312+
</goals>
313+
<configuration>
314+
<executable>sh</executable>
315+
<workingDirectory>${project.parent.basedir}</workingDirectory>
316+
<arguments>
317+
<argument>${project.parent.basedir}/tools/generate-cn1-access-registry.sh</argument>
318+
</arguments>
319+
</configuration>
320+
</execution>
321+
</executions>
322+
</plugin>
303323
<plugin>
304324
<groupId>org.apache.maven.plugins</groupId>
305325
<artifactId>maven-compiler-plugin</artifactId>

scripts/cn1playground/common/src/main/java/bsh/BSHAllocationExpression.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,38 @@ private Object arrayNewInstance(
247247
Class<?> type, BSHArrayDimensions dimensionsNode,
248248
CallStack callstack, Interpreter interpreter) throws EvalError {
249249
if ( dimensionsNode.numUndefinedDims > 0 ) {
250-
Object proto = Array.newInstance(
251-
type, new int [dimensionsNode.numUndefinedDims] ); // zeros
252-
type = proto.getClass();
250+
type = arrayClass(type, dimensionsNode.numUndefinedDims);
253251
}
254252

255253
try {
256-
Object arr = Array.newInstance(
257-
type, dimensionsNode.definedDimensions);
254+
Object arr = allocateArray(type, dimensionsNode.definedDimensions, 0);
258255
return arr;
259256
} catch( NegativeArraySizeException e1 ) {
260257
throw new TargetError( e1, this, callstack );
261258
} catch( Exception e ) {
262259
throw new EvalException("Can't construct primitive array: "
263260
+ e.getMessage(), this, callstack, e);
261+
}
262+
}
263+
264+
private static Class<?> arrayClass(Class<?> baseType, int dimensions) {
265+
Class<?> type = baseType;
266+
for (int i = 0; i < dimensions; i++) {
267+
type = Array.newInstance(type, 0).getClass();
268+
}
269+
return type;
270+
}
271+
272+
private static Object allocateArray(Class<?> baseType, int[] dimensions, int offset) {
273+
Class<?> componentType = arrayClass(baseType, dimensions.length - offset - 1);
274+
Object array = Array.newInstance(componentType, dimensions[offset]);
275+
if (offset == dimensions.length - 1) {
276+
return array;
277+
}
278+
Object[] objectArray = (Object[]) array;
279+
for (int i = 0; i < dimensions[offset]; i++) {
280+
objectArray[i] = allocateArray(baseType, dimensions, offset + 1);
264281
}
282+
return array;
265283
}
266284
}

scripts/cn1playground/common/src/main/java/bsh/BSHType.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,10 @@ public Class<?> getType( CallStack callstack, Interpreter interpreter )
149149

150150
if ( arrayDims > 0 ) {
151151
try {
152-
// Get the type by constructing a prototype array with
153-
// arbitrary (zero) length in each dimension.
154-
int[] dims = new int[arrayDims]; // int array default zeros
155-
Object obj = Array.newInstance(
156-
null == baseType ? Object.class : baseType, dims);
157-
type = obj.getClass();
152+
// Build the array class incrementally to avoid relying on
153+
// Array.newInstance(Class, int[]), which is not available in
154+
// the JavaScript runtime subset.
155+
type = arrayClass(null == baseType ? Object.class : baseType, arrayDims);
158156
} catch(Exception e) {
159157
throw new EvalException("Couldn't construct array type",
160158
this, callstack, e);
@@ -171,6 +169,14 @@ public Class<?> getType( CallStack callstack, Interpreter interpreter )
171169
return type;
172170
}
173171

172+
private static Class<?> arrayClass(Class<?> baseType, int dimensions) {
173+
Class<?> type = baseType;
174+
for (int i = 0; i < dimensions; i++) {
175+
type = Array.newInstance(type, 0).getClass();
176+
}
177+
return type;
178+
}
179+
174180
/**
175181
baseType is used during evaluation of full type and retained for the
176182
case where we are an array type.

scripts/cn1playground/common/src/main/java/bsh/Interpreter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public void run() {
526526
}
527527

528528
if ( interactive && exitOnEOF )
529-
System.exit(0);
529+
return;
530530
}
531531

532532
// begin source and eval

scripts/cn1playground/common/src/main/java/bsh/NameSpace.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ void setLocalVariableOrProperty(final String name, final Object value,
365365
* @param recurse determines whether we will search for the variable in our
366366
* parent's scope before assigning locally.
367367
* @throws UtilEvalError the util eval error */
368-
void setVariableOrProperty(final String name, final Object value,
368+
void setVariableOrProperty(final String name, Object value,
369369
final boolean strictJava, final boolean recurse)
370370
throws UtilEvalError {
371371
// primitives should have been wrapped
372372
if (value == null)
373-
throw new InterpreterError("null variable value");
373+
value = Primitive.NULL;
374374
// Locate the variable definition if it exists.
375375
final Variable existing = this.getVariableImpl(name, recurse);
376376
// Found an existing variable here (or above if recurse allowed)

scripts/cn1playground/common/src/main/java/bsh/cn1/GeneratedCN1Access.java

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@
7171
import bsh.cn1.gen.GeneratedAccess_com_codenameone_playground;
7272
import bsh.cn1.gen.GeneratedAccess_java_io;
7373
import bsh.cn1.gen.GeneratedAccess_java_lang;
74-
import bsh.cn1.gen.GeneratedAccess_java_lang_annotation;
75-
import bsh.cn1.gen.GeneratedAccess_java_lang_invoke;
7674
import bsh.cn1.gen.GeneratedAccess_java_lang_ref;
7775
import bsh.cn1.gen.GeneratedAccess_java_lang_reflect;
7876
import bsh.cn1.gen.GeneratedAccess_java_net;
@@ -369,14 +367,6 @@ public Class<?> findClass(String name) {
369367
if (found != null) {
370368
return found;
371369
}
372-
found = GeneratedAccess_java_lang_annotation.findClass(name);
373-
if (found != null) {
374-
return found;
375-
}
376-
found = GeneratedAccess_java_lang_invoke.findClass(name);
377-
if (found != null) {
378-
return found;
379-
}
380370
found = GeneratedAccess_java_lang_ref.findClass(name);
381371
if (found != null) {
382372
return found;
@@ -618,12 +608,6 @@ public Object construct(Class<?> type, Object[] args) throws Exception {
618608
if ("java.lang".equals(packageName)) {
619609
return GeneratedAccess_java_lang.construct(type, args);
620610
}
621-
if ("java.lang.annotation".equals(packageName)) {
622-
return GeneratedAccess_java_lang_annotation.construct(type, args);
623-
}
624-
if ("java.lang.invoke".equals(packageName)) {
625-
return GeneratedAccess_java_lang_invoke.construct(type, args);
626-
}
627611
if ("java.lang.ref".equals(packageName)) {
628612
return GeneratedAccess_java_lang_ref.construct(type, args);
629613
}
@@ -858,12 +842,6 @@ public Object invokeStatic(Class<?> type, String name, Object[] args) throws Exc
858842
if ("java.lang".equals(packageName)) {
859843
return GeneratedAccess_java_lang.invokeStatic(type, name, args);
860844
}
861-
if ("java.lang.annotation".equals(packageName)) {
862-
return GeneratedAccess_java_lang_annotation.invokeStatic(type, name, args);
863-
}
864-
if ("java.lang.invoke".equals(packageName)) {
865-
return GeneratedAccess_java_lang_invoke.invokeStatic(type, name, args);
866-
}
867845
if ("java.lang.ref".equals(packageName)) {
868846
return GeneratedAccess_java_lang_ref.invokeStatic(type, name, args);
869847
}
@@ -1239,16 +1217,6 @@ public Object invoke(Object target, String name, Object[] args) throws Exception
12391217
} catch (CN1AccessException ex) {
12401218
unsupported = ex;
12411219
}
1242-
try {
1243-
return GeneratedAccess_java_lang_annotation.invoke(target, name, args);
1244-
} catch (CN1AccessException ex) {
1245-
unsupported = ex;
1246-
}
1247-
try {
1248-
return GeneratedAccess_java_lang_invoke.invoke(target, name, args);
1249-
} catch (CN1AccessException ex) {
1250-
unsupported = ex;
1251-
}
12521220
try {
12531221
return GeneratedAccess_java_lang_ref.invoke(target, name, args);
12541222
} catch (CN1AccessException ex) {
@@ -1512,12 +1480,6 @@ public Object getStaticField(Class<?> type, String name) throws Exception {
15121480
if ("java.lang".equals(packageName)) {
15131481
return GeneratedAccess_java_lang.getStaticField(type, name);
15141482
}
1515-
if ("java.lang.annotation".equals(packageName)) {
1516-
return GeneratedAccess_java_lang_annotation.getStaticField(type, name);
1517-
}
1518-
if ("java.lang.invoke".equals(packageName)) {
1519-
return GeneratedAccess_java_lang_invoke.getStaticField(type, name);
1520-
}
15211483
if ("java.lang.ref".equals(packageName)) {
15221484
return GeneratedAccess_java_lang_ref.getStaticField(type, name);
15231485
}
@@ -1890,16 +1852,6 @@ public Object getField(Object target, String name) throws Exception {
18901852
} catch (CN1AccessException ex) {
18911853
unsupported = ex;
18921854
}
1893-
try {
1894-
return GeneratedAccess_java_lang_annotation.getField(target, name);
1895-
} catch (CN1AccessException ex) {
1896-
unsupported = ex;
1897-
}
1898-
try {
1899-
return GeneratedAccess_java_lang_invoke.getField(target, name);
1900-
} catch (CN1AccessException ex) {
1901-
unsupported = ex;
1902-
}
19031855
try {
19041856
return GeneratedAccess_java_lang_ref.getField(target, name);
19051857
} catch (CN1AccessException ex) {
@@ -2220,14 +2172,6 @@ public void setStaticField(Class<?> type, String name, Object value) throws Exce
22202172
GeneratedAccess_java_lang.setStaticField(type, name, value);
22212173
return;
22222174
}
2223-
if ("java.lang.annotation".equals(packageName)) {
2224-
GeneratedAccess_java_lang_annotation.setStaticField(type, name, value);
2225-
return;
2226-
}
2227-
if ("java.lang.invoke".equals(packageName)) {
2228-
GeneratedAccess_java_lang_invoke.setStaticField(type, name, value);
2229-
return;
2230-
}
22312175
if ("java.lang.ref".equals(packageName)) {
22322176
GeneratedAccess_java_lang_ref.setStaticField(type, name, value);
22332177
return;
@@ -2676,18 +2620,6 @@ public void setField(Object target, String name, Object value) throws Exception
26762620
} catch (CN1AccessException ex) {
26772621
unsupported = ex;
26782622
}
2679-
try {
2680-
GeneratedAccess_java_lang_annotation.setField(target, name, value);
2681-
return;
2682-
} catch (CN1AccessException ex) {
2683-
unsupported = ex;
2684-
}
2685-
try {
2686-
GeneratedAccess_java_lang_invoke.setField(target, name, value);
2687-
return;
2688-
} catch (CN1AccessException ex) {
2689-
unsupported = ex;
2690-
}
26912623
try {
26922624
GeneratedAccess_java_lang_ref.setField(target, name, value);
26932625
return;

scripts/cn1playground/common/src/main/java/bsh/cn1/gen/GeneratedAccess_com_codenameone_playground.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ private static Object invokeStatic0(String name, Object[] safeArgs) throws Excep
3434
return com.codenameone.playground.PlaygroundContext.getCurrent();
3535
}
3636
}
37+
if ("interceptMethodInvocation".equals(name)) {
38+
if (matches(safeArgs, new Class<?>[]{java.lang.Object.class, java.lang.String.class, java.lang.Object[].class}, false)) {
39+
return com.codenameone.playground.PlaygroundContext.interceptMethodInvocation((java.lang.Object) safeArgs[0], (java.lang.String) safeArgs[1], (java.lang.Object[]) safeArgs[2]);
40+
}
41+
}
3742
throw unsupportedStatic(com.codenameone.playground.PlaygroundContext.class, name, safeArgs);
3843
}
3944

scripts/cn1playground/common/src/main/java/bsh/cn1/gen/GeneratedAccess_java_io.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -781,11 +781,6 @@ private static Object invoke8(java.io.PrintStream typedTarget, String name, Obje
781781
typedTarget.flush(); return null;
782782
}
783783
}
784-
if ("print".equals(name)) {
785-
if (matches(safeArgs, new Class<?>[]{java.lang.Boolean.class}, false)) {
786-
typedTarget.print(((Boolean) safeArgs[0]).booleanValue()); return null;
787-
}
788-
}
789784
if ("print".equals(name)) {
790785
if (matches(safeArgs, new Class<?>[]{java.lang.Character.class}, false)) {
791786
typedTarget.print(((Character) safeArgs[0]).charValue()); return null;
@@ -796,11 +791,6 @@ private static Object invoke8(java.io.PrintStream typedTarget, String name, Obje
796791
typedTarget.print(((Number) safeArgs[0]).doubleValue()); return null;
797792
}
798793
}
799-
if ("print".equals(name)) {
800-
if (matches(safeArgs, new Class<?>[]{java.lang.Float.class}, false)) {
801-
typedTarget.print(((Number) safeArgs[0]).floatValue()); return null;
802-
}
803-
}
804794
if ("print".equals(name)) {
805795
if (matches(safeArgs, new Class<?>[]{java.lang.Integer.class}, false)) {
806796
typedTarget.print(((Number) safeArgs[0]).intValue()); return null;

0 commit comments

Comments
 (0)