スクリプト本文をバージョンスタンプで変更時のみ転送
このコミットが含まれているのは:
4ファイル変更
+95
-23
検索条件に一致するファイルはありません
@@ -201,6 +201,51 @@ namespace LuaScript.Tests
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AlternatingScripts_RecompileWhenVersionChanges()
|
||||
{
|
||||
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
|
||||
|
||||
var pixels = new byte[16];
|
||||
const string scriptA = "obj.x = obj.time * 2";
|
||||
const string scriptB = "obj.x = obj.time * 3";
|
||||
|
||||
for (int k = 1; k <= 4; k++)
|
||||
{
|
||||
var fa = Fields(2, 2, k);
|
||||
bool okA = _worker.Execute(scriptA, fa, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? errorA);
|
||||
Assert.True(okA, errorA);
|
||||
Assert.Equal(k * 2d, fa[NativeProtocol.X]);
|
||||
|
||||
var fb = Fields(2, 2, k);
|
||||
bool okB = _worker.Execute(scriptB, fb, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? errorB);
|
||||
Assert.True(okB, errorB);
|
||||
Assert.Equal(k * 3d, fb[NativeProtocol.X]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CompileError_IsReportedEveryFrame_AndRecovers()
|
||||
{
|
||||
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
|
||||
|
||||
var pixels = new byte[16];
|
||||
const string broken = "obj.x = (";
|
||||
|
||||
for (int k = 0; k < 2; k++)
|
||||
{
|
||||
bool ok = _worker.Execute(broken, Fields(2, 2, 0d), NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? error);
|
||||
Assert.False(ok);
|
||||
Assert.False(string.IsNullOrEmpty(error));
|
||||
Assert.DoesNotContain("timed out", error);
|
||||
}
|
||||
|
||||
var fields = Fields(2, 2, 9d);
|
||||
bool recovered = _worker.Execute("obj.x = obj.time", fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? recoverError);
|
||||
Assert.True(recovered, recoverError);
|
||||
Assert.Equal(9d, fields[NativeProtocol.X]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Timeout_KillsWorker_AndRecovers()
|
||||
{
|
||||
|
||||
@@ -30,6 +30,10 @@ namespace LuaScript.Engine
|
||||
private int _lastCallbackTagLength = -1;
|
||||
private string _lastCallbackTagText = string.Empty;
|
||||
private byte[]? _resultBuffer;
|
||||
private int _scriptVersion;
|
||||
private string? _lastScript;
|
||||
private byte[] _lastScriptBytes = Array.Empty<byte>();
|
||||
private bool _scriptWritten;
|
||||
private readonly Dictionary<string, byte[]> _stringNameBytes = new(StringComparer.Ordinal);
|
||||
private byte[] _stringValueBytes = new byte[256];
|
||||
|
||||
@@ -74,11 +78,18 @@ namespace LuaScript.Engine
|
||||
resultHeight = height;
|
||||
error = null;
|
||||
|
||||
byte[] scriptBytes = Encoding.UTF8.GetBytes(script);
|
||||
if (scriptBytes.Length > NativeProtocol.ScriptMax)
|
||||
bool scriptChanged = !ReferenceEquals(script, _lastScript);
|
||||
if (scriptChanged)
|
||||
{
|
||||
error = "script too large for native lane";
|
||||
return false;
|
||||
byte[] scriptBytes = Encoding.UTF8.GetBytes(script);
|
||||
if (scriptBytes.Length > NativeProtocol.ScriptMax)
|
||||
{
|
||||
error = "script too large for native lane";
|
||||
return false;
|
||||
}
|
||||
_lastScript = script;
|
||||
_lastScriptBytes = scriptBytes;
|
||||
_scriptVersion++;
|
||||
}
|
||||
|
||||
int stringCapacity = ResolveStringCapacity(_stringParamsCapacity, MeasureStringParameters(stringParameters));
|
||||
@@ -89,8 +100,13 @@ namespace LuaScript.Engine
|
||||
WriteStringParameters(view, stringParameters);
|
||||
view.Write(NativeProtocol.OffWidth, width);
|
||||
view.Write(NativeProtocol.OffHeight, height);
|
||||
view.Write(NativeProtocol.OffScriptLen, scriptBytes.Length);
|
||||
view.WriteArray(NativeProtocol.ScriptOffset, scriptBytes, 0, scriptBytes.Length);
|
||||
if (scriptChanged || !_scriptWritten)
|
||||
{
|
||||
view.Write(NativeProtocol.OffScriptLen, _lastScriptBytes.Length);
|
||||
view.WriteArray(NativeProtocol.ScriptOffset, _lastScriptBytes, 0, _lastScriptBytes.Length);
|
||||
_scriptWritten = true;
|
||||
}
|
||||
view.Write(NativeProtocol.OffScriptVersion, _scriptVersion);
|
||||
view.Write(NativeProtocol.OffPixelsDirty, 0);
|
||||
view.Write(NativeProtocol.OffErrorLen, 0);
|
||||
view.Write(NativeProtocol.OffStatus, NativeProtocol.StatusIdle);
|
||||
@@ -553,6 +569,7 @@ namespace LuaScript.Engine
|
||||
private void StartWorker(int width, int height, int stringCapacity)
|
||||
{
|
||||
_stringParamsCapacity = stringCapacity;
|
||||
_scriptWritten = false;
|
||||
_pixelOffset = NativeProtocol.PixelOffset(stringCapacity);
|
||||
long size = Math.Max(NativeProtocol.BufferSize(width, height, stringCapacity), _allocatedSize);
|
||||
_allocatedSize = size;
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace LuaScript.Engine
|
||||
public const int OffLoadResultWidth = 48;
|
||||
public const int OffLoadResultHeight = 52;
|
||||
public const int OffStringParamsLen = 56;
|
||||
public const int OffScriptVersion = 60;
|
||||
|
||||
public const int HeaderSize = 64;
|
||||
|
||||
|
||||
+26
-17
@@ -51,6 +51,7 @@ local OFF_CB_KIND = 11
|
||||
local OFF_LOAD_RESULT_W = 12
|
||||
local OFF_LOAD_RESULT_H = 13
|
||||
local OFF_STRING_PARAMS_LEN = 14
|
||||
local OFF_SCRIPT_VERSION = 15
|
||||
local SCRIPT_OFFSET = 64 + 64 * 8
|
||||
local ERROR_OFFSET = SCRIPT_OFFSET + 128 * 1024
|
||||
local CB_TAG_OFFSET = ERROR_OFFSET + 4 * 1024
|
||||
@@ -705,7 +706,8 @@ local objKeys = captureKeys(obj)
|
||||
local sceneKeys = captureKeys(scene)
|
||||
local ymm4Keys = captureKeys(ymm4)
|
||||
|
||||
local compiledCode, compiledChunk
|
||||
local compiledCode, compiledChunk, compiledError
|
||||
local lastScriptVersion
|
||||
|
||||
local function loadFields()
|
||||
width = i32[OFF_WIDTH]
|
||||
@@ -769,25 +771,32 @@ while true do
|
||||
|
||||
dirty = false
|
||||
pixelsValid = false
|
||||
local scriptLen = i32[OFF_SCRIPTLEN]
|
||||
local code = ffi.string(base + SCRIPT_OFFSET, scriptLen)
|
||||
|
||||
if code ~= compiledCode then
|
||||
local chunk, err = loadstring(code, "LuaScript")
|
||||
if chunk then
|
||||
compiledChunk = chunk
|
||||
compiledCode = code
|
||||
else
|
||||
compiledChunk = nil
|
||||
local msg = err or "compile error"
|
||||
ffi.copy(base + ERROR_OFFSET, msg, math.min(#msg, 4096))
|
||||
i32[OFF_ERRORLEN] = math.min(#msg, 4096)
|
||||
i32[OFF_STATUS] = 2
|
||||
k32.SetEvent(doneEvent)
|
||||
local version = i32[OFF_SCRIPT_VERSION]
|
||||
if version ~= lastScriptVersion then
|
||||
lastScriptVersion = version
|
||||
local scriptLen = i32[OFF_SCRIPTLEN]
|
||||
local code = ffi.string(base + SCRIPT_OFFSET, scriptLen)
|
||||
if code ~= compiledCode then
|
||||
local chunk, err = loadstring(code, "LuaScript")
|
||||
if chunk then
|
||||
compiledChunk = chunk
|
||||
compiledCode = code
|
||||
compiledError = nil
|
||||
else
|
||||
compiledChunk = nil
|
||||
compiledCode = code
|
||||
compiledError = err or "compile error"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if compiledChunk and code == compiledCode then
|
||||
if not compiledChunk then
|
||||
local msg = compiledError or "compile error"
|
||||
ffi.copy(base + ERROR_OFFSET, msg, math.min(#msg, 4096))
|
||||
i32[OFF_ERRORLEN] = math.min(#msg, 4096)
|
||||
i32[OFF_STATUS] = 2
|
||||
k32.SetEvent(doneEvent)
|
||||
else
|
||||
resetExtraKeys(sandbox, sandboxKeys)
|
||||
resetExtraKeys(obj, objKeys)
|
||||
resetExtraKeys(scene, sceneKeys)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする