Skip to content

Commit e0e8307

Browse files
committed
mapguard compiled version
1 parent 023e84d commit e0e8307

12 files changed

Lines changed: 146 additions & 53 deletions

File tree

compiler/src/main/java/com/github/mustachejava/compile/CodeCompiler.java renamed to compiler/src/main/java/com/github/mustachejava/asm/CodeCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.mustachejava.compile;
1+
package com.github.mustachejava.asm;
22

33
import com.github.mustachejava.Code;
44
import org.objectweb.asm.ClassWriter;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.mustachejava.asm;
2+
3+
import com.github.mustachejava.reflect.Guard;
4+
import org.objectweb.asm.ClassWriter;
5+
import org.objectweb.asm.Label;
6+
import org.objectweb.asm.Opcodes;
7+
import org.objectweb.asm.commons.GeneratorAdapter;
8+
9+
import java.util.List;
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
12+
/**
13+
* Optimizable guards
14+
*/
15+
public interface CompilableGuard extends Guard, Opcodes {
16+
void addGuard(Label returnFalse,
17+
GeneratorAdapter gm,
18+
GeneratorAdapter cm,
19+
GeneratorAdapter sm,
20+
ClassWriter cw,
21+
AtomicInteger atomicId,
22+
String className,
23+
List<Object> cargs);
24+
}

compiler/src/main/java/com/github/mustachejava/compile/CompiledCodes.java renamed to compiler/src/main/java/com/github/mustachejava/asm/CompiledCodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.mustachejava.compile;
1+
package com.github.mustachejava.asm;
22

33
import java.io.Writer;
44

compiler/src/main/java/com/github/mustachejava/compile/GuardCompiler.java renamed to compiler/src/main/java/com/github/mustachejava/asm/GuardCompiler.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.mustachejava.compile;
1+
package com.github.mustachejava.asm;
22

33
import com.github.mustachejava.reflect.Guard;
44
import org.objectweb.asm.ClassWriter;
@@ -48,37 +48,44 @@ public static Guard compile(String source, Iterable<CompilableGuard> guards) thr
4848
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, internalClassName, null, "java/lang/Object", new String[]{Guard.class.getName().replace(".", "/")});
4949
cw.visitSource(source, null);
5050

51-
GeneratorAdapter cm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("void <init> ()"), null, null, cw);
51+
// Constructor
52+
GeneratorAdapter cm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("void <init> (Object[])"), null, null, cw);
5253
cm.loadThis();
5354
cm.invokeConstructor(Type.getType(Object.class), getMethod("void <init> ()"));
54-
cm.returnValue();
55-
cm.endMethod();
5655

56+
// Static initializer
5757
GeneratorAdapter sm = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, getMethod("void <clinit> ()"), null, null, cw);
5858

59-
{
60-
GeneratorAdapter gm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("boolean apply(Object[])"), null, null, cw);
61-
Label returnFalse = new Label();
62-
for (CompilableGuard guard : guards) {
63-
guard.addGuard(returnFalse, gm, sm, cw, id, internalClassName);
64-
}
59+
// Method implementation
60+
GeneratorAdapter gm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("boolean apply(Object[])"), null, null, cw);
61+
Label returnFalse = new Label();
6562

66-
// Makes it through the guard, success
67-
gm.push(true);
68-
gm.returnValue();
69-
// Jumps to returnFalse, failure
70-
gm.visitLabel(returnFalse);
71-
gm.push(false);
72-
gm.returnValue();
73-
gm.endMethod();
63+
// Add each guard in the list
64+
List<Object> cargs = new ArrayList<Object>();
65+
for (CompilableGuard guard : guards) {
66+
guard.addGuard(returnFalse, gm, cm, sm, cw, id, internalClassName, cargs);
7467
}
7568

69+
// Makes it through the guard, success
70+
gm.push(true);
71+
gm.returnValue();
72+
// Jumps to returnFalse, failure
73+
gm.visitLabel(returnFalse);
74+
gm.push(false);
75+
gm.returnValue();
76+
gm.endMethod();
77+
78+
// Close the constructor
79+
cm.returnValue();
80+
cm.endMethod();
81+
82+
// Close the static initializer
7683
sm.returnValue();
7784
sm.endMethod();
7885

7986
cw.visitEnd();
8087
Class<?> aClass = defineClass(className, cw.toByteArray());
81-
return (Guard) aClass.getConstructor().newInstance();
88+
return (Guard) aClass.getConstructor(Object[].class).newInstance((Object) cargs.toArray(new Object[cargs.size()]));
8289
}
8390

8491
private static final DefiningClassLoader cl = new DefiningClassLoader();

compiler/src/main/java/com/github/mustachejava/codes/DefaultMustache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.github.mustachejava.Mustache;
55
import com.github.mustachejava.MustacheFactory;
66
import com.github.mustachejava.TemplateContext;
7-
import com.github.mustachejava.compile.CodeCompiler;
8-
import com.github.mustachejava.compile.CompiledCodes;
7+
import com.github.mustachejava.asm.CodeCompiler;
8+
import com.github.mustachejava.asm.CompiledCodes;
99
import org.objectweb.asm.Opcodes;
1010

1111
import java.io.Writer;

compiler/src/main/java/com/github/mustachejava/compile/CompilableGuard.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

compiler/src/main/java/com/github/mustachejava/reflect/ClassGuard.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.github.mustachejava.reflect;
22

3-
import com.github.mustachejava.compile.CompilableGuard;
3+
import com.github.mustachejava.asm.CompilableGuard;
44
import org.objectweb.asm.ClassWriter;
55
import org.objectweb.asm.Label;
66
import org.objectweb.asm.Type;
77
import org.objectweb.asm.commons.GeneratorAdapter;
88
import org.objectweb.asm.commons.Method;
99

10+
import java.util.List;
1011
import java.util.concurrent.atomic.AtomicInteger;
1112

1213
import static org.objectweb.asm.commons.GeneratorAdapter.LE;
@@ -49,16 +50,17 @@ public boolean apply(Object[] scopes) {
4950
}
5051

5152
@Override
52-
public void addGuard(Label returnFalse, GeneratorAdapter gm, GeneratorAdapter sm, ClassWriter cw, AtomicInteger atomicId, String className) {
53+
public void addGuard(Label returnFalse, GeneratorAdapter gm, GeneratorAdapter cm, GeneratorAdapter sm, ClassWriter cw, AtomicInteger atomicId, String className, List<Object> cargs) {
5354
int id = atomicId.incrementAndGet();
5455

5556
// Add the field for the class guard
56-
cw.visitField(ACC_PUBLIC | ACC_STATIC, "classGuard" + id, "Ljava/lang/Class;", null, null);
57+
String fieldName = "classGuard" + id;
58+
cw.visitField(ACC_PUBLIC | ACC_STATIC, fieldName, "Ljava/lang/Class;", null, null);
5759

5860
// Initialize the field
5961
sm.push(classGuard.getName());
6062
sm.invokeStatic(Type.getType(Class.class), Method.getMethod("Class forName(String)"));
61-
sm.putStatic(Type.getType(className), "classGuard" + id, Type.getType(Class.class));
63+
sm.putStatic(Type.getType(className), fieldName, Type.getType(Class.class));
6264

6365
// Check that the scopes are not null
6466
gm.loadArg(0); // scopes
@@ -77,7 +79,7 @@ public void addGuard(Label returnFalse, GeneratorAdapter gm, GeneratorAdapter sm
7779
int scopeLocal = gm.newLocal(Type.getType(Object.class));
7880
gm.storeLocal(scopeLocal);
7981
int classGuardLocal = gm.newLocal(Type.getType(Class.class));
80-
gm.getStatic(Type.getType(className), "classGuard" + id, Type.getType(Class.class));
82+
gm.getStatic(Type.getType(className), fieldName, Type.getType(Class.class));
8183
gm.storeLocal(classGuardLocal);
8284

8385
// Check to see if the scope is null

compiler/src/main/java/com/github/mustachejava/reflect/DepthGuard.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.github.mustachejava.reflect;
22

3-
import com.github.mustachejava.compile.CompilableGuard;
3+
import com.github.mustachejava.asm.CompilableGuard;
44
import org.objectweb.asm.ClassWriter;
55
import org.objectweb.asm.Label;
66
import org.objectweb.asm.commons.GeneratorAdapter;
77

88
import javax.annotation.Nullable;
9+
import java.util.List;
910
import java.util.concurrent.atomic.AtomicInteger;
1011

1112
/**
@@ -38,7 +39,7 @@ public boolean apply(@Nullable Object[] objects) {
3839
}
3940

4041
@Override
41-
public void addGuard(Label returnFalse, GeneratorAdapter gm, GeneratorAdapter sm, ClassWriter cw, AtomicInteger id, String className) {
42+
public void addGuard(Label returnFalse, GeneratorAdapter gm, GeneratorAdapter cm, GeneratorAdapter sm, ClassWriter cw, AtomicInteger atomicId, String className, List<Object> cargs) {
4243
// If objects is null return false
4344
gm.loadArg(0);
4445
gm.ifNull(returnFalse);

compiler/src/main/java/com/github/mustachejava/reflect/DotGuard.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.github.mustachejava.reflect;
22

3-
import com.github.mustachejava.compile.CompilableGuard;
3+
import com.github.mustachejava.asm.CompilableGuard;
44
import org.objectweb.asm.ClassWriter;
55
import org.objectweb.asm.Label;
66
import org.objectweb.asm.commons.GeneratorAdapter;
77

8+
import java.util.List;
89
import java.util.concurrent.atomic.AtomicInteger;
910

1011
/**
@@ -42,7 +43,7 @@ public boolean apply(Object[] objects) {
4243
}
4344

4445
@Override
45-
public void addGuard(Label returnFalse, GeneratorAdapter gm, GeneratorAdapter cm, ClassWriter cw, AtomicInteger id, String className) {
46+
public void addGuard(Label returnFalse, GeneratorAdapter gm, GeneratorAdapter cm, GeneratorAdapter sm, ClassWriter cw, AtomicInteger atomicId, String className, List<Object> cargs) {
4647
// do nothing and it is assumed true
4748
}
4849
}

compiler/src/main/java/com/github/mustachejava/reflect/GuardedWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.mustachejava.reflect;
22

3-
import com.github.mustachejava.compile.GuardCompiler;
3+
import com.github.mustachejava.asm.GuardCompiler;
44
import com.github.mustachejava.util.GuardException;
55
import com.github.mustachejava.util.Wrapper;
66

0 commit comments

Comments
 (0)