7 Replies Latest reply on Nov 19, 2009 5:41 PM by areohbee

    setfenv - Supported? Not Supported?? Sortof Supported???

    areohbee Level 5

      I've been using setfenv for quite a while, and I recently noticed two things:

       

      1.  I've been having some strange problems.

      2.  The SDK doc says its not supported.

       

      I've since solved my problems, and it seems setfenv is doing what it should, but its bothering me that I'm using a function that is on the "bad" list.

       

      Anyone???

       

      Rob Cole

      areohbee ceeohelee

      robcole.com

        • 1. Re: setfenv - Supported? Not Supported?? Sortof Supported???
          escouten Adobe Employee

          Hi Rob, you're probably right to be bothered by this. It turns out that in 2.0 we said a number of the built-in functions were not supported, but we didn't actually enforce that rule. We probably will be enforcing that rule as of 3.0.

           

          What are you hoping to accomplish by using setfenv? Looking forward to LR3, is there an alternative way that we could help you solve that problem?

          • 2. Re: setfenv - Supported? Not Supported?? Sortof Supported???
            areohbee Level 5

            Hi Eric,

             

            I use a third-party module called markdown.lua (which uses setfenv) to translate plain text metadata into html to go with photos being displayed in a web browser.

             

            I've also used setfenv in my code to make more object oriented programming. Oh, and if I remember correctly, my first use was when I overflowed the local variable space in an Export Task module. I suppose I could have broken it into two files instead.

             

            Since its working, I'm guessing the reason for not supporting it is either security or some potential for anomaly (no?).

             

            Would it not be possible for there to be a LrUtils.setfenv or some such Adobe controlled implementation in Lr3?

             

            Actually, it would be easy for me, at this point, to pull setfenv from my own code. And, if Adobe/Lightroom supported some scheme for entering metadata in html translatable format, that would be better than my sort-of half-baked markdown-based scheme anyway. Then I would not need setfenv at all.

             

            What ya think?

             

            PS - I have written my own dofile-based loader that implements package-based module loading, since it is useful, and that is not supported in Lightroom. I hope that won't be a problem in Lr3 - any ideas?

             

            Thanks for the info and the heads up.

             

            Say, are there any whitepapers or anything that talk about the integration of lua into Lightroom - things like module loading, initialization, namespaces, global variables, ...? Or any new info that would help Lightroom-3 developers?

             

            Rob

            • 3. Re: setfenv - Supported? Not Supported?? Sortof Supported???
              Vladimir Vinogradsky

              Oh, please go easy with disabling "unsupported" functions and namespaces.

              The SDK spec says the debug namespace is unavailable, but I found it IS available. For me debug.getinfo() is indispensable when logging trace information. Please don't kill it off! In fact, I truly hope for SDK 3.0 to offer more functionality, not less.

              • 4. Re: setfenv - Supported? Not Supported?? Sortof Supported???
                areohbee Level 5

                I whole heartedly agree. setfenv is working perfectly for me - if it remains disabled in Lr3 I'm going to have to re-write all my plugins. If you have problems with it, just document its limitations, or add a "use at your own risk" clause, or enforce some limitations, or come up with an equivalent Lr library function if necessary. Killing things that are working and being used is stepping backwards.

                 

                Rob Cole - Lightroom Plugin Developer

                www.robcole.com

                • 5. Re: setfenv - Supported? Not Supported?? Sortof Supported???
                  escouten Adobe Employee

                  So here's the tradeoff I'm weighing

                   

                  Lightroom runs plug-ins in basically the same environment as it runs its own code. The SDK was designed to prevent at least some efforts at bad code (either innocent or malicious) doing damage to the core process. There are definitely some holes in that firewall and to some extent I'm trying to tighten them up.

                   

                  I'm worried about the possibility of someone using setfenv on Lightroom API functions. That could cause not only your own plug-in but other plug-ins and even the app as a whole to behave unpredictably. That was why setfenv was initially on the unsupported list. There was a bug in 1.x and 2.x that caused it to be available despite it being listed as unavailable, but I have fixed that bug as of 3.0.

                   

                  So far the most valid case I've seen for using setfenv is to safely run a function in an empty environment. For that reason, I've added two new functions in the 3.0 SDK, documented as follows:

                   

                  --------------------------------------------------------------------------------
                  --- Runs the main function in a known-safe function environment.
                   -- Equivalent to <code>setfenv( func, {} ); return func( ... )</code>.
                   -- <p>First supported in version 3.0 of the Lightroom SDK.</p>
                      -- @param func (function) The main function to call with an empty
                          -- environment.
                      -- @param ... (any further arguments are passed through to the function)
                      -- @return any function results from the function
                  
                  function LrFunctionContext.callWithEmptyEnvironment( func, ... ) ... end
                  
                  --------------------------------------------------------------------------------
                  --- Runs the main function in a known-safe function environment. Catch any
                   -- exceptions that may occur.
                   -- Equivalent to <code>setfenv( func, {} ); return pcall( func, ... )</code>.
                   -- <p>First supported in version 3.0 of the Lightroom SDK.</p>
                      -- @param func (function) The main function to call with an empty
                          -- environment.
                      -- @param ... (any further arguments are passed through to the function)
                      -- @return any function results from the function
                  
                  function LrFunctionContext.pcallWithEmptyEnvironment( func, ... ) ... end
                  

                   

                   

                  Rob, perhaps if you explain (dating back to your August post) how you're using setfenv for object-oriented programming, we might be able to work out something that makes sense for both of us. FWIW, our OO paradigm is based on setmetatable, which does exist in the 3.0 SDK with some reasonable safeguards against misuse.

                  • 6. Re: setfenv - Supported? Not Supported?? Sortof Supported???
                    escouten Adobe Employee

                    Vladimir, FYI debug.getinfo has been added to the officially supported list for 3.0.

                    • 7. Re: setfenv - Supported? Not Supported?? Sortof Supported???
                      areohbee Level 5

                      escouten,

                       

                      Thanks for addressing this issue.

                       

                      My first use of setfenv occurred when I got an error: "so strange I don't even remember what it was" - that I traced to some sort of local module-level variable limit being exceeded. I've since recoded all my plugins to circumvent this particular problem without using setfenv.

                       

                      The one problem I have remaining is that I depend on another organization's module: 'markdown.lua', which uses setfenv. I was hoping not to have to recode it too much, since its big, unfamiliar, I'd no doubt introduce insidious bugs, and I'd have to redo it in order to upgrade.

                       

                      I am considering recoding my plugin to not use markdown - its far from ideal anyway. What I would prefer is a Lightroom-supported "single-line-of-text to html" converter to convert single-line metadata stored in the catalog into html for the web. There would be some compatibility issues me and/or my users would have to address but hey...

                       

                      Anyway, you can check out how markdown.lua uses setfenv here. Note: I stripped all the command-line stuff out in the version I use to support my plugin.

                       

                      To wrap up: what I need is just one or two functions, for example:

                       

                      - LrUtils.setDefaultNamespace( {my_module_table} ) - so intra module variables and functions are interpreted as module-table members by default - see setfenv in markdown.lua near the top.

                       

                      - LrUtils.restoreDefaultNamesapce() - to restore global namespace as default (see setfenv in markdown.lua near the bottom). Or maybe just omit this function and just have it be automatic at end-of-module.

                       

                      I'm eagerly awaiting your response.

                       

                      Rob Cole

                      www.robcole.com