Skip to content

Commit afd1c55

Browse files
committed
fix yanghuan#226 & some libs
1 parent d30daa4 commit afd1c55

8 files changed

Lines changed: 107 additions & 38 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ local function toNumber(value, min, max, parse, objectTo, sign)
175175
value = value + 1
176176
end
177177
else
178-
if dif < 0.5 or (dif == -0.5 and value % 2 ~= 0) then
178+
if dif < -0.5 or (dif == -0.5 and value % 2 ~= 0) then
179179
value = value - 1
180180
end
181181
end

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Math.lua‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,27 @@ local function divRem(a, b)
3232
return (a - remainder) / b, remainder
3333
end
3434

35-
local function round(value, digits)
36-
local i = value >= 0 and 0.5 or -0.5
35+
local function round(value, digits, mode)
3736
local mult = 10 ^ (digits or 0)
38-
return trunc(value * mult + i) / mult
37+
local i = value * mult
38+
if mode == 1 then
39+
value = trunc(i + (value >= 0 and 0.5 or -0.5))
40+
else
41+
value = trunc(i)
42+
if value ~= i then
43+
local dif = i - value
44+
if value >= 0 then
45+
if dif > 0.5 or (dif == 0.5 and value % 2 ~= 0) then
46+
value = value + 1
47+
end
48+
else
49+
if dif < -0.5 or (dif == -0.5 and value % 2 ~= 0) then
50+
value = value - 1
51+
end
52+
end
53+
end
54+
end
55+
return value / mult
3956
end
4057

4158
local function sign(v)
@@ -78,6 +95,10 @@ local function clamp(a, b, c)
7895
return min(max(a, b), c)
7996
end
8097

98+
local function truncate(d)
99+
return trunc(d) * 1.0
100+
end
101+
81102
local exp = math.exp
82103
local cosh = math.cosh or function(x) return (exp(x) + exp(-x)) / 2.0 end
83104
local pow = math.pow or function(x, y) return x ^ y end
@@ -111,6 +132,6 @@ Math.Sinh = sinh
111132
Math.Sqrt = math.sqrt
112133
Math.Tan = math.tan
113134
Math.Tanh = tanh
114-
Math.Truncate = trunc
135+
Math.Truncate = truncate
115136

116137
System.define("System.Math", Math)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Number.lua‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,15 @@ end
214214

215215
local function toStringWithFormat(this, format)
216216
if #format ~= 0 then
217-
local i, j, x, n = format:find("^%s*([xX])(%d?)%s*$")
217+
local i, j, x, n = format:find("^%s*([xXdDfF])(%d?)%s*$")
218218
if i then
219-
format = n == "" and "%" .. x or "%0" .. n .. x
219+
if x == 'x' or x == 'X' then
220+
format = n == "" and "%" .. x or "%0" .. n .. x
221+
elseif x == 'f' or x == 'F' then
222+
format = n == "" and "%.f" or "%." .. n .. 'f'
223+
else
224+
format = n == "" and "%d" or "%0" .. n .. 'd'
225+
end
220226
return format:format(this)
221227
end
222228
end

‎CSharp.lua/LuaAst/LuaTableExpression.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ internal override void Render(LuaRenderer renderer) {
5151
}
5252

5353
public static readonly LuaTableExpression Empty = new LuaTableExpression();
54+
55+
public LuaExpressionSyntax GetSingleExpression(int index) {
56+
var item = (LuaSingleTableItemSyntax)Items[index];
57+
return item.Expression;
58+
}
5459
}
5560

5661
public abstract class LuaTableItemSyntax : LuaSyntaxNode {

‎CSharp.lua/LuaAst/LuaTypeDeclarationSyntax.cs‎

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,45 @@ private static void SortMetaData(LuaTableExpression metaData) {
510510
}
511511
}
512512

513-
private void CheckMetadataMoreThanUpvalues() {
513+
private static IEnumerable<LuaTableExpression> GetMetatables(LuaTableExpression metaMethods) {
514+
return metaMethods.Items.OfType<LuaSingleTableItemSyntax>().Select(i => (LuaTableExpression)i.Expression);
515+
}
516+
517+
private void CheckMetatableMoreThanUpvalues(LuaTableExpression table, int nameIndex, ref int upvalueCount) {
514518
const int kMaxUpvalueCount = kUpvaluesMaxCount - 2;
515-
const int kNameIndex = 2;
519+
var expression = table.GetSingleExpression(nameIndex);
520+
if (expression is LuaIdentifierNameSyntax name) {
521+
if (upvalueCount >= kMaxUpvalueCount) {
522+
CheckTooManyVariables(false);
523+
var left = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.MorenManyLocalVarTempTable, name);
524+
methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(left, name));
525+
table.Items[nameIndex] = new LuaSingleTableItemSyntax(left);
526+
} else {
527+
++upvalueCount;
528+
}
529+
}
530+
}
531+
532+
private void CheckMetadataMoreThanUpvalues() {
533+
int upvalueCount = 0;
516534
if (metaMethods_ != null) {
517-
int upvalueCount = 0;
518-
foreach (LuaSingleTableItemSyntax item in metaMethods_.Items) {
519-
var table = (LuaTableExpression)item.Expression;
520-
var expression = (LuaSingleTableItemSyntax)table.Items[kNameIndex];
521-
if (expression.Expression is LuaIdentifierNameSyntax name) {
522-
if (upvalueCount >= kMaxUpvalueCount) {
523-
CheckTooManyVariables(false);
524-
var left = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.MorenManyLocalVarTempTable, name);
525-
methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(left, name));
526-
table.Items[kNameIndex] = new LuaSingleTableItemSyntax(left);
527-
} else {
528-
++upvalueCount;
535+
const int kNameIndex = 2;
536+
foreach (var table in GetMetatables(metaMethods_)) {
537+
CheckMetatableMoreThanUpvalues(table, kNameIndex, ref upvalueCount);
538+
}
539+
}
540+
if (metaProperties_ != null) {
541+
const int kFlagIndex = 1;
542+
const int kGetIndex = 3;
543+
const int kSetIndex = 4;
544+
foreach (var table in GetMetatables(metaProperties_)) {
545+
var flagIdentifierName = (LuaIdentifierNameSyntax)table.GetSingleExpression(kFlagIndex);
546+
int flag = Convert.ToInt32(flagIdentifierName.ValueText, 16);
547+
var kind = (PropertyMethodKind)((flag >> 8) & 0xff);
548+
if (kind != PropertyMethodKind.Field) {
549+
CheckMetatableMoreThanUpvalues(table, kGetIndex, ref upvalueCount);
550+
if (kind == PropertyMethodKind.Both) {
551+
CheckMetatableMoreThanUpvalues(table, kSetIndex, ref upvalueCount);
529552
}
530553
}
531554
}

‎CSharp.lua/LuaSyntaxNodeTransform.cs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -906,14 +906,18 @@ public PropertyMethodResult(LuaPropertyOrEventIdentifierNameSyntax name, List<Lu
906906
private void AddPropertyOrEventMetaData(ISymbol symol, PropertyMethodResult get, PropertyMethodResult set, LuaIdentifierNameSyntax name, List<LuaExpressionSyntax> attributes) {
907907
bool isProperty = symol.Kind == SymbolKind.Property;
908908
PropertyMethodKind kind;
909-
if (get == null && set == null) {
910-
kind = PropertyMethodKind.Field;
911-
} else if (get != null) {
912-
kind = PropertyMethodKind.GetOnly;
913-
} else if (set != null) {
914-
kind = PropertyMethodKind.SetOnly;
915-
} else {
916-
kind = PropertyMethodKind.Both;
909+
if (get != null) {
910+
if (set != null) {
911+
kind = PropertyMethodKind.Both;
912+
} else {
913+
kind = PropertyMethodKind.GetOnly;
914+
}
915+
} else {
916+
if (set != null) {
917+
kind = PropertyMethodKind.SetOnly;
918+
} else {
919+
kind = PropertyMethodKind.Field;
920+
}
917921
}
918922
var data = new LuaTableExpression() { IsSingleLine = true };
919923
data.Add(new LuaStringLiteralExpressionSyntax(symol.Name));

‎test/BridgeNetTests/Batch1/src/Diagnostics/StopwatchTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void ElapsedWorks()
194194

195195
Assert.True(hasIncreased, "Times should increase inside the loop");
196196
Assert.True(watch.ElapsedMilliseconds > 150, "ElapsedMilliseconds > 150" + " Actual: " + watch.ElapsedMilliseconds);
197-
Assert.AreEqual(watch.Elapsed, new TimeSpan(0, 0, 0, 0, (int)watch.ElapsedMilliseconds));
197+
Assert.AreEqual((long)watch.Elapsed.TotalMilliseconds, watch.ElapsedMilliseconds);
198198

199199
var value = (double)watch.ElapsedTicks / Stopwatch.Frequency;
200200

‎test/BridgeNetTests/ClientTestHelper/src/Internal/Extension.cs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,46 @@
55
using System.Reflection;
66

77
public static class Extension {
8+
private static string ToFormat(int format) {
9+
if (format == 10) {
10+
return "D";
11+
}
12+
if (format == 16) {
13+
return "x";
14+
}
15+
return "";
16+
}
17+
818
public static string ToString(this sbyte b, int format) {
9-
return b.ToString(format.ToString());
19+
return b.ToString(ToFormat(format));
1020
}
1121

1222
public static string ToString(this byte b, int format) {
13-
return b.ToString(format.ToString());
23+
return b.ToString(ToFormat(format));
1424
}
1525

1626
public static string ToString(this ushort v, int format) {
17-
return v.ToString(format.ToString());
27+
return v.ToString(ToFormat(format));
1828
}
1929

2030
public static string ToString(this short v, int format) {
21-
return v.ToString(format.ToString());
31+
return v.ToString(ToFormat(format));
2232
}
2333

2434
public static string ToString(this int v, int format) {
25-
return v.ToString(format.ToString());
35+
return v.ToString(ToFormat(format));
2636
}
2737

2838
public static string ToString(this uint v, int format) {
29-
return v.ToString(format.ToString());
39+
return v.ToString(ToFormat(format));
3040
}
3141

3242
public static string ToString(this long v, int format) {
33-
return v.ToString(format.ToString());
43+
return v.ToString(ToFormat(format));
3444
}
3545

3646
public static string ToString(this ulong v, int format) {
37-
return v.ToString(format.ToString());
47+
return v.ToString(ToFormat(format));
3848
}
3949

4050
public static string ToPrecision(this float v, int decimals = 0) {

0 commit comments

Comments
 (0)