* nil from TeX to Lua
@ 2021-08-09 19:58 Hans van der Meer via ntg-context
2021-08-09 20:05 ` Aditya Mahajan via ntg-context
0 siblings, 1 reply; 4+ messages in thread
From: Hans van der Meer via ntg-context @ 2021-08-09 19:58 UTC (permalink / raw)
To: NTG ConTeXt; +Cc: Hans van der Meer
[-- Attachment #1.1: Type: text/plain, Size: 602 bytes --]
In the TeX to Lua interface with
\def\Macro#1{\directlua{luafunction("#1")}}
the transfer from TeX to Lua is by string. However what to do at the Lua end when calling \Macro{}?
As I understand it, then on the Lua side an empty argument and an empty string are indistinguishable. This is not always wanted, for example when one needs to place either a nil or an empty string into a Lua table. I am running in trouble when to t.key set to \Macro{} should become nil or when it should become "".
Is there some common understanding about how to handle this situation?
dr. Hans van der Meer
[-- Attachment #1.2: Type: text/html, Size: 2151 bytes --]
[-- Attachment #2: Type: text/plain, Size: 493 bytes --]
___________________________________________________________________________________
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nil from TeX to Lua
2021-08-09 19:58 nil from TeX to Lua Hans van der Meer via ntg-context
@ 2021-08-09 20:05 ` Aditya Mahajan via ntg-context
2021-08-10 7:23 ` Taco Hoekwater via ntg-context
0 siblings, 1 reply; 4+ messages in thread
From: Aditya Mahajan via ntg-context @ 2021-08-09 20:05 UTC (permalink / raw)
To: Hans van der Meer via ntg-context; +Cc: Aditya Mahajan
On Mon, 9 Aug 2021, Hans van der Meer via ntg-context wrote:
> In the TeX to Lua interface with
>
> \def\Macro#1{\directlua{luafunction("#1")}}
>
> the transfer from TeX to Lua is by string. However what to do at the Lua end when calling \Macro{}?
> As I understand it, then on the Lua side an empty argument and an empty string are indistinguishable.
That is not the case:
$ echo 'print("" == nil)' | lua
false
> This is not always wanted, for example when one needs to place either a nil or an empty string into a Lua table. I am running in trouble when to t.key set to \Macro{} should become nil or when it should become "".
If you call luafunction(""), the string is empty and not nil.
\startluacode
userdata = userdata or {}
function userdata.macro(str)
if str == nil then
context("Argument is nil")
elseif str == "" then
context("Argument is empty")
else
context("Argument is %s", str)
end
end
\stopluacode
\define[1]\Macro{\ctxlua{userdata.macro([==[#1]==])}}
\starttext
\startlines
\Macro{}
\Macro{ABC}
\ctxlua{userdata.macro()}
\stoplines
\stoptext
Aditya
___________________________________________________________________________________
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nil from TeX to Lua
2021-08-09 20:05 ` Aditya Mahajan via ntg-context
@ 2021-08-10 7:23 ` Taco Hoekwater via ntg-context
2021-08-10 7:59 ` Hans Hagen via ntg-context
0 siblings, 1 reply; 4+ messages in thread
From: Taco Hoekwater via ntg-context @ 2021-08-10 7:23 UTC (permalink / raw)
To: mailing list for ConTeXt users; +Cc: Taco Hoekwater
> On 9 Aug 2021, at 22:05, Aditya Mahajan via ntg-context <ntg-context@ntg.nl> wrote:
>
> On Mon, 9 Aug 2021, Hans van der Meer via ntg-context wrote:
>
>> In the TeX to Lua interface with
>>
>> \def\Macro#1{\directlua{luafunction("#1")}}
>>
>> the transfer from TeX to Lua is by string. However what to do at the Lua end when calling \Macro{}?
>> As I understand it, then on the Lua side an empty argument and an empty string are indistinguishable.
>
On the TeX side, these are always indistinguishable (as TeX doesn’t have ’strings’) so depending
on the called macro, you could just assume str == "” actually means nil?
But a clearer example of where you run into trouble would be helpful.
Best wishes,
Taco
—
Taco Hoekwater E: taco@bittext.nl
genderfluid (all pronouns)
___________________________________________________________________________________
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nil from TeX to Lua
2021-08-10 7:23 ` Taco Hoekwater via ntg-context
@ 2021-08-10 7:59 ` Hans Hagen via ntg-context
0 siblings, 0 replies; 4+ messages in thread
From: Hans Hagen via ntg-context @ 2021-08-10 7:59 UTC (permalink / raw)
To: mailing list for ConTeXt users; +Cc: Hans Hagen, Taco Hoekwater
On 8/10/2021 9:23 AM, Taco Hoekwater via ntg-context wrote:
>
>
>> On 9 Aug 2021, at 22:05, Aditya Mahajan via ntg-context <ntg-context@ntg.nl> wrote:
>>
>> On Mon, 9 Aug 2021, Hans van der Meer via ntg-context wrote:
>>
>>> In the TeX to Lua interface with
>>>
>>> \def\Macro#1{\directlua{luafunction("#1")}}
>>>
>>> the transfer from TeX to Lua is by string. However what to do at the Lua end when calling \Macro{}?
>>> As I understand it, then on the Lua side an empty argument and an empty string are indistinguishable.
>>
>
> On the TeX side, these are always indistinguishable (as TeX doesn’t have ’strings’) so depending
> on the called macro, you could just assume str == "” actually means nil?
indeed, just check for ""
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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-08-10 7:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-09 19:58 nil from TeX to Lua Hans van der Meer via ntg-context
2021-08-09 20:05 ` Aditya Mahajan via ntg-context
2021-08-10 7:23 ` Taco Hoekwater via ntg-context
2021-08-10 7:59 ` Hans Hagen via ntg-context
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).