1
1
using Lua ;
2
+ using Lua . CodeAnalysis . Compilation ;
3
+ using Lua . CodeAnalysis . Syntax ;
4
+ using Lua . Runtime ;
2
5
using Lua . Standard ;
3
6
using ScriptingBenchmark . Shared ;
4
7
@@ -8,10 +11,11 @@ public class LuaCSBenchmark : IBenchmarkableAsync
8
11
{
9
12
public int LoopCount { get ; private set ; }
10
13
private LuaState ? _luaVM ;
14
+ private readonly LuaValue [ ] _returnBuffer = new LuaValue [ 1024 ] ;
11
15
12
- private string ? _CSharpToLangCode ;
13
- private string ? _LangToCSharpCode ;
14
- private string ? _LangAllocCode ;
16
+ private Chunk ? _CSharpToLangCode ;
17
+ private Chunk ? _LangToCSharpCode ;
18
+ private Chunk ? _LangAllocCode ;
15
19
16
20
public LuaCSBenchmark ( int loopCount )
17
21
{
@@ -20,9 +24,9 @@ public LuaCSBenchmark(int loopCount)
20
24
21
25
public void Setup ( )
22
26
{
23
- _CSharpToLangCode = Codes . GetLuaCSharpToLang ( ) ;
24
- _LangToCSharpCode = Codes . GetLuaLangToCSharp ( LoopCount ) ;
25
- _LangAllocCode = Codes . GetLuaAlloc ( LoopCount ) ;
27
+ _CSharpToLangCode = Compile ( Codes . GetLuaCSharpToLang ( ) ) ;
28
+ _LangToCSharpCode = Compile ( Codes . GetLuaLangToCSharp ( LoopCount ) ) ;
29
+ _LangAllocCode = Compile ( Codes . GetLuaAlloc ( LoopCount ) ) ;
26
30
27
31
_luaVM = LuaState . Create ( ) ;
28
32
_luaVM . OpenTableLibrary ( ) ;
@@ -37,6 +41,12 @@ public void Setup()
37
41
} ) ;
38
42
}
39
43
44
+ private static Chunk Compile ( string source )
45
+ {
46
+ var syntaxTree = LuaSyntaxTree . Parse ( source ) ;
47
+ return LuaCompiler . Default . Compile ( syntaxTree ) ;
48
+ }
49
+
40
50
public void Cleanup ( )
41
51
{
42
52
}
@@ -67,9 +77,11 @@ public string LangAlloc()
67
77
68
78
public async Task < int > CSharpToLangAsync ( )
69
79
{
70
- LuaValue [ ] result = await _luaVM ! . DoStringAsync ( _CSharpToLangCode ! ) ;
71
- var func = result [ 0 ] . Read < LuaFunction > ( ) ;
80
+ var returnCount = await _luaVM ! . RunAsync ( _CSharpToLangCode ! , _returnBuffer ) ;
81
+ if ( returnCount != 1 )
82
+ throw new InvalidOperationException ( "Invalid return count" ) ;
72
83
84
+ var func = _returnBuffer [ 0 ] . Read < LuaFunction > ( ) ;
73
85
var number = 0 ;
74
86
75
87
for ( int i = 0 ; i < LoopCount ; i ++ )
@@ -83,16 +95,21 @@ public async Task<int> CSharpToLangAsync()
83
95
84
96
public async Task < int > LangToCSharpAsync ( )
85
97
{
86
- LuaValue [ ] result = await _luaVM ! . DoStringAsync ( _LangToCSharpCode ! ) ;
87
- var number = result [ 0 ] . Read < int > ( ) ;
98
+ var returnCount = await _luaVM ! . RunAsync ( _LangToCSharpCode ! , _returnBuffer ) ;
99
+ if ( returnCount != 1 )
100
+ throw new InvalidOperationException ( "Invalid return count" ) ;
101
+
102
+ var number = _returnBuffer [ 0 ] . Read < int > ( ) ;
88
103
return number ;
89
104
}
90
105
91
106
public async Task < string > LangAllocAsync ( )
92
107
{
93
- LuaValue [ ] result = await _luaVM ! . DoStringAsync ( _LangAllocCode ! ) ;
108
+ var returnCount = await _luaVM ! . RunAsync ( _LangAllocCode ! , _returnBuffer ) ;
109
+ if ( returnCount != 1 )
110
+ throw new InvalidOperationException ( "Invalid return count" ) ;
94
111
95
- var arr = result [ 0 ] . Read < LuaTable > ( ) ;
112
+ var arr = _returnBuffer [ 0 ] . Read < LuaTable > ( ) ;
96
113
var arrItem = arr [ LoopCount ] . Read < LuaTable > ( ) ;
97
114
return arrItem [ "test" ] . Read < string > ( ) ;
98
115
}
0 commit comments