-->
Page 3 of 9

Re: Creating a nodeMCU Lua unofficial FAQ

PostPosted: Mon Jun 01, 2015 3:41 pm
by TerryE
I've just uploaded another tranche of edits and content.

Re: Discussions on my nodeMCU Lua unofficial FAQ

PostPosted: Mon Jun 08, 2015 5:56 am
by vowstar
Although I'm not the creator of nodemcu and have no access to official website, I want do something and contribute this wiki. My English is poor but I will do my best, please help me if I make some error.

Re: Discussions on my nodeMCU Lua unofficial FAQ

PostPosted: Thu Jun 25, 2015 2:19 pm
by Nubble
Hi Terry,
A really, really great write up!! Thank you so much for this. Ive finished my second read of it.

I have a question on volatile modules and specifically the volatile function. You posted up some code:
Code: Select all  local module  ...  -- this is a situation where using an upvalue is essential!
  return function (csocket)
    package.loaded[module]=nil
    module = nil
    -- . . .
  end


and I mostly understand all of this except for the very first line "local module ... --comment"
When i compile this in the esp i get a lua error: stdin:1: spl.lua:1: unexpected symbol near '...'
I cannot seem to determine why this would be. Any help please??

Here is the full volatile module/function:
Code: Select alllocal spl ...

return function split(inputstr, sep)
        if sep == nil then
            sep = "%s"
        end
        local t={}
        local i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            t[i] = str
            i = i + 1
        end
        package.loaded['spl']=nil
        spl=nil
        return t
end

Re: Discussions on my nodeMCU Lua unofficial FAQ

PostPosted: Fri Jun 26, 2015 12:52 pm
by TerryE
Durr,, my bad. There's an = missing. Require passes a single argument: it's name, so the tuple ... which the varargs reference is a list with a single variable in it -- the name, so if your file is "spl.lua" then if the first assignment is
Code: Select alllocal module  = ...

will set the local variable module to "spl". The require() will set package.loaded.spl to reference the returned function, preventing its garbage collection, hence
Code: Select allpackage.loaded[module]=nil

will remove this reference so once all other references go out of scope, the memory used by the module can be garbage collected.

I hope this explanation helps. :D