From 79a408cafd760c0c5f19a44868e897fe1d09cdc1 Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 14:05:11 +0900 Subject: [PATCH] =?UTF-8?q?=E5=AE=A3=E8=A8=80=E7=9A=84=20API=20=E3=82=AB?= =?UTF-8?q?=E3=82=BF=E3=83=AD=E3=82=B0=E5=9F=BA=E7=9B=A4=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=97=E7=94=9F=E6=88=90=E5=99=A8=E3=82=92=E6=8E=A5?= =?UTF-8?q?=E7=B6=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Runtime/Api/LuaApiAttributes.cs | 38 +++++++ LuaScript.Runtime/Api/LuaApiCatalog.cs | 53 ++++++++++ LuaScript.Runtime/Api/LuaCompletionSource.cs | 78 ++++++++++++++ LuaScript.Runtime/Api/LuaStandardLibrary.cs | 101 +++++++++++++++++++ LuaScript.Runtime/LuaScript.Runtime.csproj | 3 +- LuaScript.slnx | 1 + 6 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 LuaScript.Runtime/Api/LuaApiAttributes.cs create mode 100644 LuaScript.Runtime/Api/LuaApiCatalog.cs create mode 100644 LuaScript.Runtime/Api/LuaCompletionSource.cs create mode 100644 LuaScript.Runtime/Api/LuaStandardLibrary.cs diff --git a/LuaScript.Runtime/Api/LuaApiAttributes.cs b/LuaScript.Runtime/Api/LuaApiAttributes.cs new file mode 100644 index 0000000..348e52f --- /dev/null +++ b/LuaScript.Runtime/Api/LuaApiAttributes.cs @@ -0,0 +1,38 @@ +namespace LuaScript.Api +{ + [AttributeUsage(AttributeTargets.Class, Inherited = false)] + internal sealed class LuaTableAttribute(string name) : Attribute + { + public string Name { get; } = name; + } + + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)] + internal sealed class LuaFunctionAttribute(string name, params string[] parameters) : Attribute + { + public string Name { get; } = name; + public string[] Parameters { get; } = parameters; + public string Table { get; init; } = string.Empty; + } + + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false)] + internal sealed class LuaConstantAttribute(string name) : Attribute + { + public string Name { get; } = name; + } + + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly | AttributeTargets.Method, Inherited = false, AllowMultiple = true)] + internal sealed class LuaVariableAttribute(string name) : Attribute + { + public string Name { get; } = name; + public string Table { get; init; } = string.Empty; + public bool InCatalog { get; init; } = true; + } + + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)] + internal sealed class LuaBuiltinAttribute(string name, params string[] parameters) : Attribute + { + public string Name { get; } = name; + public string[] Parameters { get; } = parameters; + public string Table { get; init; } = string.Empty; + } +} diff --git a/LuaScript.Runtime/Api/LuaApiCatalog.cs b/LuaScript.Runtime/Api/LuaApiCatalog.cs new file mode 100644 index 0000000..999cefb --- /dev/null +++ b/LuaScript.Runtime/Api/LuaApiCatalog.cs @@ -0,0 +1,53 @@ +namespace LuaScript.Api +{ + internal enum LuaApiMemberKind + { + Function, + Variable, + } + + internal sealed record LuaApiMember( + string Table, + string Name, + LuaApiMemberKind Kind, + IReadOnlyList Parameters) + { + public string QualifiedName => Table.Length == 0 ? Name : $"{Table}.{Name}"; + } + + internal static class LuaApiCatalog + { + private static readonly object s_gate = new(); + private static readonly List s_members = []; + private static LuaApiMember[]? s_snapshot; + private static int s_version; + + internal static int Version + { + get + { + lock (s_gate) + return s_version; + } + } + + internal static void Register(IReadOnlyList members) + { + lock (s_gate) + { + s_members.AddRange(members); + s_snapshot = null; + s_version++; + } + } + + internal static IReadOnlyList Members + { + get + { + lock (s_gate) + return s_snapshot ??= [.. s_members]; + } + } + } +} diff --git a/LuaScript.Runtime/Api/LuaCompletionSource.cs b/LuaScript.Runtime/Api/LuaCompletionSource.cs new file mode 100644 index 0000000..c18cfc8 --- /dev/null +++ b/LuaScript.Runtime/Api/LuaCompletionSource.cs @@ -0,0 +1,78 @@ +namespace LuaScript.Api +{ + internal static class LuaCompletionSource + { + internal static readonly string[] Keywords = + [ + "and", "break", "do", "else", "elseif", "end", "false", "for", + "function", "goto", "if", "in", "local", "nil", "not", "or", + "repeat", "return", "then", "true", "until", "while" + ]; + + private sealed record Snapshot( + int Version, + string[] StaticCandidates, + IReadOnlyDictionary TableMembers); + + private static Snapshot? s_snapshot; + + internal static string[] StaticCandidates => Current().StaticCandidates; + + internal static bool TryGetTableMembers(string table, out string[] members) + { + if (Current().TableMembers.TryGetValue(table, out var found)) + { + members = found; + return true; + } + members = []; + return false; + } + + private static Snapshot Current() + { + var snapshot = s_snapshot; + int version = LuaApiCatalog.Version; + if (snapshot is null || snapshot.Version != version) + s_snapshot = snapshot = Build(version); + return snapshot; + } + + private static Snapshot Build(int version) + { + var members = LuaApiCatalog.Members; + + var tableMembers = new Dictionary>(StringComparer.Ordinal); + var globals = new List(); + foreach (var member in members) + { + if (member.Table.Length == 0) + { + globals.Add(member.Name); + continue; + } + if (!tableMembers.TryGetValue(member.Table, out var list)) + { + list = []; + tableMembers[member.Table] = list; + } + list.Add(member.QualifiedName); + } + + var candidates = Keywords + .Concat(globals) + .Concat(tableMembers.Keys) + .Concat(tableMembers.Values.SelectMany(x => x)) + .Distinct() + .OrderBy(x => x.Length) + .ThenBy(x => x, StringComparer.OrdinalIgnoreCase) + .ToArray(); + + var frozen = new Dictionary(StringComparer.Ordinal); + foreach (var (table, list) in tableMembers) + frozen[table] = [.. list.Distinct()]; + + return new Snapshot(version, candidates, frozen); + } + } +} diff --git a/LuaScript.Runtime/Api/LuaStandardLibrary.cs b/LuaScript.Runtime/Api/LuaStandardLibrary.cs new file mode 100644 index 0000000..a6a45bc --- /dev/null +++ b/LuaScript.Runtime/Api/LuaStandardLibrary.cs @@ -0,0 +1,101 @@ +namespace LuaScript.Api +{ + [LuaTable("math")] + [LuaBuiltin("abs", "x")] + [LuaBuiltin("ceil", "x")] + [LuaBuiltin("cos", "x")] + [LuaBuiltin("exp", "x")] + [LuaBuiltin("floor", "x")] + [LuaBuiltin("fmod", "x", "y")] + [LuaVariable("huge")] + [LuaBuiltin("log", "x", "base")] + [LuaBuiltin("max", "x", "...")] + [LuaBuiltin("min", "x", "...")] + [LuaBuiltin("modf", "x")] + [LuaVariable("pi")] + [LuaBuiltin("random", "m", "n")] + [LuaBuiltin("randomseed", "x")] + [LuaBuiltin("sin", "x")] + [LuaBuiltin("sqrt", "x")] + [LuaBuiltin("tan", "x")] + [LuaBuiltin("atan", "y", "x")] + [LuaBuiltin("atan2", "y", "x")] + [LuaBuiltin("deg", "x")] + [LuaBuiltin("rad", "x")] + [LuaBuiltin("ldexp", "m", "e")] + [LuaBuiltin("frexp", "x")] + [LuaBuiltin("sinh", "x")] + [LuaBuiltin("cosh", "x")] + [LuaBuiltin("tanh", "x")] + [LuaBuiltin("type", "x")] + [LuaBuiltin("tointeger", "x")] + [LuaVariable("maxinteger")] + [LuaVariable("mininteger")] + internal static class LuaMathLibrary; + + [LuaTable("string")] + [LuaBuiltin("format", "formatstring", "...")] + [LuaBuiltin("len", "s")] + [LuaBuiltin("sub", "s", "i", "j")] + [LuaBuiltin("upper", "s")] + [LuaBuiltin("lower", "s")] + [LuaBuiltin("rep", "s", "n", "sep")] + [LuaBuiltin("reverse", "s")] + [LuaBuiltin("find", "s", "pattern", "init", "plain")] + [LuaBuiltin("match", "s", "pattern", "init")] + [LuaBuiltin("gmatch", "s", "pattern")] + [LuaBuiltin("gsub", "s", "pattern", "repl", "n")] + [LuaBuiltin("byte", "s", "i", "j")] + [LuaBuiltin("char", "...")] + [LuaBuiltin("dump", "function")] + [LuaBuiltin("pack", "fmt", "...")] + [LuaBuiltin("unpack", "fmt", "s", "pos")] + [LuaBuiltin("packsize", "fmt")] + internal static class LuaStringLibrary; + + [LuaTable("table")] + [LuaBuiltin("insert", "list", "pos", "value")] + [LuaBuiltin("remove", "list", "pos")] + [LuaBuiltin("sort", "list", "comp")] + [LuaBuiltin("concat", "list", "sep", "i", "j")] + [LuaBuiltin("unpack", "list", "i", "j")] + [LuaBuiltin("move", "a1", "f", "e", "t", "a2")] + internal static class LuaTableLibrary; + + [LuaTable("bit32")] + [LuaBuiltin("band", "...")] + [LuaBuiltin("bor", "...")] + [LuaBuiltin("bxor", "...")] + [LuaBuiltin("bnot", "x")] + [LuaBuiltin("lshift", "x", "disp")] + [LuaBuiltin("rshift", "x", "disp")] + [LuaBuiltin("arshift", "x", "disp")] + [LuaBuiltin("extract", "n", "field", "width")] + [LuaBuiltin("replace", "n", "v", "field", "width")] + [LuaBuiltin("btest", "...")] + [LuaBuiltin("countlz", "x")] + [LuaBuiltin("countrz", "x")] + internal static class LuaBit32Library; + + [LuaTable("")] + [LuaBuiltin("type", "v")] + [LuaBuiltin("tostring", "v")] + [LuaBuiltin("tonumber", "e", "base")] + [LuaBuiltin("select", "n", "...")] + [LuaBuiltin("error", "message", "level")] + [LuaBuiltin("assert", "v", "message")] + [LuaBuiltin("print", "...")] + [LuaBuiltin("ipairs", "t")] + [LuaBuiltin("pairs", "t")] + [LuaBuiltin("next", "table", "index")] + [LuaBuiltin("unpack", "list", "i", "j")] + [LuaBuiltin("setmetatable", "table", "metatable")] + [LuaBuiltin("getmetatable", "object")] + [LuaBuiltin("rawget", "table", "index")] + [LuaBuiltin("rawset", "table", "index", "value")] + [LuaBuiltin("rawequal", "v1", "v2")] + [LuaBuiltin("rawlen", "v")] + [LuaBuiltin("pcall", "f", "...")] + [LuaBuiltin("xpcall", "f", "msgh", "...")] + internal static class LuaBasicLibrary; +} diff --git a/LuaScript.Runtime/LuaScript.Runtime.csproj b/LuaScript.Runtime/LuaScript.Runtime.csproj index 386382b..b6e9cfa 100644 --- a/LuaScript.Runtime/LuaScript.Runtime.csproj +++ b/LuaScript.Runtime/LuaScript.Runtime.csproj @@ -1,4 +1,4 @@ - + net10.0-windows10.0.19041.0 @@ -17,6 +17,7 @@ + diff --git a/LuaScript.slnx b/LuaScript.slnx index 144ccca..7d17923 100644 --- a/LuaScript.slnx +++ b/LuaScript.slnx @@ -3,6 +3,7 @@ +