Coder Social home page Coder Social logo

lua-in-lua's Introduction

Lua in Lua

Questions

  • Why?
    • Simple, because why not.
  • How does this work?
    • It's a tree walker, meaning it's not as efficient as bytecode.
  • Does it support everything from Lua?
    • It should. It might not have a thing or two but almost everything should work just fine.
    • Unsupported things (that I know of):
      • Using = in multiline comments

Usage

local lua = require("lua.main")

local script = [[
print("Hello, World!")
]]

local environment = {
  print = print -- global print _G.print
}

-- If you want, you can pass the _G table as the environment, but I recommend making a special one
lua:run(script, environment)

Can you run Lua in Lua in Lua?

Yes! You can.

local lua = require("lua.main")

-- Custom environment
local env = {
    print = print,
    getmetatable = getmetatable,
    setmetatable = setmetatable,
    type = type,
    pcall = function(f, ...) -- we need a simple pcall and error wrapper if you wanna call Lua in Lua in Lua in Lua and so on... :^)
        local data = {pcall(f, ...)}
        if not data[1] then
            return false, data[2].error_object
        else
            return unpack(data)
        end
    end,
    error = function(object)
        error {error_object=object}
    end,
    pairs = pairs,
    ipairs = ipairs,
    unpack = unpack,
    table = table,
    string = string,
    tostring = tostring,
    tonumber = tonumber,
    math = math
}

-- Custom require function
env._G = env
env.require = function(fpath)
    local fpath = fpath:gsub("%.", "/")
    local f = io.open(fpath .. ".lua", "r")
    local code = f:read("*a")
    f:close()

    local success, ret = pcall(lua.run, lua, code, env)

    if not success then
        error("In file " .. fpath .. ": " .. ret)
    end

    return ret
end

local script = [[
local lua = require("lua.main")
local script = "print('Hello World')"

local env = {
  print = print
}

lua:run(script, env)
]]

lua:run(script, env)

Other

There's a special statement debugdmpenvstack
All this does is it prints out the environment stack starting from the local stack up to the global one
This won't affect any lua scripts and if it does, you can disable it yourself :^) (Its useful for debugging and playing around)

local number = 2000

local function test()
  local message = "Hello World"
  debugdmpenvstack -- dumps the environment stack to the output
end

test()

lua-in-lua's People

Contributors

vladimirdabic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.