Menu

#41 (load) doesn't work when called from a function

Future
open
nobody
None
5
2014-09-21
2014-09-20
No

If a function calls (load), then the file isn't loaded into global namespace - all code is loaded into the calling function's context.

I have attached a patch. When the patch is applied, I can use a TinyScheme port of SLIB.

1 Attachments

Discussion

  • James Woodcock

    James Woodcock - 2014-09-20

    Here is a test case. Usage:
    ./scheme
    (load "load_test.scm")
    (test-load)

    Without the patch, TinyScheme reports that variable "afun" is unbound. With the patch, TinyScheme will print "Success!".

     
  • Kevin Cozens

    Kevin Cozens - 2014-09-21

    The R5RS has little information on the load operation and R6RS has nothing. The R5RS says nothing about what environment will be affected. I think it is a feature and not a bug. You can call a function that loads a file. The function can use the contents of the file and when the function terminates the cells used within the function to load the external file may be GC'ed. If you want to load a file whose contents need to be available in the global environment you invoke load outside of any defined function which puts the contents in the global environment.

    I'm open to discussions as to why the code should be changed to always load files using the global, rather than local, environment.

     
  • James Woodcock

    James Woodcock - 2014-09-21

    I hadn't thought of checking the spec... I'll leave the Bug-or-Feature decision to you.

    My main argument for the change is SLIB. SLIB assumes that the definitions contained in any file loaded via (load) will end up in global namespace. So, making this change will make TinyScheme's (load) compatible with all the Scheme implementations supported by SLIB (listed here: http://people.csail.mit.edu/jaffer/SLIB).

    With this change, it is easy to port SLIB to TinyScheme.

    As a worked example, say you wanted to use SLIB's getopt. Your script would have the line:
    (require 'getopt)

    This would end up calling the SLIB function (slib:load) to load the file containing getopt. (slib:load) would call (slib:load-source), which would call (load). SLIB assumes that getopt is in the global environment is the (load) call succeeds.

    You can see the Kawa specific file containting (slib:load) here; http://cvs.savannah.gnu.org/viewvc/slib/slib/kawa.init?view=markup.

    Let me know if you have any questions.

     

Log in to post a comment.