Переглянути джерело

Fixed: "local" doesn't work rigth #120

Xanathar 10 роки тому
батько
коміт
cb845f2d03

+ 26 - 96
src/DevTools/Playground/Program.cs

@@ -1,130 +1,60 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
 using System.Threading;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using MoonSharp.Interpreter;
 using MoonSharp.Interpreter;
 using MoonSharp.Interpreter.Interop;
 using MoonSharp.Interpreter.Interop;
+using MoonSharp.RemoteDebugger;
 
 
 namespace Playground
 namespace Playground
 {
 {
-	public class ScriptSharedVars : IUserDataType
-	{
-		Dictionary<string, DynValue> m_Values = new Dictionary<string, DynValue>();
-		object m_Lock = new object();
-
-		public object this[string property]
-		{
-			get { return m_Values[property].ToObject(); }
-			set { m_Values[property] = DynValue.FromObject(null, value); }
-		}
-
-		public DynValue Index(Script script, DynValue index, bool isDirectIndexing)
-		{
-			if (index.Type != DataType.String)
-				throw new ScriptRuntimeException("string property was expected");
-
-			lock (m_Lock)
-			{
-				if (m_Values.ContainsKey(index.String))
-					return m_Values[index.String].Clone();
-				else
-					return DynValue.Nil;
-			}
-		}
-
-		public bool SetIndex(Script script, DynValue index, DynValue value, bool isDirectIndexing)
-		{
-			if (index.Type != DataType.String)
-				throw new ScriptRuntimeException("string property was expected");
-
-			lock (m_Lock)
-			{
-				switch (value.Type)
-				{
-					case DataType.Void:
-					case DataType.Nil:
-						m_Values.Remove(index.String);
-						return true;
-					case DataType.UserData:
-						// HERE YOU CAN CHOOSE A DIFFERENT POLICY.. AND TRY TO SHARE IF NEEDED. DANGEROUS, THOUGH.
-						throw new ScriptRuntimeException("Cannot share a value of type {0}", value.Type.ToErrorTypeString());
-					case DataType.ClrFunction:
-						// HERE YOU CAN CHOOSE A DIFFERENT POLICY.. AND TRY TO SHARE IF NEEDED. DANGEROUS, THOUGH.
-						throw new ScriptRuntimeException("Cannot share a value of type {0}", value.Type.ToErrorTypeString());
-					case DataType.Boolean:
-					case DataType.Number:
-					case DataType.String:
-						m_Values[index.String] = value.Clone();
-						return true;
-					case DataType.Function:
-					case DataType.Table:
-					case DataType.Tuple:
-					case DataType.Thread:
-					case DataType.TailCallRequest:
-					case DataType.YieldRequest:
-					default:
-						throw new ScriptRuntimeException("Cannot share a value of type {0}", value.Type.ToErrorTypeString());
-				}
-			}
-		}
-
-		public DynValue MetaIndex(Script script, string metaname)
-		{
-			return null;
-		}
-	}
-
 	
 	
 	class Program
 	class Program
 	{
 	{
 		static void Main(string[] args)
 		static void Main(string[] args)
 		{
 		{
-			UserData.RegisterType<ScriptSharedVars>();
+			Script S = new Script(CoreModules.Basic);
 
 
-			ScriptSharedVars sharedVars = new ScriptSharedVars();
+			RemoteDebuggerService remoteDebugger;
 
 
-			sharedVars["mystring"] = "let's go:";
+			remoteDebugger = new RemoteDebuggerService();
+		
+			remoteDebugger.Attach(S, "MyScript", false);
 
 
-			ManualResetEvent ev = new ManualResetEvent(false);
+			Process.Start(remoteDebugger.HttpUrlStringLocalHost);
+	
+			S.DoString(@"
 
 
-			StartScriptThread(sharedVars, "bum ", ev);
-			StartScriptThread(sharedVars, "chack ", ev);
+local hi = 'hello'
 
 
-			ev.Set();
+local function test()
+    print(hi)
+end
 
 
-			Thread.Sleep(2000); // too bored to do proper synchronization at this time of the evening...
+test();
 
 
-			Console.WriteLine("{0}", sharedVars["mystring"]);
+hi = 'X'
 
 
-			Console.ReadKey();
-		}
+test();
+
+local hi = '!';
+
+test();
 
 
-		private static void StartScriptThread(ScriptSharedVars sharedVars, string somestr, ManualResetEvent ev)
-		{
-			Thread T = new Thread((ThreadStart)delegate
-			{
-				string script = @"
-				for i = 1, 1000 do
-					shared.mystring = shared.mystring .. somestring;
-				end
-			";
 
 
-				Script S = new Script();
 
 
-				S.Globals["shared"] = sharedVars;
-				S.Globals["somestring"] = somestr;
 
 
-				ev.WaitOne();
+");
 
 
-				S.DoString(script);
-			});
+			Console.WriteLine("DONE");
 
 
-			T.IsBackground = true;
-			T.Name = "Lua script for " + somestr;
-			T.Start();
+			Console.ReadKey();
 		}
 		}
 
 
+
+
 	}
 	}
 }
 }

+ 34 - 0
src/MoonSharp.Interpreter.Tests/EndToEnd/ClosureTests.cs

@@ -288,5 +288,39 @@ return g(|x,y|f(x,y,1), 2)
 			Assert.AreEqual(10, res.Number);
 			Assert.AreEqual(10, res.Number);
 		}
 		}
 
 
+
+		[Test]
+		public void LocalRedefinition()
+		{
+			string script = @"
+
+				result = ''
+
+				local hi = 'hello'
+
+				local function test()
+					result = result .. hi;
+				end
+
+				test();
+
+				hi = 'X'
+
+				test();
+
+				local hi = '!';
+
+				test();
+
+				return result;
+								";
+
+			DynValue res = new Script(CoreModules.Preset_HardSandbox).DoString(script);
+
+			Assert.AreEqual(DataType.String, res.Type);
+			Assert.AreEqual("helloXX", res.String);
+		}
+
+
 	}
 	}
 }
 }

+ 8 - 9
src/MoonSharp.Interpreter/Execution/Scopes/BuildTimeScopeBlock.cs

@@ -17,6 +17,14 @@ namespace MoonSharp.Interpreter.Execution.Scopes
 		Dictionary<string, SymbolRef> m_DefinedNames = new Dictionary<string, SymbolRef>();
 		Dictionary<string, SymbolRef> m_DefinedNames = new Dictionary<string, SymbolRef>();
 
 
 
 
+
+		internal void Rename(string name)
+		{
+			SymbolRef sref = m_DefinedNames[name];
+			m_DefinedNames.Remove(name);
+			m_DefinedNames.Add(string.Format("@{0}_{1}", name, Guid.NewGuid().ToString("N")), sref);
+		}
+
 		internal BuildTimeScopeBlock(BuildTimeScopeBlock parent)
 		internal BuildTimeScopeBlock(BuildTimeScopeBlock parent)
 		{
 		{
 			Parent = parent;
 			Parent = parent;
@@ -139,14 +147,5 @@ namespace MoonSharp.Interpreter.Execution.Scopes
 
 
 			m_PendingGotos.Clear();
 			m_PendingGotos.Clear();
 		}
 		}
-
-
-
-
-
-
-
-
-
 	}
 	}
 }
 }

+ 6 - 1
src/MoonSharp.Interpreter/Execution/Scopes/BuildTimeScopeFrame.cs

@@ -69,7 +69,12 @@ namespace MoonSharp.Interpreter.Execution.Scopes
 
 
 		internal SymbolRef TryDefineLocal(string name)
 		internal SymbolRef TryDefineLocal(string name)
 		{
 		{
-			return m_ScopeTreeHead.Find(name) ?? m_ScopeTreeHead.Define(name);
+			if (m_ScopeTreeHead.Find(name) != null)
+			{
+				m_ScopeTreeHead.Rename(name);
+			}
+
+			return m_ScopeTreeHead.Define(name);
 		}
 		}
 
 
 		internal void ResolveLRefs()
 		internal void ResolveLRefs()

+ 1 - 1
src/MoonSharp.Interpreter/Script.cs

@@ -28,7 +28,7 @@ namespace MoonSharp.Interpreter
 		/// <summary>
 		/// <summary>
 		/// The version of the MoonSharp engine
 		/// The version of the MoonSharp engine
 		/// </summary>
 		/// </summary>
-		public const string VERSION = "1.0.0.0"; 
+		public const string VERSION = "1.1.0.0"; 
 
 
 		/// <summary>
 		/// <summary>
 		/// The Lua version being supported
 		/// The Lua version being supported

+ 1 - 1
src/MoonSharp.Interpreter/Tree/Expressions/FunctionDefinitionExpression.cs

@@ -54,7 +54,7 @@ namespace MoonSharp.Interpreter.Tree.Expressions
 			if (globalContext != null)
 			if (globalContext != null)
 			{
 			{
 				m_GlobalEnv = globalContext;
 				m_GlobalEnv = globalContext;
-				m_Env = lcontext.Scope.TryDefineLocal(WellKnownSymbols.ENV);
+				m_Env = lcontext.Scope.DefineLocal(WellKnownSymbols.ENV);
 			}
 			}
 			else
 			else
 			{
 			{

+ 9 - 1
src/MoonSharp.RemoteDebugger/Network/Utf8TcpPeer.cs

@@ -40,6 +40,13 @@ namespace MoonSharp.RemoteDebugger.Network
 			{
 			{
 				bool dataReceived = false;
 				bool dataReceived = false;
 				int size = m_Socket.EndReceive(ar);
 				int size = m_Socket.EndReceive(ar);
+
+				if (size == 0)
+				{
+					CloseConnection("zero byte received");
+					return;
+				}
+
 				int ptr0 = m_PrevSize;
 				int ptr0 = m_PrevSize;
 				m_PrevSize += size;
 				m_PrevSize += size;
 
 
@@ -75,7 +82,8 @@ namespace MoonSharp.RemoteDebugger.Network
 					}
 					}
 				} while (dataReceived);
 				} while (dataReceived);
 
 
-				m_Socket.BeginReceive(m_RecvBuffer, m_PrevSize, m_RecvBuffer.Length - m_PrevSize, SocketFlags.None, OnDataReceived, null);
+				if (m_Socket.Connected)
+					m_Socket.BeginReceive(m_RecvBuffer, m_PrevSize, m_RecvBuffer.Length - m_PrevSize, SocketFlags.None, OnDataReceived, null);
 			}
 			}
 			catch (SocketException ex)
 			catch (SocketException ex)
 			{
 			{