ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Evaluating a Lua expression at the end (repeat)
@ 2016-08-25 11:28 Kumar Appaiah
  2016-08-25 12:19 ` Henri Menke
  0 siblings, 1 reply; 3+ messages in thread
From: Kumar Appaiah @ 2016-08-25 11:28 UTC (permalink / raw)
  To: ntg-context

Dear List,

Last year, Hans helped me with this example:


\starttext

\startluacode
     local name = nil
          local temp = 0

     function document.startwhatever(s)
           name = s
           temp = 0
            end
         function document.addwhatever(n)
          temp = temp + n
           context(n)
        end
     function
              document.stopwhatever()
               job.variables.save("document:temp:"..name,temp)
            end
         function document.getwhatever(s)
          context(job.variables.collected["document:temp:"..s])
       end
       \stopluacode

\def\startwhatever[#1]{\ctxlua{document.startwhatever("#1")}}
\def\stopwhatever     {\ctxlua{document.stopwhatever()}}
\def\addwhatever    #1{\ctxlua{document.addwhatever(#1)}}
\def\getwhatever    #1{\ctxlua{document.getwhatever("#1")}}

total: \getwhatever{foo}

\startwhatever[foo]

test 1: \addwhatever{10}\par
test 2: \addwhatever{20}\par
test 3: \addwhatever{30}\par

\stopwhatever

\stoptext

However, on my current ConTeXt on Debian (2016.05.17.20160523-1), I am
unable to get it working. It says:

lua error       > lua error on line 37 in file /tmp/test.tex:

/usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: attempt to
index upvalue 'tobesavedmacros' (a nil value)
stack traceback:
        /usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: in
	function 'save'
	        [ctxlua]:14: in function 'stopwhatever'
		        [ctxlua]:1: in main chunk

27     \def\getwhatever    #1{\ctxlua{document.getwhatever("#1")}}
28
29     total: \getwhatever{foo}
30
31     \startwhatever[foo]
32
33     test 1: \addwhatever{10}\par
34     test 2: \addwhatever{20}\par
35     test 3: \addwhatever{30}\par
36
37 >>  \stopwhatever
38
39     \stoptext
40
41

Could you please suggest a workaround?

Thanks.

Kumar
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Evaluating a Lua expression at the end (repeat)
  2016-08-25 11:28 Evaluating a Lua expression at the end (repeat) Kumar Appaiah
@ 2016-08-25 12:19 ` Henri Menke
  2016-08-26  3:57   ` Kumar Appaiah
  0 siblings, 1 reply; 3+ messages in thread
From: Henri Menke @ 2016-08-25 12:19 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Kumar,

the function job.variables.save seems to save the key in job.variables.collected.macros instead of job.variables.collected.  The problem is that macros is not created unconditionally, which is why you might run into a »attempt to index a nil value« error without some manual error checking.  Therefore I recommend assigning the temp value to job.variables.tobesaved["document:temp:"..name].  Perhaps Hans joins this discussion and schools me about why this could be a bad idea, but so far it works.

Also, I'd recommend making all your user level macros protected and check for the optional argument in \startwhatever (otherwise you'll get errors like »Macro doesn't match its definition«).  Because I'm really cautious I also use \luaescapestring{#1} (Try for example »foo"bar« as a value without).

\starttext

\startluacode
local name = nil
local temp = 0

function document.startwhatever(s)
   name = s
   temp = 0
end

function document.addwhatever(n)
   temp = temp + n
   context(n)
end

function document.stopwhatever()
   job.variables.tobesaved["document:temp:"..name] = temp
end

function document.getwhatever(s)
   context(job.variables.collected["document:temp:"..s])
end
\stopluacode

\define\startwhatever{\dosingleempty\dostartwhatever}
\def\dostartwhatever[#1]{\ctxlua{document.startwhatever("\luaescapestring{#1}")}}
\define   \stopwhatever {\ctxlua{document.stopwhatever()}}
\define[1]\addwhatever  {\ctxlua{document.addwhatever(\luaescapestring{#1})}}
\define[1]\getwhatever  {\ctxlua{document.getwhatever("\luaescapestring{#1}")}}

total: \getwhatever{foo}

\startwhatever[foo]

test 1: \addwhatever{10}\par
test 2: \addwhatever{20}\par
test 3: \addwhatever{30}\par

\stopwhatever

\stoptext

On 08/25/2016 01:28 PM, Kumar Appaiah wrote:
> Dear List,
> 
> Last year, Hans helped me with this example:
> 
> 
> \starttext
> 
> \startluacode
>      local name = nil
>           local temp = 0
> 
>      function document.startwhatever(s)
>            name = s
>            temp = 0
>             end
>          function document.addwhatever(n)
>           temp = temp + n
>            context(n)
>         end
>      function
>               document.stopwhatever()
>                job.variables.save("document:temp:"..name,temp)
>             end
>          function document.getwhatever(s)
>           context(job.variables.collected["document:temp:"..s])
>        end
>        \stopluacode
> 
> \def\startwhatever[#1]{\ctxlua{document.startwhatever("#1")}}
> \def\stopwhatever     {\ctxlua{document.stopwhatever()}}
> \def\addwhatever    #1{\ctxlua{document.addwhatever(#1)}}
> \def\getwhatever    #1{\ctxlua{document.getwhatever("#1")}}
> 
> total: \getwhatever{foo}
> 
> \startwhatever[foo]
> 
> test 1: \addwhatever{10}\par
> test 2: \addwhatever{20}\par
> test 3: \addwhatever{30}\par
> 
> \stopwhatever
> 
> \stoptext
> 
> However, on my current ConTeXt on Debian (2016.05.17.20160523-1), I am
> unable to get it working. It says:
> 
> lua error       > lua error on line 37 in file /tmp/test.tex:
> 
> /usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: attempt to
> index upvalue 'tobesavedmacros' (a nil value)
> stack traceback:
>         /usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: in
> 	function 'save'
> 	        [ctxlua]:14: in function 'stopwhatever'
> 		        [ctxlua]:1: in main chunk
> 
> 27     \def\getwhatever    #1{\ctxlua{document.getwhatever("#1")}}
> 28
> 29     total: \getwhatever{foo}
> 30
> 31     \startwhatever[foo]
> 32
> 33     test 1: \addwhatever{10}\par
> 34     test 2: \addwhatever{20}\par
> 35     test 3: \addwhatever{30}\par
> 36
> 37 >>  \stopwhatever
> 38
> 39     \stoptext
> 40
> 41
> 
> Could you please suggest a workaround?
> 
> Thanks.
> 
> Kumar
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
> 

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Evaluating a Lua expression at the end (repeat)
  2016-08-25 12:19 ` Henri Menke
@ 2016-08-26  3:57   ` Kumar Appaiah
  0 siblings, 0 replies; 3+ messages in thread
From: Kumar Appaiah @ 2016-08-26  3:57 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Thu, Aug 25, 2016 at 5:49 PM, Henri Menke <henrimenke@gmail.com> wrote:
> Hi Kumar,
>
> the function job.variables.save seems to save the key in job.variables.collected.macros instead of job.variables.collected.  The problem is that macros is not created unconditionally, which is why you might run into a »attempt to index a nil value« error without some manual error checking.  Therefore I recommend assigning the temp value to job.variables.tobesaved["document:temp:"..name].  Perhaps Hans joins this discussion and schools me about why this could be a bad idea, but so far it works.
>
> Also, I'd recommend making all your user level macros protected and check for the optional argument in \startwhatever (otherwise you'll get errors like »Macro doesn't match its definition«).  Because I'm really cautious I also use \luaescapestring{#1} (Try for example »foo"bar« as a value without).


<snipped code>

Indeed, this worked! I'll try to understand the internals myself to
know what is going on exactly. Thanks for the solution.

Kumar
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-08-26  3:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-25 11:28 Evaluating a Lua expression at the end (repeat) Kumar Appaiah
2016-08-25 12:19 ` Henri Menke
2016-08-26  3:57   ` Kumar Appaiah

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).