2

A Lua script is using one of my C defined functions as bellow:

function lua_func()

    local var = 5

    -- Do some stuff here, possibly using var.

    c_func(var)

    -- Do other stuff here, that must not use var.

end

This C function takes an argument that the caller has created and does what it needs.

This argument to the C function must be of single-use, i.e. after the C function has used it, I don't want it to be accessible any more to the rest of the Lua script.

I am looking for a way for the C function to "consume" this argument. To use it, and then set it to nil, so it is no longer usable.

Is this possible, and if so how?

5
  • I don’t know lua but does it support scopes? If yes then just create a scope around where var should be used. Commented Apr 2, 2019 at 10:18
  • The Lua script is not in my control. It is intended for the users to modify the system's behavior. So I am looking for a solution in C, that will "invalidate" the variable immediately after use. Commented Apr 2, 2019 at 10:21
  • The scope of var is controlled by the lua script, there is nothing you can do if you can’t change the lua script. The C function will always get a copy of the variable, in C everything is pass by value. Commented Apr 2, 2019 at 10:26
  • You could send in a pointer to var and then set var to some invalid value, like -1 or something that makes sense in your context. Commented Apr 2, 2019 at 10:27
  • Lua is pass-by-value, too. Isn't it the value 5 that you don't want used again? If not, there is no requirement that a function argument has anything to do with a variable. Syntactically, an argument is an expression. Commented Apr 5, 2019 at 1:20

1 Answer 1

3

Variant 1:

function lua_func()

    do
        local var = 5

        -- Do some stuff here, possibly using var.

        c_func(var)
    end

    -- Do other stuff here, that must not use var.

end

Variant 2:

function lua_func()

    local var_container = {5}

    -- Do some stuff here, possibly using var.

    c_func(var_container)  -- it assigns nil to var_container[1] before exit

    -- Do other stuff here, that must not use var.

end

Variant 3:

function lua_func()

    local var = 5
    local destructor = function() var = nil end

    -- Do some stuff here, possibly using var.

    c_func(var, destructor)  -- it invokes destructor() before exit

    -- Do other stuff here, that must not use var.

end
Sign up to request clarification or add additional context in comments.

3 Comments

Skriptunoff Variant 2 seems the most appealing for my case. Would it work in case var is a lightuserdata? If so can you please provide me with a C example on how C can set this lightuserdata to NULL?
I mean by directly passing a lightuserdata variable, not wrapping it in a table.
@FotisPanagiotopoulos - Variant 2 means you must wrap your value into a table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.