#1 Implement modules support and import() function

Closed
opened 2 years ago by belliash · 8 comments
belliash commented 2 years ago
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 2 years ago
belliash self-assigned this 2 years ago
belliash commented 2 years ago
Poster
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!')
belliash commented 2 years ago
Poster
Owner

Still TODO:

  • Unload all modules on exit,
  • Add Windows support.
Still TODO: * Unload all modules on exit, * Add Windows support.
belliash commented 2 years ago
Poster
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.
belliash commented 2 years ago
Poster
Owner

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

I think it is fully implemented now by 28dbeeb1ad & 0dc9a04f0e.
belliash closed this issue 2 years ago
devnexen commented 2 years ago
Poster
Collaborator

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 ?
belliash commented 2 years ago
Poster
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.
devnexen commented 2 years ago
Poster
Collaborator

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.
belliash commented 2 years ago
Poster
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 2 years ago
devnexen was assigned by belliash 2 years ago
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.