magic-lang / rock

ooc compiler written in ooc
http://ooc-lang.org/
MIT License
14 stars 4 forks source link

Remove __onheap__, automatically generate free methods that clean up generics #68

Open alexnask opened 8 years ago

alexnask commented 8 years ago

This remains backwards compatible as free is not generated if it already exists. To start using this feature, simply declare __destroy__ instead of free methods.
The only required change in the SDK (ooc-kean) is removing the (virtual) __destroy__ call from Object free as well as any __onheap__ keyword usage.

Here is a trivial example:

MyCell: class <T> {
    val: T

    init: func (=val)

    __destroy__: func {
        "Destroy" println()
    }

}

MySubCell: class <T> extends MyCell <T> {
    init: super func

    __destroy__: func {
        "Subdestroy" println()
    }

}

cell := MySubCell new(42)
cell val toString() println()
cell free()

Output:

42
Subdestroy
Destroy

(Additional 'Subdestroy' if __destroy__() is present in Object free)

EDIT: Closes #48

thomasfanell commented 8 years ago

@shamanas did you test this with ooc-kean? rock segfaults when I run test.sh:

(SIGSEGV) segmentation fault
[fancy backtrace]
0     Backtrace class()                                                   in lang/Backtrace               (at ../rock/sdk/lang/Backtrace.ooc:293)
1     Exception getCurrentBacktrace()                                     in lang/Exception               (at ../rock/sdk/lang/Exception.ooc:223)
2     _signalHandler()                                                    in lang/Exception               (at ../rock/sdk/lang/Exception.ooc:283)
3     killpg()                                                            in                              (at (null):0)
4     Type isGeneric()                                                    in rock/middle/Type             (at ../rock/source/rock/middle/Type.ooc:80)
5     __rock_middle_ClassDecl_rock_middle_ClassDecl_closure26()           in rock/middle/ClassDecl        (at ../rock/source/rock/middle/ClassDecl.ooc:115)
6     HashMap each_withKeys_impl()                                        in structs/HashMap              (at ../rock/sdk/structs/HashMap.ooc:493)
7     ClassDecl resolve_impl()                                            in rock/middle/ClassDecl        (at ../rock/source/rock/middle/ClassDecl.ooc:124)
8     TypeDecl resolve_impl()                                             in rock/middle/TypeDecl         (at ../rock/source/rock/middle/TypeDecl.ooc:944)
9     ClassDecl resolve_impl()                                            in rock/middle/ClassDecl        (at ../rock/source/rock/middle/ClassDecl.ooc:136)
10    Module resolve_impl()                                               in rock/middle/Module           (at ../rock/source/rock/middle/Module.ooc:547)
11    Resolver process_impl()                                             in rock/middle/tinker/Resolver  (at ../rock/source/rock/middle/tinker/Resolver.ooc:59)
12    Tinkerer process_impl()                                             in rock/middle/tinker/Tinkerer  (at ../rock/source/rock/middle/tinker/Tinkerer.ooc:90)
13    __rock_frontend_CommandLine_rock_frontend_CommandLine_closure329()  in rock/frontend/CommandLine    (at ../rock/source/rock/frontend/CommandLine.ooc:692)
14    Time runTime()                                                      in os/Time                      (at ../rock/sdk/os/Time.ooc:134)
15    CommandLine postParsing_impl()                                      in rock/frontend/CommandLine    (at ../rock/source/rock/frontend/CommandLine.ooc:696)
16    CommandLine parse_impl()                                            in rock/frontend/CommandLine    (at ../rock/source/rock/frontend/CommandLine.ooc:666)
17    CommandLine init()                                                  in rock/frontend/CommandLine    (at ../rock/source/rock/frontend/CommandLine.ooc:563)
18    CommandLine new()                                                   in rock/frontend/CommandLine    (at ../rock/source/rock/frontend/CommandLine.ooc:30)
19    main()                                                              in                              (at ../rock/source/rock/rock.ooc:2)
20    libc_start_main()                                                   in                              (at /build/eglibc-oGUzwX/eglibc-2.19/csu/libc-start.c:321)
21    _start()      
alexnask commented 8 years ago

@thomasfanell

Sorry, was out of town.
It's true I haven't been so diligent in checking against the ooc-kean tests when it comes to testing language features.

I will be taking a look at what is causing the crash (from the backtrace, it seems to be coming from Type isGeneric() which is surprising).

EDIT: Probably a null variable decl type
EDIT2: Yeah, the code seems to be pretty fail, the if that checks for a generic type should be an else if, we check for null types and then immediately go on to call a method on them.

alexnask commented 8 years ago

Tests no longer segfault, although they do fail for some seemingly unrelated reasons

C:\dev\ooc_libs\ooc-kean\source\math/FloatVectorList.ooc:18:27 error No such function __getcount__() for `FloatVectorList`

    mean ::= this sum / this count
                             ~~~~~

Looking into it.