intptr_t bouncer(IoBlock *self, intptr_t ret, intptr_t a, intptr_t b, intptr_t c, intptr_t d, intptr_t e) { IoObject *lobby = IoState_lobby(IOSTATE); IoNumber *n; static IoMessage *m = NULL; List *argNames = ((IoBlockData*)IoObject_dataPointer(self))->argNames; if (m == NULL) m = IoMessage_new(IOSTATE); if (0 < argNames->size) IoMessage_setCachedArg_toInt_(m, 0, (int)a); if (1 < argNames->size) IoMessage_setCachedArg_toInt_(m, 1, (int)b); if (2 < argNames->size) IoMessage_setCachedArg_toInt_(m, 2, (int)c); if (3 < argNames->size) IoMessage_setCachedArg_toInt_(m, 3, (int)d); if (4 < argNames->size) IoMessage_setCachedArg_toInt_(m, 4, (int)e); n = IoBlock_activate(self, lobby, lobby, m, lobby); if (ISNUMBER(n)) { return (intptr_t)IoNumber_asInt(n); } return 0; }
IO_METHOD(IoSandbox, doSandboxString) { /*doc Sandbox doSandboxString(aString) Evaluate aString inside the Sandbox. */ IoState *boxState = IoSandbox_boxState(self); char *s = IoMessage_locals_cStringArgAt_(m, locals, 0); IoObject *result = IoState_doSandboxCString_(boxState, s); if (ISSYMBOL(result)) { return IOSYMBOL(CSTRING(result)); } if (ISSEQ(result)) { return IOSEQ(IOSEQ_BYTES(result), IOSEQ_LENGTH(result)); } if (ISNUMBER(result)) { return IONUMBER(CNUMBER(result)); } return IONIL(self); }
void IoCoroutine_rawRun(IoCoroutine *self) { Coro *coro = DATA(self)->cid; if (!coro) { coro = Coro_new(); DATA(self)->cid = coro; } { IoObject *stackSize = IoObject_getSlot_(self, IOSTATE->stackSizeSymbol); if(ISNUMBER(stackSize)) { Coro_setStackSize_(coro, CNUMBER(stackSize)); } } { IoCoroutine *current = IoState_currentCoroutine(IOSTATE); Coro *currentCoro = IoCoroutine_rawCoro(current); //IoState_stackRetain_(IOSTATE, self); Coro_startCoro_(currentCoro, coro, self, (CoroStartCallback *)IoCoroutine_coroStart); //IoState_setCurrentCoroutine_(IOSTATE, current); } }
int Levels_levelForOp(Levels *self, char *messageName, IoSymbol *messageSymbol, IoMessage *msg) { IoObject *value = IoMap_rawAt(self->operatorTable, messageSymbol); if (!value) { return -1; } if (ISNUMBER(value)) { int precedence = IoNumber_asInt((IoNumber*)value); if (precedence < 0 || precedence >= IO_OP_MAX_LEVEL) { IoState_error_(IoObject_state(msg), msg, "compile error: Precedence for operators must be between 0 and %d. Precedence was %d.", IO_OP_MAX_LEVEL - 1, precedence); } return precedence; } else { IoState_error_(IoObject_state(msg), msg, "compile error: Value for '%s' in Message OperatorTable operators is not a number. Values in the OperatorTable operators are numbers which indicate the precedence of the operator.", messageName); return -1; // The C compiler does not know that IoState_error_() will never return. } }
IO_METHOD(IoClutter, ungrabPointer) { IoObject *device_n = IoMessage_locals_valueArgAt_(m, locals, 0); if(ISNUMBER(device_n)) clutter_ungrab_pointer_for_device(CNUMBER(device_n)); else clutter_ungrab_pointer(); return self; }
IoObject* IoMySQL_connect(IoObject* self, IoObject* locals, IoMessage* m) { IoObject *host = NULL, *user = NULL, *password = NULL, *database = NULL, *port = NULL, *socket = NULL, *ssl = NULL; /*doc MySQL connect(host, user, password, database, port, unixSocket, useSSL) Connect to a MySQL database. */ switch(IoMessage_argCount(m)) { case 7: ssl = IoMessage_locals_quickValueArgAt_(m, locals, 6); case 6: socket = IoMessage_locals_quickValueArgAt_(m, locals, 5); case 5: port = IoMessage_locals_quickValueArgAt_(m, locals, 4); case 4: database = IoMessage_locals_quickValueArgAt_(m, locals, 3); case 3: password = IoMessage_locals_quickValueArgAt_(m, locals, 2); case 2: user = IoMessage_locals_quickValueArgAt_(m, locals, 1); case 1: host = IoMessage_locals_quickValueArgAt_(m, locals, 0); } if(DATA(self)->connected) { mysql_close(&DATA(self)->connection); DATA(self)->connected = 0; } if(mysql_real_connect( &DATA(self)->connection, host && ISSEQ(host) ? IoSeq_asCString(host) : NULL, user && ISSEQ(user) ? IoSeq_asCString(user) : NULL, password && ISSEQ(password) ? IoSeq_asCString(password) : NULL, database && ISSEQ(database) ? IoSeq_asCString(database) : NULL, port && ISNUMBER(port) ? (unsigned) IoNumber_asInt(port) : 0, socket && ISSEQ(socket) ? IoSeq_asCString(socket) : NULL, ssl && ISFALSE(ssl) ? 0 : CLIENT_SSL )) { DATA(self)->connected = 1; IoObject_setSlot_to_(self, IOSYMBOL("host"), host ? host : IONIL(self)); IoObject_setSlot_to_(self, IOSYMBOL("user"), user ? user : IONIL(self)); IoObject_setSlot_to_(self, IOSYMBOL("password"), password ? password : IONIL(self)); IoObject_setSlot_to_(self, IOSYMBOL("database"), database ? database : IONIL(self)); IoObject_setSlot_to_(self, IOSYMBOL("port"), port ? port : IONIL(self)); IoObject_setSlot_to_(self, IOSYMBOL("socket"), socket ? socket : IONIL(self)); IoObject_setSlot_to_(self, IOSYMBOL("usingSSL"), ssl ? IOBOOL(self, ISTRUE(ssl)) : IOFALSE(self)); } else IoState_error_(IOSTATE, m, "connection error(%d): %s", mysql_errno(&DATA(self)->connection), mysql_error(&DATA(self)->connection)); return self; }
IoObject *IoMessage_locals_numberArgAt_(IoMessage *self, IoObject *locals, int n) { IoObject *v = IoMessage_locals_valueArgAt_(self, locals, n); if (!ISNUMBER(v)) { IoMessage_locals_numberArgAt_errorForType_(self, locals, n, "Number"); } return v; }
//doc Clutter grabPointer(actor[, deviceId]) IO_METHOD(IoClutter, grabPointer) { ClutterActor *actor = IOCACTOR(IoMessage_locals_clutterActorArgAt_(m, locals, 0)); IoObject *device_n = IoMessage_locals_valueArgAt_(m, locals, 1); if(ISNUMBER(device_n)) clutter_grab_pointer_for_device(actor, CNUMBER(device_n)); else clutter_grab_pointer(actor); return self; }
int IoNumber_compare(IoNumber *self, IoNumber *v) { if (ISNUMBER(v)) { if (DATA(self) == DATA(v)) { return 0; } return (DATA(self) > DATA(v)) ? 1 : -1; } return IoObject_defaultCompare(self, v); }
size_t IoObject_memorySize(IoObject *self) { //return (IoObject_tag(self)->memorySizeFunc) ? (IoObject_tag(self)->memorySizeFunc)(self) : 0; size_t size = sizeof(IoObject); if (IoObject_ownsSlots(self)) size += PHash_memorySize(IoObject_slots(self)); if (!ISNUMBER(self)) { // if(IoObject_dataPointer(self)) size += io_memsize(IoObject_dataPointer(self)); } return size; }
intptr_t marshal(IoDynLib *self, IoObject *arg) { intptr_t n = 0; if (ISNUMBER(arg)) { n = IoNumber_asInt(arg); } else if (ISSYMBOL(arg)) { n = (intptr_t)CSTRING(arg); } else if (ISLIST(arg)) { int i; intptr_t *l = io_calloc(1, IoList_rawSize(arg) * sizeof(intptr_t)); for (i = 0; i < IoList_rawSize(arg); i ++) l[i] = marshal(self, List_rawAt_(IoList_rawList(arg), i)); n = (intptr_t)l; } else if (ISBUFFER(arg)) { n = (intptr_t)IoSeq_rawBytes(arg); } else if (ISBLOCK(arg)) { unsigned char *blk = io_calloc(1, 20), *p = blk; // FIXME: need trampoline code for other architectures *p++ = 0x68; *((intptr_t *)p) = (intptr_t)arg; p += sizeof(intptr_t); *p++ = 0xb8; *((intptr_t *)p) = (intptr_t)bouncer; p += sizeof(intptr_t); *p++ = 0xff; *p++ = 0xd0; *p++ = 0x83; *p++ = 0xc4; *p++ = 0x04; *p++ = 0xc3; n = (intptr_t)blk; } else { n = (intptr_t)arg; //IONIL(self); } return n; }
//doc ClutterUnits -(otherUnit) IO_METHOD(IoClutterUnits, subtract) { IoObject *other = IoMessage_locals_valueArgAt_(m, locals, 0); int self_in_pixels = clutter_units_to_pixels(&IOCUNITS(self)), other_in_pixels = 0; ClutterUnits units; if(ISNUMBER(other)) { other_in_pixels = CNUMBER(other); } else if(ISCLUTTERUNITS(other)) { other_in_pixels = clutter_units_to_pixels(&IOCUNITS(other)); } else { IoState_error_(IOSTATE, m, "ClutterUnits arithmetic works only for Numbers and other ClutterUnits."); return IONIL(self); } clutter_units_from_pixels(&units, self_in_pixels - other_in_pixels); return IoClutterUnits_newWithUnits(IOSTATE, units); }
//doc Loudmouth connect Connects to the server. Returns <code>self</code>. IoObject *IoLoudmouth_connect(IoLoudmouth *self, IoObject *locals, IoMessage *m) { // Q: Should we io_free() these? IoSeq* username = IoObject_getSlot_(self, IOSYMBOL("username")); IoSeq* password = IoObject_getSlot_(self, IOSYMBOL("password")); IoSeq* resource = IoObject_getSlot_(self, IOSYMBOL("resource")); IoSeq* host = IoObject_getSlot_(self, IOSYMBOL("host")); IoNumber* port = IoObject_getSlot_(self, IOSYMBOL("port")); IoObject* use_ssl = IoObject_getSlot_(self, IOSYMBOL("useSsl")); IOASSERT(ISSEQ(username), "Loudmouth: username should be a Sequence"); IOASSERT(ISSEQ(password), "Loudmouth: password should be a Sequence"); IOASSERT(ISSEQ(resource), "Loudmouth: resource should be a Sequence"); IOASSERT(ISSEQ(host), "Loudmouth: host should be a Sequence"); IOASSERT(ISNUMBER(port), "Loudmouth: port should be a Number"); if(LMCONN(self) == NULL) { LmConnection *connection = lm_connection_new_with_context(CSTRING(host), main_context); IoObject_setDataPointer_(self, connection); lm_connection_set_jid(connection, CSTRING(IoObject_getSlot_(self, IOSYMBOL("jid")))); lm_connection_set_port(connection, CNUMBER(port)); if(ISTRUE(use_ssl) && lm_ssl_is_supported()) { LmSSL *ssl = lm_ssl_new(NULL, onSslError, NULL, NULL); lm_connection_set_ssl(connection, ssl); lm_ssl_unref(ssl); } LmMessageHandler* handler = lm_message_handler_new(onXmppMessage, self, NULL); lm_connection_register_message_handler( connection, handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL ); lm_message_handler_unref(handler); lm_connection_set_disconnect_function(connection, onXmppDisconnect, NULL, NULL); } lm_connection_open(LMCONN(self), onXmppConnect, self, NULL, NULL); return self; }
// Serialize/Deserialize char *IoMemcached_serialize(IoMemcached *self, IoObject *locals, IoObject *object, size_t *size, uint32_t *flags) { char *cvalue; if(ISSEQ(object)) { *flags = _FLAG_SEQUENCE; *size = IOSEQ_LENGTH(object); cvalue = (char *) malloc(*size); strncpy(cvalue, CSTRING(object), *size); } else if(ISNUMBER(object)) { *flags = _FLAG_NUMBER; double cnumber = IoNumber_asDouble(object); cvalue = (char *) malloc(128 * sizeof(char)); *size = snprintf(cvalue, 127, "%.16f", cnumber); } else if(ISNIL(object)) { *flags = _FLAG_NIL; *size = 3; cvalue = (char *) malloc(3 * sizeof(char)); strncpy(cvalue, "nil", 3); } else if(ISBOOL(object)) { *flags = _FLAG_BOOLEAN; *size = 1; cvalue = (char *) malloc(sizeof(char)); if(object == IOSTATE->ioTrue) strncpy(cvalue, "1", 1); if(object == IOSTATE->ioFalse) strncpy(cvalue, "0", 1); } else { *flags = _FLAG_OBJECT; IoMessage *serialize = IoMessage_newWithName_(IOSTATE, IOSYMBOL("serialized")); IoSeq *serialized = IoMessage_locals_performOn_(serialize, locals, object); *size = IOSEQ_LENGTH(serialized); cvalue = (char *) malloc(*size); strncpy(cvalue, CSTRING(serialized), *size); } return cvalue; }
IoObject *demarshal(IoObject *self, IoObject *arg, intptr_t n) { if (ISNUMBER(arg)) { return IONUMBER(n); } else if (ISSYMBOL(arg)) { if (n == 0) return IOSYMBOL(""); return IOSYMBOL((char*)n); } else if (ISLIST(arg)) { intptr_t *values = (intptr_t *)n; int i; for (i = 0; i < IoList_rawSize(arg); i ++) { IoObject *value = List_at_(IoList_rawList(arg), i); List_at_put_(IoList_rawList(arg), i, demarshal(self, value, values[i])); } io_free(values); return arg; } else if (ISBUFFER(arg)) { return arg; } else if (ISBLOCK(arg)) { return arg; } return IONIL(self); }
IoObject *IoRegexMatches_setEndPosition(IoRegexMatches *self, IoObject *locals, IoMessage *m) { /*doc RegexMatches setEndPosition(anIndex) Sets the index in the string where the receiver should stop searching. It will be as if the string ends at that index. If <em>index</em> is nil, the end position will be set to the end of string. Returns self. <pre> Io> "funkadelic" matchesOfRegex("\\w+") setEndPosition(4) next string ==> funk Io> "funkadelic" matchesOfRegex("\\w+") setEndPosition(nil) next string ==> funkadelic </pre> */ IoObject *arg = IoMessage_locals_valueArgAt_(m, locals, 0); int stringLength = IoSeq_rawSize(DATA(self)->string); int endPos = stringLength; if (ISNIL(arg)) { DATA(self)->endPosition = endPos; return self; } if (!ISNUMBER(arg)) IoState_error_(IOSTATE, m, "The argument to setEndPosition must be either a Number or nil"); endPos = IoNumber_asInt(arg); if (endPos < 0) endPos = 0; else if (endPos > stringLength) endPos = stringLength; DATA(self)->endPosition = endPos; return self; }