Skip to content

Commit

Permalink
anonymous fn 2
Browse files Browse the repository at this point in the history
  • Loading branch information
NeumimTo committed Sep 13, 2021
1 parent 307f05f commit aa3ac58
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 66 deletions.
11 changes: 10 additions & 1 deletion src/main/java/cz/neumimto/nts/NTScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -148,7 +149,15 @@ public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVis
for (int i = scopes.size()-1; i > 0; i--) {
Scope scope = scopes.get(i);

bb = bb.defineMethod(Scope.LAMBDA_METHOD_NAME.apply(i), void.class, Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC)
List<TypeDescription> params = new ArrayList<>();
TypeDescription typeDefinitions = bb.toTypeDescription();
params.add(typeDefinitions);
for (Variable value : scope.variables.values()) {
params.add(new TypeDescription.ForLoadedType(value.getRuntimeType()));
}

bb = bb.defineMethod(Scope.LAMBDA_METHOD_NAME.apply(i - 1), void.class, Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC)
.withParameters(params)
.intercept(new Implementation() {
@Override
public ByteCodeAppender appender(Target implementationTarget) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cz/neumimto/nts/ScriptContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ protected MethodVariableAccess getRalReturnType(ntsParser.Assignment_valuesConte
return MethodVariableAccess.INTEGER;
} else if (rval.type_comparison() != null) {
return MethodVariableAccess.INTEGER;
} else if (rval.variable_reference() != null) {
return MethodVariableAccess.of(new TypeDescription.ForLoadedType(currentScope().variables.get(rval.getText()).getRuntimeType()));
}
throw new RuntimeException("Unknown type " + rval.getText());
}
Expand Down
45 changes: 32 additions & 13 deletions src/main/java/cz/neumimto/nts/bytecode/InvokeDynamic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.jar.asm.Handle;
import net.bytebuddy.jar.asm.MethodVisitor;
import net.bytebuddy.jar.asm.Opcodes;
import net.bytebuddy.jar.asm.Type;

import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.*;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -23,6 +22,15 @@ public class InvokeDynamic implements StackManipulation {
private final Map<String, Variable> fnVars;
private ScriptContext scriptContext;

private static Method metaFactory;
static {
try {
metaFactory = LambdaMetafactory.class.getDeclaredMethod("metafactory", MethodHandles.Lookup.class, String.class, MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}

public InvokeDynamic(ScriptContext scriptContext, Map<String, Variable> fnVars) {
this.scriptContext = scriptContext;
this.fnVars = fnVars;
Expand All @@ -41,20 +49,31 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context implementa
.collect(Collectors.joining());
descriptor = descriptor.replaceAll("%s",scriptContext.getInsnType().getDescriptor() + k);

methodVisitor.visitInvokeDynamicInsn("run",


// methodVisitor.visitInvokeDynamicInsn("run",
// "(Lcz/neumimto/nts/DecompileTest;IZ)Ljava/lang/Runnable;",
// new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;", false),
// Type.getType("()V"),
// new Handle(Opcodes.H_INVOKEVIRTUAL, "cz/neumimto/nts/DecompileTest", "lambda$a$0", "(IZ)V", false),
// Type.getType("()V"));
//



methodVisitor.visitInvokeDynamicInsn("run",
descriptor,
new Handle(Opcodes.H_INVOKESTATIC,
new TypeDescription.ForLoadedType(LambdaMetafactory.class).getInternalName(),
new Handle(Opcodes.H_INVOKESTATIC, new TypeDescription.ForLoadedType(LambdaMetafactory.class).getInternalName(),
"metafactory",
MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, String.class, Object[].class).toMethodDescriptorString(),
Type.getMethodDescriptor(metaFactory),
false),
Type.VOID_TYPE,
new Handle(Opcodes.H_INVOKESPECIAL,
Type.getType("()V"),
new Handle(Opcodes.H_INVOKEVIRTUAL,
scriptContext.getInsnType().getInternalName(),
Scope.LAMBDA_METHOD_NAME.apply(scriptContext.getScopes().size() - 1),
Scope.LAMBDA_METHOD_NAME.apply(scriptContext.getScopes().size() - 2),
"("+k+")V",
false),
Type.VOID_TYPE);
return null;
Type.getType("()V"));
return new StackManipulation.Size(1, 1);
}
}
5 changes: 0 additions & 5 deletions src/test/java/cz/neumimto/nts/DecompileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ public Tests.Result a() {
// boolean b = false;
// boolean a = true;
int ldc = 70000;
int b = 55000;
String o = ";";

boolean c = ldc < 70000;

Runnable r = () -> {
int w = ldc;
boolean a =c;
A.a(null,null);
String k = "inside";
};
String w = "after";
int k = 0;
MP.mp(r);

// int i = (int) (1 * Math.random());
// int b = 30;
Expand Down
94 changes: 47 additions & 47 deletions src/test/java/cz/neumimto/nts/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,54 @@ public Result test(){
@org.junit.jupiter.api.Test
public void test() throws Exception {
String k = """
@text = "test"
@bool_t = t
@bool_f = false
# @text = "test"
# @bool_t = t
# @bool_f = false
@int = 10000
#C O M M E N T
@param = call{string=@text, func=method{int=50}}
IF test{int= @int, string=call{string=@text}}
@int2 = 5000
END
@text="re-assigment"
print{val=@text}
IF @bool_f
@int3=700000
END
@x = list{size=3}
FOREACH @entity IN @x
print{val=@entity}
END
print{val="POP NEXT"}
list{size=50}
FOREACH @entity IN list{size=5}
print{val=@entity}
END
print{val="XXXX"}
FOREACH @entity IN @x
IF True
print{val="CCC"}
END
END
IF @int <= 70000
print{val="@int <= 70000"}
END
# #C O M M E N T
#
# @param = call{string=@text, func=method{int=50}}
#
# IF test{int= @int, string=call{string=@text}}
# @int2 = 5000
# END
#
# @text="re-assigment"
# print{val=@text}
#
# IF @bool_f
# @int3=700000
# END
#
# @x = list{size=3}
# FOREACH @entity IN @x
# print{val=@entity}
# END
#
# print{val="POP NEXT"}
# list{size=50}
#
# FOREACH @entity IN list{size=5}
# print{val=@entity}
# END
#
# print{val="XXXX"}
#
# FOREACH @entity IN @x
# IF True
# print{val="CCC"}
# END
# END
#
# IF @int <= 70000
# print{val="@int <= 70000"}
# END
@lesser = @int <= 70000
@function = fn @int @lesser
print{val="FN"}
@k = @int
END
Expand All @@ -88,12 +88,12 @@ public void test() throws Exception {
Class aClass = script.parseScript(k);
Object o = aClass.newInstance();
try {
o.getClass().getDeclaredField("A").set(o, new A());
o.getClass().getDeclaredField("B").set(o, new B());
o.getClass().getDeclaredField("C").set(o, new C());
o.getClass().getDeclaredField("L").set(o, new L());
// o.getClass().getDeclaredField("A").set(o, new A());
// o.getClass().getDeclaredField("B").set(o, new B());
// o.getClass().getDeclaredField("C").set(o, new C());
// o.getClass().getDeclaredField("L").set(o, new L());
o.getClass().getDeclaredField("P").set(o, new P());
o.getClass().getDeclaredMethod("test").invoke(o);
// o.getClass().getDeclaredMethod("test").invoke(o);
} catch (Throwable t) {
t.printStackTrace();
}
Expand Down

0 comments on commit aa3ac58

Please sign in to comment.