Skip to content

Commit 5400ba5

Browse files
authored
Add support for GUID (#136)
1 parent a4ef7c5 commit 5400ba5

File tree

9 files changed

+126
-1
lines changed

9 files changed

+126
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ jobs:
9393
- name: Install hl
9494
run: |
9595
git clone https://github.com/HaxeFoundation/hashlink.git hashlink
96+
sudo apt-get update
9697
sudo apt-get install -qqy libpng-dev libturbojpeg-dev libvorbis-dev libopenal-dev libsdl2-dev libglu1-mesa-dev libmbedtls-dev libuv1-dev libsqlite3-dev
9798
cd hashlink
9899
make

hld/Debugger.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ class Debugger {
398398

399399
readThreads();
400400
prepareStack(cmd.r == Watchbreak);
401+
eval.onBeforeBreak();
401402

402403
// if breakpoint has a condition, try to evaluate and do not actually break on false
403404
if( !onStep && !onEvalCall && !onPause && condition != null ) {

hld/Eval.hx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Eval {
4747
var funIndex : Int;
4848
var codePos : Int;
4949
var ebp : Pointer;
50+
var guidNames : Int64Map<String>;
5051

5152
public var maxArrLength : Int = 10;
5253
public var maxBytesLength : Int = 128;
@@ -83,6 +84,10 @@ class Eval {
8384
this.ebp = ebp;
8485
}
8586

87+
public function onBeforeBreak() {
88+
this.guidNames = null;
89+
}
90+
8691
public function eval( expr : String ) : Value {
8792
if( expr == null || expr == "" )
8893
return null;
@@ -986,6 +991,16 @@ class Eval {
986991
c + "(" + [for( v in values ) valueStr(v,maxStringRec)].join(", ") + ")";
987992
case VInlined(_):
988993
"inlined";
994+
case VGuid(i, name):
995+
switch( v.hint ) {
996+
case HBin: Value.int64Str(i, 2);
997+
case HHex: Value.int64Str(i, 16);
998+
default:
999+
var str = Value.int64GuidStr(i);
1000+
if( name != null )
1001+
str = '$name ($str)';
1002+
str;
1003+
}
9891004
}
9901005
return str;
9911006
}
@@ -1078,6 +1093,10 @@ class Eval {
10781093
VBool(m.getUI8(0) != 0);
10791094
case HPacked(t):
10801095
return { v : VPointer(p), t : t.v };
1096+
case HGUID:
1097+
var i64 = haxe.Int64.make(readI32(p.offset(4)), readI32(p));
1098+
var name = getGuidName(i64);
1099+
return { v : VGuid(i64, name), t : t };
10811100
default:
10821101
p = readPointer(p);
10831102
return valueCast(p, t);
@@ -1611,4 +1630,43 @@ class Eval {
16111630
return module.code.types[tid];
16121631
}
16131632

1633+
function getGuidName( i64 : haxe.Int64 ) : Null<String> {
1634+
if( guidNames == null ) {
1635+
guidNames = new Int64Map();
1636+
if( jit.hlVersion >= 1.15 ) {
1637+
var pmap = readPointer(jit.threads.offset(jit.align.ptr*3 + 8));
1638+
if( !pmap.isNull() ) {
1639+
var m = @:privateAccess makeMap(pmap, HI64);
1640+
switch( m ) {
1641+
case VMap(_, nkeys, readKey, readValue, _):
1642+
for (n in 0...nkeys) {
1643+
var k = readKey(n);
1644+
var v = readValue(n);
1645+
switch( [k.v, v.v] ) {
1646+
case [VInt64(ki64), VString(vname, _)]:
1647+
guidNames.set(ki64, vname);
1648+
default:
1649+
}
1650+
}
1651+
default: throw "assert";
1652+
}
1653+
}
1654+
}
1655+
}
1656+
return guidNames.get(i64);
1657+
}
1658+
1659+
}
1660+
1661+
// Map<Int64, V> does not work on JS, see https://github.com/HaxeFoundation/haxe/issues/9872
1662+
class Int64Map<T> extends haxe.ds.BalancedTree<haxe.Int64, T> {
1663+
override function compare(k1:haxe.Int64, k2:haxe.Int64):Int {
1664+
return if( k1 == k2 ) {
1665+
0;
1666+
} else if( k1 > k2 ) {
1667+
1;
1668+
} else {
1669+
-1;
1670+
}
1671+
}
16141672
}

hld/Value.hx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum ValueRepr {
1818
VEnum( c : String, values : Array<Value>, p : Pointer );
1919
VBytes( length : Int, read : Int -> Int, p : Pointer );
2020
VInlined( fields : Array<InlinedField> );
21+
VGuid( i : haxe.Int64, name : String );
2122
}
2223

2324
enum FunRepr {
@@ -125,6 +126,20 @@ enum Hint {
125126
return value;
126127
}
127128

129+
static final GUIDBASE = "#&0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
130+
public static function int64GuidStr( value : haxe.Int64 ) : String {
131+
if( value == 0 )
132+
return "0";
133+
var s = "";
134+
for( i in 0...11 ) {
135+
if( i == 3 || i == 7 )
136+
s = '-' + s;
137+
s = GUIDBASE.charAt(value.low&63) + s;
138+
value = value >> 6;
139+
}
140+
return s;
141+
}
142+
128143
public static function intEnumFlags( value : Int, eproto : format.hl.Data.EnumPrototype ) : String {
129144
var f = "";
130145
for( i in 0...eproto.constructs.length ) {

tests/RunCi.hx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ class RunCi {
1111
var fullPath = sys.FileSystem.absolutePath(basePath + "unit/" + test);
1212
log('[INFO] $test begin');
1313
changeDirectory(fullPath);
14-
Sys.command("haxe", ["--main", "Test", "-hl", "test.hl"]);
14+
var compileargs = ["--main", "Test", "-hl", "test.hl"];
15+
try {
16+
var flags = sys.io.File.getContent(fullPath + "/compile.txt").split(" ");
17+
compileargs = compileargs.concat(flags);
18+
} catch( e ) {
19+
trace("error" + e.message);
20+
}
21+
trace("run haxe with " + compileargs);
22+
Sys.command("haxe", compileargs);
1523
var process = new sys.io.Process("hl", [debuggerHL, "--input", "input.txt"]);
1624
var expectedOutput = sys.io.File.getContent(fullPath + "/output.txt");
1725
var startingTime = haxe.Timer.stamp();

tests/unit/TestGuid/Test.hx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Test {
2+
static function main() {
3+
var id : hl.GUID = makeGUID(0xF1561985,0xF15008);
4+
var id2 : hl.GUID = makeGUID(0xF1561985,0xF15009);
5+
hl.Api.registerGUIDName(id, "SomeName");
6+
Sys.println(Std.string(id));
7+
Sys.println(Std.string(id2));
8+
}
9+
10+
static function makeGUID(high,low) : hl.GUID {
11+
return haxe.Int64.make(high,low);
12+
}
13+
}

tests/unit/TestGuid/compile.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-D hl-ver=1.15.0

tests/unit/TestGuid/input.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--ci
2+
test.hl
3+
"b Test.hx:5"
4+
"b Test.hx:7"
5+
r
6+
"p id"
7+
"p id2"
8+
r
9+
"p id"
10+
"p id2"
11+
q

tests/unit/TestGuid/output.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
> b Test.hx:5
2+
Breakpoint set line 5
3+
> b Test.hx:7
4+
Breakpoint set line 7
5+
> r
6+
Thread paused Test.hx:5 ($Test::main)
7+
> p id
8+
z3K4-MI#w-J#6 : hl.GUID
9+
> p id2
10+
z3K4-MI#w-J#7 : hl.GUID
11+
> r
12+
Thread paused Test.hx:7 ($Test::main)
13+
> p id
14+
SomeName (z3K4-MI#w-J#6) : hl.GUID
15+
> p id2
16+
z3K4-MI#w-J#7 : hl.GUID
17+
> q

0 commit comments

Comments
 (0)