예제 #1
0
static void initRangeStartStopStep(Thread *thread) {
    EmojicodeRange *range = stackGetThisObject(thread)->value;
    range->start = stackGetVariable(0, thread).raw;
    range->stop = stackGetVariable(1, thread).raw;
    range->step = stackGetVariable(2, thread).raw;
    if (range->step == 0) rangeSetDefaultStep(range);
}
예제 #2
0
Something integerToString(Thread *thread) {
    EmojicodeInteger base = stackGetVariable(0, thread).raw;
    EmojicodeInteger n = stackGetThisContext(thread).raw, a = llabs(n);
    bool negative = n < 0;
    
    EmojicodeInteger d = negative ? 2 : 1;
    while (n /= base) d++;
    
    Object *co = newArray(d * sizeof(EmojicodeChar));
    stackSetVariable(0, somethingObject(co), thread);
    
    Object *stringObject = newObject(CL_STRING);
    String *string = stringObject->value;
    string->length = d;
    string->characters = stackGetVariable(0, thread).object;
    
    EmojicodeChar *characters = characters(string) + d;
    do
        *--characters =  "0123456789abcdefghijklmnopqrstuvxyz"[a % base % 35];
    while (a /= base);
    
    if (negative) characters[-1] = '-';
    
    return somethingObject(stringObject);
}
예제 #3
0
Something socketSendData(Thread *thread) {
    int connectionAddress = *(int *)stackGetThisObject(thread)->value;
    Data *data = stackGetVariable(0, thread).object->value;
    if (send(connectionAddress, data->bytes, data->length, 0) == -1) {
        return EMOJICODE_FALSE;
    }
    return EMOJICODE_TRUE;
}
예제 #4
0
static Something dataEqual(Thread *thread) {
    Data *d = stackGetThisObject(thread)->value;
    Data *b = stackGetVariable(0, thread).object->value;
    
    if(d->length != b->length){
        return EMOJICODE_FALSE;
    }
    
    return memcmp(d->bytes, b->bytes, d->length) == 0 ? EMOJICODE_TRUE : EMOJICODE_FALSE;
}
예제 #5
0
static Something systemGetEnv(Thread *thread){
    char* variableName = stringToChar(stackGetVariable(0, thread).object->value);
    char* env = getenv(variableName);
    
    if(!env)
        return NOTHINGNESS;
    
    free(variableName);
    return somethingObject(stringFromChar(env));
}
예제 #6
0
static Something doubleToString(Thread *thread) {
    EmojicodeInteger precision = stackGetVariable(0, thread).raw;
    double d = stackGetThisContext(thread).doubl;
    double absD = fabs(d);
    
    bool negative = d < 0;
    
    EmojicodeInteger length = negative ? 1 : 0;
    if (precision != 0) {
        length++;
    }
    length += precision;
    EmojicodeInteger iLength = 1;
    for (size_t i = 1; pow(10, i) < absD; i++) {
        iLength++;
    }
    length += iLength;
    
    Object *co = newArray(length * sizeof(EmojicodeChar));
    stackSetVariable(0, somethingObject(co), thread);
    Object *stringObject = newObject(CL_STRING);
    String *string = stringObject->value;
    string->length = length;
    string->characters = stackGetVariable(0, thread).object;
    
    EmojicodeChar *characters = characters(string) + length;
    
    for (size_t i = precision; i > 0; i--) {
        *--characters =  (unsigned char) (fmod(absD * pow(10, i), 10.0)) % 10 + '0';
    }
    
    if (precision != 0) {
        *--characters = '.';
    }
    
    for (size_t i = 0; i < iLength; i++) {
        *--characters =  (unsigned char) (fmod(absD / pow(10, i), 10.0)) % 10 + '0';
    }
    
    if (negative) characters[-1] = '-';
    return somethingObject(stringObject);
}
예제 #7
0
static Something systemSystem(Thread *thread) {
    char *command = stringToChar(stackGetVariable(0, thread).object->value);
    FILE *f = popen(command, "r");
    free(command);
    
    if (!f) {
        return NOTHINGNESS;
    }
    
    size_t bufferUsedSize = 0;
    int bufferSize = 50;
    Object *buffer = newArray(bufferSize);
    
    while (fgets((char *)buffer->value + bufferUsedSize, bufferSize - (int)bufferUsedSize, f) != NULL) {
        bufferUsedSize = strlen(buffer->value);
        
        if (bufferSize - bufferUsedSize < 2) {
            bufferSize *= 2;
            buffer = resizeArray(buffer, bufferSize);
        }
    }
    
    bufferUsedSize = strlen(buffer->value);
    
    EmojicodeInteger len = u8_strlen_l(buffer->value, bufferUsedSize);
    
    Object *so = newObject(CL_STRING);
    stackSetVariable(0, somethingObject(so), thread);
    String *string = so->value;
    string->length = len;
    
    Object *chars = newArray(len * sizeof(EmojicodeChar));
    string = stackGetVariable(0, thread).object->value;
    string->characters = chars;
    
    u8_toucs(characters(string), len, buffer->value, bufferUsedSize);
    
    return stackGetVariable(0, thread);
}
예제 #8
0
static Something dataGetByte(Thread *thread) {
    Data *d = stackGetThisObject(thread)->value;
    
    EmojicodeInteger index = unwrapInteger(stackGetVariable(0, thread));
    if (index < 0) {
        index += d->length;
    }
    if (index < 0 || d->length <= index){
        return NOTHINGNESS;
    }
    
    return somethingInteger(d->bytes[index]);
}
예제 #9
0
void socketInitWithHost(Thread *thread) {
    char *string = stringToChar(stackGetVariable(0, thread).object->value);
    char *service = stringToChar(stackGetVariable(1, thread).object->value);
    
    struct addrinfo *res;
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    if (getaddrinfo(string, service, &hints, &res) == -1) {
        stackGetThisObject(thread)->value = NULL;
        return;
    }
    free(string);
    free(service);
    int socketDescriptor = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (socketDescriptor == -1 || connect(socketDescriptor, res->ai_addr, res->ai_addrlen) == -1) {
        freeaddrinfo(res);
        stackGetThisObject(thread)->value = NULL;
        return;
    }
    freeaddrinfo(res);
    *(int *)stackGetThisObject(thread)->value = socketDescriptor;
}
예제 #10
0
static Something systemArgs(Thread *thread) {
    stackPush(NOTHINGNESS, 1, 0, thread);
    
    Object *listObject = newObject(CL_LIST);
    stackSetVariable(0, somethingObject(listObject), thread);
    
    List *newList = listObject->value;
    newList->capacity = cliArgumentCount;
    Object *items = newArray(sizeof(Something) * cliArgumentCount);
    
    listObject = stackGetVariable(0, thread).object;
    
    ((List *)listObject->value)->items = items;
    
    for (int i = 0; i < cliArgumentCount; i++) {
        listAppend(listObject, somethingObject(stringFromChar(cliArguments[i])), thread);
    }
    
    stackPop(thread);
    return somethingObject(listObject);
}
예제 #11
0
void serverInitWithPort(Thread *thread) {
    int listenerDescriptor = socket(PF_INET, SOCK_STREAM, 0);
    if (listenerDescriptor == -1) {
        stackGetThisObject(thread)->value = NULL;
        return;
    }
    
    struct sockaddr_in name;
    name.sin_family = PF_INET;
    name.sin_port = htons(stackGetVariable(0, thread).raw);
    name.sin_addr.s_addr = htonl(INADDR_ANY);
    
    int reuse = 1;
    if (setsockopt(listenerDescriptor, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(int)) == -1 ||
        bind(listenerDescriptor, (struct sockaddr *)&name, sizeof(name)) == -1 ||
        listen(listenerDescriptor, 10) == -1) {
        stackGetThisObject(thread)->value = NULL;
        return;
    }
    
    *(int *)stackGetThisObject(thread)->value = listenerDescriptor;
}
예제 #12
0
Something socketReadBytes(Thread *thread) {
    int connectionAddress = *(int *)stackGetThisObject(thread)->value;
    EmojicodeInteger n = unwrapInteger(stackGetVariable(0, thread));
    
    Object *bytesObject = newArray(n);
    
    size_t read = recv(connectionAddress, bytesObject->value, n, 0);
    
    if (read < 1) {
        return NOTHINGNESS;
    }
    
    stackPush(somethingObject(bytesObject), 0, 0, thread);
    
    Object *obj = newObject(CL_DATA);
    Data *data = obj->value;
    data->length = read;
    data->bytesObject = stackGetThisObject(thread);
    data->bytes = data->bytesObject->value;
    
    stackPop(thread);
    return somethingObject(obj);
}
예제 #13
0
static Something systemExit(Thread *thread){
    EmojicodeInteger state = unwrapInteger(stackGetVariable(0, thread));
    exit((int)state);
    return NOTHINGNESS;
}
예제 #14
0
static Something doublePow(Thread *thread) {
    return somethingDouble(pow(stackGetThisContext(thread).doubl, stackGetVariable(0, thread).doubl));
}
예제 #15
0
static void initThread(Thread *thread) {
    Thread *t = allocateThread();
    stackPush(stackGetVariable(0, thread), 0, 0, t);
    pthread_create((pthread_t *)((Object *)stackGetThisObject(thread))->value, NULL, threadStarter, t);
}
예제 #16
0
static Something rangeGet(Thread *thread) {
    EmojicodeRange *range = stackGetThisObject(thread)->value;
    EmojicodeInteger h = range->start + stackGetVariable(0, thread).raw * range->step;
    return (range->step > 0 ? range->start <= h && h < range->stop : range->stop < h && h <= range->start) ? somethingInteger(h) : NOTHINGNESS;
}
static Something bridgeDictionaryRemove(Thread *thread){
    dictionaryRemove(stackGetThis(thread)->value, stackGetVariable(0, thread).object);
    return NOTHINGNESS;
}
예제 #18
0
void newErrorBridge(Thread *thread){
    EmojicodeError *error = stackGetThisObject(thread)->value;
    error->message = stringToChar(stackGetVariable(0, thread).object->value);
    error->code = unwrapInteger(stackGetVariable(1, thread));
}
static Something bridgeDictionaryGet(Thread *thread){
    return dictionaryLookup(stackGetThis(thread)->value, stackGetVariable(0, thread).object);
}
예제 #20
0
static Something threadSleep(Thread *thread){
    sleep((unsigned int)stackGetVariable(0, thread).raw);
    return NOTHINGNESS;
}
예제 #21
0
static Something integerRandom(Thread *thread) {
    return somethingInteger(secureRandomNumber(stackGetVariable(1, thread).raw, stackGetVariable(1, thread).raw));
}
static Something bridgeDictionarySet(Thread *thread){
    dictionarySet(stackGetThis(thread), stackGetVariable(0, thread).object, stackGetVariable(1, thread), thread);
    return NOTHINGNESS;
}