Can you receive and renew Luajit accompanying materials from anywhere in C?

I am trying to come up with a solution for getting a Luajit coroutine from a C function that immediately creates a breakpoint to be processed in another OS thread.

According to various Lua docs, and everything has become very contradictory, is this not entirely possible? The documentation is not very clear, and they do not explain the reasoning.

Lua 5.1 claims that every every coroutine had a stack. However, there was only one global stack C. I am not quite sure why this is an obstacle.

Lua 5.2 seems to fix this with lua_pcallk and lua_yieldk. But the explanations are very confusing.

But none of these states is used by the VM that I am using ... which is LuaJIT 2.0.4 and LuaJIT 2.1.0.

A google search told me that Luajit 1.x had CoCo implemented, which apparently used real C-stacks for every lua thread (coroutine). That allows you to get a crop from anywhere.

And only one search led me to the fact that, obviously, LuaJIT 2.x does not implement Coco, since each coroutine uses the C stack.

Can someone please tell me what is the problem with getting coroutines from C? And check if I can safely give / renew the luajit 2.x accompanying file from c?

+5
source share
1 answer

In the Lua implementation of the link, Lua coroutine has its own Lua stack, which is just an array inside lua_State and has nothing to do with the C stack. Lua cannot save the C stack (because this is not possible in the C standard), therefore it cannot provide a coroutine if function C is currently running.

For example, if you have a Lua a function that calls the C b function that calls the Lua c function, and c tries to execute, Lua will not be able to save the local variables for b (since it is a C function) and it will fail.

This also applies to many of Lua's built-in functions. As you pointed out in Lua 5.1, the implementation did not support the assignment in pcall until Lua 5.2 apparently added special functions to make it work.

Coco is a patch for the standard Lua implementation, which implements individual C-stacks in coroutines, so Lua can now “save” the C function variables. Obviously, LuaJit 1.x also includes it. This does not apply to LuaJit 2.x because it is a completely different implementation of Lua.


LuaJit 2.x has the following paragraph on the Extensions page:

Fully Revolving VM

LuaJIT VM is fully renewable. This means that you can get from coroutines even in contexts where this is not possible using the standard Lua 5.1 VM: for example. you can get through pcall () and xpcall (), through iterators and metamotodes.

Thus, it is obvious that the execution of built-in functions should work, although it is still ambiguous if it is applied to arbitrary functions of the Lua C API. However, it is easy to verify; write a simple C API function that takes a Lua function and calls it, and then passes it a function that gives. If it does not work, it should throw an error.

Note that the simple C functions loaded by FFI cannot generally concern the state of Lua. This includes attempts to yield.

+3
source

Source: https://habr.com/ru/post/1259983/


All Articles