Ejemplo n.º 1
0
Archivo: libs.c Proyecto: clarkok/cript
static Value
_lib_sizeof(VMState *vm, Value value)
{
    Hash *args = value_to_ptr(value);
    Value query_val = hash_find(args, 1);

    if (value_is_string(query_val)) {
        CString *string = value_to_string(query_val);
        return value_from_int(string->length);
    }
    else if (value_is_ptr(query_val)) {
        Hash *hash = value_to_ptr(query_val);
        while (hash->type == HT_REDIRECT) hash = (Hash*)hash->size;
        assert(hash->type != HT_GC_LEFT);

        switch (hash->type) {
            case HT_OBJECT:
            case HT_ARRAY:
                return value_from_int(hash_size(hash));
            case HT_LIGHTFUNC:
            case HT_CLOSURE:
            case HT_USERDATA:
                return value_from_int(-1);
            default: assert(0);
        }
    }

    return value_from_int(-1);
}
Ejemplo n.º 2
0
Archivo: libs.c Proyecto: clarkok/cript
static Value
_lib_parse_number(VMState *vm, Value value)
{
    Hash *args = value_to_ptr(value);
    CString *string = value_to_string(hash_find(args, 1));
    int ret_val;
    sscanf(string->content, "%d", &ret_val);
    return value_from_int(ret_val);
}
Ejemplo n.º 3
0
Archivo: libs.c Proyecto: clarkok/cript
static Value
_lib_char_code_at(VMState *vm, Value value)
{
    Hash *args = value_to_ptr(value);
    CString *string = value_to_string(hash_find(args, 1));
    int index = value_to_int(hash_find(args, 2));

    if (index < 0 || index >= string->length) {
        error_f("Out of range when get char_at(%.*s, %d)", string->length, string->content, index);
        return value_undefined();
    }

    return value_from_int(string->content[index]);
}
Ejemplo n.º 4
0
    X(fixnum, "Small ints can be stored inline") \
    X(bool, "Booleans can be stored inline") \
    X(ptr, "Boxed values should be heap-allocated and a pointer stored") \
    X(value_type, "Value type should be not depend on whether or not the value is boxed") \
    X(gray_bit, "Boxed values should maintain a gray bit") \
    X(hash, "Value hashes should be well-distributed") \
    X(hash_tuple, "Hashes from equal tuples should be equal") \
    X(equal, "Only equal values should be equal")

TEST(double)
    value_t x = value_from_double(42.101010);
    PT_ASSERT_EQ(value_to_double(x), 42.101010);
END(double)

TEST(fixnum)
    value_t x = value_from_int(322);
    PT_ASSERT_EQ(value_to_int(x), 322);
END(fixnum)

TEST(bool)
    value_t x = value_true();
    PT_ASSERT(value_to_bool(x));
    x = value_false();
    PT_ASSERT(!value_to_bool(x));
END(bool)

TEST(ptr)
    struct arena_handle *a = arena_new((yu_allocator *)&mctx);
    struct boxed_value *v = arena_alloc_val(a);
    value_t x = value_from_ptr(&v);
    PT_ASSERT_EQ(value_get_ptr(x), v);
Ejemplo n.º 5
0
Archivo: libs.c Proyecto: clarkok/cript
static Value
_lib_random(VMState *vm, Value value)
{ return value_from_int(random()); }