add remove_loader function

This commit is contained in:
leaf corcoran 2013-10-06 11:30:15 -07:00
parent 0f4f01d000
commit 99437bfd4c
2 changed files with 47 additions and 10 deletions

View File

@ -1,9 +1,9 @@
local compile = require("moonscript.compile") local compile = require("moonscript.compile")
local parse = require("moonscript.parse") local parse = require("moonscript.parse")
local concat, insert local concat, insert, remove
do do
local _obj_0 = table local _obj_0 = table
concat, insert = _obj_0.concat, _obj_0.insert concat, insert, remove = _obj_0.concat, _obj_0.insert, _obj_0.remove
end end
local split, dump, get_options, unpack local split, dump, get_options, unpack
do do
@ -14,7 +14,7 @@ local lua = {
loadstring = loadstring, loadstring = loadstring,
load = load load = load
} }
local dirsep, line_tables, create_moonpath, to_lua, moon_loader, loadstring, loadfile, dofile, insert_loader local dirsep, line_tables, create_moonpath, to_lua, moon_loader, loadstring, loadfile, dofile, insert_loader, remove_loader
dirsep = "/" dirsep = "/"
line_tables = require("moonscript.line_tables") line_tables = require("moonscript.line_tables")
create_moonpath = function(package_path) create_moonpath = function(package_path)
@ -93,15 +93,37 @@ dofile = function(...)
local f = assert(loadfile(...)) local f = assert(loadfile(...))
return f() return f()
end end
insert_loader = function() insert_loader = function(pos)
if pos == nil then
pos = 2
end
if not package.moonpath then if not package.moonpath then
package.moonpath = create_moonpath(package.path) package.moonpath = create_moonpath(package.path)
end end
return insert(package.loaders or package.searchers, 2, moon_loader) local loaders = package.loaders or package.searchers
for _index_0 = 1, #loaders do
local loader = loaders[_index_0]
if loader == moon_loader then
return false
end
end
insert(loaders, pos, moon_loader)
return true
end
remove_loader = function()
local loaders = package.loaders or package.searchers
for i, loader in ipairs(loaders) do
if loader == moon_loader then
remove(loaders, i)
return true
end
end
return false
end end
return { return {
_NAME = "moonscript", _NAME = "moonscript",
insert_loader = insert_loader, insert_loader = insert_loader,
remove_loader = remove_loader,
to_lua = to_lua, to_lua = to_lua,
moon_chunk = moon_chunk, moon_chunk = moon_chunk,
moon_loader = moon_loader, moon_loader = moon_loader,

View File

@ -1,7 +1,7 @@
compile = require "moonscript.compile" compile = require "moonscript.compile"
parse = require "moonscript.parse" parse = require "moonscript.parse"
import concat, insert from table import concat, insert, remove from table
import split, dump, get_options, unpack from require "moonscript.util" import split, dump, get_options, unpack from require "moonscript.util"
lua = :loadstring, :load lua = :loadstring, :load
@ -75,15 +75,30 @@ dofile = (...) ->
f = assert loadfile ... f = assert loadfile ...
f! f!
insert_loader = -> insert_loader = (pos=2) ->
if not package.moonpath if not package.moonpath
package.moonpath = create_moonpath package.path package.moonpath = create_moonpath package.path
insert package.loaders or package.searchers, 2, moon_loader loaders = package.loaders or package.searchers
for loader in *loaders
return false if loader == moon_loader
insert loaders, pos, moon_loader
true
remove_loader = ->
loaders = package.loaders or package.searchers
for i, loader in ipairs loaders
if loader == moon_loader
remove loaders, i
return true
false
{ {
_NAME: "moonscript" _NAME: "moonscript"
:insert_loader, :to_lua, :moon_chunk, :moon_loader, :dirsep, :dofile, :insert_loader, :remove_loader, :to_lua, :moon_chunk, :moon_loader, :dirsep,
:loadfile, :loadstring :dofile, :loadfile, :loadstring
} }