From 99437bfd4c439887c6b1865c7b376761bf2a8bce Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Oct 2013 11:30:15 -0700 Subject: [PATCH] add remove_loader function --- moonscript/base.lua | 32 +++++++++++++++++++++++++++----- moonscript/base.moon | 25 ++++++++++++++++++++----- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/moonscript/base.lua b/moonscript/base.lua index 7d074c0..d3f83c2 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -1,9 +1,9 @@ local compile = require("moonscript.compile") local parse = require("moonscript.parse") -local concat, insert +local concat, insert, remove do 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 local split, dump, get_options, unpack do @@ -14,7 +14,7 @@ local lua = { loadstring = loadstring, 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 = "/" line_tables = require("moonscript.line_tables") create_moonpath = function(package_path) @@ -93,15 +93,37 @@ dofile = function(...) local f = assert(loadfile(...)) return f() end -insert_loader = function() +insert_loader = function(pos) + if pos == nil then + pos = 2 + end if not package.moonpath then package.moonpath = create_moonpath(package.path) 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 return { _NAME = "moonscript", insert_loader = insert_loader, + remove_loader = remove_loader, to_lua = to_lua, moon_chunk = moon_chunk, moon_loader = moon_loader, diff --git a/moonscript/base.moon b/moonscript/base.moon index fc22929..4e3e931 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -1,7 +1,7 @@ compile = require "moonscript.compile" 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" lua = :loadstring, :load @@ -75,15 +75,30 @@ dofile = (...) -> f = assert loadfile ... f! -insert_loader = -> +insert_loader = (pos=2) -> if not package.moonpath 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" - :insert_loader, :to_lua, :moon_chunk, :moon_loader, :dirsep, :dofile, - :loadfile, :loadstring + :insert_loader, :remove_loader, :to_lua, :moon_chunk, :moon_loader, :dirsep, + :dofile, :loadfile, :loadstring }