Permalink
Browse files

Merge pull request #55 from edwardw/master

Made interpreted mode work again.
  • Loading branch information...
2 parents 4e1b538 + 94e418c commit d73bbb5ff3784a2caf45d32f7dc37fad67186551 @krestenkrab krestenkrab committed Jun 22, 2011
Showing with 26 additions and 1 deletion.
  1. +26 −1 src/main/java/erjang/EFun.java
@@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.regex.Pattern;
import java.util.Set;
import kilim.Pausable;
@@ -269,6 +270,7 @@ private static String erlangNameOfMethod(Method method) {
}
static Map<String, Constructor<? extends EFun>> handlers = new HashMap<String, Constructor<? extends EFun>>();
+ static final Pattern JAVA_ID = Pattern.compile("([a-z]|[A-Z]|$|_|[0-9])*"); // valid java identifier
public static EFun get_fun_with_handler(String module, String function, int arity, EFunHandler handler, ClassLoader loader) {
String signature = module + function + arity;
@@ -278,7 +280,11 @@ public static EFun get_fun_with_handler(String module, String function, int arit
get_fun_class(arity);
- String self_type = EFUN_TYPE.getInternalName() + "Handler" + arity;
+ String safe_function = JAVA_ID.matcher(function).matches() ? function : make_valid_java_id(function);
+ StringBuffer sb = new StringBuffer();
+ String self_type = sb.append(EFUN_TYPE.getInternalName())
+ .append(module).append(safe_function)
+ .append("Handler").append(arity).toString();
ClassWriter cw = new ClassWriter(true);
String super_class_name = EFUN_TYPE.getInternalName() + arity;
@@ -494,6 +500,25 @@ static void make_encode_method(ClassWriter cw, String className, int arity) {
mv.visitMaxs(4, 1);
mv.visitEnd();
}
+
+ private static String make_valid_java_id(CharSequence seq) {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < seq.length(); i++) {
+ char ch = seq.charAt(i);
+ if ((ch >= 'a' && ch <= 'z') ||
+ (ch >= 'A' && ch <= 'Z') ||
+ (ch >= '0' && ch <= '9') ||
+ ch == '_' || ch == '$') {
+ sb.append(ch);
+ } else {
+ sb.append('_').append('x');
+ if (ch < 0x10) sb.append('0');
+ sb.append(Integer.toHexString(ch).toUpperCase());
+ }
+ }
+
+ return sb.toString();
+ }
/*^^^^^^^^^^^^^^^^^^^^ Code generation of EFun{arity} ^^^^^^^^^^^^^^^^^^*/
/*==================== Code generation of EFun{arity}Exported: ==========*/

0 comments on commit d73bbb5

Please sign in to comment.