Esempio n. 1
0
/*
 * The indicated node has failed, find and unavailable any pending
 * operations.  We need to go through outstandingInvokes and aggressively
 * locate the state that holds our continuation.
 */
void invokeHandleDown(struct noderecord *nd)
{
  State *state, *nextstate;

  ISetForEach(allProcesses, state) {
    if (!isNoOID(state->nsoid)) {
      nextstate = (State *)OIDFetch(state->nsoid);
      if (ISNIL(nextstate) || !RESDNT(nextstate->firstThing)) {
	if (ISNIL(nextstate)) nextstate = stateFetch(state->nsoid, limbo);
	aggressivelyLocate((Object)nextstate);
	/*
	 * Because aggressivelyLocate may remove the state from allProcesss
	 * we have to fudge with the index
	 */
	if (allProcesses->table[ISetxx_index].key != (int)state) ISetxx_index--;
      }
    } else if (!isNoOID(state->nstoid)) {
      /*
       * This is an outstanding move, we should do something.
       */
      TRACE(rinvoke, 0, ("A node has failed while a move is outstanding for %s",
			 OIDString(state->nstoid)));
      makeReady(state);
    }
  } ISetNext();
}
Esempio n. 2
0
File: IoSyslog.c Progetto: Akiyah/io
IoObject *IoSyslog_open(IoSyslog *self, IoObject *locals, IoMessage *m)
{
	/*doc Syslog open(aPriority, someOptions, optionalIdentity)
	Opens the syslog for writing. optionalIdentity need not be entered 
	and will default to the name of the distribution of Io you are running 
	or if you have embedded Io into your application and set 
	Lobby distribution = "foo", it will be set to "foo".
	*/
	 
	int syslog_facility, syslog_options;
	//int i, max;
	char *syslog_ident;

	if (DATA(self)->syslog_opened)
	{
		IoState_error_(IOSTATE, m, "System log is already open");
		return IONIL(self);
	}

	{
		DATA(self)->facility = IOREF(IoMessage_locals_numberArgAt_(m, locals, 0));
		if (ISNIL(DATA(self)->facility))
		{
			syslog_facility = LOG_USER;
		}
		else
		{
			syslog_facility = IoObject_dataUint32(DATA(self)->facility);
		}

		DATA(self)->options = IOREF(IoMessage_locals_listArgAt_(m, locals, 1));
		syslog_options = 0;
		if (ISNIL(DATA(self)->options))
		{
			syslog_options = LOG_PID | LOG_CONS;
		}
		else
		{
			List *list = IoList_rawList(DATA(self)->options);

			LIST_FOREACH(list, i, v,
				syslog_options |= (int)CNUMBER(v);
			);
		}

		syslog_ident = (char *)IOSYMBOL_BYTES(DATA(self)->ident);
		if ((strlen(syslog_ident) == 0) || ISNIL(DATA(self)->ident))
		{
			char *s = CSTRING(IoState_doCString_(IOSTATE, "Lobby distribution"));
			strncpy(syslog_ident, s, strlen(s));
		}

		openlog(syslog_ident, syslog_options, syslog_facility);
		DATA(self)->syslog_opened = 1;
		DATA(self)->syslog_mask = setlogmask(0);
		setlogmask(DATA(self)->syslog_mask);
	}
Esempio n. 3
0
IO_METHOD(IoSeq, between)
{
	/*doc Sequence between(aSequence, anotherSequence)
	Returns a new Sequence containing the bytes between the
	occurance of aSequence and anotherSequence in the receiver.
	*/

	long start = 0;
	long end = 0;
	IoSeq *fromSeq, *toSeq;

	fromSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 0);

	if (ISSEQ(fromSeq))
	{
		start = UArray_find_from_(DATA(self), DATA(fromSeq), 0) + IoSeq_rawSize(fromSeq);

		if (start == -1)
		{
			start = 0;
		}
	}
	else if (ISNIL(fromSeq))
	{
		start = 0;
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 0, not a %s",
					IoObject_name((IoObject *)fromSeq));
	}

	toSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 1);

	if (ISSEQ(toSeq))
	{
		end = UArray_find_from_(DATA(self), DATA(toSeq), start);
		if (end == -1) start = UArray_size(DATA(self));
	}
	else if (ISNIL(toSeq))
	{
		end = UArray_size(DATA(self));
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 1, not a %s",
					IoObject_name((IoObject *)toSeq));
	}

	{
		UArray *ba = UArray_slice(DATA(self), start, end);
		IoSeq *result = IoSeq_newWithUArray_copy_(IOSTATE, ba, 0);
		return result;
	}
}
Esempio n. 4
0
Vector getnodes(int onlyactive)
{
  ConcreteType nle = BuiltinInstCT(NODELISTELEMENTI);
  ConcreteType nl = BuiltinInstCT(NODELISTI);
  ConcreteType ct;
  Vector thenl;
  Object thenle;
  unsigned int stack[32];
  int i, howmany = 0;
  noderecord **nd;

  for (nd = &allnodes->p; *nd; nd = &(*nd)->p) {
    /* Ignore not yet filled out entries */
    if (ISNIL(OIDFetch((*nd)->node))) continue;
    if (onlyactive && ! (*nd)->up) continue;
    howmany ++;
  }
  anticipateGC(64 * 1024 + howmany * 6 * sizeof(u32));
  thenl = CreateVector(nl, howmany);
  for(i = 0, nd = &allnodes->p ; *nd ; nd = &(*nd)->p) {

    /* Ignore not yet filled out entries */
    if (ISNIL(OIDFetch((*nd)->node))) continue;
    if (onlyactive && ! (*nd)->up) continue;

    TRACE(rinvoke, 6, ("Node %d is on %s and up is %d", i,
		       NodeString((*nd)->srv), (*nd)->up));
    /* Build the node list element, it takes 4 arguments */
    /* The node */
    ct = BuiltinInstCT(NODEI); assert(ct);
    stack[0] = (unsigned int) OIDFetch((*nd)->node);
    stack[1] = (unsigned int) ct;
    /* Is it up? */
    ct = BuiltinInstCT(BOOLEANI); assert(ct);
    stack[4] = (unsigned int) (*nd)->up;
    stack[5] = (unsigned int) ct;
    /* incarnation Time */
    ct = BuiltinInstCT(TIMEI); assert(ct);
    stack[2] = (unsigned int) OIDFetch((*nd)->inctm);
    stack[3] = (unsigned int) ct;
    /* lnn */
    stack[6] = (*nd)->node.port << 16 | (*nd)->node.epoch;
    stack[7] = (unsigned int) intct;
    thenle = CreateObjectFromOutside(nle, (u32)stack);
    ((Object*)thenl->d.data)[i] = thenle;
    i ++;
  }
  inhibit_gc--;

  TRACE(rinvoke, 5, ("getactivenodes() got %d nodes", i));
  return thenl;
}
Esempio n. 5
0
IoObject *IoRegexMatches_next(IoRegexMatches *self, IoObject *locals, IoMessage *m)
{
	/*doc RegexMatches next
	Returns the next match, or nil if there is none.
	*/
	IoRegexMatch *match = 0;

	if (DATA(self)->position >= DATA(self)->endPosition)
		/* We've passed the end position, so we're done. */
		return IONIL(self);

	if (!DATA(self)->currentMatchIsEmpty)
		/* The previous match was not a zero length match, so we can just continue searching
		from the end of that match. */
		return IoRegexMatches_search(self, m);

	/* The last match was a zero length match. If we just continue searching as normal,
	we'll get the same empty match again, and we'll end up in an infinite loop when trying
	to iterate through all matches. Instead we try to find an alternative match by performing
	a search using the options PCRE_NOTEMPTY and PCRE_ANCHORED: */
	match = IoRegexMatches_searchWithOptions_(self, m, PCRE_NOTEMPTY | PCRE_ANCHORED);
	if (!ISNIL(match))
		return match;

	/* No alternative match was found, so we do what Perl does: we advance our position
	by one character, and continue searching from there: */
	++DATA(self)->position;
	return IoRegexMatches_search(self, m);
}
Esempio n. 6
0
File: IoCoroutine.c Progetto: jdp/io
void IoCoroutine_rawReturnToParent(IoCoroutine *self)
{
	IoCoroutine *parent = IoCoroutine_rawParentCoroutine(self);

	if (parent && ISCOROUTINE(parent))
	{
		IoCoroutine_rawResume(parent);
	}
	else
	{
		if (self == IOSTATE->mainCoroutine)
		{
			printf("IoCoroutine error: attempt to return from main coro\n");
			exit(-1);
		}
	}

	if (!ISNIL(IoCoroutine_rawException(self)))
	{
		IoCoroutine_rawPrintBackTrace(self);
	}

	printf("IoCoroutine error: unable to auto abort coro %p by resuming parent coro %s_%p\n",
			(void *)self, IoObject_name(parent), (void *)parent);
	exit(-1);
}
Esempio n. 7
0
IO_METHOD(IoSeq, asBinaryNumber)
{
	/*doc Sequence asBinaryNumber
	Returns a Number containing the first 8 bytes of the
	receiver without casting them to a double. Endian is same as machine.
	*/

	IoNumber *byteCount = IoMessage_locals_valueArgAt_(m, locals, 0);
	size_t max = UArray_size(DATA(self));
	int bc = sizeof(double);
	double d = 0;

	if (!ISNIL(byteCount))
	{
		bc = IoNumber_asInt(byteCount);
	}

	if (max < bc)
	{
		IoState_error_(IOSTATE, m, "requested first %i bytes, but Sequence only contians %i bytes", bc, max);
	}

	memcpy(&d, UArray_bytes(DATA(self)), bc);
	return IONUMBER(d);
}
Esempio n. 8
0
IoObject *IoCFFIPointer_castTo(IoCFFIPointer *self, IoObject *locals, IoMessage *m)
{
    IoObject *toType = IoMessage_locals_valueArgAt_(m, locals, 0);
    IoObject *o = IoState_on_doCString_withLabel_(IOSTATE, toType, "?typeString", "IoCFFIPointer_castTo");

    if(!ISNIL(o)) {
        char *typeStr = CSTRING(o);

        switch(typeStr[0]) {
            case '^':
                toType = IOCLONE(toType);
                *(DATA(toType)->valuePointer) = *((void **)IoCFFIDataType_ValuePointerFromObject_(toType, self));
                return toType;
            case '*':
                toType = IOCLONE(toType);
                IoCFFIDataType_rawSetValue(toType, self);
                return toType;
            default:
                IoState_error_(IOSTATE, m, "Wrong type to cast to.");
                break;
        }
    }
    else {
        // Mm... well, if the type to cast to does not have a typeString slot,
        // it should be an Io Object, so the address stored here is a pointer to an
        // Io Object. Simply cast the pointer and return it... dangerous but...
        
        IoObject *obj = (IoObject *)*(DATA(self)->valuePointer);
        if(ISOBJECT(obj))
            return (IoObject *)*(DATA(self)->valuePointer);
    }

    return IONIL(self);
}
Esempio n. 9
0
File: IoFile.c Progetto: achoy/io
IO_METHOD(IoFile, foreachLine)
{
	/*doc File foreachLine(optionalLineNumber, line, message)
	For each line, set index to the line number of the line
and line and execute aMessage.
Example usage:
<pre>	
aFile foreachLine(i, v, writeln("Line ", i, ": ", v))
aFile foreach(v, writeln("Line: ", v))
</pre>	
*/

	IoObject *result;

	IoSymbol *indexSlotName, *lineSlotName;
	IoMessage *doMessage;
	IoObject *newLine;
	int i = 0;

	IoState *state;

	IoFile_assertOpen(self, locals, m);

	IoMessage_foreachArgs(m, self, &indexSlotName, &lineSlotName, &doMessage);

	result = IONIL(self);
	state = IOSTATE;

	IoState_pushRetainPool(state);

	for (;;)
	{
		IoState_clearTopPool(state);
		newLine = IoFile_readLine(self, locals, m);

		if (ISNIL(newLine))
		{
			break;
		}

		if (indexSlotName)
		{
			IoObject_setSlot_to_(locals, indexSlotName, IONUMBER(i));
		}
		IoObject_setSlot_to_(locals, lineSlotName, newLine);

		result = IoMessage_locals_performOn_(doMessage, locals, locals);
		if (IoState_handleStatus(IOSTATE))
		{
			break;
		}
		i ++;
	}

	IoState_popRetainPool(state);
	return result;
}
Esempio n. 10
0
IO_METHOD(IoMessage, setNext)
{
	/*doc Message setNext(aMessageOrNil)
	Sets the next message in the message chain to a deep copy of
	aMessage or it removes the next message if aMessage is nil. 
	*/

	IoObject *v = IoMessage_locals_valueArgAt_(m , locals, 0);
	IOASSERT(ISMESSAGE(v) || ISNIL(v), "argument must be Message or Nil");

	if (ISNIL(v))
	{
		v = NULL;
	}

	IoMessage_rawSetNext_(self, v);
	return self;
}
Esempio n. 11
0
void handleInvokeForwardRequest(RemoteOpHeader *h, Node srv, Stream str)
{
  ConcreteType ct;
  Object o;
  unsigned int answer;

  anticipateGC(64*1024 + 2 * StreamLength(str));
  TRACE(rinvoke, 3, ("InvokeForwardRequest received"));
  TRACE(rinvoke, 6, ("Checking for CT for incoming activation"));
  ct = (ConcreteType) doObjectRequest(srv, &h->targetct, ctct);
  assert(! ISNIL(ct));

  TRACE(rinvoke, 4, ("InvokeForwarded for object with ID %s", OIDString(h->target)));
  o = OIDFetch(h->target);
  assert(!ISNIL(o));
  if (RESDNT(o->flags)) {
    int more;
    State *state;
    TRACE(rinvoke, 4, ("The object is here, accepting activation"));
    ReadInt(&answer, str);
    more = memcmp(ReadStream(str, 4), "ACT!", 4);
    assert(!more);
    /* Suck out an activation record - argh! */
    TRACE(rinvoke, 6, ("Incoming activation record!!"));
    state = extractActivation(o, ct, str, srv);
    if (!ISNIL(answer)) {
#define sp state->sp
      PUSH(u32, answer);
      PUSH(ConcreteType, BuiltinInstCT(BOOLEANI));
    }
#undef sp
  } else {
    Node newsrv = getLocFromObj(o);
    TRACE(rinvoke, 4, ("Forwarding request to %s", NodeString(newsrv)));
    if (forwardMsg(newsrv, h, str) < 0) {
      Stream newstr;
      RewindStream(str);
      newstr = StealStream(str);
      findAndSendTo(h->target, newstr);
    }
  }
  TRACE(rinvoke, 4, ("Invoke forward request done"));
  inhibit_gc--;
}
Esempio n. 12
0
File: IoBox.c Progetto: ADTSH/io
IoObject *IoBox_resizeBy(IoBox *self, IoObject *locals, IoMessage *m)
{
	IoSeq *d         = IoMessage_locals_pointArgAt_(m, locals, 0);
	int resizeWidth  = IoMessage_locals_intArgAt_(m, locals, 1);
	int resizeHeight = IoMessage_locals_intArgAt_(m, locals, 2);
	IoSeq *minSize   = IoMessage_locals_valueArgAt_(m, locals, 3);
	IoSeq *maxSize   = IoMessage_locals_valueArgAt_(m, locals, 4);

	UArray *mins = ISNIL(minSize) ? 0x0 : IoSeq_rawUArray(minSize);
	UArray *maxs = ISNIL(maxSize) ? 0x0 : IoSeq_rawUArray(maxSize);

	UArray *outd = IoBox_rawResizeBy(self,
	IoSeq_rawUArray(d),
	resizeWidth, resizeHeight,
	mins, maxs);

	IoSeq *out = IoSeq_newWithUArray_copy_(IOSTATE, outd, 0);

	return out;
}
Esempio n. 13
0
IoODEBody *IoMessage_locals_odeBodyArgAt_(IoMessage *self, void *locals, int n)
{
	IoObject *b = IoMessage_locals_valueArgAt_(self, locals, n);

	if (!ISODEBODY(b) && !ISNIL(b))
	{
		IoMessage_locals_numberArgAt_errorForType_(self, locals, n, "ODEBody");
	}

	return b;
}
Esempio n. 14
0
File: geom.c Progetto: ADTSH/io
IoObject *IoMessage_locals_odeGeomArgAt_(IoMessage *self, void *locals, int n)
{
	IoObject *g = IoMessage_locals_valueArgAt_(self, locals, n);

	if (!ISODEGEOM(g) && !ISNIL(g))
	{
		IoMessage_locals_numberArgAt_errorForType_(self, locals, n, "ODEGeom");
	}

	return g;
}
Esempio n. 15
0
dBodyID IoMessage_locals_odeBodyIdArgAt_(IoMessage *self, void *locals, int n)
{
	IoObject *body = IoMessage_locals_odeBodyArgAt_(self, locals, n);
	if (ISNIL(body))
	{
		return 0;
	}
	else
	{
		return DATA(body)->bodyId;
	}
}
Esempio n. 16
0
File: geom.c Progetto: ADTSH/io
dGeomID IoMessage_locals_odeGeomIdArgAt_(IoMessage *self, void *locals, int n)
{
	IoObject *geom = IoMessage_locals_odeGeomArgAt_(self, locals, n);

	if (ISNIL(geom))
	{
		return 0;
	}
	else
	{
		return DATA(geom)->geomId;
	}
}
Esempio n. 17
0
File: IoSocket.c Progetto: BMeph/io
IoSocket *IoSocket_rawSetupEvent_(IoSocket *self, IoObject *locals, IoMessage *m, char *eventSlotName)
{
	IoObject *event = IoObject_rawGetSlot_(self, IOSYMBOL(eventSlotName));
	if(!event || ISNIL(event))
	{
		IoState_error_(IOSTATE, m, "Expected %s slot to be set!", eventSlotName);
		return IONIL(self);
	}
	else
	{
		IoObject_setSlot_to_(event, IOSYMBOL("descriptorId"), IoSocket_descriptorId(self, locals, m));
		return self;
	}
}
Esempio n. 18
0
IoRegexMatches *IoRegexMatches_rawClone(IoRegexMatches *proto)
{
	IoObject *self = IoObject_rawClonePrimitive(proto);
	IoObject_setDataPointer_(self, calloc(1, sizeof(IoRegexMatchesData)));

	if (!ISNIL(DATA(proto)->regex))
		DATA(self)->regex = IOREF(DATA(proto)->regex);
	else
		DATA(self)->regex = IONIL(self);
	DATA(self)->string = IOREF(DATA(proto)->string);

	DATA(self)->captureArray = UArray_clone(DATA(proto)->captureArray);
	return self;
}
Esempio n. 19
0
/*
 * sac_to_gmp() assumes that the incoming integer really is a bignum and is
 * not representable as a machine precision integer.  If the argument sac is
 * machine precision, the while loop becomes infinite (this is bad).
 *
 * On entry I also assume that the caller has initialized gmp_int.
 */
static void sac_to_gmp(mpz_ptr gmp_int, Word sac)
{
    short set_neg_flag = -1; /* when set, this will be 0 or 1 */
    MP_INT pwr, temp;
    Word lsac, d;
#ifdef MP_DEBUG   
    fprintf(stderr, "sac_to_gmp: entering, the bignum here is ");
    IWRITE(sac);printf("\n"); fflush(stderr);
#endif

    if (ISATOM(sac)) {
      if (sac < 0) {
	 set_neg_flag = 1;
	 sac = -sac;
	 }
      else set_neg_flag = 0;
      mpz_set_si(gmp_int, (int) sac);
      if (set_neg_flag) mpz_neg(gmp_int, gmp_int);
      return;
     }
    ADV(sac, &d, &lsac);
    mpz_init(&pwr); mpz_set_si(&pwr, 1);
    
    if (d != 0) {
      set_neg_flag = (d < 0) ? 1 : 0;
      if (set_neg_flag) d = -d;
      }

    mpz_set_si(gmp_int, d);
    mpz_init(&temp);
    while (!ISNIL(lsac)) {
        mpz_mul_ui(&pwr, &pwr, BETA);
        ADV(lsac, &d, &lsac);
        if (d != 0) {
            mpz_clear(&temp); 
	    /* Argh!!  We need to keep checking because we may have
	       had all leading zeroes to this point! */
	    if (set_neg_flag == -1) set_neg_flag = (d < 0) ? 1 : 0;
            if (set_neg_flag) d = -d;
            mpz_mul_ui(&temp, &pwr, d);
            mpz_add(gmp_int, gmp_int, &temp);
        }
    }
    if (set_neg_flag) mpz_neg(gmp_int, gmp_int);

#ifdef MP_DEBUG
    fprintf(stderr,"sac_to_gmp: exiting\n");fflush(stderr);
#endif
   return;
}
Esempio n. 20
0
File: IoNumber.c Progetto: Akiyah/io
IO_METHOD(IoNumber, asBuffer)
{
	/*doc Number asBuffer(optionalNumberOfBytes)
	Returns a Buffer containing a the number of bytes specified by
	optionalNumberOfBytes (up to the size of a double on the platform) of the reciever.
	If no optionalNumberOfBytes is specified, it is assumed to be the number of bytes
	in a double on the host platform.
	*/
	IoNumber *byteCount = IoMessage_locals_valueArgAt_(m, locals, 0);
	int bc = sizeof(double);

	if (!ISNIL(byteCount))
	{
		bc = DATA(byteCount);
	}
	return IoSeq_newWithData_length_(IOSTATE, (unsigned char *)&(DATA(self)), bc);
}
Esempio n. 21
0
IoCFFIArray *IoCFFIArray_rawClone(IoCFFIArray *proto)
{
	IoObject *self = IoObject_rawClonePrimitive(proto);
	IoObject_setDataPointer_(self, io_calloc(1, sizeof(IoCFFIArrayData)));
	memset(DATA(self), 0, sizeof(IoCFFIArrayData));

	IoObject* arrayType = IoObject_getSlot_(proto, IOSYMBOL("arrayType"));

	if ( !ISNIL(arrayType) ) {
		DATA(self)->ffiType = DATA(proto)->ffiType;
		DATA(self)->itemSize = DATA(proto)->itemSize;
		DATA(self)->arraySize = DATA(proto)->arraySize;
		DATA(self)->buffer = io_calloc(DATA(self)->arraySize, DATA(self)->itemSize);
		DATA(self)->needToFreeBuffer = 1;
	}
	return self;
}
Esempio n. 22
0
IoObject *IoCairoContext_setDash(IoCairoContext *self, IoObject *locals, IoMessage *m)
{
	IoList *dashList = IoMessage_locals_valueArgAt_(m, locals, 0);
	double offset = IoMessage_locals_doubleArgAt_(m, locals, 1);
	List *list = 0;
	int dashCount = 0;
	double *dashes = 0;

	if (!ISNIL(dashList)) {
		list = IoList_rawList(dashList);
		dashCount = List_size(list);
	}

	if (dashCount > 0)
	{
		dashes = malloc(sizeof(double) * dashCount);
		LIST_FOREACH(list, i, number,
			dashes[i] = IoNumber_asDouble(number);
		);
Esempio n. 23
0
// 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;
}
Esempio n. 24
0
File: IoFile.c Progetto: achoy/io
IO_METHOD(IoFile, readLines)
{
	/*doc File readLines
	Returns list containing all lines in the file.
	*/

	IoState *state = IOSTATE;

	if (!DATA(self)->stream)
	{
		IoFile_openForReading(self, locals, m);
	}

	IoFile_assertOpen(self, locals, m);

	{
		IoList *lines = IoList_new(state);
		IoObject *newLine;

		IoState_pushRetainPool(state);

		for (;;)
		{
			IoState_clearTopPool(state);
			newLine = IoFile_readLine(self, locals, m);

			if (ISNIL(newLine))
			{
				break;
			}

			IoList_rawAppend_(lines, newLine);
		}
		IoState_popRetainPool(state);

		return lines;
	}
}
Esempio n. 25
0
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;
}
Esempio n. 26
0
bool bpx_is_nil(TERM t)
{
    XDEREF(t);
    return ISNIL(t);
}
Esempio n. 27
0
int IoMessage_locals_boolArgAt_(IoMessage *self, IoObject *locals, int n)
{
	IoObject *v = IoMessage_locals_valueArgAt_(self, locals, n);

	return !ISNIL(v) && !ISFALSE(v);
}
Esempio n. 28
0
IO_METHOD(IoSeq, between)
{
	/*doc Sequence betweenSeq(aSequence, anotherSequence)
	Returns a new Sequence containing the bytes between the
	occurrence of aSequence and anotherSequence in the receiver. 
	If aSequence is empty, this method is equivalent to beforeSeq(anotherSequence).
	If anotherSequence is nil, this method is equivalent to afterSeq(aSequence).
	nil is returned if no match is found.
	*/

	long start = 0;
	long end = 0;
	IoSeq *fromSeq, *toSeq;

	fromSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 0);

	if (ISSEQ(fromSeq))
	{
		if (IoSeq_rawSize(fromSeq) == 0)
		{
			start = 0;
		}
		else
		{
			start = UArray_find_from_(DATA(self), DATA(fromSeq), 0);

			if (start == -1)
			{
				//start = 0;
				return IONIL(self);
			}
			start += IoSeq_rawSize(fromSeq);
		}
	}
	else if (ISNIL(fromSeq))
	{
		start = 0;
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 0, not a %s",
					IoObject_name((IoObject *)fromSeq));
	}

	toSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 1);

	if (ISSEQ(toSeq))
	{
		end = UArray_find_from_(DATA(self), DATA(toSeq), start);
		//if (end == -1) start = UArray_size(DATA(self));
		if (end == -1) return IONIL(self);
	}
	else if (ISNIL(toSeq))
	{
		end = UArray_size(DATA(self));
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 1, not a %s",
					IoObject_name((IoObject *)toSeq));
	}

	{
		UArray *ba = UArray_slice(DATA(self), start, end);
		IoSeq *result = IoSeq_newWithUArray_copy_(IOSTATE, ba, 0);
		return result;
	}
}
Esempio n. 29
0
void handleInvokeRequest(RemoteOpHeader *h, Node srv, Stream str)
{
  Stream newstr;
  int argc = h->option2, retc = h->option1, fn = h->status, i;
  Object obj;
  ConcreteType ct = 0;
  RemoteOpHeader replyh;
  int *sp;
  State *state;

  anticipateGC(64 * 1024 + 2 * StreamLength(str));
  TRACE(rinvoke, 3, ("InvokeRequest received"));
  /* figure out who we're invoking on */
  obj = OIDFetch(h->target);

  if (!ISNIL(obj)) {
    ct = CODEPTR(obj->flags);
    TRACE(rinvoke, 4, ("Target is a %.*s, operation name is %.*s[%d]",
		       ct->d.name->d.items, ct->d.name->d.data, 
		       ct->d.opVector->d.data[fn]->d.name->d.items,
		       ct->d.opVector->d.data[fn]->d.name->d.data, argc));
  } else {
    TRACE(rinvoke, 1, ("Invoking %s op %d [%d] -> [%d]", OIDString(h->target),
		       fn, argc, retc));
  }

  if (ISNIL(obj)) {
    /*
     * Invoke came here, but we don't know anything about this object.
     * First find it, then send it the message.
     */
    TRACE(rinvoke, 1, ("Trying to find the object and send it the message"));
    ct = (ConcreteType)doObjectRequest(replyh.sslocation, &h->targetct, ctct);
    obj = createStub(ct, getNodeRecordFromSrv(replyh.sslocation), h->target);

    RewindStream(str);
    newstr = StealStream(str);
    findAndSendTo(h->target, newstr);
  } else if (!RESDNT(obj->flags)) {
    Node newsrv = getLocFromObj(obj);
    /* Invoke came here, but the object is elsewhere */
    /* First check to see if we think the object is where this invoke
       came from */
    if (SameNode(srv, newsrv) || SameNode(myid, newsrv) || SameNode(limbo, newsrv)) {
      TRACE(rinvoke, 1, ("Have stub, but points back.  Forwarding to limbo"));
      RewindStream(str);
      newstr = StealStream(str);
      findAndSendTo(h->target, newstr);
    } else {
      TRACE(rinvoke, 1, ("Forwarding invoke to %s", NodeString(newsrv)));
      if (forwardMsg(newsrv, h, str) < 0) {
	RewindStream(str);
	newstr = StealStream(str);
	findAndSendTo(h->target, newstr);
      }
    }
  } else if (fakeUnavailable && ((random() % 100) < fakeUnavailable)) {
    newstr = StealStream(str);
    sendUnavailableReply(newstr);
  } else {
    OID oid;
    state = newState(obj, ct);
    OIDRemoveAny((Object)state);
    ReadOID(&oid, str);
    OIDInsert(oid, (Object) state);
    for (sp = (int *)state->sb, i = 0 ; i < 2 * retc ; i++) *sp++ = JNIL;
    extractNVars(str, argc, sp, &state->ep, &state->et, srv);
    sp += argc * 2;
    TRACE(rinvoke, 4, ("Doing upcall on a %.*s",
		       CODEPTR(obj->flags)->d.name->d.items, 
		       CODEPTR(obj->flags)->d.name->d.data));
    state->sp = (u32)sp;
    pushBottomAR(state);

    /* set up the interpreter state */
    state->pc = (u32) ct->d.opVector->d.data[fn]->d.code->d.data;
    dependsOn(state, stateFetch(h->ss, h->sslocation), retc);
    makeReady(state);
  }
  inhibit_gc--;
}
Esempio n. 30
0
static int iconforms(AbstractType a, AbstractType b, int l)
{
  OID ao, bo;
  int result, i, j, k, found, foundincache = 0;
  ATOpVectorElement aove, bove;

  if (!conformMap) conformMap = OOIScCreate();
  assert(assumeMap);
  if (ISNIL(a) || ISNIL(b)) {
    TRACE(conform, 0, ("emx: conforms on nil type"));
    return 1;
  }

  ao = OIDOf(a);
  bo = OIDOf(b);
  TRACE(conform, 2, ((pad(l), "==> %.*s (%#x) vs %.*s (%#x)"), 
		     a->d.name->d.items,
		     a->d.name->d.data,
		     ao.Seq,
		     b->d.name->d.items,
		     b->d.name->d.data,
		     bo.Seq));
  l++;
  result = OOIScLookup(conformMap, ao, bo);
  if (result >= 0) {
    TRACE(conform, 2, ((pad(l), "Found answer in cache")));
    foundincache = 1;
    DONE(result);
  }
  result = OOIScLookup(assumeMap, ao, bo);
  if (result >= 0) {
    TRACE(conform, 2, ((pad(l), "We are assuming these conform")));
    DONE(result);
  }
  if (a == BuiltinInstAT(NILI)) {
    TRACE(conform, 2, ((pad(l), "%.*s is None"),
	   a->d.name->d.items,
	   a->d.name->d.data));
    DONE(1);
  }
  if (b == BuiltinInstAT(ANYI)) {
    TRACE(conform, 2, ((pad(l), "%.*s is Any"),
		       b->d.name->d.items,
		       b->d.name->d.data));
    DONE(1);
  } 
  if (b == BuiltinInstAT(NILI)) {
    TRACE(conform, 2, ((pad(l), "%.*s is None"),
		       b->d.name->d.items,
		       b->d.name->d.data));
    DONE(0);
  } 
  if (a == b) {
    TRACE(conform, 2, ((pad(l), "%.*s == %.*s"),
		       a->d.name->d.items,
		       a->d.name->d.data,
		       b->d.name->d.items,
		       b->d.name->d.data));
    DONE(1);
  } 
  if (EqOID(ao, bo)) {
    TRACE(conform, 2, ((pad(l), "%.*s = %.*s"),
		       a->d.name->d.items,
		       a->d.name->d.data,
		       b->d.name->d.items,
		       b->d.name->d.data));
    DONE(1);
  } 
  if (b->d.flags & AT_ISVECTOR) {
    if (!(a->d.flags & AT_ISVECTOR)) {
      TRACE(conform, 2, ((pad(l), "%.*s is a vector and %.*s is not"),
			 a->d.name->d.items,
			 a->d.name->d.data,
			 b->d.name->d.items,
			 b->d.name->d.data));
      DONE(0);
    }
  } else if (isNoNode(bo) && isBuiltinINSTAT(bo.Seq) &&
	     (bo.Seq == OIDOfBuiltin(B_INSTAT, BOOLEANI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, CHARACTERI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, INTEGERI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, NODEI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, SIGNATUREI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, REALI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, STRINGI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, TIMEI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, NODELISTELEMENTI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, NODELISTI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, INSTREAMI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, OUTSTREAMI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, BITCHUNKI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, CONCRETETYPEI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, COPVECTORI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, COPVECTOREI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, AOPVECTORI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, AOPVECTOREI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, APARAMLISTI) ||
	      bo.Seq == OIDOfBuiltin(B_INSTAT, INTERPRETERSTATEI))) {
    TRACE(conform, 2, ((pad(l), "%.*s is a cannot-be-conformed-to builtin"),
		       b->d.name->d.items,
		       b->d.name->d.data));
    DONE(0);
  } 
  if (b->d.flags & AT_ISIMMUTABLE
      && !a->d.flags & AT_ISIMMUTABLE) {
    TRACE(conform, 2, ((pad(l), "%.*s is not immutable"), 
		       a->d.name->d.items,
		       a->d.name->d.data));
    DONE(0);
  } 
  if (a->d.ops->d.items < b->d.ops->d.items) {
    TRACE(conform, 2, ((pad(l), "%.*s doesn't have enough operations"),
		       a->d.name->d.items,
		       a->d.name->d.data));
    DONE(0);
  }
  OOIScInsert(assumeMap, ao, bo, 1);
  for (i = 0; i < b->d.ops->d.items; i++) {
    bove = b->d.ops->d.data[i];
    found = 0;
    TRACE(conform, 4, ((pad(l-1), "Looking for operation %.*s[%d]"),
		       bove->d.name->d.items,
		       bove->d.name->d.data,
		       VLen(bove->d.arguments)));
    for (j = 0;
	 j < a->d.ops->d.items;
	 j++) {
      aove = a->d.ops->d.data[j];
      if (samename(aove, bove)) {
	if (bove->d.isFunction
	    && !aove->d.isFunction) {
	  TRACE(conform, 2, ((pad(l), "Operation %.*s[%d] is not a function"),
			     aove->d.name->d.items,
			     aove->d.name->d.data,
			     VLen(aove->d.arguments)));
	  DONE(0);
	}
	if (!ISNIL(aove->d.arguments)) {
	  for (k = 0; k < aove->d.arguments->d.items; k++) {
	    TRACE(conform, 4, ((pad(l-1), "Checking argument %d"), k));
	    if (!iconforms(bove->d.arguments->
			   d.data[k],
			   aove->d.arguments->
			   d.data[k], l+1)) {
	      TRACE(conform, 2, ((pad(l), "Operation %.*s[%d] argument %d doesn't conform"),
				 aove->d.name->d.items,
				 aove->d.name->d.data,
				 VLen(aove->d.arguments), k));
	      DONE(0);
	    }
	  }
	}
	if (!ISNIL(aove->d.results)) {
	  for (k = 0;
	       k < aove->d.results->d.items;
	       k++) {
	    TRACE(conform, 4, ((pad(l-1), "Checking result %d"), k));
	    if (!iconforms(aove->d.results->
			   d.data[k],
			   bove->d.results->
			   d.data[k], l+1)) {
	      TRACE(conform, 2, ((pad(l), "Operation %.*s[%d] result %d doesn't conform"),
				 aove->d.name->d.items,
				 aove->d.name->d.data, k));
	      DONE(0);
	    }
	  }
	}
	found = 1;
	break;
      }
    }
    if (!found) {
      TRACE(conform, 2, ((pad(l), "Operation %.*s[%d] is not defined"),
			 bove->d.name->d.items,
			 bove->d.name->d.data,
			 VLen(bove->d.arguments)));
      DONE(0);
    }
  }
  result = 1;
 done:
  l--;
  TRACE(conform, 2, ((pad(l), "<== %s"), result ? "yes" : "no"));
  if (l == 0 && !foundincache) {
    OOIScInsert(conformMap, ao, bo, result);
  }
  return result;
}