From 361f6a92268333e3a73204ef825782634617a1a3 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sat, 18 Feb 2023 01:15:31 +0000 Subject: [PATCH] add from string, from lsl string and from span lsl vector3 and rotation variants --- .../Shared/Api/Implementation/LSL_Api.cs | 34 +++++------ .../Region/ScriptEngine/Shared/LSL_Types.cs | 56 +++++++++++-------- .../ScriptEngine/YEngine/MMRScriptInlines.cs | 2 + .../ScriptEngine/YEngine/MMRScriptReduce.cs | 4 +- .../ScriptEngine/YEngine/MMRScriptTypeCast.cs | 2 +- 5 files changed, 50 insertions(+), 48 deletions(-) diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs index d7bef3cfc8..3f07555dc0 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs @@ -5562,14 +5562,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api object item = src.Data[index]; - //if (item.GetType() == typeof(LSL_Vector)) - // return (LSL_Vector)item; - if(item is LSL_Vector vec) return vec; if (item is LSL_String lsv) - return new LSL_Vector(lsv.m_string); + return new LSL_Vector(lsv); if (item is string sv) // xengine sees string return new LSL_Vector(sv); @@ -5582,24 +5579,18 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api index = src.Length + index; if (index >= src.Length || index < 0) - return new LSL_Rotation(0, 0, 0, 1); + return LSL_Rotation.Identity; object item = src.Data[index]; - - // SL spits always out ZERO_ROTATION for anything other than - // strings or vectors. Although keys always return ZERO_ROTATION, - // it is currently difficult to make the distinction between - // a string, a key as string and a string that by coincidence - // is a string, so we're going to leave that up to the - // LSL_Rotation constructor. - if (item.GetType() == typeof(LSL_Rotation)) - return (LSL_Rotation)item; + if (item is LSL_Rotation rot) + return rot; + if (item is LSL_String lls) + return new LSL_Rotation(lls); + if (item is string ls) // xengine sees string) + return new LSL_Rotation(ls); - if (item is LSL_String || item is string) // xengine sees string) - return new LSL_Rotation(src.Data[index].ToString()); - - return new LSL_Rotation(0, 0, 0, 1); + return LSL_Rotation.Identity; } public LSL_List llList2List(LSL_List src, int start, int end) @@ -5676,10 +5667,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api int parens = 0; int start = 0; int length = 0; - - for (int i = 0; i < src.Length; i++) + + ReadOnlySpan s = src.AsSpan(); + for (int i = 0; i < s.Length; i++) { - switch (src[i]) + switch (s[i]) { case '<': parens++; diff --git a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs index 7d7d5d8b80..9a56ea45ae 100644 --- a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs @@ -87,7 +87,13 @@ namespace OpenSim.Region.ScriptEngine.Shared z = Z; } - public Vector3(string str) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector3(string str) : this(str.AsSpan()) { } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector3(LSLString str) : this(str.m_string.AsSpan()) { } + + public Vector3(ReadOnlySpan str) { if (str.Length < 5) { @@ -95,14 +101,12 @@ namespace OpenSim.Region.ScriptEngine.Shared return; } - var strspan = str.AsSpan(); - int start = 0; int comma = 0; char c; do { - c = Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma); + c = Unsafe.Add(ref MemoryMarshal.GetReference(str), comma); if (c == ',' || c == '<') break; } @@ -113,7 +117,7 @@ namespace OpenSim.Region.ScriptEngine.Shared start = ++comma; while (++comma < str.Length) { - if (Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma) == ',') + if (Unsafe.Add(ref MemoryMarshal.GetReference(str), comma) == ',') break; } } @@ -123,7 +127,7 @@ namespace OpenSim.Region.ScriptEngine.Shared return; } - if (!double.TryParse(strspan[start..comma], NumberStyles.Float, Utils.EnUsCulture, out x)) + if (!double.TryParse(str[start..comma], NumberStyles.Float, Utils.EnUsCulture, out x)) { z = y = 0; return; @@ -132,7 +136,7 @@ namespace OpenSim.Region.ScriptEngine.Shared start = ++comma; while (++comma < str.Length) { - if (Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma) == ',') + if (Unsafe.Add(ref MemoryMarshal.GetReference(str), comma) == ',') break; } if (comma > str.Length - 1) @@ -140,7 +144,7 @@ namespace OpenSim.Region.ScriptEngine.Shared z = y = x = 0; return; } - if (!double.TryParse(strspan[start..comma], NumberStyles.Float, Utils.EnUsCulture, out y)) + if (!double.TryParse(str[start..comma], NumberStyles.Float, Utils.EnUsCulture, out y)) { z = x = 0; return; @@ -149,12 +153,12 @@ namespace OpenSim.Region.ScriptEngine.Shared start = ++comma; while (++comma < str.Length) { - c = Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma); + c = Unsafe.Add(ref MemoryMarshal.GetReference(str), comma); if (c == ' ' || c == '>') break; } - if (!double.TryParse(strspan[start..comma], NumberStyles.Float, Utils.EnUsCulture, out z)) + if (!double.TryParse(str[start..comma], NumberStyles.Float, Utils.EnUsCulture, out z)) { y = x = 0; return; @@ -478,7 +482,13 @@ namespace OpenSim.Region.ScriptEngine.Shared s = rot.W; } - public Quaternion(string str) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Quaternion(string str) : this(str.AsSpan()) { } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Quaternion(LSLString str) : this(str.m_string.AsSpan()) { } + + public Quaternion(ReadOnlySpan str) { if (str.Length < 7) { @@ -487,15 +497,13 @@ namespace OpenSim.Region.ScriptEngine.Shared return; } - var strspan = str.AsSpan(); - int start = 0; int comma = 0; char c; do { - c = Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma); + c = Unsafe.Add(ref MemoryMarshal.GetReference(str), comma); if (c == ',' || c == '<') break; } @@ -506,7 +514,7 @@ namespace OpenSim.Region.ScriptEngine.Shared start = ++comma; while (++comma < str.Length) { - if (Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma) == ',') + if (Unsafe.Add(ref MemoryMarshal.GetReference(str), comma) == ',') break; } } @@ -517,7 +525,7 @@ namespace OpenSim.Region.ScriptEngine.Shared return; } - if (!double.TryParse(strspan[start..comma], NumberStyles.Float, Utils.EnUsCulture, out x)) + if (!double.TryParse(str[start..comma], NumberStyles.Float, Utils.EnUsCulture, out x)) { z = y = 0; s = 1; @@ -527,7 +535,7 @@ namespace OpenSim.Region.ScriptEngine.Shared start = ++comma; while (++comma < str.Length) { - if (Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma) == ',') + if (Unsafe.Add(ref MemoryMarshal.GetReference(str), comma) == ',') break; } if (comma > str.Length - 3) @@ -537,7 +545,7 @@ namespace OpenSim.Region.ScriptEngine.Shared return; } - if (!double.TryParse(strspan[start..comma], NumberStyles.Float, Utils.EnUsCulture, out y)) + if (!double.TryParse(str[start..comma], NumberStyles.Float, Utils.EnUsCulture, out y)) { z = x = 0; s = 1; @@ -546,7 +554,7 @@ namespace OpenSim.Region.ScriptEngine.Shared start = ++comma; while (++comma < str.Length) { - if (Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma) == ',') + if (Unsafe.Add(ref MemoryMarshal.GetReference(str), comma) == ',') break; } if (comma > str.Length - 1) @@ -556,7 +564,7 @@ namespace OpenSim.Region.ScriptEngine.Shared return; } - if (!double.TryParse(strspan[start..comma], NumberStyles.Float, Utils.EnUsCulture, out z)) + if (!double.TryParse(str[start..comma], NumberStyles.Float, Utils.EnUsCulture, out z)) { y = x = 0; s = 1; @@ -566,12 +574,12 @@ namespace OpenSim.Region.ScriptEngine.Shared start = ++comma; while (++comma < str.Length) { - c = Unsafe.Add(ref MemoryMarshal.GetReference(strspan), comma); + c = Unsafe.Add(ref MemoryMarshal.GetReference(str), comma); if (c == ' ' || c == '>') break; } - if (!double.TryParse(strspan[start..comma], NumberStyles.Float, Utils.EnUsCulture, out s)) + if (!double.TryParse(str[start..comma], NumberStyles.Float, Utils.EnUsCulture, out s)) { z = y = x = 0; s = 1; @@ -2095,12 +2103,12 @@ namespace OpenSim.Region.ScriptEngine.Shared public static implicit operator Vector3(LSLString s) { - return new Vector3(s.m_string); + return new Vector3(s); } public static implicit operator Quaternion(LSLString s) { - return new Quaternion(s.m_string); + return new Quaternion(s); } public static implicit operator LSLFloat(LSLString s) diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptInlines.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptInlines.cs index 1b3d83f4ee..d7df7d7829 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptInlines.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptInlines.cs @@ -63,6 +63,8 @@ namespace OpenSim.Region.ScriptEngine.Yengine /* * For those listed in noCheckRun, we just generate the call (simple computations). * For all others, we generate the call then a call to CheckRun(). + * note to self: a change here implies change on magic numbers to invalidate older code and + * script state */ noCheckRuns = new HashSet() { "llBase64ToString", diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptReduce.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptReduce.cs index f2d9cfd52b..4571262ddb 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptReduce.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptReduce.cs @@ -6761,8 +6761,8 @@ namespace OpenSim.Region.ScriptEngine.Yengine { if(val is LSL_Vector) return rVal; - if(val is string) - nval = new LSL_Vector((string)val); + if(val is string sval) + nval = new LSL_Vector(sval); } if(nval != null) diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptTypeCast.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptTypeCast.cs index 038e79b998..4b125443ab 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptTypeCast.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptTypeCast.cs @@ -926,7 +926,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine public static LSL_Vector ObjectToVector(object x) { if(x is LSL_String lsx) - return new LSL_Vector(lsx.m_string); + return new LSL_Vector(lsx); if(x is string sx) return new LSL_Vector(sx); return (LSL_Vector)x;