Removed all code that uses OpenGL from Image.
[io/quag.git] / addons / Loki / source / IoLinker.c
blob2b17322c8f689c79736fe234845eb2e228135a59
1 /*#io
2 Linker ioDoc(
3 docCopyright("Marc Fauconneau", 2006)
4 docLicense("BSD revised")
5 docDescription("An object that enables low level introspection into a running Io VM.")
6 */
8 #include "IoLinker.h"
9 #include "IoState.h"
10 #include "IoCFunction.h"
11 #include "IoSeq.h"
12 #include "IoMap.h"
14 double fptrToDouble(void* fptr)
16 double x;
17 long y = (long)fptr;
18 x = y;
19 return x;
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},
34 {NULL, NULL},
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_)));
42 return self;
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);
50 IoCFunction *f;
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);
55 return f;
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;
63 return 0;
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);
80 return ba;
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);