3 docCopyright("Steve Dekorte", 2002)
4 docLicense("BSD revised")
6 docDescription("Sandbox can be used to run separate instances of Io within the same process.")
10 #include "IoSandbox.h"
13 #include "IoCFunction.h"
18 #include "PortableTruncate.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
);
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
},
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
);
54 IoState
*IoSandbox_boxState(IoSandbox
*self
)
58 IoObject_setDataPointer_(self
, IoState_new());
59 IoSandbox_addPrintCallback(self
);
65 IoSandbox
*IoSandbox_rawClone(IoSandbox
*proto
)
67 IoObject
*self
= IoObject_rawClonePrimitive(proto
);
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
)
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
)
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);
129 IoNumber
*IoSandbox_timeLimit(IoSandbox
*self
, IoObject
*locals
, IoMessage
*m
)
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
)
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);
152 IoObject
*IoSandbox_doSandboxString(IoSandbox
*self
, IoObject
*locals
, IoMessage
*m
)
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
));
171 return IOSEQ(IOSEQ_BYTES(result
), IOSEQ_LENGTH(result
));
174 if (ISNUMBER(result
))
176 return IONUMBER(CNUMBER(result
));