ネイティブ文字列容量を必要量に応じ動的に拡張縮小

このコミットが含まれているのは:
routersys committed 2026-07-01 07:58:28 +09:00
1 親 01a0dcb9ae
コミット 981fb2ceeb
2ファイル変更
+52 -1

検索条件に一致するファイルはありません

+25
ファイルの表示
@@ -71,6 +71,31 @@ namespace LuaScript.Tests
Assert.Equal(System.Text.Encoding.UTF8.GetByteCount("C:/サンプル/画像.png"), fields[NativeProtocol.Alpha]);
}
[Fact]
public void StringParameters_GrowAndShrinkAroundCapacity()
{
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
var pixels = new byte[16];
string big = new string('x', 100 * 1024);
var large = new System.Collections.Generic.Dictionary<string, string> { ["text"] = big };
var fieldsLarge = Fields(2, 2, 0d);
bool ok = _worker.Execute(
"obj.x = string.len(obj.text)",
fieldsLarge, large, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(big.Length, fieldsLarge[NativeProtocol.X]);
var small = new System.Collections.Generic.Dictionary<string, string> { ["text"] = "ok" };
var fieldsSmall = Fields(2, 2, 0d);
ok = _worker.Execute(
"obj.x = string.len(obj.text)",
fieldsSmall, small, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error);
Assert.True(ok, error);
Assert.Equal(2d, fieldsSmall[NativeProtocol.X]);
}
[Fact]
public void PixelScript_MatchesExpectedGrayscale()
{
+27 -1
ファイルの表示
@@ -80,7 +80,8 @@ namespace LuaScript.Engine
return false;
}
EnsureWorker(width, height, NativeProtocol.MinStringParamsCapacity);
int stringCapacity = ResolveStringCapacity(_stringParamsCapacity, MeasureStringParameters(stringParameters));
EnsureWorker(width, height, stringCapacity);
var view = _view!;
view.WriteArray(NativeProtocol.FieldsOffset, fields, 0, NativeProtocol.FieldCount);
@@ -153,6 +154,31 @@ namespace LuaScript.Engine
return true;
}
private static int MeasureStringParameters(IReadOnlyDictionary<string, string> stringParameters)
{
int total = 4;
foreach (var pair in stringParameters)
total += 8 + Encoding.UTF8.GetByteCount(pair.Key) + Encoding.UTF8.GetByteCount(pair.Value ?? string.Empty);
return total;
}
private static int ResolveStringCapacity(int current, int required)
{
int capacity = current;
while (capacity < required)
{
if (capacity > int.MaxValue / 2)
{
capacity = required;
break;
}
capacity *= 2;
}
while (capacity > NativeProtocol.MinStringParamsCapacity && required <= capacity / 2)
capacity /= 2;
return capacity;
}
private void WriteStringParameters(MemoryMappedViewAccessor view, IReadOnlyDictionary<string, string> stringParameters)
{
long basePos = NativeProtocol.StringParamsOffset;