/// <summary>
        ///     Finds a symbol in the expression
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public SymbolRef FindSymbol(ScriptExecutionContext context)
        {
            this.CheckScriptOwnership(context.GetScript());

            if (m_Exp != null)
                return m_Exp.FindDynamic(context);
            return null;
        }
		/// <summary>
		/// Evaluates the expression
		/// </summary>
		/// <param name="context">The context.</param>
		/// <returns></returns>
		public DynValue Evaluate(ScriptExecutionContext context = null)
		{
			context = context ?? OwnerScript.CreateDynamicExecutionContext();

			this.CheckScriptOwnership(context.GetScript());

			if (m_Constant != null)
				return m_Constant;

			return m_Exp.Eval(context);
		}
		/// <summary>
		/// Invokes the callback function
		/// </summary>
		/// <param name="executionContext">The execution context.</param>
		/// <param name="args">The arguments.</param>
		/// <param name="isMethodCall">if set to <c>true</c> this is a method call.</param>
		/// <returns></returns>
		public DynValue Invoke(ScriptExecutionContext executionContext, IList<DynValue> args, bool isMethodCall = false)
		{
			if (isMethodCall)
			{
				var colon = executionContext.GetScript().Options.ColonOperatorClrCallbackBehaviour;

				if (colon == ColonOperatorBehaviour.TreatAsColon)
					isMethodCall = false;
				else if (colon == ColonOperatorBehaviour.TreatAsDotOnUserData)
					isMethodCall = (args.Count > 0 && args[0].Type == DataType.UserData);
			}

			return ClrCallback(executionContext, new CallbackArguments(args, isMethodCall));
		}
 /// <summary>
 ///     Invokes the callback function
 /// </summary>
 /// <param name="executionContext">The execution context.</param>
 /// <param name="args">The arguments.</param>
 /// <param name="isMethodCall">if set to <c>true</c> this is a method call.</param>
 /// <returns></returns>
 public DynValue Invoke(ScriptExecutionContext executionContext, IList<DynValue> args, bool isMethodCall = false)
 {
     return ClrCallback(executionContext, new CallbackArguments(args, isMethodCall));
 }
		/// <summary>
		/// The internal callback which actually executes the method
		/// </summary>
		/// <param name="script">The script.</param>
		/// <param name="obj">The object.</param>
		/// <param name="context">The context.</param>
		/// <param name="args">The arguments.</param>
		/// <returns></returns>
		public override DynValue Execute(Script script, object obj, ScriptExecutionContext context, CallbackArguments args)
		{
			this.CheckAccess(MemberDescriptorAccess.CanExecute, obj);

			if (AccessMode == InteropAccessMode.LazyOptimized &&
				m_OptimizedFunc == null && m_OptimizedAction == null)
				((IOptimizableDescriptor)this).Optimize();

			List<int> outParams = null;
			object[] pars = base.BuildArgumentList(script, obj, context, args, out outParams);
			object retv = null;

			if (m_OptimizedFunc != null)
			{
				retv = m_OptimizedFunc(obj, pars);
			}
			else if (m_OptimizedAction != null)
			{
				m_OptimizedAction(obj, pars);
				retv = DynValue.Void;
			}
			else if (m_IsAction)
			{
				MethodInfo.Invoke(obj, pars);
				retv = DynValue.Void;
			}
			else
			{
				if (IsConstructor)
					retv = ((ConstructorInfo)MethodInfo).Invoke(pars);
				else
					retv = MethodInfo.Invoke(obj, pars);
			}

			return BuildReturnValue(script, outParams, pars, retv);
		}
Exemple #6
0
 /// <summary>
 /// Resumes the coroutine.
 /// </summary>
 /// <param name="context">The ScriptExecutionContext.</param>
 /// <returns></returns>
 public DynValue Resume(ScriptExecutionContext context)
 {
     return(Resume(context, new DynValue[0]));
 }
Exemple #7
0
 /// <summary>
 /// Invokes the callback function
 /// </summary>
 /// <param name="executionContext">The execution context.</param>
 /// <param name="args">The arguments.</param>
 /// <param name="isMethodCall">if set to <c>true</c> this is a method call.</param>
 /// <returns></returns>
 public DynValue Invoke(ScriptExecutionContext executionContext, IList <DynValue> args, bool isMethodCall = false)
 {
     return(ClrCallback(executionContext, new CallbackArguments(args, isMethodCall)));
 }
Exemple #8
0
		public static DynValue Check(ScriptExecutionContext executionContext, CallbackArguments values)
		{
			//foreach (var val in values.GetArray())
			//{
			//	g_MoonSharpStr.Append(val.ToPrintString());
			//}

			//g_MoonSharpStr.AppendLine();
			return DynValue.Nil;
		}
 /// <summary>
 /// Resumes the coroutine
 ///
 /// This method is supported only on .NET 4.x and .NET 4.x PCL targets.
 /// </summary>
 /// <param name="cor">The coroutine</param>
 /// <param name="context">The ScriptExecutionContext.</param>
 /// <param name="args">The arguments.</param>
 /// <returns></returns>
 public static Task <DynValue> ResumeAsync(this Coroutine cor, ScriptExecutionContext context, params object[] args)
 {
     return(ExecAsync(() => cor.Resume(context, args)));
 }
 /// <summary>
 /// Resumes the coroutine.
 ///
 /// This method is supported only on .NET 4.x and .NET 4.x PCL targets.
 /// </summary>
 /// <param name="cor">The coroutine</param>
 /// <param name="context">The ScriptExecutionContext.</param>
 /// <returns></returns>
 public static Task <DynValue> ResumeAsync(this Coroutine cor, ScriptExecutionContext context)
 {
     return(ExecAsync(() => cor.Resume(context)));
 }
		/// <summary>
		/// The internal callback which actually executes the method
		/// </summary>
		/// <param name="script">The script.</param>
		/// <param name="obj">The object.</param>
		/// <param name="context">The context.</param>
		/// <param name="args">The arguments.</param>
		/// <returns></returns>
		public abstract DynValue Execute(Script script, object obj, ScriptExecutionContext context, CallbackArguments args);
		/// <summary>
		/// Builds the argument list.
		/// </summary>
		/// <param name="script">The script.</param>
		/// <param name="obj">The object.</param>
		/// <param name="context">The context.</param>
		/// <param name="args">The arguments.</param>
		/// <param name="outParams">Output: A list containing the indices of all "out" parameters, or null if no out parameters are specified.</param>
		/// <returns>The arguments, appropriately converted.</returns>
		protected virtual object[] BuildArgumentList(Script script, object obj, ScriptExecutionContext context, CallbackArguments args,
			out List<int> outParams)
		{
			ParameterDescriptor[] parameters = Parameters;

			object[] pars = new object[parameters.Length];

			int j = args.IsMethodCall ? 1 : 0;

			outParams = null;

			for (int i = 0; i < pars.Length; i++)
			{
				// keep track of out and ref params
				if (parameters[i].Type.IsByRef)
				{
					if (outParams == null) outParams = new List<int>();
					outParams.Add(i);
				}

				// if an ext method, we have an obj -> fill the first param
				if (ExtensionMethodType != null && obj != null && i == 0)
				{
					pars[i] = obj;
					continue;
				}
				// else, fill types with a supported type
				else if (parameters[i].Type == typeof(Script))
				{
					pars[i] = script;
				}
				else if (parameters[i].Type == typeof(ScriptExecutionContext))
				{
					pars[i] = context;
				}
				else if (parameters[i].Type == typeof(CallbackArguments))
				{
					pars[i] = args.SkipMethodCall();
				}
				// else, ignore out params
				else if (parameters[i].IsOut)
				{
					pars[i] = null;
				}
				else if (i == parameters.Length - 1 && VarArgsArrayType != null)
				{
					List<DynValue> extraArgs = new List<DynValue>();

					while (true)
					{
						DynValue arg = args.RawGet(j, false);
						j += 1;
						if (arg != null)
							extraArgs.Add(arg);
						else
							break;
					}

					// here we have to worry we already have an array.. damn. We only support this for userdata.
					// remains to be analyzed what's the correct behavior here. For example, let's take a params object[]..
					// given a single table parameter, should it use it as an array or as an object itself ?
					if (extraArgs.Count == 1)
					{
						DynValue arg = extraArgs[0];

						if (arg.Type == DataType.UserData && arg.UserData.Object != null)
						{
							if (VarArgsArrayType.IsAssignableFrom(arg.UserData.Object.GetType()))
							{
								pars[i] = arg.UserData.Object;
								continue;
							}
						}
					}

					// ok let's create an array, and loop
					Array vararg = Array.CreateInstance(VarArgsElementType, extraArgs.Count);

					for (int ii = 0; ii < extraArgs.Count; ii++)
					{
						vararg.SetValue(ScriptToClrConversions.DynValueToObjectOfType(extraArgs[ii], VarArgsElementType,
						null, false), ii);
					}

					pars[i] = vararg;

				}
				// else, convert it
				else
				{
					var arg = args.RawGet(j, false) ?? DynValue.Void;
					pars[i] = ScriptToClrConversions.DynValueToObjectOfType(arg, parameters[i].Type,
						parameters[i].DefaultValue, parameters[i].HasDefaultValue);
					j += 1;
				}
			}

			return pars;
		}