neolithos / neolua

A Lua implementation for the Dynamic Language Runtime (DLR).
https://neolua.codeplex.com/
Apache License 2.0
466 stars 76 forks source link

string and math libraries are not tables, so function differently than native lua #146

Closed Whoome closed 1 year ago

Whoome commented 2 years ago

This one is pretty simple, and I was able to work around it fairly easily, but I think it might be best to do this at the library level. A number of scripts that we use add common functions to the string and math library.

For example, this throws an exception by default (but works in standard lua)

math.degrees = function(rads) return 180.0 * rads / math.pi end

Unfortunately due to the way the library is implemented, this throws an exception (math is a LuaType object, not a table)

To work around it, I added this to my initialization routine, which seems to get me back to the behavior I expect. It seems like this might just be the right default approach, I don't -think- it has any performance impact.

//Make the math and string libraries tables.  By default these are just the string/math library objects 
//which breaks any code that replaces or adds functions to these libraries
NewTable("math");
LuaTable math = this["math"] as LuaTable;
math["huge"]  = Neo.IronLua.LuaLibraryMath.huge;
math["pi"]  = Neo.IronLua.LuaLibraryMath.pi;
math["e"]  = Neo.IronLua.LuaLibraryMath.e;
math["mininteger"]  = Neo.IronLua.LuaLibraryMath.mininteger;
math["maxinteger"]  = Neo.IronLua.LuaLibraryMath.maxinteger;

math["abs"]   = new Func<double, double>(Neo.IronLua.LuaLibraryMath.abs);
math["acos"]  = new Func<double, double>(Neo.IronLua.LuaLibraryMath.acos);
math["asin"]  = new Func<double, double>(Neo.IronLua.LuaLibraryMath.asin);
math["atan"]  = new Func<double, double>(Neo.IronLua.LuaLibraryMath.atan);
math["atan2"] = new Func<double, double, double>(Neo.IronLua.LuaLibraryMath.atan2);
math["ceil"]  = new Func<double, double>(Neo.IronLua.LuaLibraryMath.ceil);
math["cos"]   = new Func<double, double>(Neo.IronLua.LuaLibraryMath.cos);
math["cosh"]  = new Func<double, double>(Neo.IronLua.LuaLibraryMath.cosh);
math["deg"]   = new Func<double, double>(Neo.IronLua.LuaLibraryMath.deg);
math["exp"]   = new Func<double, double>(Neo.IronLua.LuaLibraryMath.exp);
math["floor"]   = new Func<double, double>(Neo.IronLua.LuaLibraryMath.floor);
math["fmod"]   = new Func<double, double, double>(Neo.IronLua.LuaLibraryMath.fmod);
math["frexp"]   = new Func<double, Neo.IronLua.LuaResult>(Neo.IronLua.LuaLibraryMath.frexp);
math["ldexp"]   = new Func<double, int, double>(Neo.IronLua.LuaLibraryMath.ldexp);
math["log"]     = new Func<double, double, double>(Neo.IronLua.LuaLibraryMath.log);
math["max"]     = new Func<double [], double>(Neo.IronLua.LuaLibraryMath.max);
math["min"]     = new Func<double [], double>(Neo.IronLua.LuaLibraryMath.min);
math["modf"]     = new Func<double, LuaResult>(Neo.IronLua.LuaLibraryMath.modf);
math["pow"]     = new Func<double, double, double>(Neo.IronLua.LuaLibraryMath.pow);
math["rad"]     = new Func<double, double>(Neo.IronLua.LuaLibraryMath.rad);
math["random"]  = new Func<object, object, object>(Neo.IronLua.LuaLibraryMath.random);
math["randomseed"]     = new Action<object>(Neo.IronLua.LuaLibraryMath.randomseed);
math["sin"]     = new Func<double, double>(Neo.IronLua.LuaLibraryMath.sin);
math["sinh"]     = new Func<double, double>(Neo.IronLua.LuaLibraryMath.sinh);
math["sqrt"]     = new Func<double, double>(Neo.IronLua.LuaLibraryMath.sqrt);
math["tan"]      = new Func<double, double>(Neo.IronLua.LuaLibraryMath.tan);
math["tanh"]      = new Func<double, double>(Neo.IronLua.LuaLibraryMath.tanh);
math["type"]      = new Func<object, string>(Neo.IronLua.LuaLibraryMath.type);
math["tointeger"] = new Func<object, object>(Neo.IronLua.LuaLibraryMath.tointeger);
math["ult"]       = new Func<long, long, bool>(Neo.IronLua.LuaLibraryMath.ult);

NewTable("string");
LuaTable str = this["string"] as LuaTable;
str["byte"]       = new byteDelg(Neo.IronLua.LuaLibraryString.@byte);
str["char"]       = new charDelg(Neo.IronLua.LuaLibraryString.@char);
str["dump"]       = new Func<Delegate, string>(Neo.IronLua.LuaLibraryString.dump);
str["find"]       = new Func<string, string, int, bool, LuaResult>(Neo.IronLua.LuaLibraryString.find);
str["format"]     = new formatDelg(Neo.IronLua.LuaLibraryString.format);
str["gmatch"]     = new Func<string, string, LuaResult>(Neo.IronLua.LuaLibraryString.gmatch);
str["gsub"]       = new Func<string, string, object, int, LuaResult>(Neo.IronLua.LuaLibraryString.gsub);
str["len"]        = new Func<string, int>(Neo.IronLua.LuaLibraryString.len);
str["lower"]      = new Func<string, string>(Neo.IronLua.LuaLibraryString.lower);
str["match"]      = new Func<string, string, int, LuaResult>(Neo.IronLua.LuaLibraryString.match);
str["rep"]        = new Func<string, int, string, string>(Neo.IronLua.LuaLibraryString.rep);
str["reverse"]    = new Func<string, string>(Neo.IronLua.LuaLibraryString.reverse);
str["sub"]        = new Func<string, int, int, string>(Neo.IronLua.LuaLibraryString.sub);
str["upper"]        = new Func<string, string>(Neo.IronLua.LuaLibraryString.upper);
neolithos commented 2 years ago

The std-libraries are still on my todo list. And there is a lot stuff todo. For compatible reasons in our projects is a really old lua-function set and for the new one we use .net.

Currently, from our project site there is not enough pressure.

Pull Requests are welcome...

Whoome commented 2 years ago

I'll try and get something together. I have to figure out how to make GitHub let me put a pull request in lol. It keeps giving me authentication errors.

Whoome commented 2 years ago

I checked in a fix for string/math/table. The rest I have not gotten to yet, but those are probably a bit less modified.

neolithos commented 2 years ago

I wrote a comment on the PR.

neolithos commented 2 years ago

I finally branched the bindconvert code. Can you revisit your pull request?