Permalink
Browse files

update load functions for 5.2

  • Loading branch information...
leafo committed Jan 3, 2013
1 parent 3ec2f18 commit cbf624d0f6f5033ad4bdb7a5eff5748cc4712be8
Showing with 25 additions and 22 deletions.
  1. +1 −1 moonscript.lua
  2. +13 −14 moonscript/init.lua
  3. +11 −7 moonscript/init.moon
View
@@ -1 +1 @@
-require "moonscript.init"
+return require "moonscript.init"
View
@@ -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
@@ -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!
{

0 comments on commit cbf624d

Please sign in to comment.