Addons updated to new doc format
[io.git] / addons / Loki / source / IoLinker.c
blob2fdf0e0534caf3765ab50ebf8fb9c45458db1d8b
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.")
5 */
7 #include "IoLinker.h"
8 #include "IoState.h"
9 #include "IoCFunction.h"
10 #include "IoSeq.h"
11 #include "IoMap.h"
13 double fptrToDouble(void* fptr)
15 double x;
16 long y = (long)fptr;
17 x = y;
18 return x;
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},
33 {NULL, NULL},
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_)));
41 return self;
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);
49 IoCFunction *f;
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);
54 return f;
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;
62 return 0;
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);
79 return ba;
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);