Mercurial > repos > rhope
diff runtime/object.c @ 7:d61550e2c001
Added current work on new runtime
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 13 May 2009 00:47:40 -0400 |
parents | |
children | 31f8182f3433 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/runtime/object.c Wed May 13 00:47:40 2009 -0400 @@ -0,0 +1,343 @@ +#include "object.h" +#include "builtin.h" +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +blueprint ** registered_types = NULL; +uint32_t max_registered_type = 0; +uint32_t type_storage = 0; + +returntype call_method(uint32_t methodid, calldata * params) +{ + int i; + blueprint * bp = get_blueprint(params->params[0]); + if(methodid >= bp->first_methodid && methodid < bp->last_methodid && bp->method_lookup[methodid - bp->first_methodid]) + { + params->tail_func = bp->method_lookup[methodid - bp->first_methodid]; + return TAIL_RETURN; + } else { + if(METHOD_MISSING >= bp->first_methodid && METHOD_MISSING < bp->last_methodid && bp->method_lookup[METHOD_MISSING - bp->first_methodid]) + { + params->tail_func = bp->method_lookup[METHOD_MISSING - bp->first_methodid]; + return TAIL_RETURN; + } else { + //TODO: Add useful info to exception + for(i = 0; i < params->num_params; ++i) + release_ref(params->params[i]); + params->params[0] = new_object(TYPE_METHODMISSINGEXCEPTION); + return EXCEPTION_RETURN; + } + } +} + +returntype set_field(uint32_t setfieldid, calldata * params) +{ + int i; + blueprint * bp = get_blueprint(params->params[0]); + if(setfieldid >= bp->first_setfieldid && setfieldid < bp->last_setfieldid && bp->setter_lookup[setfieldid - bp->first_setfieldid]) + { + params->tail_func = bp->setter_lookup[setfieldid - bp->first_setfieldid]; + return TAIL_RETURN; + } else { + if(METHOD_SETFIELDMISSING >= bp->first_setfieldid && METHOD_SETFIELDMISSING < bp->last_setfieldid && bp->method_lookup[METHOD_SETFIELDMISSING - bp->first_methodid]) + { + params->tail_func = bp->method_lookup[METHOD_SETFIELDMISSING - bp->first_methodid]; + params->original_methodid = setfieldid; + return TAIL_RETURN; + } else { + //TODO: Add useful info to exception + for(i = 0; i < params->num_params; ++i) + release_ref(params->params[i]); + params->params[0] = new_object(TYPE_FIELDMISSINGEXCEPTION); + return EXCEPTION_RETURN; + } + } +} + + +returntype get_field(uint32_t getfieldid, calldata * params) +{ + int i; + blueprint * bp = get_blueprint(params->params[0]); + if(getfieldid >= bp->first_getfieldid && getfieldid < bp->last_getfieldid && bp->getter_lookup[getfieldid - bp->first_getfieldid]) + { + params->tail_func = bp->getter_lookup[getfieldid - bp->first_getfieldid]; + return TAIL_RETURN; + } else { + if(METHOD_GETFIELDMISSING >= bp->first_getfieldid && METHOD_GETFIELDMISSING < bp->last_getfieldid && bp->method_lookup[METHOD_GETFIELDMISSING - bp->first_methodid]) + { + params->tail_func = bp->method_lookup[METHOD_GETFIELDMISSING - bp->first_methodid]; + params->original_methodid = getfieldid; + return TAIL_RETURN; + } else { + //TODO: Add useful info to exception + for(i = 0; i < params->num_params; ++i) + release_ref(params->params[i]); + params->params[0] = new_object(TYPE_FIELDMISSINGEXCEPTION); + return EXCEPTION_RETURN; + } + } +} + +returntype convert_to(uint32_t convertto, calldata * params) +{ + int i; + blueprint * bp = get_blueprint(params->params[0]); + if(convertto >= bp->first_convertto && convertto < bp->last_convertto && bp->convert_to[convertto]) + { + params->tail_func = bp->convert_to[convertto - bp->first_convertto]; + return TAIL_RETURN; + } else { + return NO_CONVERSION; + } +} + +returntype convert_from(uint32_t convertfrom, calldata * params) +{ + int i; + blueprint * bp = registered_types[convertfrom]; + if(convertfrom >= bp->first_convertfrom && convertfrom < bp->last_convertfrom && bp->convert_from[convertfrom]) + { + params->tail_func = bp->convert_from[convertfrom - bp->first_convertfrom]; + return TAIL_RETURN; + } else { + return NO_CONVERSION; + } +} + +returntype coerce_value(uint32_t type, calldata * params) +{ + int i; + blueprint * bp = get_blueprint(params->params[0]); + if(bp == registered_types[type]) + return NORMAL_RETURN; + if(convert_to(type, params) == TAIL_RETURN) + return TAIL_RETURN; + if(convert_from(type, params) == TAIL_RETURN) + return TAIL_RETURN; + //TODO: Add useful info to exception + for(i = 0; i < params->num_params; ++i) + release_ref(params->params[i]); + params->params[0] = new_object(TYPE_WRONGTYPEEXCEPTION); + return EXCEPTION_RETURN; +} + +object * alloc_object(blueprint * bp) +{ + //TODO: Replace with something more performant + return malloc(bp->boxed_size); +} + +void * alloc_variable(uint32_t size) +{ + return malloc(size); +} + +void dealloc_object(blueprint * bp, object * obj) +{ + //TODO: Replace with something more performant + free(obj); +} + +object * new_object(uint32_t type) +{ + blueprint * bp; + object * ret; + if(type >= max_registered_type || !registered_types[type]) + return NULL; + bp = registered_types[type]; + ret = alloc_object(bp); + if(ret) + { + ret->bprint = bp; + rh_atomic_set(ret, refcount, 1); + memset(((char *)ret) + sizeof(object), '\0', bp->size); + bp->init(ret); + } + return ret; +} + +multisize * new_multisize(uint32_t type, uint32_t size) +{ + blueprint *bp; + multisize * ret; + if(type >= max_registered_type || !registered_types[type]) + return NULL; + ret = alloc_variable(sizeof(multisize) + type); + if(ret) + { + bp = registered_types[type]; + ret->base.bprint = bp; + ret->size = size; + rh_atomic_set(&(ret->base), refcount, 1); + memset(((char *)ret) + sizeof(multisize), '\0', size); + bp->init((object *)ret); + } + return ret; +} + +object * copy_object(object * tocopy) +{ + object * copy; + multisize * mcopy, *mtocopy; + blueprint * bp; + if(rh_atomic_get(tocopy, refcount) == 1) + return tocopy; + bp = get_blueprint(tocopy); + if(bp->size < 0) { + mtocopy = (multisize *)tocopy; + mcopy = alloc_variable(sizeof(multisize) + mtocopy->size); + mcopy->size = mtocopy->size; + memcpy(((char *)mcopy)+sizeof(multisize), ((char *)mtocopy)+sizeof(multisize), mtocopy->size); + copy = (object *)mcopy; + } else { + copy = alloc_object(bp); + memcpy(((char *)copy) + sizeof(object), ((char *)tocopy)+sizeof(object), bp->size); + } + copy->bprint = bp; + rh_atomic_set(copy, refcount, 1); + bp->copy(copy); + release_ref(tocopy); + return copy; +} + +void free_object(object * obj) +{ + blueprint * bp = get_blueprint(obj); + if(bp->cleanup) + bp->cleanup(obj); + dealloc_object(bp, obj); +} + +void release_ref(object * obj) +{ + if(rh_atomic_sub_testzero(obj, refcount, 1)) + free_object(obj); +} + +void check_type_storage(type) +{ + uint32_t type_storage_temp; + blueprint ** temp; + if(type >= type_storage) + if(type_storage) + { + type_storage_temp = (type + (type_storage >> 1)); + temp = realloc(registered_types, type_storage_temp * sizeof(blueprint *)); + if(temp) + { + registered_types = temp; + memset(registered_types + type_storage, '\0', (type_storage_temp - type_storage) * sizeof(blueprint *)); + type_storage = type_storage_temp; + } + else + { + free(registered_types); + fprintf(stderr, "Couldn't allocate %d bytes for type storage array\n", type_storage_temp * sizeof(blueprint *)); + exit(-1); + } + } else { + + if(type < INITIAL_TYPE_STORAGE) + type_storage =INITIAL_TYPE_STORAGE; + else + type_storage = type + 8; + registered_types = malloc(type_storage * sizeof(blueprint *)); + if(registered_types) + memset(registered_types, '\0', type_storage * sizeof(blueprint *)); + else + { + fprintf(stderr, "Couldn't allocate %d bytes for type storage array\n", type_storage * sizeof(blueprint *)); + exit(-1); + } + + } +} + +void default_action(object * obj) +{ +} + +blueprint * new_blueprint(uint32_t type, uint32_t size, special_func init, special_func copy, special_func cleanup) +{ + blueprint * bp = malloc(sizeof(blueprint)); + if(bp) + { + bp->size = size; + bp->boxed_size = size >= 0 ? size + sizeof(object) : size; + bp->method_lookup = bp->getter_lookup = bp->setter_lookup = bp->convert_to = bp->convert_from = NULL; + bp->init = init ? init : default_action; + bp->copy = copy ? copy : default_action; + bp->cleanup = cleanup ? cleanup : default_action; + bp->first_methodid = bp->last_methodid = bp->first_getfieldid = bp->last_getfieldid = bp->first_setfieldid = bp->last_setfieldid = bp->first_convertto = bp->last_convertto = bp->first_convertfrom = bp->last_convertfrom = 0; + //TODO: Handle names + bp->name = NULL; + } + return bp; +} + +blueprint * register_type_byid(uint32_t type, uint32_t size, special_func init, special_func copy, special_func cleanup) +{ + check_type_storage(type); + if(registered_types[type]) + return registered_types[type]; + registered_types[type] = new_blueprint(type, size, init, copy, cleanup); + if(!registered_types[type]) + { + fputs("Couldn't allocate new object blueprint\n", stderr); + exit(-1); + } + if(type >= max_registered_type) + max_registered_type = type + 1; + return registered_types[type]; +} + +void add_method(blueprint * bp, uint32_t methodid, rhope_func impl) +{ + rhope_func * temp; + if(methodid < 1) { + fputs("Attempt to add a method with an ID < 1\n", stderr); + exit(-1); + } + if (!bp->method_lookup) + { + bp->method_lookup = malloc(sizeof(rhope_func) * INITIAL_METHOD_LOOKUP); + if(!bp->method_lookup) { + fprintf(stderr, "Couldn't allocate %d bytes for method lookup table\n", sizeof(rhope_func) * INITIAL_METHOD_LOOKUP); + exit(-1); + } + if(methodid - BELOW_INITIAL_METHOD < 1) { + bp->first_methodid = 1; + bp->last_methodid = 1+INITIAL_METHOD_LOOKUP; + } else { + bp->first_methodid = methodid - BELOW_INITIAL_METHOD; + bp->last_methodid = bp->first_methodid + INITIAL_METHOD_LOOKUP; + } + memset(bp->method_lookup, '\0', sizeof(rhope_func) * INITIAL_METHOD_LOOKUP); + } else { + if (methodid < bp->first_methodid) { + temp = bp->method_lookup; + //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer + bp->method_lookup = malloc(sizeof(rhope_func) * (bp->last_methodid-methodid)); + if(!bp->method_lookup) { + fprintf(stderr, "Couldn't allocate %d bytes for method lookup table\n", sizeof(rhope_func) * (bp->last_methodid-methodid)); + exit(-1); + } + memset(bp->method_lookup, '\0', (bp->first_methodid-methodid) * sizeof(rhope_func)); + memcpy(bp->method_lookup + bp->first_methodid-methodid, temp, (bp->last_methodid-bp->first_methodid)*sizeof(rhope_func)); + free(temp); + bp->first_methodid = methodid; + } else if(methodid >= bp->last_methodid) { + //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer + bp->method_lookup = realloc(bp->method_lookup, (methodid+1-bp->first_methodid) * sizeof(rhope_func)); + if(!bp->method_lookup) { + fprintf(stderr, "Couldn't resize method lookup table to %d bytes\n", (methodid+1-bp->first_methodid) * sizeof(rhope_func)); + exit(-1); + } + memset(bp->method_lookup+bp->last_methodid, '\0', (methodid+1)-bp->last_methodid); + bp->last_methodid = methodid; + } + } + bp->method_lookup[methodid-bp->first_methodid] = impl; +}