Created
September 23, 2013 18:21
-
-
Save qmx/6674705 to your computer and use it in GitHub Desktop.
Jitescript with macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.qmx.jipsy; | |
import me.qmx.jitescript.CodeBlock; | |
import me.qmx.jitescript.JiteClass; | |
import java.io.PrintStream; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import static me.qmx.jitescript.util.CodegenUtils.c; | |
import static me.qmx.jitescript.util.CodegenUtils.ci; | |
import static me.qmx.jitescript.util.CodegenUtils.p; | |
import static me.qmx.jitescript.util.CodegenUtils.sig; | |
public class HelloJite { | |
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | |
JiteClass jiteClass = new JiteClass("Hello") {{ | |
defineMethod("main", ACC_STATIC | ACC_PUBLIC, sig(void.class, String[].class), new MyCodeBlock() { | |
{ | |
ldc("hello french fries"); | |
myAprintln(); | |
voidreturn(); | |
} | |
}); | |
}}; | |
Class<?> clazz = new DynamicClassLoader().define(jiteClass); | |
Method mainMethod = clazz.getMethod("main", String[].class); | |
mainMethod.invoke(null, (Object) new String[]{}); | |
} | |
public static class DynamicClassLoader extends ClassLoader { | |
public Class<?> define(JiteClass jiteClass) { | |
byte[] classBytes = jiteClass.toBytes(); | |
return super.defineClass(c(jiteClass.getClassName()), classBytes, 0, classBytes.length); | |
} | |
} | |
/** | |
* you can add your macros under something that inherits from CodeBlock | |
*/ | |
public static class MyCodeBlock extends CodeBlock { | |
public void myAprintln() { | |
getstatic(p(System.class), "out", ci(PrintStream.class)); | |
swap(); | |
invokevirtual(p(PrintStream.class), "println", sig(void.class, Object.class)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment