Пример #1
0
        public void LaunchScript(Script script, string runId, IStorageManager callerStorageManager, Action onRunning, Action <bool> onComplete)
        {
            AnalyticsHelper.FireEvent("LaunchScript", new Dictionary <string, object>
            {
                { "Name", script.Name },
                { "Id", script.Id },
                { "RunId", runId }
            });

            var key = GetScriptRunId(script.Id, runId);

            if (m_runningProcessors.ContainsKey(key))
            {
                // Can only run one instance at a time with this implementation.
                // It is entirely possible to run the same script with multiple
                // script processors as long as they have different state files.
                if (onComplete != null)
                {
                    onComplete(false);
                }

                return;
            }

            // Store script state in its own directory so we can reset all
            // scripts easily.
            var stateMgr = callerStorageManager.GetManager(GetScriptFolderName(script.Id), runId);

            var proc = new ScriptProcessor(script, stateMgr, runId);

            m_runningProcessors[key] = proc;

            proc.Run(onRunning, onComplete);
        }
Пример #2
0
        /// <summary>
        /// Executes the function.
        /// </summary>
        /// <param name="processor">The processor with context that called this functions.</param>
        /// <param name="caller">The calling object.</param>
        /// <param name="This">The "This" reference used in the call context.</param>
        /// <param name="parameters">The parameters used in this function call.</param>
        public SObject Call(ScriptProcessor processor, SObject caller, SObject This, SObject[] parameters)
        {
            var     functionProcessor = new ScriptProcessor(processor.Context, processor.GetLineNumber());
            SObject functionReturnObject;

            if (HasBuiltInMethod)
            {
                if (Method != null)
                {
                    functionReturnObject = Method(functionProcessor, caller, This, parameters);
                }
                else
                {
                    var dotNetParams    = parameters.Select(ScriptOutAdapter.Translate);
                    var dotNetReturnObj = DotNetMethod(ScriptOutAdapter.Translate(caller), new ScriptObjectLink(processor, caller), dotNetParams.ToArray());
                    functionReturnObject = ScriptInAdapter.Translate(processor, dotNetReturnObj);
                }
            }
            else
            {
                for (var i = 0; i < _parameters.Length; i++)
                {
                    functionProcessor.Context.AddVariable(_parameters[i],
                                                          parameters.Length > i ? parameters[i] : functionProcessor.Undefined);
                }

                functionProcessor.Context.This = This;
                functionReturnObject           = functionProcessor.Run(Body);
            }

            return(functionReturnObject);
        }
Пример #3
0
        /// <summary>
        /// Creates a new <see cref="ScriptProcessor"/> and runs the provided source code.
        /// </summary>
        internal static ScriptProcessor GetRun(string source)
        {
            var processor = new ScriptProcessor();

            processor.Run(source);

            return(processor);
        }
Пример #4
0
        internal void LaunchScript(ScriptLauncher launcher, FrameContext callingFrameContext, string runId, Action <bool> onComplete)
        {
            if (launcher == null ||
                launcher.ScriptReference == null ||
                launcher.ScriptReference.ObjectId == null)
            {
                if (onComplete != null)
                {
                    onComplete(false);
                }

                return;
            }

            var script = GetScript(launcher.ScriptReference.ObjectId);

            if (script == null)
            {
                if (onComplete != null)
                {
                    onComplete(false);
                }

                return;
            }

            var key = GetScriptRunId(script.Id, runId);

            if (m_runningProcessors.ContainsKey(key))
            {
                // Can only run one instance at a time with this implementation.
                // It is entirely possible to run the same script with multiple
                // script processors as long as they have different state files.
                if (onComplete != null)
                {
                    onComplete(false);
                }

                return;
            }

            // Store script state in its own directory so we can reset all
            // scripts easily.
            var folderName = callingFrameContext.ScriptContext.GetCompactId(launcher.Id);

            var stateMgr = callingFrameContext.ScriptContext.StorageManager.GetManager(folderName);

            var proc = new ScriptProcessor(launcher, callingFrameContext, script, stateMgr, runId);

            proc.HandlingError += Proc_HandlingError;

            m_runningProcessors[key] = proc;

            proc.Run(null, onComplete);
        }
Пример #5
0
        public void ObjectTranslateTest()
        {
            var processor = new ScriptProcessor();

            ScriptContextManipulator.AddPrototype(processor, typeof(Pokemon));

            processor.Run("var p = new Pokemon(); p.SetName(\"Pika\");");

            object objp = ScriptContextManipulator.GetVariableTranslated(processor, "p");

            Assert.IsTrue(objp.GetType() == typeof(Pokemon));

            Pokemon p = (Pokemon)objp;

            Assert.AreEqual("Pikachu", p.OriginalName);
            Assert.AreEqual("Pika", p.Name);
        }
Пример #6
0
        /// <summary>
        /// Executes the function.
        /// </summary>
        /// <param name="processor">The processor with context that called this functions.</param>
        /// <param name="caller">The calling object.</param>
        /// <param name="This">The "This" reference used in the call context.</param>
        /// <param name="parameters">The parameters used in this function call.</param>
        public SObject Call(ScriptProcessor processor, SObject caller, SObject This, SObject[] parameters)
        {
            ScriptProcessor functionProcessor = new ScriptProcessor(processor.Context);
            SObject functionReturnObject;

            if (_method != null)
            {
                functionReturnObject = _method(functionProcessor, caller, This, parameters);
            }
            else
            {
                for (int i = 0; i < _parameters.Length; i++)
                {
                    if (parameters.Length > i)
                    {
                        functionProcessor.Context.AddVariable(_parameters[i], parameters[i]);
                    }
                    else
                    {
                        functionProcessor.Context.AddVariable(_parameters[i], functionProcessor.Undefined);
                    }
                }

                functionProcessor.Context.This = This;
                functionReturnObject = functionProcessor.Run(Body);
            }

            if (functionProcessor.ErrorHandler.ThrownError)
                processor.ErrorHandler.ThrowError(functionProcessor.ErrorHandler.ErrorObject);

            return functionReturnObject;
        }
Пример #7
0
        /// <summary>
        /// Executes the function.
        /// </summary>
        /// <param name="processor">The processor with context that called this functions.</param>
        /// <param name="caller">The calling object.</param>
        /// <param name="This">The "This" reference used in the call context.</param>
        /// <param name="parameters">The parameters used in this function call.</param>
        public SObject Call(ScriptProcessor processor, SObject caller, SObject This, SObject[] parameters)
        {
            var functionProcessor = new ScriptProcessor(processor.Context, processor.GetLineNumber());
            SObject functionReturnObject;

            if (HasBuiltInMethod)
            {
                if (Method != null)
                {
                    functionReturnObject = Method(functionProcessor, caller, This, parameters);
                }
                else
                {
                    var dotNetParams = parameters.Select(p => ScriptOutAdapter.Translate(p));
                    var dotNetReturnObj = DotNetMethod(ScriptOutAdapter.Translate(caller), new ScriptObjectLink(processor, caller), dotNetParams.ToArray());
                    functionReturnObject = ScriptInAdapter.Translate(processor, dotNetReturnObj);
                }
            }
            else
            {
                for (var i = 0; i < _parameters.Length; i++)
                {
                    if (parameters.Length > i)
                    {
                        functionProcessor.Context.AddVariable(_parameters[i], parameters[i]);
                    }
                    else
                    {
                        functionProcessor.Context.AddVariable(_parameters[i], functionProcessor.Undefined);
                    }
                }

                functionProcessor.Context.This = This;
                functionReturnObject = functionProcessor.Run(Body);
            }

            return functionReturnObject;
        }