The shortest Lua C function (0 bytes)

98 views
Skip to first unread message

Steven

unread,
Jul 17, 2025, 1:49:05 PMJul 17
to lu...@googlegroups.com
Hi everyone. I just stumbled upon this neat trick and wanted to share it.
There is a somewhat useful C function that takes 0 bytes to implement!
What it does is return all its own parameters in order (via multiple return).

To register it:

    lua_pushcfunction(L, lua_gettop);
    lua_setglobal(L, "pass");

To use it:

    print(pass("hi", 123, true, "bye"))

Hope this helps someone!

Dmitry Meyer

unread,
Jul 17, 2025, 2:08:50 PMJul 17
to lu...@googlegroups.com
On 2025-07-17 17:48, Steven wrote:
> What it does is return all its own parameters in order (via multiple
> return).

>     lua_setglobal(L, "pass");

FWIW, this type of function is usually called "id(entity) function". For
me, "pass" sounds more like "noop".

Francisco Olarte

unread,
Jul 17, 2025, 2:11:12 PMJul 17
to lu...@googlegroups.com
On Thu, 17 Jul 2025 at 19:49, Steven <sbde...@gmail.com> wrote:
> Hi everyone. I just stumbled upon this neat trick and wanted to share it.
> There is a somewhat useful C function that takes 0 bytes to implement!
> What it does is return all its own parameters in order (via multiple return).
>
> To register it:
> lua_pushcfunction(L, lua_gettop);
> lua_setglobal(L, "pass");

Global gives me the heebie jeevies. Besides, reference manual, chapter
4, paragraph 2, sentence 1: "Even when we use the term "function", any
facility in the API may be provided as a macro instead."

I must say I have it ( an use it in some code ) as
require("func_utils").ARGS, just done in lua via return ...


Francisco Olarte.

Sainan

unread,
Jul 17, 2025, 2:23:59 PMJul 17
to lu...@googlegroups.com
Well, technically your function is still at least 4 bytes for the OP_CALL, I suggest optimising it like so:

print("hi", 123, true, "bye")

-- Sainan

Roberto Ierusalimschy

unread,
Jul 17, 2025, 2:37:42 PMJul 17
to lu...@googlegroups.com
> Hi everyone. I just stumbled upon this neat trick and wanted to share it.
> There is a somewhat useful C function that takes 0 bytes to implement!
> What it does is return all its own parameters in order (via multiple
> return).
>
> To register it:
>
> lua_pushcfunction(L, lua_gettop);
> lua_setglobal(L, "pass");

Neat :-)

-- Roberto

Steven

unread,
Jul 17, 2025, 2:39:57 PMJul 17
to lu...@googlegroups.com
Sainan: I meant the implementation is 0 bytes. My original email should have said this:

[[

To implement it:

To register it:

    lua_pushcfunction(L, lua_gettop);
    lua_setglobal(L, "pass");

To use it:

    print(id("pass", 123, true, "bye"))

]]

Dmitry: Good call, identity is probably a better name, I just picked pass because it passes its arguments to its return values.

Francisco: Glad to see someone actually *uses* this function at all! It means it actually isn't so trivial after all! And yeah I agree it should not be global generally. This was just a quick example of usage.

On that note, am I missing something, or is there truly no way of automatically finding an luaopen_* function in the current executable? If so, I'd recommend that be a new addition to Lua 5.5. I'll make a new thread about it.

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/v3oFtbVtnfwe3aPU7mR51-7_LzpU7yNXAlRNPSIdR5_SWYMDMKOpwVhTLYte8FprQB4xHJwsqX4GMP5G-y7I4eZZny2vovCytZLwF_lorhQ%3D%40calamity.inc.

云风 Cloud Wu

unread,
Jul 17, 2025, 10:08:53 PMJul 17
to lu...@googlegroups.com
Steven <sbde...@gmail.com> 于2025年7月18日周五 01:49写道:
Interesting :)

I recommend a more practical version :

lua_pushcfunction(L, lua_pushthread);
lua_setglobal(L, "inmainthread");

To use it:

if not inmainthread() then
print "In a coroutine"
end

--
http://blog.codingnow.com

Sainan

unread,
Jul 18, 2025, 1:22:16 AMJul 18
to lu...@googlegroups.com
Also already provided in standard Lua:

print(select(2, coroutine.running())) --> true
print(coroutine.isyieldable()) --> false

coroutine.resume(coroutine.create(function()
print(select(2, coroutine.running())) --> false
print(coroutine.isyieldable()) --> true
end))

-- Sainan

Francisco Olarte

unread,
Jul 18, 2025, 3:00:36 AMJul 18
to lu...@googlegroups.com
On Thu, 17 Jul 2025 at 20:39, Steven <sbde...@gmail.com> wrote:
> Francisco: Glad to see someone actually *uses* this function at all! It means it actually isn't so trivial after all! And yeah I agree it should not be global generally. This was just a quick example of usage.

After many years I've found that having optional hook functions in a
network of objects if error prone and normally slower then just having
the default value of the hook be a simple function. I collect them in
a library, ARGS, ARG1..ARG9, NOP, NIL, TRUE/FALSE/ZERO and many more
are there along with some binders and other things.

One thing, beware of passing the lua-c barrier, it seems to have a
measurable costs. IIRC once I (tried to) overprimized an integer index
table access by using rageti and found it was actually slower than
just using normal indexing.

Francisco Olarte.
Reply all
Reply to author
Forward
0 new messages