From b06a818e78d1a26c34f2c87f607e78ad156e4921 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 13 Feb 2020 21:44:22 +0800 Subject: update readme and rockspec. --- src/MoonPlus.h | 125 ------------------------------------------- src/Moonscript.h | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/StackTracePlus.h | 4 +- src/moonp.cpp | 2 +- 4 files changed, 148 insertions(+), 129 deletions(-) delete mode 100644 src/MoonPlus.h create mode 100644 src/Moonscript.h (limited to 'src') diff --git a/src/MoonPlus.h b/src/MoonPlus.h deleted file mode 100644 index da5f3ac..0000000 --- a/src/MoonPlus.h +++ /dev/null @@ -1,125 +0,0 @@ -R"moonscript_codes( -import "moonp" -import concat, insert, remove from table -unpack = unpack or table.unpack -lua = :loadstring, :load - -local * - -dirsep = "/" - -moonp.moon_compiled = {} - -split = (str, delim) -> - return {} if str == "" - str ..= delim - [m for m in str\gmatch("(.-)"..delim)] - -get_options = (...) -> - count = select "#", ... - opts = select count, ... - if type(opts) == "table" - opts, unpack {...}, nil, count - 1 - else - {}, ... - --- create moon path package from lua package path -create_moonpath = (package_path) -> - moonpaths = for path in *split package_path, ";" - prefix = path\match "^(.-)%.lua$" - continue unless prefix - prefix .. ".moon" - concat moonpaths, ";" - -moon_loader = (name) -> - name_path = name\gsub "%.", dirsep - - local file, file_path - for path in package.moonpath\gmatch "[^;]+" - file_path = path\gsub "?", name_path - file = io.open file_path - break if file - - if file - text = file\read "*a" - file\close! - res, err = loadstring text, "@#{file_path}" - if not res - error file_path .. ": " .. err - - return res - - return nil, "Could not find moon file" - - -loadstring = (...) -> - options, str, chunk_name, mode, env = get_options ... - chunk_name or= "=(moonscript.loadstring)" - - code, err = moonp.to_lua str, options - unless code - return nil, err - - moonp.moon_compiled[chunk_name] = code if chunk_name - -- the unpack prevents us from passing nil - (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } - -loadfile = (fname, ...) -> - file, err = io.open fname - return nil, err unless file - text = assert file\read "*a" - file\close! - loadstring text, "@#{fname}", ... - --- throws errros -dofile = (...) -> - f = assert loadfile ... - f! - -insert_loader = (pos=2) -> - if not package.moonpath - package.moonpath = create_moonpath package.path - - 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 - -moon_require = (name)-> - insert_loader! - xpcall (-> require name), (err)-> - msg = moonp.stp.stacktrace err, 2 - print msg - -setmetatable moonp, { - __index: (key)=> - return nil unless key == "stp" - stp = rawget moonp,"stp" - unless stp - stp = with moonp.load_stacktraceplus! - .dump_locals = false - .simplified = true - rawset moonp,"stp",stp - rawset moonp,"load_stacktraceplus",nil - stp - __call: (name)=> @.require name -} - -moonp[k] = v for k,v in pairs { - :insert_loader, :remove_loader, :moon_loader, :dirsep, - :dofile, :loadfile, :loadstring, :create_moonpath, - require:moon_require -} -)moonscript_codes"; diff --git a/src/Moonscript.h b/src/Moonscript.h new file mode 100644 index 0000000..6e84552 --- /dev/null +++ b/src/Moonscript.h @@ -0,0 +1,146 @@ +R"moonscript_codes( +--[[ +Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.]] + +import "moonp" +import concat, insert, remove from table +unpack = unpack or table.unpack +lua = :loadstring, :load + +local * + +dirsep = "/" + +moonp.moon_compiled = {} + +split = (str, delim) -> + return {} if str == "" + str ..= delim + [m for m in str\gmatch("(.-)"..delim)] + +get_options = (...) -> + count = select "#", ... + opts = select count, ... + if type(opts) == "table" + opts, unpack {...}, nil, count - 1 + else + {}, ... + +-- create moon path package from lua package path +create_moonpath = (package_path) -> + moonpaths = for path in *split package_path, ";" + prefix = path\match "^(.-)%.lua$" + continue unless prefix + prefix .. ".moon" + concat moonpaths, ";" + +moon_loader = (name) -> + name_path = name\gsub "%.", dirsep + + local file, file_path + for path in package.moonpath\gmatch "[^;]+" + file_path = path\gsub "?", name_path + file = io.open file_path + break if file + + if file + text = file\read "*a" + file\close! + res, err = loadstring text, "@#{file_path}" + if not res + error file_path .. ": " .. err + + return res + + return nil, "Could not find moon file" + + +loadstring = (...) -> + options, str, chunk_name, mode, env = get_options ... + chunk_name or= "=(moonscript.loadstring)" + + code, err = moonp.to_lua str, options + unless code + return nil, err + + moonp.moon_compiled[chunk_name] = code if chunk_name + -- the unpack prevents us from passing nil + (lua.loadstring or lua.load) code, chunk_name, unpack { mode, env } + +loadfile = (fname, ...) -> + file, err = io.open fname + return nil, err unless file + text = assert file\read "*a" + file\close! + loadstring text, "@#{fname}", ... + +-- throws errros +dofile = (...) -> + f = assert loadfile ... + f! + +insert_loader = (pos=2) -> + if not package.moonpath + package.moonpath = create_moonpath package.path + + 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 + +moon_require = (name)-> + insert_loader! + xpcall (-> require name), (err)-> + msg = moonp.stp.stacktrace err, 2 + print msg + +setmetatable moonp, { + __index: (key)=> + return nil unless key == "stp" + stp = rawget moonp,"stp" + unless stp + stp = with moonp.load_stacktraceplus! + .dump_locals = false + .simplified = true + rawset moonp,"stp",stp + rawset moonp,"load_stacktraceplus",nil + stp + __call: (name)=> @.require name +} + +moonp[k] = v for k,v in pairs { + :insert_loader, :remove_loader, :moon_loader, :dirsep, + :dofile, :loadfile, :loadstring, :create_moonpath, + require:moon_require +} +)moonscript_codes"; diff --git a/src/StackTracePlus.h b/src/StackTracePlus.h index 1a909fd..202ae22 100644 --- a/src/StackTracePlus.h +++ b/src/StackTracePlus.h @@ -1,8 +1,6 @@ R"lua_codes( --[[ -The MIT License - -Copyright (c) 2010 Ignacio Burgueño +Copyright (c) 2010 Ignacio Burgueño, modified by Li Jin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/moonp.cpp b/src/moonp.cpp index a6c99df..cb53b55 100644 --- a/src/moonp.cpp +++ b/src/moonp.cpp @@ -182,7 +182,7 @@ extern "C" { #include "lauxlib.h" static const char moonplusCodes[] = -#include "MoonPlus.h" +#include "Moonscript.h" static int init_moonplus(lua_State* L) { MoonP::MoonConfig config; -- cgit v1.2.3-55-g6feb