-
Notifications
You must be signed in to change notification settings - Fork 3k
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
@printbuffer #10499
base: master
Are you sure you want to change the base?
@printbuffer #10499
Changes from 3 commits
f887aae
3c04e56
ae7a551
49f0f92
5a7e66b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ public class LExecutor{ | |
/** Non-constant variables used for network sync */ | ||
public LVar[] vars = {}; | ||
|
||
public LVar counter, unit, thisv, ipt; | ||
public LVar counter, unit, thisv, ipt, printbuffer; | ||
|
||
public int[] binds; | ||
public boolean yield; | ||
|
@@ -62,7 +62,7 @@ public class LExecutor{ | |
//yes, this is a minor memory leak, but it's probably not significant enough to matter | ||
protected static IntFloatMap unitTimeouts = new IntFloatMap(); | ||
//lookup variable by name, lazy init. | ||
protected @Nullable ObjectIntMap<String> nameMap; | ||
protected @Nullable ObjectIntMap<CharSequence> nameMap; | ||
|
||
static{ | ||
Events.on(ResetEvent.class, e -> unitTimeouts.clear()); | ||
|
@@ -105,12 +105,13 @@ public void load(LAssembler builder){ | |
unit = builder.getVar("@unit"); | ||
thisv = builder.getVar("@this"); | ||
ipt = builder.putConst("@ipt", build != null ? build.ipt : 0); | ||
printbuffer = builder.putConst("@printbuffer", textBuffer); | ||
} | ||
|
||
//region utility | ||
|
||
|
||
public @Nullable LVar optionalVar(String name){ | ||
public @Nullable LVar optionalVar(CharSequence name){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test this? Query with StringBuilder but String in Map. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gonna test it in a few hours There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i found a solution but cant fix it yet (nother 2 hours or so) |
||
if(nameMap == null){ | ||
nameMap = new ObjectIntMap<>(); | ||
for(int i = 0; i < vars.length; i++){ | ||
|
@@ -571,7 +572,7 @@ public void run(LExecutor exec){ | |
|
||
if(from instanceof MemoryBuild mem && (exec.privileged || (from.team == exec.team && !mem.block.privileged))){ | ||
output.setnum(address < 0 || address >= mem.memory.length ? 0 : mem.memory[address]); | ||
}else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof String name){ | ||
}else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof CharSequence name){ | ||
LVar fromVar = logic.executor.optionalVar(name); | ||
if(fromVar != null && !output.constant){ | ||
output.objval = fromVar.objval; | ||
|
@@ -601,7 +602,7 @@ public void run(LExecutor exec){ | |
|
||
if(from instanceof MemoryBuild mem && (exec.privileged || (from.team == exec.team && !mem.block.privileged)) && address >= 0 && address < mem.memory.length){ | ||
mem.memory[address] = value.num(); | ||
}else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof String name){ | ||
}else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof CharSequence name){ | ||
LVar toVar = logic.executor.optionalVar(name); | ||
if(toVar != null && !toVar.constant){ | ||
toVar.objval = value.objval; | ||
|
@@ -634,6 +635,11 @@ public void run(LExecutor exec){ | |
return; | ||
} | ||
|
||
if(sense == LAccess.type && target instanceof CharSequence){ | ||
to.setobj(LogicDialog.typeName(from)); | ||
return; | ||
} | ||
|
||
//note that remote units/buildings can be sensed as well | ||
if(target instanceof Senseable se){ | ||
if(sense instanceof Content co){ | ||
|
@@ -803,7 +809,10 @@ public OpI(LogicOp op, LVar a, LVar b, LVar dest){ | |
@Override | ||
public void run(LExecutor exec){ | ||
if(op == LogicOp.strictEqual){ | ||
dest.setnum(a.isobj == b.isobj && ((a.isobj && Structs.eq(a.objval, b.objval)) || (!a.isobj && a.numval == b.numval)) ? 1 : 0); | ||
dest.setnum(a.isobj == b.isobj && ( | ||
(a.isobj && a.objval.getClass().equals(b.objval.getClass()) && a.objval instanceof CharSequence sa && b.objval instanceof CharSequence sb && strEquals(sa, sb)) || | ||
(a.isobj && Structs.eq(a.objval, b.objval)) || | ||
(!a.isobj && a.numval == b.numval)) ? 1 : 0); | ||
}else if(op.unary){ | ||
dest.setnum(op.function1.get(a.num())); | ||
}else{ | ||
|
@@ -817,6 +826,25 @@ public void run(LExecutor exec){ | |
|
||
} | ||
} | ||
|
||
public static boolean strEquals(CharSequence a, CharSequence b){ | ||
//This should be faster than the other approach | ||
if(a instanceof String || b instanceof String){ | ||
String p = a instanceof String sa ? sa : (String)b; | ||
CharSequence s = p == a ? b : a; | ||
return(p.contentEquals(s)); | ||
} | ||
if(a.length()!=b.length()) return(false); | ||
for(int i = 0; i < a.length(); i++){ | ||
if(a.charAt(i) != b.charAt(i)) return(false); | ||
} | ||
return(true); | ||
} | ||
|
||
public static boolean equal(Object a, Object b){ | ||
if(a instanceof CharSequence sa && b instanceof CharSequence sb) return(strEquals(sa, sb)); | ||
return(Structs.eq(a, b)); | ||
} | ||
} | ||
|
||
public static class EndI implements LInstruction{ | ||
|
@@ -989,6 +1017,12 @@ public void run(LExecutor exec){ | |
|
||
//this should avoid any garbage allocation | ||
if(value.isobj){ | ||
|
||
if(value.objval instanceof StringBuilder builder){ | ||
exec.textBuffer.append(builder); | ||
return; | ||
} | ||
|
||
String strValue = toString(value.objval); | ||
|
||
exec.textBuffer.append(strValue); | ||
|
@@ -1006,6 +1040,7 @@ public static String toString(Object obj){ | |
return | ||
obj == null ? "null" : | ||
obj instanceof String s ? s : | ||
obj instanceof StringBuilder ? "[dyn-string]" : //Maybe use .toString, but it would allocate memory... | ||
obj instanceof MappableContent content ? content.name : | ||
obj instanceof Content ? "[content]" : | ||
obj instanceof Building build ? build.block.name : | ||
|
@@ -1073,6 +1108,12 @@ public void run(LExecutor exec){ | |
|
||
//this should avoid any garbage allocation | ||
if(value.isobj){ | ||
//avoid it even more | ||
if(value.objval instanceof StringBuilder b){ | ||
exec.textBuffer.replace(placeholderIndex, placeholderIndex+3, ""); | ||
exec.textBuffer.insert(placeholderIndex,b); | ||
return; | ||
} | ||
String strValue = PrintI.toString(value.objval); | ||
|
||
exec.textBuffer.replace(placeholderIndex, placeholderIndex + 3, strValue); | ||
|
@@ -1134,7 +1175,10 @@ public void run(LExecutor exec){ | |
boolean cmp; | ||
|
||
if(op == ConditionOp.strictEqual){ | ||
cmp = va.isobj == vb.isobj && ((va.isobj && va.objval == vb.objval) || (!va.isobj && va.numval == vb.numval)); | ||
cmp = va.isobj == vb.isobj && ( | ||
(va.isobj && va.objval.getClass().equals(vb.objval.getClass()) && va.objval instanceof CharSequence sa && vb.objval instanceof CharSequence sb && OpI.strEquals(sa, sb)) || | ||
(va.isobj && Structs.eq(va.objval, vb.objval)) || | ||
(!va.isobj && va.numval == vb.numval)); | ||
}else if(op.objFunction != null && va.isobj && vb.isobj){ | ||
//use object function if both are objects | ||
cmp = op.objFunction.get(value.obj(), compare.obj()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HashMap should use immutable class(String) as key.