1 //metadoc Linker copyright Marc Fauconneau, 2006
2 //metadoc Linker license BSD revised
3 /*metadoc Linker description
4 An object that enables low level introspection into a running Io VM.")
9 #include "IoCFunction.h"
13 double fptrToDouble(void* fptr
)
22 IoLinker
*IoLinker_proto(void *state
)
24 IoObject
*self
= IoObject_new(state
);
26 IoState_registerProtoWithFunc_(state
, self
, IoLinker_proto
);
29 IoMethodTable methodTable
[] = {
30 {"makeCFunction", IoLinker_makeCFunction
},
31 {"bytesToHexSeq", IoLinker_bytesToHexSeq
},
32 {"hexSeqToBytes", IoLinker_hexSeqToBytes
},
35 IoObject_addMethodTable_(self
, methodTable
);
38 IoObject_setSlot_to_(self
, IOSYMBOL("IoState_numberWithDouble_"), IONUMBER(fptrToDouble(IoState_numberWithDouble_
)));
39 IoObject_setSlot_to_(self
, IOSYMBOL("IoMessage_locals_valueArgAt_"), IONUMBER(fptrToDouble(IoMessage_locals_valueArgAt_
)));
44 IoObject
*IoLinker_makeCFunction(IoLinker
*self
, IoObject
*locals
, IoMessage
*m
)
46 IoSeq
*buffer
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
47 IoSeq
*slotName
= IoMessage_locals_seqArgAt_(m
, locals
, 1);
48 IoObject
*object
= IoMessage_locals_valueArgAt_(m
, locals
, 2);
50 IoUserFunction
* fp
= (IoUserFunction
*)IoSeq_rawBytes(buffer
);
52 f
= IoCFunction_newWithFunctionPointer_tag_name_(IOSTATE
, fp
, IoObject_tag(object
), CSTRING(slotName
));
53 IoObject_setSlot_to_(f
, IOSYMBOL("compiledCode"), buffer
);
57 int charFromHex(int c
)
59 if (c
>='0'&& c
<='9') return c
-'0';
60 if (c
>='a'&& c
<='f') return c
-'a'+10;
61 if (c
>='A'&& c
<='F') return c
-'A'+10;
65 UArray
*UArray_fromHexStringUArray(UArray
*self
)
67 size_t i
, newSize
= self
->size
/ 2;
68 UArray
*ba
= UArray_new();
69 UArray_setSize_(ba
, newSize
);
71 for(i
= 0; i
< newSize
; i
++)
73 int h
= self
->data
[i
*2];
74 int l
= self
->data
[i
*2+1];
76 ba
->data
[i
] = (charFromHex(h
)<<4) + charFromHex(l
);
82 IoObject
*IoLinker_bytesToHexSeq(IoLinker
*self
, IoObject
*locals
, IoMessage
*m
)
84 IoSeq
*buffer
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
85 UArray
*ba
= IoSeq_rawUArray(buffer
);
86 return IoSeq_newWithUArray_copy_(IOSTATE
, UArray_asNewHexStringUArray(ba
), 0);
89 IoObject
*IoLinker_hexSeqToBytes(IoLinker
*self
, IoObject
*locals
, IoMessage
*m
)
91 IoSeq
*buffer
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
92 UArray
*ba
= IoSeq_rawUArray(buffer
);
93 return IoSeq_newWithUArray_copy_(IOSTATE
, UArray_fromHexStringUArray(ba
), 0);