Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart dynamic class loading

I want to build an application server with Dart. The httpServer in the dart:io library is certainly a good starting point for that. But I struggle with the task to "deploy" an application without restarting the server process.

To be more precise: I want to have something like a servlet container in Java, like Tomcat, into which I can easily deploy or redeploy an application without restarting the container. I thought I could utilize the mirror system, which allows me in principle to load a library and its contained classes from the filesystem. But unfortunately it seems that I cannot re-load the library. When I add for example a new class to the library, or change the coding of an existing class, a new reflection of the library without restarting the dart process, does not reflect the changes. Only when I stop the process and restart it again, the changes are visible.

So: is there a way to scrub the mirror system and let it load the library and its classes again, within the same Dart process?

like image 484
Gregor Avatar asked Jul 02 '26 00:07

Gregor


2 Answers

I think isolates are a good fit for this requirement.

I haven't used them myself much yet but as far as I know you can load and unload them dynamically. The documentation is not very extensive yet. A few things I found:

  • https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-isolate
  • Recent documentation about Dart Isolates
  • https://www.youtube.com/watch?v=TQJ1qnrbTwk
  • https://www.youtube.com/watch?v=4GlK-Ln7HAc
like image 163
Günter Zöchbauer Avatar answered Jul 04 '26 23:07

Günter Zöchbauer


So, yes, it is possible in Dart to dynamically (re-)load dart-files at runtime. Every new isolate has its own MirrorSystem. If you want to reload a dart-file you must create a new isolate and use the MirrorSystem of this isolate to iterate over the contents in the libraries known to this MirrorSystem. If your dart-file is part of a library known to the MirrorSystem, all functions and classes contained in this file are loaded and reflected anew.

This solution has some drawbacks: First, it is quite heavyweight. The programming of inter-isolate communication is cumbersome. Also it is to be seen whether the memory consumption increases with each reload. Second, the solution is not really dynamic: Isolates load only libraries that are "known" at design-time. They must be directly or indirectly imported into the dart file that contains the static function, which is called when the isolate is created.

Two ideas how the situation could be improved: 1. It would help if the spawn and the spawnUri methods of Isolate could get a list of additional libraries as parameter, which are included in the MirrorSystem of the isolate. 2. The classloaders in Java are independent of processes and threads. They just load classes. Why isn't this possible in Dart?

like image 28
Gregor Avatar answered Jul 04 '26 23:07

Gregor