Skip to content

Commit

Permalink
More Cobalt support
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Mar 17, 2016
1 parent 2fec561 commit f157fee
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
63 changes: 34 additions & 29 deletions bsrocks/env/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ local function addWithMeta(src, dest)
end
end

return function(options)
options = options or {}

return function()
local nImplemented = function(name)
return function()
error(name .. " is not implemented", 2)
Expand Down Expand Up @@ -75,7 +73,7 @@ return function(options)

-- Customised loadfile function to work with relative files
function _G.loadfile(path)
path = fs.combine(env.dir, path)
path = env.resolve(path)
if fs.exists(path) then
return load(fileWrapper.read(path), path, "t", _G)
else
Expand Down Expand Up @@ -114,36 +112,40 @@ return function(options)
end
return result
end
local function extractError(...)
local success, message = ...
if success then
return ...
else
return false, getError(message)
end
end
env.getError = getError

function _G.error(message, level)
level = level or 1
if level > 0 then level = level + 1 end
local e = {}
if select(2, pcall(error, e)) ~= e then
local function extractError(...)
local success, message = ...
if success then
return ...
else
return false, getError(message)
end
end

if type(message) ~= "string" then
local key = tostring({}) .. tostring(message)
if message == nil then message = nilFiller end
errors[key] = message
error(key, 0)
else
error(message, level)
function _G.error(message, level)
level = level or 1
if level > 0 then level = level + 1 end

if type(message) ~= "string" then
local key = tostring({}) .. tostring(message)
if message == nil then message = nilFiller end
errors[key] = message
error(key, 0)
else
error(message, level)
end
end
end

function _G.pcall(func, ...)
return extractError(pcall(func, ...))
end
function _G.pcall(func, ...)
return extractError(pcall(func, ...))
end

function _G.xpcall(func, handler)
return xpcall(func, function(result) return handler(getError(result)) end)
function _G.xpcall(func, handler)
return xpcall(func, function(result) return handler(getError(result)) end)
end
end

-- Setup other items
Expand All @@ -152,14 +154,17 @@ return function(options)
require "bsrocks.env.io"(env)
require "bsrocks.env.os"(env)

if options.debug ~= false then
if not debug then
require "bsrocks.env.debug"(env)
else
_G.debug = debug
end

require "bsrocks.env.package"(env)

-- Copy functions across
addWithMeta(getfenv(), _G)
_G._NATIVE = getfenv()

return env
end
4 changes: 3 additions & 1 deletion bsrocks/env/io.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ local fileMeta = {
if n == 0 then n = 1 end
for i = 1, n do
local format = data[i] or "l"
format = checkType(format, "string"):gsub("%*", "") -- "*" is not needed after Lua 5.1 - lets be friendly
format = checkType(format, "string"):gsub("%*", ""):sub(1, 1) -- "*" is not needed after Lua 5.1 - lets be friendly
if format == "l" then
returns[#returns + 1] = handle.readLine()
elseif format == "a" then
Expand Down Expand Up @@ -83,6 +83,8 @@ local fileMeta = {

handle.write(tostring(item))
end

return true
end,

lines = function(self, ...)
Expand Down

0 comments on commit f157fee

Please sign in to comment.