diff --git a/std/js/_std/haxe/io/Bytes.hx b/std/js/_std/haxe/io/Bytes.hx index 2bab2f9f4cb..bf1785c0c2c 100644 --- a/std/js/_std/haxe/io/Bytes.hx +++ b/std/js/_std/haxe/io/Bytes.hx @@ -156,7 +156,7 @@ class Bytes { public function toHex():String { var s = new StringBuf(); - var chars = []; + var chars:Array = []; var str = "0123456789abcdef"; for (i in 0...str.length) chars.push(str.charCodeAt(i)); diff --git a/tests/unit/src/unit/issues/Issue13003.hx b/tests/unit/src/unit/issues/Issue13003.hx new file mode 100644 index 00000000000..c12d42fa480 --- /dev/null +++ b/tests/unit/src/unit/issues/Issue13003.hx @@ -0,0 +1,40 @@ +package unit.issues; + +class Issue13003 extends Test { + #if js + function test() { + eq("Array", 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}; + } +}