385 posts
Location
San Diego, CA
Posted 27 July 2015 - 01:27 AM
Hi! I am interested in making a ComputerCraft-like-thing-that-is-actually-in-love2d-which-uses-luajit-so-yeah.
How would one make os.loadAPI() in PURE Lua 5.1?
Thanks!
P.S. not sure if this is the right place to ask but I think it is considering this is the place to ask questions :P/>
P.S.S. pretty sure how unloadAPI would work, it's just loadAPI I am curious about
Edited on 26 July 2015 - 11:28 PM
7083 posts
Location
Tasmania (AU)
Posted 27 July 2015 - 01:45 AM
You can find the source in
bios.lua. You'll need an understanding of
environment tables to see how it works.
Edited on 26 July 2015 - 11:46 PM
8543 posts
Posted 27 July 2015 - 01:47 AM
Replace the second line of the function with something like:
local sName = string.match(_sPath, "([^/\\]+)$")
Replace printError calls with error calls.
385 posts
Location
San Diego, CA
Posted 27 July 2015 - 02:28 AM
Here's my code so far.
My Modified LoadAPI Code
function love.load()
function customParse(parseString, parseCharacter)
local parseResults = {}
local stringCycle = ""
for i = 1, #parseString do
local c = parseString:sub(i,i)
if c == parseCharacter then
if stringCycle == "" then
else
table.insert(parseResults, stringCycle)
stringCycle = ""
end
else
stringCycle = stringCycle .. c
end
if i == #parseString and c ~= parseCharacter then
table.insert(parseResults, stringCycle)
stringCycle = ""
end
end
return parseResults
end
tAPIsLoading = {}
function loadAPI(_sPath)
customParsedPath = customParse(_sPath, "/")
local sName = customParsedPath[#customParsedPath]
if tAPIsLoading[sName] == true then
error("API " ..sName.." is already being loaded")
return false
end
tAPIsLoading[sName] = true
local tEnv = {}
setmetatable(tEnv, {__index = _G})
local fnAPI, err = loadfile(_sPath, tEnv)
if fnAPI then
local ok, err = pcall(fnAPI)
if not ok then
error(err)
tAPIsLoading[sName] = nil
return false
end
else
error(err)
tAPIsLoading[sName] = nil
return false
end
local tAPI = {}
for k, v in pairs(tEnv) do
if k ~= "_ENV" then
tAPI[k] = v
end
end
_G[sName] = tAPI
tAPIsLoading[sName] = nil
return true
end
loadAPI("test.lua")
end
function love.draw()
foo()
end
However I get the following message:
"Error: main.lua:37: bad argument #2 to 'loadfile' (string expected, got table)
[c]: main.lua:37: in function 'loadfile'
main.lua:37: in function 'loadAPI'
main.lua:63: in function 'load'
[C]: in function 'xpcall'"
any suggestions?
Yes I am writing this in Love2D.
Edited on 27 July 2015 - 12:54 AM
8543 posts
Posted 27 July 2015 - 04:11 AM
Base it off of the 1.73 version of os.loadAPI, not the 1.74 version.