Updating built in Io code to use += instead of x = x + y
[io/quag.git] / libs / iovm / source / IoSandbox.c
blob98ead93b77b32f093fa21a766c8b75a5f892a7dc
1 /*#io
2 Sandbox ioDoc(
3 docCopyright("Steve Dekorte", 2002)
4 docLicense("BSD revised")
5 docObject("Sandbox")
6 docDescription("Sandbox can be used to run separate instances of Io within the same process.")
7 docCategory("Core")
8 */
10 #include "IoSandbox.h"
11 #include "IoSeq.h"
12 #include "IoState.h"
13 #include "IoCFunction.h"
14 #include "IoObject.h"
15 #include "IoList.h"
16 #include "IoSeq.h"
17 #include "UArray.h"
18 #include "PortableTruncate.h"
19 #include <errno.h>
20 #include <stdio.h>
22 #define DATA(self) ((IoState *)IoObject_dataPointer(self))
24 IoTag *IoSandbox_newTag(void *state)
26 IoTag *tag = IoTag_newWithName_("Sandbox");
27 IoTag_state_(tag, state);
28 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoSandbox_rawClone);
29 IoTag_freeFunc_(tag, (IoTagFreeFunc *)IoSandbox_free);
30 return tag;
33 IoSandbox *IoSandbox_proto(void *state)
35 IoMethodTable methodTable[] = {
36 {"messageCount", IoSandbox_messageCount},
37 {"setMessageCount", IoSandbox_setMessageCount},
38 {"timeLimit", IoSandbox_timeLimit},
39 {"setTimeLimit", IoSandbox_setTimeLimit},
40 {"doSandboxString", IoSandbox_doSandboxString},
41 {NULL, NULL},
44 IoObject *self = IoObject_new(state);
45 IoObject_tag_(self, IoSandbox_newTag(state));
47 IoState_registerProtoWithFunc_((IoState *)state, self, IoSandbox_proto);
49 IoObject_addMethodTable_(self, methodTable);
51 return self;
54 IoState *IoSandbox_boxState(IoSandbox *self)
56 if (!DATA(self))
58 IoObject_setDataPointer_(self, IoState_new());
59 IoSandbox_addPrintCallback(self);
62 return DATA(self);
65 IoSandbox *IoSandbox_rawClone(IoSandbox *proto)
67 IoObject *self = IoObject_rawClonePrimitive(proto);
68 return self;
71 void IoSandbox_addPrintCallback(IoSandbox *self)
73 IoState *boxState = IoSandbox_boxState(self);
74 IoState_callbackContext_(boxState, self);
75 IoState_printCallback_(boxState, IoSandbox_printCallback);
78 void IoSandbox_printCallback(void *voidSelf, const UArray *ba)
80 IoSandbox *self = voidSelf;
82 IoState *state = IOSTATE;
83 IoSeq *buf = IoSeq_newWithUArray_copy_(IOSTATE, ba, 1);
84 IoMessage *m = IoMessage_newWithName_(state, IOSYMBOL("printCallback"));
85 IoMessage *arg = IoMessage_newWithName_returnsValue_(state, IOSYMBOL("buffer"), buf);
86 IoMessage_addArg_(m, arg);
87 IoMessage_locals_performOn_(m, state->lobby, self);
90 IoSandbox *IoSandbox_new(void *state)
92 IoObject *proto = IoState_protoWithInitFunction_((IoState *)state, IoSandbox_proto);
93 return IOCLONE(proto);
96 void IoSandbox_free(IoSandbox *self)
98 if (IoObject_dataPointer(self))
100 IoState_free(IoSandbox_boxState(self));
104 /* ----------------------------------------------------------- */
106 IoNumber *IoSandbox_messageCount(IoSandbox *self, IoObject *locals, IoMessage *m)
108 /*#io
109 docSlot("messageCount",
110 "Returns a number containing the messageCount limit of the Sandbox. ")
113 IoState *boxState = IoSandbox_boxState(self);
114 return IONUMBER(boxState->messageCountLimit);
117 IoObject *IoSandbox_setMessageCount(IoSandbox *self, IoObject *locals, IoMessage *m)
119 /*#io
120 docSlot("setMessageCount(anInteger)",
121 "Sets the messageCount limit of the receiver. ")
124 IoState *boxState = IoSandbox_boxState(self);
125 boxState->messageCountLimit = IoMessage_locals_intArgAt_(m, locals, 0);
126 return self;
129 IoNumber *IoSandbox_timeLimit(IoSandbox *self, IoObject *locals, IoMessage *m)
131 /*#io
132 docSlot("timeLimit",
133 "Returns a number containing the time limit of calls made to the Sandbox. ")
136 IoState *boxState = IoSandbox_boxState(self);
137 return IONUMBER(boxState->timeLimit);
140 IoObject *IoSandbox_setTimeLimit(IoSandbox *self, IoObject *locals, IoMessage *m)
142 /*#io
143 docSlot("setTimeLimit(aDouble)",
144 "Sets the time limit of the Sandbox. ")
147 IoState *boxState = IoSandbox_boxState(self);
148 boxState->timeLimit = IoMessage_locals_doubleArgAt_(m, locals, 0);
149 return self;
152 IoObject *IoSandbox_doSandboxString(IoSandbox *self, IoObject *locals, IoMessage *m)
154 /*#io
155 docSlot("doSandboxString(aString)",
156 "Evaluate aString instead the Sandbox. ")
159 IoState *boxState = IoSandbox_boxState(self);
160 char *s = IoMessage_locals_cStringArgAt_(m, locals, 0);
162 IoObject *result = IoState_doSandboxCString_(boxState, s);
164 if (ISSYMBOL(result))
166 return IOSYMBOL(CSTRING(result));
169 if (ISSEQ(result))
171 return IOSEQ(IOSEQ_BYTES(result), IOSEQ_LENGTH(result));
174 if (ISNUMBER(result))
176 return IONUMBER(CNUMBER(result));
179 return IONIL(self);