fasterthanlime / jooc-legacy

:crocodile: The historical, initial implementation of an ooc compiler in Java
http://ooc-lang.org/
MIT License
115 stars 4 forks source link

wrong use of memcpy when returning generic members in a generic function #27

Closed fredreichbier closed 15 years ago

fredreichbier commented 15 years ago

This code:

Box: class  {
    value: T

    init: func (v: T) {
        value = v
    }
}

test: func  (T: Class) -> T {
    box := Box new("Hey there!")
    box value as T
}

main: func {
    test(String) println()
}

should print "Hey there!", but instead it prints a strange character.

The C code for test looks like this:

void test(lang__Pointer __returnArg, lang__Class *T)
{
    lang__String tparam = "Hey there!";
    Box *box = Box_new(String_class(), (uint8_t*) &tparam);
    if (__returnArg)
    {
        memcpy(__returnArg, ((lang__Pointer) (&(box->value))), T->size);
    }
    return;
}

where this line:

memcpy(__returnArg, ((lang__Pointer) (&(box->value))), T->size);

should be:

memcpy(__returnArg, ((lang__Pointer) ((box->value))), T->size);

With this change applied, it works \o/

nddrylliog commented 15 years ago

Fi-fi-fi-fixed. Thanks for reporting, fred. http://github.com/nddrylliog/ooc/commit/c1ff295652dd592417f23b8041817894ba0384dd