Links

Categories

Tags


« | Main | »

Disposable objects: semi-destructors added

By Jewe | March 19, 2016

JewelScript 1.4 supports disposable objects.

Destructors in C++ are not only a necessity to avoid memory leaks. There are other useful applications. One useful application is the possibility to create stack-objects: Objects that you create at the start of a function to safely modify some other object’s state. When the stack-object goes out of scope, no matter how the function is left, it can restore the state of the modified object.

For a long time I have thought about ways how I could support something like that in JewelScript. However, in an interpreted language without JIT compiler, you cannot support automatic destructor calls for every instance of every class. This just would slow down the language to a crawl.

C# introduced the using-statement for this purpose. When allocating an object inside a using-statement, the runtime will automatically call a dispose-method, when the variable holding the object goes out of scope. I thought about adopting this in JewelScript. However, I feel that the code clarity suffers, if there are many nested using-statements in a function. Plus, your function cannot return a disposable object that way.

So I came up with my own solution: The disposable modifier.

The disposable modifier is only valid following operator new. Basically it tells the compiler that you want to mark the new instance as a disposable object. When the reference counter of a disposable object reaches zero, no matter where and when this happens, the runtime will automatically call the object’s dispose method.

This of course requires that the class has such a method. The compiler will check this and only allow you to use the disposable modifier for classes where you have declared a dispose method.

Of course programmers can still slow down the runtime to a crawl, by just allocating everything using disposable. But then it’s their own fault. Making the execution of a destructor an option gives script-developers the power to execute code right before an object gets destroyed. And as we all know from reading Marvel Comics, with great power comes great responsibility. ;)

Here’s a code example:

/*
 * dispose.jc
 *
 * Test disposable objects in JewelScript 1.4
 */

import stdlib;

class Logger
{
    method Logger()
    {
        Messages = {};
    }
    
    method Add(string msg)
    {
        Messages += msg;
    }

    // A disposable object must have a dispose() method
    method dispose()
    {
        // print all logged messages to screen
        for (int i; i < Messages.length; i++)
            stdlib::println(Messages[i]);
    }

    string[] Messages;
}

function Main()
{
    // a disposable object is created using the 'disposable' modifier in a new-expression
    Logger log = new disposable Logger();
    for (int i; i < 10; i++)
    {
        log.Add("This is log message #" + i);
    }
    // when the object's reference count reaches zero, no matter when and where this happens,
    // JewelScript will call dispose() before destroying the object.
    // In this example, all logged messages are printed when Main() exits.
}

Topics: code examples, docs, news | Comments Off on Disposable objects: semi-destructors added

Comments are closed.