Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion std/js/_std/haxe/io/Bytes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Bytes {

public function toHex():String {
var s = new StringBuf();
var chars = [];
var chars:Array<Int> = [];
var str = "0123456789abcdef";
for (i in 0...str.length)
chars.push(str.charCodeAt(i));
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/src/unit/issues/Issue13003.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package unit.issues;

class Issue13003 extends Test {
#if js
function test() {
eq("Array<Int>", bytesToHexLookupType());

var bytes = haxe.io.Bytes.alloc(6);
for (index => value in [0x00, 0x0F, 0x10, 0x7F, 0x80, 0xFF])
bytes.set(index, value);
eq("000f107f80ff", bytes.toHex());
}
#end

macro static function bytesToHexLookupType() {
var bytes = switch haxe.macro.Context.getType("haxe.io.Bytes") {
case TInst(type, _):
type.get();
case type:
haxe.macro.Context.error('Expected haxe.io.Bytes to be a class, got ${haxe.macro.TypeTools.toString(type)}', haxe.macro.Context.currentPos());
}
var toHex = Lambda.find(bytes.fields.get(), field -> field.name == "toHex");
if (toHex == null)
haxe.macro.Context.error("Could not find haxe.io.Bytes.toHex", bytes.pos);

var lookupType = null;
function visit(expression:haxe.macro.Type.TypedExpr) {
switch expression.expr {
case TVar(variable, _) if (variable.name == "chars"):
lookupType = haxe.macro.TypeTools.toString(variable.t);
case _:
haxe.macro.TypedExprTools.iter(expression, visit);
}
}
visit(toHex.expr());
if (lookupType == null)
haxe.macro.Context.error("Could not find the haxe.io.Bytes.toHex lookup table", toHex.pos);
return macro $v{lookupType};
}
}