コード例 #1
0
ファイル: Evaluator.cs プロジェクト: sexypreneur/uREPL
        static public CompileResult Evaluate(string code, bool autoCompleteComma = true)
        {
            if (autoCompleteComma && !code.EndsWith(";"))
            {
                code += ";";
            }

            var result = new CompileResult();

            result.code = code;

            // find commands and expand it if found.
            if (RuntimeCommands.ConvertIntoCodeIfCommand(ref code) ||
                Commands.ConvertIntoCodeIfCommand(ref code))
            {
                // the give code is a command and converted into an actual code.
            }

            // eval the code using Mono.
            object ret            = null;
            bool   hasReturnValue = false;

            var  originalOutput = Mono.MessageOutput;
            var  errorWriter    = new System.IO.StringWriter();
            bool isPartial      = false;

            Mono.MessageOutput = errorWriter;
            try {
                isPartial = Mono.Evaluate(code, out ret, out hasReturnValue) != null;
            } catch (System.Exception e) {
                errorWriter.Write(e.Message);
            }
            Mono.MessageOutput = originalOutput;

            var error = errorWriter.ToString();

            if (!string.IsNullOrEmpty(error))
            {
                error = error.Replace("{interactive}", "");
                var lastLineBreakPos = error.LastIndexOf('\n');
                if (lastLineBreakPos != -1)
                {
                    error = error.Remove(lastLineBreakPos);
                }
                result.type  = CompileResult.Type.Error;
                result.error = error;
                return(result);
            }
            errorWriter.Dispose();

            if (isPartial)
            {
                result.type = CompileResult.Type.Partial;
                return(result);
            }

            result.type  = CompileResult.Type.Success;
            result.value = (hasReturnValue && ret != null) ? ret : "null";
            return(result);
        }
コード例 #2
0
ファイル: Evaluator.cs プロジェクト: sexypreneur/uREPL
 static private void ReferenceAllAssemblies()
 {
     // See the detailed information about this hack at:
     //   http://forum.unity3d.com/threads/mono-csharp-evaluator.102162/
     for (int n = 0; n < 2;)
     {
         foreach (var assembly in System.AppDomain.CurrentDomain.GetAssemblies())
         {
             if (assembly == null)
             {
                 continue;
             }
             Mono.ReferenceAssembly(assembly);
         }
         Mono.Evaluate("null;");
         n++;
     }
 }