Skip to content

[Draft] version 0.5 #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 89 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
5210c37
Change: Return values on LuaStack
Akeit0 Apr 26, 2025
6512032
Remove: LuaValueArrayPool
Akeit0 Apr 26, 2025
ae7ac64
Change: New lua compiler and API
Akeit0 Apr 26, 2025
b034a01
fix: table.sort extra arguments
Akeit0 Apr 27, 2025
27ccf7b
fix: meta SetTab
Akeit0 Apr 27, 2025
fbdf8f7
fix: table.unpack with arg value int32 overflow
Akeit0 Apr 27, 2025
fad3b5e
fix: meta unary methods vararg
Akeit0 Apr 27, 2025
4c54e42
fix: meta concat callstack
Akeit0 Apr 27, 2025
0c3dd1c
fix: getmetatable for all types
Akeit0 Apr 27, 2025
c15dcab
fix: meta newindex target
Akeit0 Apr 27, 2025
4783ea0
fix: uncleared stack for call
Akeit0 Apr 27, 2025
c5d3f9f
fix: string.find empty pattern
Akeit0 Apr 27, 2025
d994432
Rename fields and parameters
Akeit0 Apr 27, 2025
a00b966
Reformat
Akeit0 Apr 27, 2025
21eeb52
fix: parser
Akeit0 Apr 27, 2025
70a9e17
fix: string.gsub didn't return replace count
Akeit0 Apr 27, 2025
a880729
fix: literal parse and format
Akeit0 Apr 27, 2025
21e7000
Simplify LuaFunctionExecutionContext
Akeit0 Apr 29, 2025
168994d
make apis LuaThread Centered
Akeit0 Apr 29, 2025
abdb7f0
refactor: clean up code style and improve Lua thread handling
Akeit0 Apr 29, 2025
2633de4
api revision
Akeit0 Apr 29, 2025
2dbe7fa
fix: minus variableArgumentCount
Akeit0 Apr 29, 2025
6a65b96
move
Akeit0 Apr 29, 2025
93f7c62
fix
Akeit0 Apr 29, 2025
9f44221
Reduce LuaCoroutine Yield Resume allocation
Akeit0 Apr 30, 2025
d976dd2
Flexible LuaModule
Akeit0 Apr 30, 2025
30ca0f2
Reduced LuaModule lifetime
Akeit0 Apr 30, 2025
58ad418
small refactor
Akeit0 Apr 30, 2025
9a0eb84
revert CodeAnalysis/Syntax
Akeit0 Apr 30, 2025
7d2e974
Pool tasks used in coroutines
Akeit0 Apr 30, 2025
e60b59b
Share resume/yield core implementations
Akeit0 Apr 30, 2025
5ce0765
fix: async coroutine bug
Akeit0 Apr 30, 2025
7452141
Add LightAsyncValueTaskMethodBuilder and related types for zero alloc…
Akeit0 Apr 30, 2025
5dda75c
Lazy stack trace
Akeit0 May 1, 2025
d031777
Add: support LoadKX
Akeit0 May 2, 2025
980d527
Merge pull request #139 from nuskey8/loadkx
Akeit0 May 2, 2025
b8fed2b
Add: Add project and code to make it easier to check JIT asm
Akeit0 May 2, 2025
4ce8f59
Merge pull request #140 from nuskey8/util/vm-markers
Akeit0 May 2, 2025
ce07778
fix: remove todo work around
Akeit0 May 2, 2025
6e784f5
fix: cannot build
Akeit0 May 2, 2025
2edbf08
better jit dump
Akeit0 May 2, 2025
cf943e3
fix: handle BOM in Lua chunk loading
Akeit0 May 2, 2025
0226102
Add: implement BOM utility for encoding detection in Lua chunk loading
Akeit0 May 2, 2025
a457cb0
refactor: simplify fixed statement usage and remove unnecessary nulla…
Akeit0 May 2, 2025
d8448b7
refactor: Lua.CodeAnalysis.Compilation
Akeit0 May 3, 2025
8182a85
refactor: remove unnecessary blank lines and simplify code structure
Akeit0 May 3, 2025
a449410
fix: update chunk name handling in **File** methods to use full path
Akeit0 May 4, 2025
058de2f
change: draft of better compile exception
Akeit0 May 4, 2025
aa2d2b4
demo of rust like compile exception
Akeit0 May 4, 2025
216a08b
fix: compile error shows incorrect position and token
Akeit0 May 5, 2025
a5115fc
refactor: remove unused properties from LuaFunctionExecutionContext a…
Akeit0 May 5, 2025
8249ad4
add: Comprehensive Lua API for basic operations
Akeit0 May 5, 2025
5882dae
fix: use __call via other metamethods
Akeit0 May 6, 2025
9a25985
feat: implement OpCall method for LuaThread and fix vararg handling
Akeit0 May 6, 2025
f295651
fix: adjust argument count handling in DoFile method
Akeit0 May 6, 2025
0bbaa01
improve: improve call stack handling and exception management
Akeit0 May 8, 2025
af01bdc
change: move apis to LuaThreadAccess to validate context
Akeit0 May 10, 2025
48e18d8
fix: parser initialization order
Akeit0 May 11, 2025
d7e81d2
refactor: replace StringBuilder with FastListCore in Scanner for redu…
Akeit0 May 11, 2025
02dad70
refactor: replace FastListCore with PooledList in Scanner
Akeit0 May 11, 2025
147b498
Merge pull request #143 from nuskey8/access-validation
Akeit0 May 12, 2025
3eacaa7
Add: add Low level APIs for compilation
Akeit0 May 13, 2025
40ec053
refactor: replace File.ReadAllBytesAsync with sync to prevent uninten…
Akeit0 May 13, 2025
e80362e
improve: better error message
Akeit0 May 13, 2025
e97aefc
improve: error message
Akeit0 May 15, 2025
df5ed5c
refactor: simplify exception hierarchy by removing LuaException base …
Akeit0 May 15, 2025
f6abe52
fix: update new line detection to handle both LF and CRLF
Akeit0 May 15, 2025
59e3678
improve: enhance cancellation handling and introduce LuaCancelledExce…
Akeit0 May 15, 2025
0d72037
improve: enhance error message for LuaCancelledException
Akeit0 May 15, 2025
d36e9b8
Merge pull request #147 from nuskey8/improve-cancel
Akeit0 May 15, 2025
2b5e0a2
rename: rename LuaCancelledException to LuaCanceledException for cons…
Akeit0 May 15, 2025
d3e99c4
Improve hook check performance
Akeit0 May 16, 2025
64e415c
Add: SetHook API
Akeit0 May 16, 2025
417a890
fix: use GetArgumentOrDefault for mask in SetHook method
Akeit0 May 16, 2025
01c919f
Add: support exception handling from hook
Akeit0 May 16, 2025
19eb52e
Add: implement IEnumerable interface for LuaTable and add enumerator
Akeit0 May 17, 2025
3cd21a8
Refactor: optimize LuaTableEnumerator and extract MoveNext logic to L…
Akeit0 May 17, 2025
060e12f
Add: support passing CancellationToken in parameter by LuaObjectGener…
Akeit0 May 17, 2025
e5e7121
Add: support passing CancellationToken in parameter by LuaObjectGener…
Akeit0 May 17, 2025
97d996f
Merge remote-tracking branch 'origin/v0.5-dev' into v0.5-dev
Akeit0 May 17, 2025
930b5d9
Fix: correct async return statement in LuaObjectGenerator generated code
Akeit0 May 17, 2025
f3d299c
fix: fix LuaObjectGenerator value conversion handling
Akeit0 May 17, 2025
30c2d34
fix: add more types in SymbolReferences for Lua.SourceGenerator
Akeit0 May 17, 2025
3dc8e41
fix: prevent null startLine from causing unnecessary line append in C…
Akeit0 May 17, 2025
0070772
fix: improve indent in LuaObjectGenerator
Akeit0 May 17, 2025
627b358
fix: handle _ENV upvalue in LuaClosure
Akeit0 May 17, 2025
d17154c
fix: required return value
Akeit0 May 17, 2025
069babf
fix: call hook pc
Akeit0 May 17, 2025
8e600ba
fix: io.write
Akeit0 May 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ csharp_style_conditional_delegate_call = true

# Modifier preferences
csharp_prefer_static_local_function = true
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async
csharp_preferred_modifier_order = public, private, protected, internal, file, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, required, volatile, async
csharp_style_prefer_readonly_struct = true
csharp_style_prefer_readonly_struct_member = true

Expand Down Expand Up @@ -208,26 +208,26 @@ dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case


Expand Down
17 changes: 17 additions & 0 deletions Lua.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lua.Tests", "tests\Lua.Tests\Lua.Tests.csproj", "{7572B7BC-FC73-42F0-B4F7-DA291B4EDB36}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sandbox", "sandbox", "{33883F28-679F-48AD-8E64-3515C7BDAF5A}"
ProjectSection(SolutionItems) = preProject
sandbox\ConsoleApp2\.gitignore = sandbox\ConsoleApp2\.gitignore
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "sandbox\ConsoleApp1\ConsoleApp1.csproj", "{718A361C-AAF3-45A4-84D4-8C4FB6BB374E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "sandbox\Benchmark\Benchmark.csproj", "{FC157C29-8AAE-49C8-9536-208E3F0698DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lua.SourceGenerator", "src\Lua.SourceGenerator\Lua.SourceGenerator.csproj", "{C4BB264C-4D37-4E2D-99FD-4918CE22D7E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp2", "sandbox\ConsoleApp2\ConsoleApp2.csproj", "{474D977A-946F-49F1-9293-F7F78E0F6A01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JitTest", "sandbox\JitTest\JitTest.csproj", "{BF479E2C-AAF5-4128-B236-EC7D6092E1F8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -48,12 +55,22 @@ Global
{C4BB264C-4D37-4E2D-99FD-4918CE22D7E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4BB264C-4D37-4E2D-99FD-4918CE22D7E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4BB264C-4D37-4E2D-99FD-4918CE22D7E4}.Release|Any CPU.Build.0 = Release|Any CPU
{474D977A-946F-49F1-9293-F7F78E0F6A01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{474D977A-946F-49F1-9293-F7F78E0F6A01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{474D977A-946F-49F1-9293-F7F78E0F6A01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{474D977A-946F-49F1-9293-F7F78E0F6A01}.Release|Any CPU.Build.0 = Release|Any CPU
{BF479E2C-AAF5-4128-B236-EC7D6092E1F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF479E2C-AAF5-4128-B236-EC7D6092E1F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF479E2C-AAF5-4128-B236-EC7D6092E1F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF479E2C-AAF5-4128-B236-EC7D6092E1F8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6E33BFBC-E51F-493E-9AF0-30C1100F5B5D} = {18A64E25-9557-457B-80AE-A6EFE853118D}
{7572B7BC-FC73-42F0-B4F7-DA291B4EDB36} = {79458370-DD8A-48A4-B11E-8DF631520E8C}
{718A361C-AAF3-45A4-84D4-8C4FB6BB374E} = {33883F28-679F-48AD-8E64-3515C7BDAF5A}
{FC157C29-8AAE-49C8-9536-208E3F0698DA} = {33883F28-679F-48AD-8E64-3515C7BDAF5A}
{C4BB264C-4D37-4E2D-99FD-4918CE22D7E4} = {18A64E25-9557-457B-80AE-A6EFE853118D}
{474D977A-946F-49F1-9293-F7F78E0F6A01} = {33883F28-679F-48AD-8E64-3515C7BDAF5A}
{BF479E2C-AAF5-4128-B236-EC7D6092E1F8} = {33883F28-679F-48AD-8E64-3515C7BDAF5A}
EndGlobalSection
EndGlobal
7 changes: 4 additions & 3 deletions sandbox/Benchmark/AddBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public void Setup()
core.Setup("add.lua");
core.LuaCSharpState.OpenStandardLibraries();

core.LuaCSharpState.Environment["add"] = new LuaFunction("add", (context, buffer, ct) =>
core.LuaCSharpState.Environment["add"] = new LuaFunction("add", (context, ct) =>
{
buffer.Span[0] = context.GetArgument<double>(0) + context.GetArgument<double>(1);
return new(1);
var a = context.GetArgument<double>(0);
var b = context.GetArgument<double>(1);
return new(context.Return(a + b));
});
core.MoonSharpState.Globals["add"] = (Func<double, double, double>)Add;
core.NLuaState.RegisterFunction("add", typeof(AddBenchmark).GetMethod(nameof(Add), BindingFlags.Static | BindingFlags.Public));
Expand Down
28 changes: 14 additions & 14 deletions sandbox/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="NLua" Version="1.7.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0"/>
<PackageReference Include="MoonSharp" Version="2.0.0"/>
<PackageReference Include="NLua" Version="1.7.3"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Lua\Lua.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Lua\Lua.csproj"/>
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions sandbox/Benchmark/HookedBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BenchmarkDotNet.Attributes;
using Lua;
using Lua.Standard;

[Config(typeof(BenchmarkConfig))]
public class HookedBenchmark
{
BenchmarkCore core = default!;
LuaValue[] buffer = new LuaValue[1];

[IterationSetup]
public void Setup()
{
core = new();
core.Setup("hooked.lua");
core.LuaCSharpState.OpenStandardLibraries();
}

[IterationCleanup]
public void Cleanup()
{
core.Dispose();
core = default!;
GC.Collect();
}


[Benchmark(Description = "NLua (DoString)", Baseline = true)]
public object[] Benchmark_NLua_String()
{
return core.NLuaState.DoString(core.SourceText);
}

[Benchmark(Description = "Lua-CSharp (DoString)")]
public async Task<LuaValue> Benchmark_LuaCSharp_String()
{
await core.LuaCSharpState.DoStringAsync(core.SourceText, buffer);
return buffer[0];
}
}
60 changes: 7 additions & 53 deletions sandbox/Benchmark/InterpreterSteps.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using BenchmarkDotNet.Attributes;
using Lua;
using Lua.CodeAnalysis.Compilation;
using Lua.CodeAnalysis.Syntax;
using Lua.Runtime;
using Lua.Standard;

Expand All @@ -10,38 +8,16 @@ public class InterpreterSteps
{
string sourceText = default!;
LuaState state = default!;
SyntaxToken[] tokens = [];
LuaSyntaxTree ast = default!;
Chunk chunk = default!;
LuaValue[] results = new LuaValue[1];
LuaClosure closure = default!;

[GlobalSetup]
public void GlobalSetup()
{
var filePath = FileHelper.GetAbsolutePath("n-body.lua");
sourceText = File.ReadAllText(filePath);

var lexer = new Lexer
{
Source = sourceText.AsMemory()
};

var buffer = new List<SyntaxToken>();
while (lexer.MoveNext())
{
buffer.Add(lexer.Current);
}

tokens = buffer.ToArray();

var parser = new Parser();
foreach (var token in tokens)
{
parser.Add(token);
}

ast = parser.Parse();
chunk = LuaCompiler.Default.Compile(ast);
state = LuaState.Create();
state.OpenStandardLibraries();
closure = state.Load(sourceText, sourceText);
}

[IterationSetup]
Expand All @@ -60,38 +36,16 @@ public void CreateState()
LuaState.Create();
}

[Benchmark]
public void Lexer()
{
var lexer = new Lexer
{
Source = sourceText.AsMemory()
};

while (lexer.MoveNext()) { }
}

[Benchmark]
public LuaSyntaxTree Parser()
{
var parser = new Parser();
foreach (var token in tokens)
{
parser.Add(token);
}

return parser.Parse();
}

[Benchmark]
public Chunk Compile()
public LuaClosure Compile()
{
return LuaCompiler.Default.Compile(ast);
return state.Load(sourceText, sourceText);
}

[Benchmark]
public async ValueTask RunAsync()
{
await state.RunAsync(chunk, results);
await state.TopLevelAccess.Call(closure, []);
}
}
2 changes: 1 addition & 1 deletion sandbox/Benchmark/NBodyBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public DynValue Benchmark_MoonSharp_File()
return core.MoonSharpState.DoFile(core.FilePath);
}

[Benchmark(Description = "NLua (DoString)")]
[Benchmark(Description = "NLua (DoString)", Baseline = true)]
public object[] Benchmark_NLua_String()
{
return core.NLuaState.DoString(core.SourceText);
Expand Down
Loading