Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for disposable types #637

Open
Hsilgos opened this issue Dec 1, 2020 · 1 comment
Open

Add support for disposable types #637

Hsilgos opened this issue Dec 1, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@Hsilgos
Copy link
Contributor

Hsilgos commented Dec 1, 2020

Consider the next use case:
I have class Foo described in lime.
I create object of class Foo which acquire some non-shareable resources.
I want to create second instance of class Foo which needs to acquire the same resources.
In swift it gonna work like this because it uses reference counter and release instances asap:

var foo = Foo()
foo = nil // Instance is released, resources are unlocked
foo = Foo()

In Java and Flutter such code doesn't work because those languages use garbage collectors:

Foo foo = new Foo()
foo = null // ooops, object is not actually destroyed, I love Java
foo = new Foo()

Usually such classes in Java have some method to close and release resources. Each method should check then if object is 'closed' and throw runtime exception if one tries to use closed object.
Currently there are two problems with code which generated by Gluecodium:

  1. All developers should know about this mechanism when they implement functions
  2. There is no support for RuntimeExceptions so all such methods should throw custom exceptions which are described in lime files and all code which already use those functions fill fail to compile because Java requires to catch such exceptions (or declare custom methods to throw those exceptions), etc.

There are few possible solutions which may help in this case.

  • Add method which should 'destroy' object (with keyword destructor in lime?). Make it possible to call this method from C++/Java/Dart. Each attempt to call any method in the object after destruction should lead to runtime exception:
// Lime
class Foo {
  destructor dispose()
  fun bar()
}

... 
// Java
class Foo {
  private boolean mIsDestroyed = false;
  public void dispose() {
    call_native_method_dispose();
    mIsDestroyed = true;
  }

  public void bar()
  {
    if (mIsDestroyed)
   {
      throw new RuntimeException("...");
   }
  }
}

// C++
// ??? Somehow add possibility to notify Java that object os destroyed?
  • Add method which checks that object is destroyed and call this method each time when any other method/property is called. Probably some special syntax for such method is also necessary. As extension of idea this method can return string which is translated to runtime exception if not null.
// Lime
class Foo {
  validator object_status()
  fun bar()
}

... 
// Java
class Foo {
  public void bar()
  {
    String objectStatus = native_object_status();
    if (objectStatus) {
       throw new RuntimeException(objectStatus);
    }
  }
}

// C++
optional<std::string> Foo::object_status()
{
   if (!my_object_is_ok()) {
    return "My object is not OK"
  }
  return nullopt;
}
  • Extend Result class to return runtime error. This approach involves a lot of work to adopt existing code and spoils C++ interfaces.
@DanielKamkha DanielKamkha added the enhancement New feature or request label Dec 2, 2020
@DanielKamkha DanielKamkha added this to the 8.7.0 milestone Dec 2, 2020
@DanielKamkha
Copy link
Contributor

After additional discussions, we settled on option number 2 ("validator"). The exact LIME IDL language keyword is undecided yet, but the general structure of the functionality is the same as described in the ticket: the "validator" function is called by platform code on each non-static function call (which also includes property accessors). Constructors are excluded due to being static functions.

This functionality applies to classes only. It does not make sense for interfaces. It is also unneeded for structs, as platform representation of a struct is a "copy" of a C++ object, so their lifetimes are not related.

@DanielKamkha DanielKamkha self-assigned this Dec 8, 2020
DanielKamkha added a commit that referenced this issue Dec 8, 2020
Updated LIME IDL grammar and model builder to add support for `validator` keyword in classes.

See: #637
Signed-off-by: Daniel Kamkha <[email protected]>
@DanielKamkha DanielKamkha removed their assignment Dec 9, 2020
@DanielKamkha DanielKamkha changed the title Add support for disposable types. Add support for disposable types Jan 4, 2021
@DanielKamkha DanielKamkha removed this from the 8.7.0 milestone Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants