|
| 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