Example #1
0
static ekU32 ekiInt(struct ekContext * E, ekU32 argCount)
{
    ekValue * v = NULL;
    if (!ekContextGetArgs(E, argCount, "?", &v)) {
        return ekContextArgsFailure(E, argCount, "int(value)");
    }

    ekArrayPush(E, &E->stack, ekValueToInt(E, v));
    return 1;
}
Example #2
0
static ekU32 fileRead(struct ekContext *E, ekU32 argCount)
{
    ekValue *thisValue;
    ekValue *ret = ekValueNullPtr;
    ekValue *bytesValue = NULL;
    int bytes = 0;
    ekFile *file;

    if(!ekContextGetArgs(E, argCount, "*F|i", &thisValue, &bytesValue))
    {
        return ekContextArgsFailure(E, argCount, "file.read([optional int] bytes)");
    }

    file = (ekFile *)thisValue->ptrVal;

    switchState(E, file, EFS_READ);

    if(file->handle)
    {
        if(bytesValue)
        {
            bytesValue = ekValueToInt(E, bytesValue);
            bytes = bytesValue->intVal;
            ekValueRemoveRefNote(E, bytesValue, "bytesValue temporary no longer needed");
        }
        else
        {
            // we want "the rest" of the file (could be the whole thing if you just opened it)
            int end;
            int currentPos = ftell(file->handle);
            fseek(file->handle, 0, SEEK_END);
            end = ftell(file->handle);
            fseek(file->handle, currentPos, SEEK_SET);
            bytes = end - currentPos;
        }

        if(bytes > 0)
        {
            char *data = ekAlloc(bytes+1);
            int bytesRead = fread(data, 1, bytes, file->handle);
            if(bytesRead >= 0)
            {
                data[bytesRead] = 0;
                ret = ekValueDonateString(E, data);
            }
            else
            {
                ekFree(data);
            }
        }
    }

    ekValueRemoveRefNote(E, thisValue, "fileRead no longer needs thisValue");
    ekContextReturn(E, ret); // will be the data if we succesfully read
}
Example #3
0
static struct ekValue * intFuncArithmetic(struct ekContext * E, struct ekValue * a, struct ekValue * b, ekValueArithmeticOp op)
{
    ekValue * c = NULL;
    b = ekValueToInt(E, b);
    switch (op) {
        case EVAO_ADD:
            c = ekValueCreateInt(E, a->intVal + b->intVal);
            break;
        case EVAO_SUB:
            c = ekValueCreateInt(E, a->intVal - b->intVal);
            break;
        case EVAO_MUL:
            c = ekValueCreateInt(E, a->intVal * b->intVal);
            break;
        case EVAO_DIV:
            if (!b->intVal) {
                ekContextSetError(E, EVE_RUNTIME, "divide by zero!");
            } else {
                c = ekValueCreateInt(E, a->intVal / b->intVal);
            }
            break;
    }
    return c;
}