Skip to content

Commit

Permalink
Add hint for reading int/float from Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiaomao committed Jul 23, 2024
1 parent f8fe128 commit c8924aa
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
49 changes: 40 additions & 9 deletions hld/Eval.hx
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,13 @@ class Eval {
case HObj(p), HStruct(p): p.name.split(".").pop(); // short form (no package)
default: typeStr(v.t);
}
case VString(s,_): "\"" + escape(s) + "\"";
case VString(s,p):
switch( [v.t, v.hint] ) {
case [HBytes, HReadBytes(t, pos)]:
readBytesStrAt(-1, i -> readByte(p.offset(i)), t, pos);
default:
"\"" + escape(s) + "\"";
}
case VClosure(f, d, _), VMethod(f, d, _): funStr(f) + "[" + valueStr(d,maxStringRec) + "]";
case VFunction(f,_): funStr(f);
case VArray(_, length, read, _):
Expand All @@ -933,14 +939,19 @@ class Eval {
"["+arr.join(",")+"]:"+length;
}
case VBytes(length, read, _):
var blen = length < maxBytesLength ? length : maxBytesLength;
var bytes = haxe.io.Bytes.alloc(blen);
for( i in 0...blen )
bytes.set(i, read(i));
var str = length+":0x" + bytes.toHex().toUpperCase();
if( length > maxBytesLength )
str += "...";
str;
switch( v.hint ) {
case HReadBytes(t, pos):
readBytesStrAt(length, read, t, pos);
default:
var blen = length < maxBytesLength ? length : maxBytesLength;
var bytes = haxe.io.Bytes.alloc(blen);
for( i in 0...blen )
bytes.set(i, read(i));
var str = length+":0x" + bytes.toHex().toUpperCase();
if( length > maxBytesLength )
str += "...";
str;
}
case VMap(_, 0, _):
"{}";
case VMap(_, nkeys, readKey, readValue, _):
Expand All @@ -964,6 +975,26 @@ class Eval {
return str;
}

function readBytesStrAt(length, read: Int->Int, t:HLType, pos:String) : String {
var pos = toInt(eval(pos));
var blen = align.typeSize(t);
if( pos < 0 || (length >= 0 && pos > length-blen) )
throw "Out of bound [0," + (length < 0 ? "???" : "" + length) + ") for " + pos + "+" + (blen - 1);
var bytes = haxe.io.Bytes.alloc(blen);
for( i in 0...blen )
bytes.set(i, read(pos+i));
return switch( t ) {
case HUi8: "" + bytes.get(0);
case HUi16: "" + bytes.getUInt16(0);
case HI32: "" + bytes.getInt32(0);
case HI64: "" + bytes.getInt64(0);
case HF32: "" + bytes.getFloat(0);
case HF64: "" + bytes.getDouble(0);
default:
throw "assert";
}
}

public function funStr( f : FunRepr ) {
return switch( f ) {
case FUnknown(p): "fun(" + p.toString() + ")";
Expand Down
13 changes: 13 additions & 0 deletions hld/Value.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum Hint {
HHex; // v:h
HBin; // v:b
HPointer; // v:p
HReadBytes(t : HLType, pos : String); // v:UI8(0), v:UI16(0), v:I32(0), v:I64(0), v:F32(0), v:F64(0)
HEnumFlags(t : String); // v:EnumFlags<T>, v:haxe.EnumFlags<T>
HEnumIndex(t : String); // v:EnumIndex<T>
HCdbEnum(t : String); // v:CDB<T>, v:CDBEnum<T> -- for CastleDB
Expand All @@ -49,6 +50,18 @@ enum Hint {
return HBin;
if( s == "p" )
return HPointer;
if( StringTools.startsWith(s,"UI8(") && StringTools.endsWith(s,")") )
return HReadBytes(HUi8, s.substr(4, s.length - 5));
if( StringTools.startsWith(s,"UI16(") && StringTools.endsWith(s,")") )
return HReadBytes(HUi16, s.substr(5, s.length - 6));
if( StringTools.startsWith(s,"I32(") && StringTools.endsWith(s,")") )
return HReadBytes(HI32, s.substr(4, s.length - 5));
if( StringTools.startsWith(s,"I64(") && StringTools.endsWith(s,")") )
return HReadBytes(HI64, s.substr(4, s.length - 5));
if( StringTools.startsWith(s,"F32(") && StringTools.endsWith(s,")") )
return HReadBytes(HF32, s.substr(4, s.length - 5));
if( StringTools.startsWith(s,"F64(") && StringTools.endsWith(s,")") )
return HReadBytes(HF64, s.substr(4, s.length - 5));
if( StringTools.startsWith(s,"EnumFlags<") && StringTools.endsWith(s,">") )
return HEnumFlags(s.substr(10, s.length - 11));
if( StringTools.startsWith(s,"haxe.EnumFlags<") && StringTools.endsWith(s,">") )
Expand Down
5 changes: 5 additions & 0 deletions src/HLAdapter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ class HLAdapter extends DebugSession {
case VArray(_, len, _, _), VMap(_, len, _, _):
return { name : name, type : tstr, value : dbg.eval.valueStr(value) + pstr, variablesReference : len == 0 ? 0 : allocValue(VValue(value)), indexedVariables : len };
case VBytes(len, _):
switch( value.hint ) {
case HReadBytes(t, _):
return { name : name, type : dbg.eval.typeStr(t), value : dbg.eval.valueStr(value), variablesReference : 0 };
default:
}
return { name : name, type : tstr, value : tstr+":"+len + pstr, variablesReference : allocValue(VValue(value)), indexedVariables : (len+15)>>4 };
case VClosure(f,context,_):
return { name : name, type : tstr, value : dbg.eval.funStr(f) + pstr, variablesReference : allocValue(VValue(value)), indexedVariables : 2 };
Expand Down

0 comments on commit c8924aa

Please sign in to comment.