Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made interpreted mode work again. #55

Merged
merged 1 commit into from
Jun 22, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/main/java/erjang/EFun.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -269,6 +270,7 @@ static byte[] gen_fun_class_data(int arity) {
}

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;
Expand All @@ -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;
Expand Down Expand Up @@ -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: ==========*/
Expand Down