\ctxlua and \startluacode ... \stopluacode
Dear List! THis MWE must be faulty, but where is the error? ==================================================== \startluacode userdata = userdata or {} function userdata.file_exists (name) local name = name local f = assert ( io.open ( name, "r" )) if f then f:close () context ( name ) context ( " exists!" ) else context ( name ) context ( " doesn't exist!" ) end end \stopluacode \starttext \def\lookupfile#1% {\ctxlua{userdata.file_exists([==[#1]==])}} \lookupfile{"$HOME/context-tests/AAA.lua"} % "AAA.lua" intentionally does exist \lookupfile{"$HOME/context-tests/BBB.lua"} % "BBB.lua" intentionally doesn't exist \stoptext ==================================================== It makes no difference in commenting out the first or the second "\lookupfile" command. The error message eventually is always nearly the same: token call, execute: [ctxlua]:8: "$HOME/context-tests/AAA.lua": No such file or directory token call, execute: [ctxlua]:8: "$HOME/context-tests/BBB.lua": No such file or directory I'm using: LuaMetaTeX, Version 2.00.0 ConTeXt ver: 2019.10.10 18:15 MKIV beta fmt: 2019.10.12 int: english/english Please, any ideas? Regards, Rudolf
I suspect the double quotes are part of file names, hence files cannot be found. Perhaps try remove them in macro argument. Hope this helps De : Rudolf Bahr Envoyé le :lundi 14 octobre 2019 18:50 À : ntg-context@ntg.nl Objet :[NTG-context] \ctxlua and \startluacode ... \stopluacode Dear List! THis MWE must be faulty, but where is the error? ==================================================== \startluacode userdata = userdata or {} function userdata.file_exists (name) local name = name local f = assert ( io.open ( name, "r" )) if f then f:close () context ( name ) context ( " exists!" ) else context ( name ) context ( " doesn't exist!" ) end end \stopluacode \starttext \def\lookupfile#1% {\ctxlua{userdata.file_exists([==[#1]==])}} \lookupfile{"$HOME/context-tests/AAA.lua"} % "AAA.lua" intentionally does exist \lookupfile{"$HOME/context-tests/BBB.lua"} % "BBB.lua" intentionally doesn't exist \stoptext ==================================================== It makes no difference in commenting out the first or the second "\lookupfile" command. The error message eventually is always nearly the same: token call, execute: [ctxlua]:8: "$HOME/context-tests/AAA.lua": No such file or directory token call, execute: [ctxlua]:8: "$HOME/context-tests/BBB.lua": No such file or directory I'm using: LuaMetaTeX, Version 2.00.0 ConTeXt ver: 2019.10.10 18:15 MKIV beta fmt: 2019.10.12 int: english/english Please, any ideas? Regards, Rudolf ___________________________________________________________________________________ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
Hi,
On 14 Oct 2019, at 18:51, Rudolf Bahr
wrote: Dear List!
THis MWE must be faulty, but where is the error?
=================================================== \lookupfile{"$HOME/context-tests/AAA.lua"} % "AAA.lua" intentionally does exist
$HOME is an actual literal here, since io.open() does not call out to a shell? Taco
On 10/14/2019 6:50 PM, Rudolf Bahr wrote:
Dear List!
THis MWE must be faulty, but where is the error?
====================================================
\startluacode
userdata = userdata or {}
function userdata.file_exists (name)
local name = name local f = assert ( io.open ( name, "r" ))
if f then f:close () context ( name ) context ( " exists!" ) else context ( name ) context ( " doesn't exist!" ) end
end
\stopluacode
\starttext
\def\lookupfile#1% {\ctxlua{userdata.file_exists([==[#1]==])}}
\lookupfile{"$HOME/context-tests/AAA.lua"} % "AAA.lua" intentionally does exist \lookupfile{"$HOME/context-tests/BBB.lua"} % "BBB.lua" intentionally doesn't exist
\stoptext
====================================================
It makes no difference in commenting out the first or the second "\lookupfile" command. The error message eventually is always nearly the same:
token call, execute: [ctxlua]:8: "$HOME/context-tests/AAA.lua": No such file or directory token call, execute: [ctxlua]:8: "$HOME/context-tests/BBB.lua": No such file or directory
You're in a tex environment, so no $HOME expansion. You can use the prefix resolvers: \startluacode function userdata.file_exists(name) local name = name and resolvers.resolve(name) if name and lfs.isfile(name) then context("file %a exists",name) else context("file %a doesn't exist",name) end end \stopluacode \starttext % \def\lookupfile#1{\ctxlua{userdata.file_exists([==[#1]==])}} \def\lookupfile{\ctxlua{userdata.file_exists(tokens.scanners.string())}} \lookupfile{home:context-tests/AAA.lua}{xx} \lookupfile{home:context-tests/BBB.lua}{xx} \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On Mon, Oct 14, 2019 at 07:19:53PM +0200, Hans Hagen wrote:
On 10/14/2019 6:50 PM, Rudolf Bahr wrote:
THis MWE must be faulty, but where is the error?
====================================================
\startluacode
userdata = userdata or {}
function userdata.file_exists (name)
local name = name local f = assert ( io.open ( name, "r" ))
if f then f:close () context ( name ) context ( " exists!" ) else context ( name ) context ( " doesn't exist!" ) end end
\stopluacode
\starttext
\def\lookupfile#1% {\ctxlua{userdata.file_exists([==[#1]==])}}
\lookupfile{"$HOME/context-tests/AAA.lua"} % "AAA.lua" intentionally does exist \lookupfile{"$HOME/context-tests/BBB.lua"} % "BBB.lua" intentionally doesn't exist
\stoptext
====================================================
It makes no difference in commenting out the first or the second "\lookupfile" command. The error message eventually is always nearly the same:
token call, execute: [ctxlua]:8: "$HOME/context-tests/AAA.lua": No such file or directory token call, execute: [ctxlua]:8: "$HOME/context-tests/BBB.lua": No such file or directory
Joseph, Taco and Hans, I thank you very much for your quick answers! I shall try Hans' interesting suggestion on prefix resolvers after I've studied it, for which I certainly need some time! But before doing so I've a comment: Your answers, Joseph, Taco and Hans, could lead to the impression, that giving up "double quotes as part of file names" and/or "$HOME" in the MWE would be the solution. And indeed, not using "double quotes" and "expanding $HOME" by hand partly brings success: 1. In the case of an existing file (AAA.lua) the MWE works without error message! I will append the output-pdf-file here (just for fun!). 2. But in the case of an not existing file (BBB.lua) the known error message comes up again: "token call, execute: [ctxlua]:8: /home/sam/context-tests/BBB.lua: No such file or directory". This, of course, is true, but shouldn't be mentioned in an error message! Within \startluacode ... \stopluacode it seems one has to abandon the normal lua world, at least partly. Are there other "special features"? Rudolf
Hi,
On 14 Oct 2019, at 21:08, Rudolf Bahr
wrote: 2. But in the case of an not existing file (BBB.lua) the known error message comes up again: "token call, execute: [ctxlua]:8: /home/sam/context-tests/BBB.lua: No such file or directory". This, of course, is true, but shouldn't be mentioned in an error message!
If you do not want the error message, don’t use assert(). You can just do f = io.open(...) If f then ... else ... end Best wishes, Taco
On Mon, Oct 14, 2019 at 09:26:58PM +0200, Taco Hoekwater wrote:
Hi,
On 14 Oct 2019, at 21:08, Rudolf Bahr
wrote: 2. But in the case of an not existing file (BBB.lua) the known error message comes up again: "token call, execute: [ctxlua]:8: /home/sam/context-tests/BBB.lua: No such file or directory". This, of course, is true, but shouldn't be mentioned in an error message!
If you do not want the error message, don’t use assert(). You can just do
f = io.open(...) If f then ... else ... end
Best wishes, Taco
Hi Taco! Thank you again for your message! My wish isn't to avoid error messages, if they are justified! In the case of my MWE I want to have a decision in my \startluacode ... \stopluacode environment, whether a certain file exists or not. With this decision I want to branch in my program and with an error message I cannot do it. Lua is a wonderfull programming language and I'm appreciative to get a way to exit ConTeXt, run Lua code and enter the ConTeXt program at the same location again! On the other side, in the last days I spent some time to eliminate errors in my \startluacode ...\stopluacode program to change code which causes no error messages in pure Lua. Now to your suggestion not to use "assert ()". Indeed it's a solution! My program works! I append again its output (again just for fun!). Did you really try it or has it been just an idea? The MWE is now as follows: ======================================================================================= \startluacode userdata = userdata or {} function userdata.file_exists (name) local name = name local f = io.open ( name, "r" ) if f then f:close () context ( name ) context ( " exists!" ) else context ( name ) context ( " doesn't exist!" ) end end \stopluacode \starttext \def\lookupfile#1% {\ctxlua{userdata.file_exists([==[#1]==])}} \lookupfile{/home/sam/context-tests/AAA.lua} % "AAA.lua" intentionally does exist \par \lookupfile{/home/sam/context-tests/BBB.lua} % "BBB.lua" intentionally doesn't exist \stoptext ======================================================================================= R. Ierusalimschy encourages the use of "assert()". But it is apparantly better not to use it under certain circumstances as in \startluacode ... \stopluacode as I've learnt now. Can I avoid therefore Hans' prefix resolvers code? Best wishes, Rudolf
Hi Rudolf,
On 15 Oct 2019, at 00:50, Rudolf Bahr
wrote: Now to your suggestion not to use "assert ()". Indeed it's a solution! My program works! I append again its output (again just for fun!). Did you really try it or has it been just an idea?
First, let me say that embedded lua versions are typically a little bit different from standalone. Just how much different depends on the embedding program. Luatex is actually pretty close to standalone lua. Luatex adds a bunch of extension libraries, but it changes very little of the core language. And all those changes are documented in the luatex manual. None of those changes affect your minimal example, except that lua errors are handled a little differently. Note that standalone lua *also* produces an error, as that is what assert() is supposed to do. And when lua runs into an error, it ignores the rest of the current chunk: “... whenever an error occurs, Lua ends the current chunk and returns to the application.” (from lua.org) Standalone lua typically sees only one chunk (the file you pass on the command line) but embedded lua implementation often see (sometimes many) more chunks. In luatex’s case, each \directlua is a separate chunk (in ConTeXt, that means every \startluacode block and every \ctxlua call is a separate chunk). The only unusual thing here is that standalone lua silently quits and returns a non-zero exit code to the shell, whereas luatex gives you the typical TeX-style error prompt. The rationale for that is: lua errors can happen in many places in your input file, and if they were silently ignored, your typeset pages could be wrong without you realising it. And to answer your question above: I did not have to try or guess. I know about how assert() works because it is documented in the lua manual (and as it closely mimics the assert() C function, that is easy for me to remember). Best wishes, Taco
On 10/15/2019 10:04 AM, Taco Hoekwater wrote:
Hi Rudolf,
On 15 Oct 2019, at 00:50, Rudolf Bahr
wrote: Now to your suggestion not to use "assert ()". Indeed it's a solution! My program works! I append again its output (again just for fun!). Did you really try it or has it been just an idea?
First, let me say that embedded lua versions are typically a little bit different from standalone. Just how much different depends on the embedding program. Luatex is actually pretty close to standalone lua. Luatex adds a bunch of extension libraries, but it changes very little of the core language. And all those changes are documented in the luatex manual. None of those changes affect your minimal example, except that lua errors are handled a little differently.
Note that standalone lua *also* produces an error, as that is what assert() is supposed to do. And when lua runs into an error, it ignores the rest of the current chunk:
“... whenever an error occurs, Lua ends the current chunk and returns to the application.” (from lua.org)
Standalone lua typically sees only one chunk (the file you pass on the command line) but embedded lua implementation often see (sometimes many) more chunks. In luatex’s case, each \directlua is a separate chunk (in ConTeXt, that means every \startluacode block and every \ctxlua call is a separate chunk).
The only unusual thing here is that standalone lua silently quits and returns a non-zero exit code to the shell, whereas luatex gives you the typical TeX-style error prompt. The rationale for that is: lua errors can happen in many places in your input file, and if they were silently ignored, your typeset pages could be wrong without you realising it.
And to answer your question above: I did not have to try or guess. I know about how assert() works because it is documented in the lua manual (and as it closely mimics the assert() C function, that is easy for me to remember). as you say, it's all normal lua behaviour:
if you do: print(io.open("crap.crap")) one gets nil crap.crap: No such file or directory 2 and i think that assert then returns the second returned argument but if one does context(io.open("crap.crap") and "yes" or "no") then the first argument is checked against Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On Tue, Oct 15, 2019 at 10:15:34AM +0200, Hans Hagen wrote:
On 10/15/2019 10:04 AM, Taco Hoekwater wrote:
Hi Rudolf,
On 15 Oct 2019, at 00:50, Rudolf Bahr
wrote: Now to your suggestion not to use "assert ()". Indeed it's a solution! My program works! I append again its output (again just for fun!). Did you really try it or has it been just an idea?
First, let me say that embedded lua versions are typically a little bit different from standalone. Just how much different depends on the embedding program. Luatex is actually pretty close to standalone lua. Luatex adds a bunch of extension libraries, but it changes very little of the core language. And all those changes are documented in the luatex manual. None of those changes affect your minimal example, except that lua errors are handled a little differently.
Note that standalone lua *also* produces an error, as that is what assert() is supposed to do. And when lua runs into an error, it ignores the rest of the current chunk:
“... whenever an error occurs, Lua ends the current chunk and returns to the application.” (from lua.org)
Standalone lua typically sees only one chunk (the file you pass on the command line) but embedded lua implementation often see (sometimes many) more chunks. In luatex’s case, each \directlua is a separate chunk (in ConTeXt, that means every \startluacode block and every \ctxlua call is a separate chunk).
The only unusual thing here is that standalone lua silently quits and returns a non-zero exit code to the shell, whereas luatex gives you the typical TeX-style error prompt. The rationale for that is: lua errors can happen in many places in your input file, and if they were silently ignored, your typeset pages could be wrong without you realising it.
And to answer your question above: I did not have to try or guess. I know about how assert() works because it is documented in the lua manual (and as it closely mimics the assert() C function, that is easy for me to remember). as you say, it's all normal lua behaviour:
if you do:
print(io.open("crap.crap"))
one gets
nil crap.crap: No such file or directory 2
and i think that assert then returns the second returned argument
but if one does
context(io.open("crap.crap") and "yes" or "no")
then the first argument is checked against
Hans
Hi Hans, hi Taco! For your answers and patient explanations I want to thank you, not to forget [1], [2], [3] for instance! Now it gets clearer to me what the distinctive features could be which one can encounter when using a combination of even so wunderful programming languages as ConTeXt and Lua are! Best wishes, Rudolf [1] https://wiki.contextgarden.net/Programming_in_LuaTeX [2] http://www.luatex.org/svn/trunk/manual/luatex.pdf [3] http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf
participants (4)
-
Hans Hagen
-
Joseph
-
Rudolf Bahr
-
Taco Hoekwater