comparison runtime/object.c @ 34:df038cef648b

More work on supporting user defined types in the C backend
author Mike Pavone <pavone@retrodev.com>
date Wed, 30 Sep 2009 01:25:03 -0400
parents 914ad38f9b59
children 495dddadd058
comparison
equal deleted inserted replaced
33:3b47a8538df2 34:df038cef648b
340 } 340 }
341 } 341 }
342 bp->method_lookup[methodid-bp->first_methodid] = impl; 342 bp->method_lookup[methodid-bp->first_methodid] = impl;
343 } 343 }
344 344
345 void add_getter(blueprint * bp, uint32_t getfieldid, rhope_func impl)
346 {
347 rhope_func * temp;
348 if(getfieldid < 1) {
349 fputs("Attempt to add a method with an ID < 1\n", stderr);
350 exit(-1);
351 }
352 if (!bp->getter_lookup)
353 {
354 bp->getter_lookup = malloc(sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
355 if(!bp->getter_lookup) {
356 fprintf(stderr, "Couldn't allocate %d bytes for getter lookup table\n", sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
357 exit(-1);
358 }
359 if(BELOW_INITIAL_METHOD > getfieldid) {
360 bp->first_getfieldid = 1;
361 bp->last_getfieldid = 1+INITIAL_METHOD_LOOKUP;
362 } else {
363 bp->first_getfieldid = getfieldid - BELOW_INITIAL_METHOD;
364 bp->last_getfieldid = bp->first_getfieldid + INITIAL_METHOD_LOOKUP;
365 }
366 memset(bp->getter_lookup, '\0', sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
367 } else {
368 if (getfieldid < bp->first_getfieldid) {
369 temp = bp->getter_lookup;
370 //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer
371 bp->getter_lookup = malloc(sizeof(rhope_func) * (bp->last_getfieldid-getfieldid));
372 if(!bp->getter_lookup) {
373 fprintf(stderr, "Couldn't allocate %d bytes for getter lookup table\n", sizeof(rhope_func) * (bp->last_getfieldid-getfieldid));
374 exit(-1);
375 }
376 memset(bp->getter_lookup, '\0', (bp->first_getfieldid-getfieldid) * sizeof(rhope_func));
377 memcpy(bp->getter_lookup + bp->first_getfieldid-getfieldid, temp, (bp->last_getfieldid-bp->first_getfieldid)*sizeof(rhope_func));
378 free(temp);
379 bp->first_getfieldid = getfieldid;
380 } else if(getfieldid >= bp->last_getfieldid) {
381 //Note: if this gets changed to generating an exception on failure, we need to restore the original buffer
382 bp->getter_lookup = realloc(bp->getter_lookup, (getfieldid+1-bp->first_getfieldid) * sizeof(rhope_func));
383 if(!bp->getter_lookup) {
384 fprintf(stderr, "Couldn't resize getter lookup table to %d bytes\n", (getfieldid+1-bp->first_getfieldid) * sizeof(rhope_func));
385 exit(-1);
386 }
387 memset(bp->getter_lookup+bp->last_getfieldid, '\0', (getfieldid+1)-bp->last_getfieldid);
388 bp->last_getfieldid = getfieldid+1;
389 }
390 }
391 bp->getter_lookup[getfieldid-bp->first_getfieldid] = impl;
392 }
393
394 void add_setter(blueprint * bp, uint32_t setfieldid, rhope_func impl)
395 {
396 rhope_func * temp;
397 if(setfieldid < 1) {
398 fputs("Attempt to add a method with an ID < 1\n", stderr);
399 exit(-1);
400 }
401 if (!bp->setter_lookup)
402 {
403 bp->setter_lookup = malloc(sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
404 if(!bp->setter_lookup) {
405 fprintf(stderr, "Couldn't allocate %d bytes for setter lookup table\n", sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
406 exit(-1);
407 }
408 if(BELOW_INITIAL_METHOD > setfieldid) {
409 bp->first_setfieldid = 1;
410 bp->last_setfieldid = 1+INITIAL_METHOD_LOOKUP;
411 } else {
412 bp->first_setfieldid = setfieldid - BELOW_INITIAL_METHOD;
413 bp->last_setfieldid = bp->first_setfieldid + INITIAL_METHOD_LOOKUP;
414 }
415 memset(bp->setter_lookup, '\0', sizeof(rhope_func) * INITIAL_METHOD_LOOKUP);
416 } else {
417 if (setfieldid < bp->first_setfieldid) {
418 temp = bp->setter_lookup;
419 //Note: if this sets changed to generating an exception on failure, we need to restore the original buffer
420 bp->setter_lookup = malloc(sizeof(rhope_func) * (bp->last_setfieldid-setfieldid));
421 if(!bp->setter_lookup) {
422 fprintf(stderr, "Couldn't allocate %d bytes for setter lookup table\n", sizeof(rhope_func) * (bp->last_setfieldid-setfieldid));
423 exit(-1);
424 }
425 memset(bp->setter_lookup, '\0', (bp->first_setfieldid-setfieldid) * sizeof(rhope_func));
426 memcpy(bp->setter_lookup + bp->first_setfieldid-setfieldid, temp, (bp->last_setfieldid-bp->first_setfieldid)*sizeof(rhope_func));
427 free(temp);
428 bp->first_setfieldid = setfieldid;
429 } else if(setfieldid >= bp->last_setfieldid) {
430 //Note: if this sets changed to generating an exception on failure, we need to restore the original buffer
431 bp->setter_lookup = realloc(bp->setter_lookup, (setfieldid+1-bp->first_setfieldid) * sizeof(rhope_func));
432 if(!bp->setter_lookup) {
433 fprintf(stderr, "Couldn't resize setter lookup table to %d bytes\n", (setfieldid+1-bp->first_setfieldid) * sizeof(rhope_func));
434 exit(-1);
435 }
436 memset(bp->setter_lookup+bp->last_setfieldid, '\0', (setfieldid+1)-bp->last_setfieldid);
437 bp->last_setfieldid = setfieldid+1;
438 }
439 }
440 bp->setter_lookup[setfieldid-bp->first_setfieldid] = impl;
441 }
442
345 blueprint * get_blueprint_byid(uint32_t type) 443 blueprint * get_blueprint_byid(uint32_t type)
346 { 444 {
347 if(type >= max_registered_type) 445 if(type >= max_registered_type)
348 return NULL; 446 return NULL;
349 return registered_types[type]; 447 return registered_types[type];