update load functions for 5.2

This commit is contained in:
leaf corcoran 2013-01-02 17:23:58 -08:00
parent 3ec2f18b6b
commit cbf624d0f6
3 changed files with 25 additions and 22 deletions

View File

@ -1 +1 @@
require "moonscript.init"
return require "moonscript.init"

View File

@ -1,10 +1,10 @@
local compile = require("moonscript.compile")
local parse = require("moonscript.parse")
local concat, insert = table.concat, table.insert
local split, dump
local split, dump, get_options, unpack
do
local _table_0 = require("moonscript.util")
split, dump = _table_0.split, _table_0.dump
split, dump, get_options, unpack = _table_0.split, _table_0.dump, _table_0.get_options, _table_0.unpack
end
local lua = {
loadstring = loadstring
@ -73,10 +73,9 @@ if not (_G.moon_no_loader) then
init_loader()
end
local loadstring
loadstring = function(str, chunk_name, options)
if options == nil then
options = nil
end
loadstring = function(...)
local options, str, chunk_name, mode, env = get_options(...)
chunk_name = chunk_name or "=(moonscript.loadstring)"
local passed, code, ltable = pcall(function()
return to_lua(str, options)
end)
@ -86,24 +85,24 @@ loadstring = function(str, chunk_name, options)
if chunk_name then
line_tables[chunk_name] = ltable
end
return lua.loadstring(code, chunk_name or "=(moonscript.loadstring)")
return (lua.loadstring or lua.load)(code, chunk_name, unpack({
mode,
env
}))
end
local loadfile
loadfile = function(fname, options)
if options == nil then
options = nil
end
loadfile = function(fname, ...)
local file, err = io.open(fname)
if not file then
return nil, err
end
local text = assert(file:read("*a"))
file:close()
return loadstring(text, fname, options)
return loadstring(text, fname, ...)
end
local dofile
dofile = function(fname, options)
local f = assert(loadfile(fname))
dofile = function(...)
local f = assert(loadfile(...))
return f()
end
return {

View File

@ -3,7 +3,7 @@ compile = require "moonscript.compile"
parse = require "moonscript.parse"
import concat, insert from table
import split, dump from require "moonscript.util"
import split, dump, get_options, unpack from require "moonscript.util"
lua = :loadstring
@ -57,24 +57,28 @@ init_loader = ->
init_loader! unless _G.moon_no_loader
loadstring = (str, chunk_name, options=nil) ->
loadstring = (...) ->
options, str, chunk_name, mode, env = get_options ...
chunk_name or= "=(moonscript.loadstring)"
passed, code, ltable = pcall -> to_lua str, options
if not passed
error chunk_name .. ": " .. code, 2
line_tables[chunk_name] = ltable if chunk_name
lua.loadstring code, chunk_name or "=(moonscript.loadstring)"
-- the unpack prevents us from passing nil
(lua.loadstring or lua.load) code, chunk_name, unpack { mode, env }
loadfile = (fname, options=nil) ->
loadfile = (fname, ...) ->
file, err = io.open fname
return nil, err if not file
text = assert file\read "*a"
file\close!
loadstring text, fname, options
loadstring text, fname, ...
-- throws errros
dofile = (fname, options) ->
f = assert loadfile fname
dofile = (...) ->
f = assert loadfile ...
f!
{