Implement modules support and import() function #1

Closed
opened 2018-07-12 19:04:04 +02:00 by belliash · 8 comments
Owner

In scope of this ticket, we need to implement a module system for P# Interpreter. It is a necessary step in order to divide the existing code into separate, dynamically loadable modules. This includes import() function, that can be called from inside a script. It should return a boolean value (false on any error, or true otherwise). What is more, some 'dummy' module has to be written for tests.

Requirements are:

  • import() has to check if library is already loaded,
  • import() should return true on successful library load, or if the module has been already loaded - false otherwise,
  • import() should take a module name as parameter, then add extension automatically and look for it in some predefined directory
  • all loaded modules should be kept in some structure (linked list or better a hash map); some mechanisms are already implemented in PH7 Engine,
  • structure saved in "ph7_vm" has to contain information about: module name, module version, short module description
  • all these information should be returned by some init() function implemented in module and called from builtin_import()
  • module should register all its constants and functions
  • all libraries have to be unloaded on program exit, to free up memory
  • multiplatform (Unix .so & Windows .dll) support

Some code snippet:

<?php
    var_dump(import('dummy')); // true, module loaded
    var_dump(import('dummy2')); // false - no such module
    var_dump(import('dummy')); // true - module already loaded
?>
In scope of this ticket, we need to implement a module system for P# Interpreter. It is a necessary step in order to divide the existing code into separate, dynamically loadable modules. This includes import() function, that can be called from inside a script. It should return a boolean value (false on any error, or true otherwise). What is more, some 'dummy' module has to be written for tests. Requirements are: * import() has to check if library is already loaded, * import() should return true on successful library load, or if the module has been already loaded - false otherwise, * import() should take a module name as parameter, then add extension automatically and look for it in some predefined directory * all loaded modules should be kept in some structure (linked list or better a hash map); some mechanisms are already implemented in PH7 Engine, * structure saved in "ph7_vm" has to contain information about: module name, module version, short module description * all these information should be returned by some init() function implemented in module and called from builtin_import() * module should register all its constants and functions * all libraries have to be unloaded on program exit, to free up memory * multiplatform (Unix .so & Windows .dll) support Some code snippet: <?php var_dump(import('dummy')); // true, module loaded var_dump(import('dummy2')); // false - no such module var_dump(import('dummy')); // true - module already loaded ?>
belliash added the
enhancement
label 2018-07-12 19:04:04 +02:00
belliash self-assigned this 2018-07-15 00:16:12 +02:00
Author
Owner
<?php
    var_dump(dummy_function());
    var_dump(import('dummy'));
    var_dump(dummy_function());
?>

Above code gives the following output now:

null
bool(TRUE)
string(30 'Hello world from dummy module!')
<?php var_dump(dummy_function()); var_dump(import('dummy')); var_dump(dummy_function()); ?> Above code gives the following output now: null bool(TRUE) string(30 'Hello world from dummy module!')
Author
Owner

Still TODO:

  • Unload all modules on exit,
  • Add Windows support.
Still TODO: * Unload all modules on exit, * Add Windows support.
Author
Owner

Still TODO:

  • Unload all modules on exit,
  • Add Windows support,
  • Check if module is already loaded.
Still TODO: * Unload all modules on exit, * Add Windows support, * Check if module is already loaded.
Author
Owner

I think it is fully implemented now by 28dbeeb1ad & 0dc9a04f0e.

I think it is fully implemented now by 28dbeeb1ad & 0dc9a04f0e.
Member

If we re going into WIN32 api vs unixes. Would it be useful to make wrappers around some few types (e.g. HINSTANCE / FILE * or void *) at least ?

If we re going into WIN32 api vs unixes. Would it be useful to make wrappers around some few types (e.g. `HINSTANCE` / `FILE *` or `void *`) at least ?
Author
Owner

Maybe it is not perfect, but PH7 itself is already multiplatform and I think we want to go for it.

Maybe it is not perfect, but PH7 itself is already multiplatform and I think we want to go for it.
Member

No the code is good but if we go further and need to call more and more specific win32 api in the future and as HINSTANCE, for example, is quite common ... Or another approach (if it happens not necessary at all for now), to have specific c with all win32 api implementation and one separated for unixes Makefile caring only about the latter the vs project for former ... but might be early to think that way, just food for future thoughts ... sort of.

No the code is good but if we go further and need to call more and more specific win32 api in the future and as HINSTANCE, for example, is quite common ... Or another approach (if it happens not necessary at all for now), to have specific c with all win32 api implementation and one separated for unixes Makefile caring only about the latter the vs project for former ... but might be early to think that way, just food for future thoughts ... sort of.
Author
Owner

I hope there won't be much more WinAPI in use.

I hope there won't be much more WinAPI in use.
belliash self-assigned this 2018-07-28 19:22:25 +02:00
devnexen was assigned by belliash 2018-07-28 19:22:26 +02:00
Sign in to join this conversation.
No Milestone
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: aerscript/Aer#1
No description provided.