3 docCopyright("Marc Fauconneau", 2006)
4 docLicense("BSD revised")
5 docDescription("An object that enables low level introspection into a running Io VM.")
10 #include "IoCFunction.h"
14 double fptrToDouble(void* fptr
)
23 IoLinker
*IoLinker_proto(void *state
)
25 IoObject
*self
= IoObject_new(state
);
27 IoState_registerProtoWithFunc_(state
, self
, IoLinker_proto
);
30 IoMethodTable methodTable
[] = {
31 {"makeCFunction", IoLinker_makeCFunction
},
32 {"bytesToHexSeq", IoLinker_bytesToHexSeq
},
33 {"hexSeqToBytes", IoLinker_hexSeqToBytes
},
36 IoObject_addMethodTable_(self
, methodTable
);
39 IoObject_setSlot_to_(self
, IOSYMBOL("IoState_numberWithDouble_"), IONUMBER(fptrToDouble(IoState_numberWithDouble_
)));
40 IoObject_setSlot_to_(self
, IOSYMBOL("IoMessage_locals_valueArgAt_"), IONUMBER(fptrToDouble(IoMessage_locals_valueArgAt_
)));
45 IoObject
*IoLinker_makeCFunction(IoLinker
*self
, IoObject
*locals
, IoMessage
*m
)
47 IoSeq
*buffer
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
48 IoSeq
*slotName
= IoMessage_locals_seqArgAt_(m
, locals
, 1);
49 IoObject
*object
= IoMessage_locals_valueArgAt_(m
, locals
, 2);
51 IoUserFunction
* fp
= (IoUserFunction
*)IoSeq_rawBytes(buffer
);
53 f
= IoCFunction_newWithFunctionPointer_tag_name_(IOSTATE
, fp
, IoObject_tag(object
), CSTRING(slotName
));
54 IoObject_setSlot_to_(f
, IOSYMBOL("compiledCode"), buffer
);
58 int charFromHex(int c
)
60 if (c
>='0'&& c
<='9') return c
-'0';
61 if (c
>='a'&& c
<='f') return c
-'a'+10;
62 if (c
>='A'&& c
<='F') return c
-'A'+10;
66 UArray
*UArray_fromHexStringUArray(UArray
*self
)
68 size_t i
, newSize
= self
->size
/ 2;
69 UArray
*ba
= UArray_new();
70 UArray_setSize_(ba
, newSize
);
72 for(i
= 0; i
< newSize
; i
++)
74 int h
= self
->data
[i
*2];
75 int l
= self
->data
[i
*2+1];
77 ba
->data
[i
] = (charFromHex(h
)<<4) + charFromHex(l
);
83 IoObject
*IoLinker_bytesToHexSeq(IoLinker
*self
, IoObject
*locals
, IoMessage
*m
)
85 IoSeq
*buffer
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
86 UArray
*ba
= IoSeq_rawUArray(buffer
);
87 return IoSeq_newWithUArray_copy_(IOSTATE
, UArray_asNewHexStringUArray(ba
), 0);
90 IoObject
*IoLinker_hexSeqToBytes(IoLinker
*self
, IoObject
*locals
, IoMessage
*m
)
92 IoSeq
*buffer
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
93 UArray
*ba
= IoSeq_rawUArray(buffer
);
94 return IoSeq_newWithUArray_copy_(IOSTATE
, UArray_fromHexStringUArray(ba
), 0);