Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:19:00+00:00 2026-06-05T16:19:00+00:00

Lets say i have at least two lua script files. test1.lua test2.lua both define

  • 0

Lets say i have at least two lua script files.

test1.lua
test2.lua

both define an init function and other functions with similar names.

How can i load each script file using c++/c into a separate environment using Lua 5.2 so that the same function names will not clash – i found a sample code for 5.1 which does not work for me (because setenv is gone and lua_setuservalue does not seem to work)

Sample here Calling lua functions from .lua's using handles?

Basically if i replace setenv with setuservalue – i get an access violation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:19:00+00:00Added an answer on June 5, 2026 at 4:19 pm

    The unofficial Lua FAQ has an entry about sandboxing in Lua.
    My guess is that you can transpose that logic easily enough to your C/C++ code.

    See also LuaFiveTo on the lua-users wiki.

    Correction

    It’s indeed not as trivial as it seemed. But in the end the point is simple: load your chunk, push the _ENV table, use lua_setupvalue(L,-2,1). The important is that the table should be at the top of the stack.

    As a small example, using 2 environments defaulting to _G for reading stuff via metatables:

    #include <lua.h>
    #include <lualib.h>
    #include <lauxlib.h>
    
    int main(void){
            lua_State *L = luaL_newstate();
            char *file1 = "file1.lua";
            char *file2 = "file2.lua";
    
            luaL_openlibs(L);
    
            luaL_loadfile(L,file2); // S: 1
            luaL_loadfile(L,file1); // S: 2
            lua_newtable(L); // ENV for file 1: S: 321
            lua_newtable(L); // ENV for file 2: S: 4321
    
            //lets have each function have its metatable, where missed lookups are
            //instead looked up in the global table _G
    
            lua_newtable(L); // metatable S: 54321
            lua_getglobal(L,"_G"); // pushes _G, which will be the __index metatable entry S: 654321
    
            lua_setfield(L,-2,"__index"); // metatable on top S: 54321
            lua_pushvalue(L,-1); // copy the metatable S: 554321
            lua_setmetatable(L,-3); // set the last copy for env2 S: 54321
            lua_setmetatable(L,-3); // set the original for env1  S: 4321
            // here we end up having 2 tables on the stack for 2 environments
            lua_setupvalue(L,1,1); // first upvalue == _ENV so set it. S: 321
            lua_setupvalue(L,2,1); // set _ENV for file S: 21
            // Remaining on the stack: 2 chunks with env set.
            lua_pcall(L,0,LUA_MULTRET,0);
            lua_pcall(L,0,LUA_MULTRET,0);
            lua_close(L);
            return 0;
    }
    

    And for the 2 Lua files:

    -- file1.lua
    function init()
            A="foo"
            print("Hello from file1")
            print(A)
    end
    init()
    
    -- file2.lua
    -- this shows that stuff defined in file1 will not polute the environment for file2
    print("init function is",tostring(init))
    function init()
            A="bar"
            print("Hello from file2")
            print(A)
    end
    init()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have files: Libs: one.cpp, one.h two.cpp, two.h three.cpp, three.h Program: program.cpp
Lets say we have a database table with two columns, entry_time and value. entry_time
Lets say I have two tables like this: person: id, first_name, last_name, phone_id phone:
Lets say I have two tables as such: People Id Name FirstDate LastDate --
Lets say I have two tables. class CreateUsers < ActiveRecord::Migration def self.up create_table :users
Lets say I have this program below. Now I want to pass both the
Lets say we have a two players game, where one player always wins (there
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have five tables named table1, table2 ... table5. I have already
Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.