Skip to content

Commit 46c5c05

Browse files
committed
Fixed lambda and inner class processing
1 parent 8caa232 commit 46c5c05

83 files changed

Lines changed: 73045 additions & 24269 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

scripts/cn1playground/common/src/main/css/theme.css

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,58 @@ TitleArea {
2020
background: #e5e7eb;
2121
}
2222

23+
PlaygroundForm, PlaygroundContent {
24+
background: #f5f7fb;
25+
}
26+
27+
PlaygroundFormDark, PlaygroundContentDark {
28+
background: #111827;
29+
}
30+
31+
PlaygroundToolbar {
32+
background: #e5e7eb;
33+
padding: 1mm 2mm 1mm 2mm;
34+
}
35+
36+
PlaygroundToolbarDark {
37+
background: #111827;
38+
padding: 1mm 2mm 1mm 2mm;
39+
}
40+
41+
PlaygroundTitle {
42+
color: #111827;
43+
background: #e5e7eb;
44+
font-family: "native:MainLight";
45+
font-size: 6mm;
46+
}
47+
48+
PlaygroundTitleDark {
49+
color: #f8fafc;
50+
background: #111827;
51+
font-family: "native:MainLight";
52+
font-size: 6mm;
53+
}
54+
55+
PlaygroundPanel {
56+
background: white;
57+
margin: 1mm;
58+
padding: 1mm;
59+
}
60+
61+
PlaygroundPanelDark {
62+
background: #1f2937;
63+
margin: 1mm;
64+
padding: 1mm;
65+
}
66+
67+
PlaygroundPreview {
68+
background: white;
69+
}
70+
71+
PlaygroundPreviewDark {
72+
background: #0f172a;
73+
}
74+
2375
/** Style for Dialog body */
2476
DialogBody {
2577
font-family: "native:MainLight";
@@ -58,6 +110,28 @@ SideCommand {
58110
border-bottom: 2px solid #cccccc;
59111
}
60112

113+
PlaygroundSideCommand {
114+
padding: 1mm;
115+
border: none;
116+
text-decoration: none;
117+
color: #111827;
118+
background: white;
119+
font-family: "native:MainLight";
120+
font-size: 4mm;
121+
border-bottom: 2px solid #cccccc;
122+
}
123+
124+
PlaygroundSideCommandDark {
125+
padding: 1mm;
126+
border: none;
127+
text-decoration: none;
128+
color: #e5e7eb;
129+
background: #0f172a;
130+
font-family: "native:MainLight";
131+
font-size: 4mm;
132+
border-bottom: 2px solid #1f2937;
133+
}
134+
61135
PlaygroundMenuSection {
62136
padding: 2mm 1mm 0.5mm 1mm;
63137
margin: 1mm 0 0 0;
@@ -66,6 +140,14 @@ PlaygroundMenuSection {
66140
background: white;
67141
}
68142

143+
PlaygroundMenuSectionDark {
144+
padding: 2mm 1mm 0.5mm 1mm;
145+
margin: 1mm 0 0 0;
146+
border: none;
147+
border-bottom: 1px solid #334155;
148+
background: #0f172a;
149+
}
150+
69151
PlaygroundMenuSectionTitle {
70152
color: #6b7280;
71153
font-family: "native:MainBold";
@@ -74,9 +156,25 @@ PlaygroundMenuSectionTitle {
74156
padding: 0 0 1mm 0;
75157
}
76158

159+
PlaygroundMenuSectionTitleDark {
160+
color: #94a3b8;
161+
font-family: "native:MainBold";
162+
font-size: 2.6mm;
163+
text-decoration: none;
164+
padding: 0 0 1mm 0;
165+
}
166+
77167
PlaygroundMenuEmpty {
78168
color: #6b7280;
79169
font-family: "native:MainLight";
80170
font-size: 3mm;
81171
padding: 1mm;
82172
}
173+
174+
PlaygroundMenuEmptyDark {
175+
color: #94a3b8;
176+
font-family: "native:MainLight";
177+
font-size: 3mm;
178+
padding: 1mm;
179+
background: #0f172a;
180+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package bsh.cn1;
2+
3+
import bsh.EvalError;
4+
import bsh.Interpreter;
5+
import bsh.NameSpace;
6+
import bsh.Primitive;
7+
import bsh.UtilEvalError;
8+
9+
public final class CN1LambdaSupport {
10+
private static final ThreadLocal<Interpreter> CURRENT_INTERPRETER = new ThreadLocal<Interpreter>();
11+
12+
private CN1LambdaSupport() {
13+
}
14+
15+
public static void pushInterpreter(Interpreter interpreter) {
16+
CURRENT_INTERPRETER.set(interpreter);
17+
}
18+
19+
public static void clearInterpreter() {
20+
CURRENT_INTERPRETER.remove();
21+
}
22+
23+
public static LambdaValue lambda(String[] parameterNames, String bodySource) {
24+
Interpreter interpreter = CURRENT_INTERPRETER.get();
25+
if (interpreter == null) {
26+
throw new IllegalStateException("No active BeanShell interpreter available for lambda capture.");
27+
}
28+
return new LambdaValue(interpreter, interpreter.getNameSpace(), sanitizeParams(parameterNames), bodySource == null ? "" : bodySource);
29+
}
30+
31+
private static String[] sanitizeParams(String[] parameterNames) {
32+
if (parameterNames == null || parameterNames.length == 0) {
33+
return new String[0];
34+
}
35+
String[] copy = new String[parameterNames.length];
36+
for (int i = 0; i < parameterNames.length; i++) {
37+
copy[i] = parameterNames[i] == null ? "" : parameterNames[i].trim();
38+
}
39+
return copy;
40+
}
41+
42+
public static Object coerceResult(Object value, Class<?> type) {
43+
if (type == null || type == Void.TYPE) {
44+
return null;
45+
}
46+
String typeName = type.getName();
47+
if (value == null || value == Primitive.NULL || value == Primitive.VOID) {
48+
return defaultValue(type);
49+
}
50+
value = Primitive.unwrap(value);
51+
if ("boolean".equals(typeName) || type == Boolean.class) {
52+
return value instanceof Boolean ? value : defaultValue(type);
53+
}
54+
if ("char".equals(typeName) || type == Character.class) {
55+
return value instanceof Character ? value : defaultValue(type);
56+
}
57+
if ("byte".equals(typeName) || type == Byte.class
58+
|| "short".equals(typeName) || type == Short.class
59+
|| "int".equals(typeName) || type == Integer.class
60+
|| "long".equals(typeName) || type == Long.class
61+
|| "float".equals(typeName) || type == Float.class
62+
|| "double".equals(typeName) || type == Double.class) {
63+
return value instanceof Number ? value : defaultValue(type);
64+
}
65+
return value;
66+
}
67+
68+
private static Object defaultValue(Class<?> type) {
69+
if (!type.isPrimitive()) {
70+
return null;
71+
}
72+
String typeName = type.getName();
73+
if ("boolean".equals(typeName)) {
74+
return Boolean.FALSE;
75+
}
76+
if ("char".equals(typeName)) {
77+
return Character.valueOf((char) 0);
78+
}
79+
if ("byte".equals(typeName)) {
80+
return Byte.valueOf((byte) 0);
81+
}
82+
if ("short".equals(typeName)) {
83+
return Short.valueOf((short) 0);
84+
}
85+
if ("int".equals(typeName)) {
86+
return Integer.valueOf(0);
87+
}
88+
if ("long".equals(typeName)) {
89+
return Long.valueOf(0L);
90+
}
91+
if ("float".equals(typeName)) {
92+
return Float.valueOf(0f);
93+
}
94+
if ("double".equals(typeName)) {
95+
return Double.valueOf(0d);
96+
}
97+
return null;
98+
}
99+
100+
public static final class LambdaValue {
101+
private final Interpreter interpreter;
102+
private final NameSpace parentNameSpace;
103+
private final String[] parameterNames;
104+
private final String bodySource;
105+
106+
LambdaValue(Interpreter interpreter, NameSpace parentNameSpace, String[] parameterNames, String bodySource) {
107+
this.interpreter = interpreter;
108+
this.parentNameSpace = parentNameSpace;
109+
this.parameterNames = parameterNames;
110+
this.bodySource = bodySource;
111+
}
112+
113+
public Object invoke(Object[] args) throws EvalError {
114+
NameSpace lambdaNs = new NameSpace(parentNameSpace, interpreter.getClassManager(), "lambda");
115+
Object[] safeArgs = args == null ? new Object[0] : args;
116+
int bindCount = Math.min(parameterNames.length, safeArgs.length);
117+
try {
118+
for (int i = 0; i < bindCount; i++) {
119+
lambdaNs.setVariable(parameterNames[i], safeArgs[i], false);
120+
}
121+
} catch (UtilEvalError ex) {
122+
throw ex.toEvalError(null, null);
123+
}
124+
synchronized (interpreter) {
125+
return Primitive.unwrap(interpreter.eval(bodySource, lambdaNs));
126+
}
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)