Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Loading squirrel functions from dynamic libraries across systems

Last post 10-04-2008, 5:59 PM by JasonP. 12 replies.
Sort Posts: Previous Next
  •  10-21-2006, 8:26 AM 1351

    Loading squirrel functions from dynamic libraries across systems

    I'm working up some code that will allow the creation of .dll/.so/etc modules in a format that allows this:

    FunctionTable = import("Functions.dll");

    The returned table contains a set of functions indexed by their name as reported by the library itself. It also support reloading of the library if it is already loaded, and such. But anyway, I digress.

    The code as written should work across *nix and win32 platforms, but I know little of macs.  If anyone wants to help me add some mac support, I'd be thankful. I'll link to the two source files after I do some testing on it this weekend.

    Figure I'd drop a notice here if anyone is interested.
  •  10-21-2006, 6:11 PM 1352 in reply to 1351

    Re: Loading squirrel functions from dynamic libraries across systems

    It would be good if this function can be considered for inclusion in the stanard Squirrel.

    Lua has this in the latest versions; Squirrel can "keep up" with this additon...

  •  10-22-2006, 7:14 AM 1353 in reply to 1352

    Re: Loading squirrel functions from dynamic libraries across systems

    The utility offered by such a function is great, since I can modularize an application which is driven almost purely by squirrel at the core, and update the function libraries as needed.

    I'd also like to see it included, but that is up to Alberto. The idea did indeed come from Lua 5.1

    Doing some final testing, and I'll put the code up here shortly.
  •  10-22-2006, 8:03 AM 1354 in reply to 1353

    Re: Loading squirrel functions from dynamic libraries across systems

    The directory containing the somewhat tested source: here.

    The header file as html: sqdyn.h
    The source file as html: sqdyn.c

    The directory also has flat ascii files for both. I'll upload some sample source for a library that returns a small set of sample functions, after a little more testing.
  •  10-22-2006, 2:04 PM 1355 in reply to 1354

    Re: Loading squirrel functions from dynamic libraries across systems

    Here is a simple dll implemented in html: sqdll.c
    and: sqdll.h
    and the entire Studio Solution folder (VC7, zipped): sqdll.zip

    If its missing some needed feature, or someone could help me support mac systems
    implicitly, let me know :)
  •  10-25-2006, 12:56 AM 1359 in reply to 1355

    Re: Loading squirrel functions from dynamic libraries across systems

    This is something I prototyped sometimes ago(3y) for squirrel 1.x, but I thought it wasn't fit for the core release. I think supporting something like this as official add-on is fine. I'd rather not write platform specific code in the core library. Maybe could end up in the 'etc' directory.

    I personally think that the lua solution is very naive, the fact that every module is required to link against lua is just nonsense; plus, there's no way to build any kind of backward compatibilty between module/VMs.

    BTW I digged you my old design notes about the topic.

    *HOW DYNAMIC MODULES WORK
    -2 kind of modules binary and squirrel only(the user is not aware of that)
    -every module has a stub ".nut" file to initialize himself and load a binary module if needed
     so if is a squirrel only module, the stub can be the implementaion as well.
    -the stub is called "modulename_stub.nut" or something like that
    -the stub file of a binary module does the actual module loading(so can check also for other resources than the dll)
    **BYNARY MODULES
     a binary module exports 4 functions:
     
     int sq_module_getapiversion()
      return the API version the the module requires to work propetly


     int sq_module_supportcharsize(int charsize)
      charsize can be 8, 16 the module can implement both or just fail if the char size is not supported


     int sq_module_open(HSQUIRRELVM v,SQModuleAPI *api)
      loadmodule push the target table in the stack(the one that will contain the module api functions)
      and calls this function.
      SQModuleAPI is a pointer to a structure containing function pointers to
      the squirrel API and the std lib utilities function(file creation, blob creation etc..)
      so a module can use stl lib compliant types.
      
      struct SQModuleAPI {
       p_sq_getinteger sq_getinteger;
       p_sq_newclosure sq_newclosure;
       ...
       ...
      }
      
      new version of the module api will add the new functions at the bottom of the structure  in order to be able to load old modules with a newer version.
      modules do not link any squirrel related lib all is passed through this struct, hopefully  if the std api is good enough a module could avoid to link the CRT lib as well(in most of the cases).
      a set of macros is provided to be able to transparently link the module
    statically or dynamically.


     int sq_module_close()
      called before unloading the module.
      .TO SOLVE can modules be unloaded runtime in a safe way?

    .TO SOLVE
     to resolve the physical location of a module
      .IDEA maybe a central db based system like MS COM or .NET?
      
     should be cool if an embedded version could kind of set is own policy
     what about passing an modulelocator callback when the module library is registered?
      sq_register_modulelib(v,locator);
      int locator(HSQUIRRELVM v,const SQChar *modulename)

    -API
    loadmodule("inet") //register the module globally
    local s = socket();

    inet <- loadmodule("inet",{}) //register the module in the table passed as second param
    local s = inet.socket();

    ciao

    Alberto

  •  10-28-2006, 6:29 AM 1368 in reply to 1359

    Re: Loading squirrel functions from dynamic libraries across systems

    I can fully understand why you'd shy away from platform specific code in the core.

    The LUA solution is indeed wasteful, inept, and undeveloped. On the other hand, it
    is very simple.

    Your dug up ideas have a lot of interesting concepts presented. I'll have to think a bit
    about where I want to go with the idea. The system I have now does just what I
    want it too, but isn't future-safe or portable.

    Thanks,
    Jason
  •  09-27-2008, 10:22 AM 2793 in reply to 1368

    Re: Loading squirrel functions from dynamic libraries across systems

    @JasonP

    Hello, is the SourceCode anywhere online?

  •  09-27-2008, 3:18 PM 2794 in reply to 2793

    Re: Loading squirrel functions from dynamic libraries across systems

    I've had numerous changes to my domain structure over the last few years, I'll see if I can locate this weekend and get back to you.
  •  09-29-2008, 3:11 AM 2802 in reply to 2794

    Re: Loading squirrel functions from dynamic libraries across systems

    ok thank you very much
  •  09-30-2008, 3:18 PM 2803 in reply to 2802

    Re: Loading squirrel functions from dynamic libraries across systems

    Found the code, but man is it pretty immature. I'll go back and write an improved version this week, and post it online by the weekend.
  •  10-01-2008, 6:50 AM 2806 in reply to 2803

    Re: Loading squirrel functions from dynamic libraries across systems

    Oke i wating... thanks
  •  10-04-2008, 5:59 PM 2817 in reply to 2806

    Re: Loading squirrel functions from dynamic libraries across systems

    Ok, here is the untested code (I haven't had much free time): sqmod

    I think its pretty clear, and I still have some issues to resolve, such as: Unicode support is borked/non-existant, though it will error out if you have that set. Support for systems that aren't WIN32 or have access to dlfcn.h does not exist, not sure if this is a problem though actually. Also, untested. I'll rectify that this week. I have no system for handling locations of files, so that is yet on the todo list.

    On the positive side, it addresses some of Alberto's concerns. Compiling the binary modules no longer needs to link against the library, as sqmod offers a whole interface into the API via function pointers inside the HSQUIRRELFRAME structure. If you call something like: import("sockets"), the functions tries to find sockets.nut first, and then fallsback on sockets.dll (or .so if not WIN32). This makes it possible to wrap a module with a .nut, which can call importdll("sockets.dll") to bypass this wrapping rule.

    If you have any questions or comments, fire away. Obviously this code is free for inclusion into any release, though its far from ready in its current state. Any comments are welcome.

    Oh, also to use it, put sqmod.c into your squirrel dir and sqmod.h into your include dir. Add those to your project and away you go. :D (you call sq_addmodulesupport() to add module support to any VM.)
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems