Skip to content

Commit 383b98f

Browse files
committed
optimised PeripheralBlockEntity.java more and fixed memory leak
1 parent 642d1cb commit 383b98f

10 files changed

Lines changed: 90 additions & 55 deletions

File tree

src/main/java/com/redtoast/APIS/EventAPI.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.redtoast.APIS;
22

33
import com.redtoast.Computer;
4-
import com.redtoast.neet.Debugger;
54
import com.redtoast.simulation.annotations.Exposed;
65
import com.redtoast.simulation.base.API;
76
import com.redtoast.simulation.base.ExposedError;
@@ -10,7 +9,6 @@
109
import com.redtoast.simulation.events.EventManager;
1110
import com.redtoast.simulation.value.Value;
1211
import com.redtoast.simulation.value.ValueTypes.List;
13-
import org.jetbrains.annotations.NotNull;
1412

1513
public class EventAPI implements API {
1614
EventManager eventManager;
@@ -67,6 +65,11 @@ public Value<?> getFirst(String category, String filter) {
6765
}
6866
}
6967

68+
@Exposed
69+
public Value<?> getFucked(){
70+
return Value.of(1,2,3,4,5,6,7,8,9,0);
71+
}
72+
7073
@Exposed
7174
public void clear(){
7275
eventManager.reset();

src/main/java/com/redtoast/Lua/LuaGlobals.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public LuaGlobals(GlobalManager globalManager){
199199
public void rawset( LuaValue key, LuaValue value ) {
200200
super.rawset(key, value);
201201
if (Objects.equals(this, value)) return;
202-
if (lua52!=null && !noForwarding && manager!=null) manager.put(uuid, lua52.toValue(key), lua52.toValue(value));
202+
//if (lua52!=null && !noForwarding && manager!=null) manager.put(uuid, lua52.toValue(key), lua52.toValue(value));
203203
}
204204

205205
@Override

src/main/java/com/redtoast/Lua/LuaTranslater.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ public Varargs fromValueWithoutMetadata(Value var) {
7171
});
7272
return newTable;
7373
case LIST, TUPLE:
74-
assert var.getValue() instanceof List;
75-
int size = ((List) var.getValue()).size();
74+
int size = var.getType()==VarType.LIST ? var.toList().size() : var.toTuple().size();
7675
LuaValue[] values = new LuaValue[size];
7776
for (int i = 0; i < size; i++){
78-
Varargs rawValue = fromValue(((List) var.pack().getValue()).get(i));
77+
Varargs rawValue = fromValue(var.toList().get(i));
7978
values[i] = (LuaValue) rawValue;
8079
}
8180
LuaTable array = LuaValue.listOf(values);
82-
if (var.getValue() instanceof Tuple){
81+
if (var.getType()==VarType.TUPLE){
8382
return array.unpack();
8483
}else{
8584
return array;

src/main/java/com/redtoast/blocks/Generics/PeripheralBlockEntity.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import com.redtoast.simulation.APILoader;
77
import com.redtoast.simulation.Runtime;
88
import com.redtoast.simulation.base.Exposable;
9-
import com.redtoast.simulation.parameter.FunctionInput;
109
import com.redtoast.simulation.value.Value;
11-
import com.redtoast.simulation.value.VarType;
1210
import net.minecraft.block.BlockState;
1311
import net.minecraft.block.entity.BlockEntity;
1412
import net.minecraft.block.entity.BlockEntityType;
@@ -17,10 +15,7 @@
1715
import net.minecraft.util.math.BlockPos;
1816
import org.jetbrains.annotations.NotNull;
1917

20-
import java.util.LinkedList;
21-
import java.util.List;
2218
import java.util.UUID;
23-
import java.util.concurrent.atomic.AtomicReference;
2419

2520
public class PeripheralBlockEntity extends BlockEntity implements PeripheralProvider, PipeRenderSource, Exposable {
2621
private final String typeName;
@@ -31,6 +26,11 @@ public PeripheralBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState s
3126
super(type, pos, state);
3227
this.typeName = typeName;
3328
functionTable = APILoader.getFunctions(this);
29+
if (doAutoCache() && functionTable.length>0) APILoader.preemptiveCache(this.getClass());
30+
}
31+
32+
public boolean doAutoCache(){
33+
return true;
3434
}
3535

3636
@Override

src/main/java/com/redtoast/simulation/APILoader.java

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -617,33 +617,73 @@ public static String getName(Method method){
617617

618618
public static Value<?> searchAndCall(Exposable obj, Runtime runtime, String name, Value<?>... args) throws LoaderError {
619619
Class<?> _class = obj.getClass();
620-
Method[] buffer = _class.getMethods();
621-
LinkedList<String> errors = new LinkedList<>();
622-
LinkedList<String> names = new LinkedList<>();
623-
for (Method method : buffer){
624-
if (getName(method).equals(name)){
625-
ParameterRules rules = rulesFromMethod(method);
626-
ParameterCheckReturn retur = ParameterRules.checkParameters(args, rules, runtime);
627-
if (!retur.isError()){
628-
return sandboxFunction(method, obj, rules, runtime).invoke(retur.getFunctionInput());
629-
}else{
630-
errors.add(retur.getMessage());
631-
names.add(name + rules.toString(runtime));
620+
if (cache.containsKey(_class)) {
621+
LoaderCache cachedObject = cache.get(_class);
622+
for (StaticFunctionCache functionCache : cachedObject.functions()){
623+
if (functionCache.functionName().equals(name)){
624+
ParameterCheckReturn retur = ParameterRules.checkParameters(args, functionCache.ruleset(), runtime);
625+
if (!retur.isError()){
626+
return sandboxFunction(functionCache.method(), obj, functionCache.ruleset(), runtime).invoke(retur.getFunctionInput());
627+
}else{
628+
return Value.asError(retur.getMessage());
629+
}
632630
}
633631
}
634-
}
635-
if (errors.isEmpty()){
636-
return Value.asError("Cant Find Function '"+name+"'");
637-
}else if (errors.size()==1){
638-
return Value.asError(errors.getFirst());
632+
LinkedList<String> errors = new LinkedList<>();
633+
LinkedList<String> names = new LinkedList<>();
634+
for (PackedFunctionCache pFunctionCache : cachedObject.packedFunctions()){
635+
if (pFunctionCache.name.equals(name)){
636+
for (StaticFunctionCache functionCache : pFunctionCache.functions){
637+
ParameterCheckReturn retur = ParameterRules.checkParameters(args, functionCache.ruleset(), runtime);
638+
if (!retur.isError()){
639+
return sandboxFunction(functionCache.method(), obj, functionCache.ruleset(), runtime).invoke(retur.getFunctionInput());
640+
}else{
641+
errors.add(retur.getMessage());
642+
names.add(name + functionCache.ruleset().toString(runtime));
643+
}
644+
}
645+
}
646+
}
647+
if (!errors.isEmpty()){
648+
names.sort(String::compareTo);
649+
StringBuilder error = new StringBuilder(errors.get(new Random().nextInt(errors.size())));
650+
for (String string : names){
651+
error.append('\n');
652+
error.append(string);
653+
}
654+
return new Exception(error.toString()).asValue();
655+
}else{
656+
return Value.asError("Cant Find Function '"+name+"'");
657+
}
639658
}else{
640-
names.sort(String::compareTo);
641-
StringBuilder error = new StringBuilder(errors.get(new Random().nextInt(errors.size())));
642-
for (String string : names){
643-
error.append('\n');
644-
error.append(string);
659+
Method[] buffer = _class.getMethods();
660+
LinkedList<String> errors = new LinkedList<>();
661+
LinkedList<String> names = new LinkedList<>();
662+
for (Method method : buffer){
663+
if (getName(method).equals(name)){
664+
ParameterRules rules = rulesFromMethod(method);
665+
ParameterCheckReturn retur = ParameterRules.checkParameters(args, rules, runtime);
666+
if (!retur.isError()){
667+
return sandboxFunction(method, obj, rules, runtime).invoke(retur.getFunctionInput());
668+
}else{
669+
errors.add(retur.getMessage());
670+
names.add(name + rules.toString(runtime));
671+
}
672+
}
673+
}
674+
if (errors.isEmpty()){
675+
return Value.asError("Cant Find Function '"+name+"'");
676+
}else if (errors.size()==1){
677+
return Value.asError(errors.getFirst());
678+
}else{
679+
names.sort(String::compareTo);
680+
StringBuilder error = new StringBuilder(errors.get(new Random().nextInt(errors.size())));
681+
for (String string : names){
682+
error.append('\n');
683+
error.append(string);
684+
}
685+
return new Exception(error.toString()).asValue();
645686
}
646-
return new Exception(error.toString()).asValue();
647687
}
648688
}
649689

src/main/java/com/redtoast/simulation/GlobalManager.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,4 @@ public void put(UUID uuid, Value key, Value value){
6767
}
6868

6969
public Runtime getParent() {return runtime;}
70-
71-
public GlobalGeneric[] getSubGlobals(){
72-
return subGlobals.values().toArray(new GlobalGeneric[0]);
73-
}
7470
}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.redtoast.simulation.base;
22

3-
import com.redtoast.simulation.APILoader;
43
import com.redtoast.simulation.value.ValueTypes.Table;
54

65
import java.lang.reflect.Method;
@@ -10,7 +9,4 @@ default void onCall(LangThread thread, Method method){}
109
default Table postProcessing(Table self){
1110
return self;
1211
}
13-
default boolean cacheSelf(){
14-
return APILoader.preemptiveCache(this.getClass());
15-
}
1612
}

src/main/java/com/redtoast/simulation/events/EventGeneric.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import java.util.LinkedList;
1414

1515
public class EventGeneric implements ValueConvertible<List> {
16-
private String Name;
17-
private List args;
16+
private final String Name;
17+
private final List args;
1818
public EventGeneric(String name, List values){
1919
Name = name;
2020
args = values;
@@ -28,9 +28,9 @@ public EventGeneric(String name, Value... values){
2828
public String getName(){return Name;}
2929
public List getValues(){return args;}
3030
public Value<List> asValue(){
31-
LinkedList<Value> list = new LinkedList<>(args);
32-
list.add(0, Value.of(Name));
33-
return Value.of(list);
31+
List list = args.duplicate();
32+
list.addFirst(Value.of(Name));
33+
return list.asValue();
3434
}
3535

3636
//writes the event into a packet, voids complex values

src/main/java/com/redtoast/simulation/events/EventManager.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.redtoast.simulation.events;
22

3-
import com.redtoast.neet.Debugger;
4-
53
import java.util.Hashtable;
64
import java.util.LinkedList;
75

src/main/java/com/redtoast/simulation/value/ValueTypes/List.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface listCast<T>{
2525
T cast(Value value);
2626
}
2727

28-
private LinkedList<Value> vals;
28+
private java.util.List<Value> vals;
2929
public List(Value<?>[] values){
3030
vals = new LinkedList<>(Arrays.stream(values).toList());
3131
}
@@ -130,8 +130,13 @@ public boolean add(Value value){
130130
return true;
131131
}
132132

133+
public boolean addFirst(Value value){
134+
vals.addFirst(value);
135+
return true;
136+
}
137+
133138
public void removeFirst(){
134-
vals.remove();
139+
vals.removeFirst();
135140
}
136141

137142
@Override
@@ -164,9 +169,7 @@ public Tuple toTuple(){
164169

165170
public List duplicate(){
166171
List clone = new List();
167-
for (Value<?> value : vals){
168-
clone.vals.add(value);
169-
}
172+
clone.vals.addAll(vals);
170173
return clone;
171174
}
172175
}

0 commit comments

Comments
 (0)