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 8165345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:30:55+00:00 2026-06-06T19:30:55+00:00

I am learning from Programing in Lua by Roberto Ierusalimschy, and I found that

  • 0

I am learning from “Programing in Lua” by Roberto Ierusalimschy, and I found that in the book, the example of Sandboxing uses the function setfenv() to change the environment of a given function, but in lua 5.2 this function is no longer available.

I tried to load some values from a file (a configuration file) into a field in a table, but, in lua 5.2 I can’t use setfenv ( so I can load the values in the given environment). After reading some articles about lua 5.2 I found that each function may have (or not) an upvalue called _ENV which serves as the environment, so, I tried the following code:

function sandbox(sb_func, sb_env)
    if not sb_func then return nil, "sandbox function not valid" end
    sb_orig_env = _ENV
    _ENV = sb_env -- yes, replaces the global _ENV
    pcall_res, message = pcall( sb_func )
    local modified_env = _ENV -- gets the environment that was used in the pcall( sb_func )
    _ENV = sb_orig_env
    return true, modified_env
end

function readFile(filename)
    code = loadfile(filename)
    res, table = sandbox(code, {})
    if res then
        --[[ Use table (modified_env) ]]--
    else
        print("Code not valid")
end

Replacing _ENV in the ‘sandbox’ function works well (can’t access the regular fields), but, when the ‘code’ is executed it seems that it ignores that I replaced _ENV, it still can access regular fields (print, loadfile, dofile, etc).

Reading a little more, I found that lua 5.2 provides a function for this purpose, this function is loadin(env, chunk), which runs the given chunk in the given environment, but, when I try to add this function to my code, the function doesn’t exist ( Is not present in the global _G field).

Some help will be appreciated.

  • 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-06T19:30:57+00:00Added an answer on June 6, 2026 at 7:30 pm

    When you assign to _ENV from within sandbox, you’re not overwriting the global environment–you’re replacing the _ENV upvalue of the currently running code. Adding calls to print(_ENV) may help you better understand the identities of the tables involved.

    For example:

    function print_env()
      print(_ENV)
    end
    
    function sandbox()
      print(_ENV) -- prints: "table: 0x100100610"
      -- need to keep access to a few globals:
      _ENV = { print = print, print_env = print_env, debug = debug, load = load }
      print(_ENV) -- prints: "table: 0x100105140"
      print_env() -- prints: "table: 0x100105140"
      local code1 = load('print(_ENV)')
      code1()     -- prints: "table: 0x100100610"
      debug.setupvalue(code1, 1, _ENV) -- set our modified env
      code1()     -- prints: "table: 0x100105140"
      local code2 = load('print(_ENV)', nil, nil, _ENV) -- pass 'env' arg
      code2()     -- prints: "table: 0x100105140"
    end
    

    The loadin function was present in some pre-release versions of Lua 5.2 but was removed before the final release. Instead, the Lua 5.2 load and loadfile functions take an env argument. You can also modify the _ENV of another function using debug.setupvalue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning DirectX from a book about game programming, and it uses the following
While learning iOS programming from a book, I notice that for long lines of
I have been reading from the book The C Programming Language learning C, and
in the book i'm learning from i came across this code snippit: while (i
I am new at ASP.net and VB.net. I am learning from the book Beginning
I am learning PHP (no programming experience) from a book. The examples in the
I jut recently started learning PHP from a book called PHP/MySQL Programming for the
Presently, i am learning Java from the book The Art and Science of Java
I'm a newbie to Rails and programming in general (currently learning from a book
I'm currently learning c++ with the book Programming: Principles and Practice Using C++ from

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.