So, I'm working on a msgbox function with fancy formatting options and support for array printing, long story. Anyway, I'm trying to make it parse each passed parameter into 2 things: (1) a formatting string (e.g. "0x{:016x}") and (2) a value to be formatted.
example:
Code: Select all
printf("<f hex: 0x{:016x}>" 0x68F20106242C, "<f int: {:i}>" 0x68F20106242C)
Code: Select all
hex: 0x000068F20106242C
int: 115388608554028
This works just fine for regular values like strings and numbers, I use RegEx to extract the formatting string from the parameter and print the value formatted accordingly. Unfortunately, object references can't be concatenated with strings, so I can't just plop an array reference at the end of the formatting string and expect it to work.
To avoid this limitation, I have resolved to using the address-of operator to instead pass the addresses to the object reference at the end of the string and then the Object() function to get the reference from that address, for example:
Code: Select all
printf("<f 0x{:x}>" &arr1:={a:1,b:2,c:3,d:4,e:5,f:6,g:7}, "<f {:i}>" &arr2:={a:1,b:2,c:3,d:4,e:5,f:6,g:7})
Code: Select all
key(a)=0x1
key(b)=0x2
key(c)=0x3
key(d)=0x4
key(e)=0x5
key(f)=0x6
key(g)=0x7
key(a)=1
key(b)=2
key(c)=3
key(d)=4
key(e)=5
key(f)=6
key(g)=7
Well, the issue is that the Object() function will throw a critical error if it is called with a pointer which doesn't point to an object, followed by the script crashing, so I can no longer use it to print simple numeric values. I will provide a very simplified, non-variadic version of the function to illustrate this behavior:
Code: Select all
aNumber:=12341232
aString:="qwerty"
anArray:={a:1,b:2,c:3,d:4,e:5,f:6,g:7}
test(str_val){
val:=RegExReplace(str_val, "This is a string and this is ")
if IsObject(obj:=Object(val)){
for k,v in obj
out.="key(" k ")=" v "`n"
ObjRelease(val)
}
else
out:=val
msgbox % out
}
; calling it like this works as intended:
test("This is a string and this is " &anArray)
; calling it like this works as well because strings can't be interpreted as addresses:
test("This is a string and this is " aString)
; calling it like this crashes the script with "Critical Error: Invalid memory read/write." which I wasn't able to circumvent in any way:
test("This is a string and this is " aNumber) ; this is because Object(12341232) will try to resolve an object reference at address 12341232 which is apparently a big no-no and we should immediately crash the script?