static SLVAL method_eq(sl_vm_t* vm, SLVAL method, SLVAL other) { if(sl_get_ptr(sl_class_of(vm, other)) != sl_get_ptr(vm->lib.Method)) { return vm->lib._false; } sl_method_t* methp = (sl_method_t*)sl_get_ptr(method); sl_method_t* othp = (sl_method_t*)sl_get_ptr(other); return sl_make_bool(vm, memcmp(methp, othp, sizeof(sl_method_t)) == 0); }
static SLVAL vm_helper_define_singleton_method(sl_vm_exec_ctx_t* ctx, SLVAL on, SLID name, sl_vm_section_t* section) { SLVAL klass = on, method; if(!sl_is_a(ctx->vm, klass, ctx->vm->lib.Class)) { klass = sl_class_of(ctx->vm, klass); } method = sl_make_method(ctx->vm, klass, name, section, ctx); sl_define_singleton_method3(ctx->vm, on, name, method); return method; }
static SLVAL bound_method_eq(sl_vm_t* vm, SLVAL method, SLVAL other) { if(sl_get_ptr(sl_class_of(vm, other)) != sl_get_ptr(vm->lib.BoundMethod)) { return vm->lib._false; } sl_method_t* methp = (sl_method_t*)sl_get_ptr(method); sl_method_t* othp = (sl_method_t*)sl_get_ptr(other); if(memcmp(methp, othp, sizeof(sl_method_t)) != 0) { return vm->lib._false; } if(memcmp(methp->extra, othp->extra, sizeof(*methp->extra)) != 0) { return vm->lib._false; } return vm->lib._true; }
static SLVAL vm_helper_define_class(sl_vm_exec_ctx_t* ctx, SLID name, SLVAL extends, sl_vm_section_t* section) { SLVAL klass, in; sl_vm_exec_ctx_t* subctx; if(sl_class_has_const2(ctx->vm, ctx->self, name)) { /* @TODO: verify same superclass */ klass = sl_class_get_const2(ctx->vm, ctx->self, name); } else { in = ctx->self; if(!sl_is_a(ctx->vm, in, ctx->vm->lib.Class)) { in = sl_class_of(ctx->vm, in); } klass = sl_define_class3(ctx->vm, name, extends, in); } subctx = sl_alloc(ctx->vm->arena, sizeof(sl_vm_exec_ctx_t)); subctx->vm = ctx->vm; subctx->section = section; subctx->registers = sl_alloc(ctx->vm->arena, sizeof(SLVAL) * section->max_registers); subctx->self = klass; subctx->parent = ctx; sl_vm_exec(subctx, 0); return klass; }
static void json_dump(json_dump_t* state, SLVAL object) { sl_string_t* str; size_t i, len; SLVAL* keys; switch(sl_get_primitive_type(object)) { case SL_T_NIL: JSON_DUMP_NEED_BYTES(4); memcpy(state->buffer + state->buffer_len, "null", 4); state->buffer_len += 4; break; case SL_T_TRUE: JSON_DUMP_NEED_BYTES(4); memcpy(state->buffer + state->buffer_len, "true", 4); state->buffer_len += 4; break; case SL_T_FALSE: JSON_DUMP_NEED_BYTES(5); memcpy(state->buffer + state->buffer_len, "false", 5); state->buffer_len += 5; break; case SL_T_INT: str = (sl_string_t*)sl_get_ptr(sl_int_to_s(state->vm, object)); JSON_DUMP_NEED_BYTES(str->buff_len); memcpy(state->buffer + state->buffer_len, str->buff, str->buff_len); state->buffer_len += str->buff_len; break; case SL_T_FLOAT: str = (sl_string_t*)sl_get_ptr(sl_float_to_s(state->vm, object)); JSON_DUMP_NEED_BYTES(str->buff_len); memcpy(state->buffer + state->buffer_len, str->buff, str->buff_len); state->buffer_len += str->buff_len; break; case SL_T_BIGNUM: str = (sl_string_t*)sl_get_ptr(sl_bignum_to_s(state->vm, object)); JSON_DUMP_NEED_BYTES(str->buff_len); memcpy(state->buffer + state->buffer_len, str->buff, str->buff_len); state->buffer_len += str->buff_len; break; case SL_T_STRING: str = (sl_string_t*)sl_get_ptr(sl_string_inspect(state->vm, object)); JSON_DUMP_NEED_BYTES(str->buff_len); memcpy(state->buffer + state->buffer_len, str->buff, str->buff_len); state->buffer_len += str->buff_len; break; case SL_T_ARRAY: JSON_CHECK_RECURSION(object); JSON_DUMP_NEED_BYTES(1); state->buffer[state->buffer_len++] = '['; len = sl_get_int(sl_array_length(state->vm, object)); for(i = 0; i < len; i++) { if(i) { JSON_DUMP_NEED_BYTES(1); state->buffer[state->buffer_len++] = ','; } json_dump(state, sl_array_get(state->vm, object, i)); } JSON_DUMP_NEED_BYTES(1); state->buffer[state->buffer_len++] = ']'; JSON_END_CHECK_RECURSION(); break; case SL_T_DICT: JSON_CHECK_RECURSION(object); JSON_DUMP_NEED_BYTES(1); state->buffer[state->buffer_len++] = '{'; keys = sl_dict_keys(state->vm, object, &len); for(i = 0; i < len; i++) { if(i) { JSON_DUMP_NEED_BYTES(1); state->buffer[state->buffer_len++] = ','; } json_dump(state, sl_to_s(state->vm, keys[i])); JSON_DUMP_NEED_BYTES(1); state->buffer[state->buffer_len++] = ':'; json_dump(state, sl_dict_get(state->vm, object, keys[i])); } JSON_DUMP_NEED_BYTES(1); state->buffer[state->buffer_len++] = '}'; JSON_END_CHECK_RECURSION(); break; default: if(!sl_responds_to(state->vm, object, "to_json")) { SLVAL DumpError = state->vm->store[cJSON_DumpError]; sl_error(state->vm, DumpError, "Can't convert type %V to JSON. You can implement #to_json to fix this.", sl_class_of(state->vm, object)); } str = (sl_string_t*)sl_get_ptr(sl_to_s(state->vm, sl_send(state->vm, object, "to_json", 0, NULL))); JSON_DUMP_NEED_BYTES(str->buff_len); memcpy(state->buffer + state->buffer_len, str->buff, str->buff_len); state->buffer_len += str->buff_len; break; } }