Beispiel #1
0
static SLVAL
sl_mysql_init(sl_vm_t* vm, SLVAL self, SLVAL host, SLVAL user, SLVAL password)
{
    char *host_s = NULL,
         *user_s = NULL,
         *password_s = NULL,
         *db_s = NULL;
    int  port_i = 3306,
         flag_i = CLIENT_IGNORE_SIGPIPE | CLIENT_MULTI_STATEMENTS;

    mysql_t* mysql = sl_data_get_ptr(vm, &mysql_data_type, self);

    if(sl_is_truthy(host)) {
        host_s = sl_to_cstr(vm, sl_expect(vm, host, vm->lib.String));
    }
    if(sl_is_truthy(user)) {
        user_s = sl_to_cstr(vm, sl_expect(vm, user, vm->lib.String));
    }
    if(sl_is_truthy(password)) {
        password_s = sl_to_cstr(vm, sl_expect(vm, password, vm->lib.String));
    }

    if(!mysql_real_connect(&mysql->mysql, host_s, user_s, password_s, db_s, port_i, NULL, flag_i)) {
        sl_mysql_check_error(vm, &mysql->mysql);
    }

    if(!mysql_set_character_set(&mysql->mysql, "utf8")) {
        sl_mysql_check_error(vm, &mysql->mysql);
    }

    mysql->valid = 1;

    return self;
}
Beispiel #2
0
static SLVAL
enumerable_reject(sl_vm_t* vm, SLVAL self, SLVAL f)
{
    SLVAL val, truthy, ary = sl_make_array(vm, 0, NULL);
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        val = sl_send(vm, enumerator, "current", 0);
        truthy = sl_send(vm, f, "call", 1, val);
        if(!sl_is_truthy(truthy)) {
            sl_array_push(vm, ary, 1, &val);
        }
    }
    return ary;
}
Beispiel #3
0
static SLVAL
enumerable_find(sl_vm_t* vm, SLVAL self, SLVAL f)
{
    SLVAL val, truthy;
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        val = sl_send(vm, enumerator, "current", 0);
        truthy = sl_send(vm, f, "call", 1, val);
        if(sl_is_truthy(truthy)) {
            return val;
        }
    }
    return vm->lib.nil;
}
Beispiel #4
0
static SLVAL
response_descriptive_error_pages_set(sl_vm_t* vm, SLVAL self, SLVAL enabled)
{
    (void)self;
    response(vm)->descriptive_error_pages = sl_is_truthy(enabled);
    return enabled;
}
Beispiel #5
0
static SLVAL
sl_string_replace(sl_vm_t* vm, SLVAL self, SLVAL search, SLVAL replace)
{
    if(sl_is_a(vm, search, vm->lib.String)) {
        return sl_enumerable_join(vm, sl_string_split(vm, self, 1, &search), 1, &replace);
    }

    sl_expect(vm, search, vm->lib.Regexp);

    SLVAL retn = sl_make_cstring(vm, "");

    while(1) {
        SLVAL match = sl_regexp_match(vm, search, 1, &self);

        if(!sl_is_truthy(match)) {
            return sl_string_concat(vm, retn, self);
        } else {
            SLVAL part = sl_regexp_match_before(vm, match);
            if(sl_is_a(vm, replace, vm->lib.String)) {
                part = sl_string_concat(vm, part, replace);
            } else {
                part = sl_string_concat(vm, part, sl_send_id(vm, replace, vm->id.call, 1, match));
            }
            retn = sl_string_concat(vm, retn, part);
        }

        self = sl_regexp_match_after(vm, match);
    }
}
Beispiel #6
0
static SLVAL
enumerable_any(sl_vm_t* vm, SLVAL self, size_t argc, SLVAL* argv)
{
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    SLVAL val;
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        val = sl_send(vm, enumerator, "current", 0);
        if(argc > 0) {
            val = sl_send(vm, argv[0], "call", 1, val);
        }
        if(sl_is_truthy(val)) {
            return vm->lib._true;
        }
    }
    return vm->lib._false;
}
Beispiel #7
0
static SLVAL
enumerable_empty(sl_vm_t* vm, SLVAL self)
{
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    if(!sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        return vm->lib._true;
    } else {
        return vm->lib._false;
    }
}
Beispiel #8
0
static SLVAL
enumerable_length(sl_vm_t* vm, SLVAL self)
{
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    int i = 0;
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        i++;
    }
    return sl_make_int(vm, i);
}
Beispiel #9
0
static SLVAL
enumerable_drop(sl_vm_t* vm, SLVAL self, SLVAL countv)
{
    SLVAL ary = sl_make_array(vm, 0, NULL);
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    
    for(int count = sl_get_int(countv); count > 0; count--) {
        if(!sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
            return ary;
        }
    }
    
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        SLVAL elem = sl_send(vm, enumerator, "current", 0);
        sl_array_push(vm, ary, 1, &elem);
    }
    
    return ary;
}
Beispiel #10
0
static SLVAL
range_init(sl_vm_t* vm, SLVAL self, size_t argc, SLVAL* argv)
{
    sl_range_t* range = get_range(vm, self);
    range->left = argv[0];
    range->right = argv[1];
    if(argc > 2 && sl_is_truthy(argv[2])) {
        range->exclusive = 1;
    }
    return self;
}
Beispiel #11
0
static SLVAL
enumerable_to_a(sl_vm_t* vm, SLVAL self)
{
    SLVAL array = sl_make_array(vm, 0, NULL);
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    SLVAL val;
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        val = sl_send(vm, enumerator, "current", 0);
        sl_array_push(vm, array, 1, &val);
    }
    return array;
}
Beispiel #12
0
SLVAL
sl_enumerable_join(sl_vm_t* vm, SLVAL self, size_t argc, SLVAL* argv)
{
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    SLVAL joiner, val, str;
    if(argc) {
        joiner = sl_to_s(vm, argv[0]);
    } else {
        joiner = sl_make_cstring(vm, "");
    }
    if(!sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        return sl_make_cstring(vm, "");
    }
    str = sl_to_s(vm, sl_send(vm, enumerator, "current", 0));
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        val = sl_send(vm, enumerator, "current", 0);
        val = sl_to_s(vm, val);
        str = sl_string_concat(vm, str, joiner);
        str = sl_string_concat(vm, str, val);
    }
    return str;
}
Beispiel #13
0
static SLVAL
enumerable_reduce(sl_vm_t* vm, SLVAL self, size_t argc, SLVAL* argv)
{
    SLVAL enumerator = sl_send(vm, self, "enumerate", 0);
    SLVAL acc, val, f;
    if(argc == 1) {
        if(!sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
            sl_throw_message2(vm, vm->lib.ArgumentError, "Can't reduce empty array without initializer");
        } else {
            acc = sl_send(vm, enumerator, "current", 0);
        }
        f = argv[0];
    } else {
        acc = argv[0];
        f = argv[1];
    }
    while(sl_is_truthy(sl_send(vm, enumerator, "next", 0))) {
        val = sl_send(vm, enumerator, "current", 0);
        acc = sl_send(vm, f, "call", 2, acc, val);
    }
    return acc;
}
Beispiel #14
0
static SLVAL
sl_regexp_eq(sl_vm_t* vm, SLVAL self, SLVAL other)
{
    if(!sl_is_a(vm, other, vm->lib.Regexp)) {
        return vm->lib._false;
    }
    sl_regexp_t* re = get_regexp(vm, self);
    sl_regexp_t* oth = get_regexp(vm, other);
    if(!oth->re || !re->re) {
        return vm->lib._false;
    }
    if(!sl_is_truthy(sl_string_eq(vm, re->source, oth->source))) {
        return vm->lib._false;
    }
    if(re->options != oth->options) {
        return vm->lib._false;
    }
    return vm->lib._true;
}
Beispiel #15
0
static SLVAL
range_enumerator_next(sl_vm_t* vm, SLVAL self)
{
    sl_range_enumerator_t* range_enum = get_range_enumerator(vm, self);
    check_range_enumerator(vm, range_enum);
    if(range_enum->state == ES_DONE) {
        return vm->lib._false;
    }
    if(range_enum->state == ES_BEFORE) {
        range_enum->state = ES_ITERATING;
    } else {
        range_enum->current = sl_send_id(vm, range_enum->current, vm->id.succ, 0);
    }
    if(sl_is_truthy(sl_send_id(vm, range_enum->current, range_enum->method, 1, range_enum->right))) {
        return vm->lib._true;
    } else {
        range_enum->state = ES_DONE;
        return vm->lib._false;
    }
}