ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Aditya Mahajan via ntg-context <ntg-context@ntg.nl>
To: Hans van der Meer via ntg-context <ntg-context@ntg.nl>
Cc: Aditya Mahajan <adityam@umich.edu>
Subject: Re: nil from TeX to Lua
Date: Mon, 9 Aug 2021 16:05:46 -0400 (EDT)	[thread overview]
Message-ID: <nycvar.YAK.7.78.908.2108091600320.772146@nqv-guvaxcnq> (raw)
In-Reply-To: <C6D68B1B-C07B-44C0-9828-5E4B4069E148@ziggo.nl>

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
___________________________________________________________________________________

  reply	other threads:[~2021-08-09 20:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-09 19:58 Hans van der Meer via ntg-context
2021-08-09 20:05 ` Aditya Mahajan via ntg-context [this message]
2021-08-10  7:23   ` Taco Hoekwater via ntg-context
2021-08-10  7:59     ` Hans Hagen via ntg-context

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=nycvar.YAK.7.78.908.2108091600320.772146@nqv-guvaxcnq \
    --to=ntg-context@ntg.nl \
    --cc=adityam@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).