public void SetValue <T>(Script script, T obj, DynValue value) { this.CheckAccess(MemberDescriptorAccess.CanWrite, obj); object v = ScriptToClrConversions.DynValueToTypedValue <T>(value, default(T), false); SetValueImpl(script, obj, v); }
private static object ArrayIndexerSet(object arrayObj, ScriptExecutionContext ctx, CallbackArguments args) { Array array = (Array)arrayObj; int[] indices = BuildArrayIndices(args, args.Count - 1); DynValue value = args[args.Count - 1]; Type elemType = array.GetType().GetElementType(); object objValue = ScriptToClrConversions.DynValueToTypedValue <object>(value, null, false); array.SetValue(objValue, indices); return(DynValue.Void); }
/// <summary> /// Converts this MoonSharp DynValue to a CLR object. /// </summary> public T ToObject <T>() { return(ScriptToClrConversions.DynValueToTypedValue(this, default(T), true)); }
/// <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.IsValid) { 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.HasValue()) { if (arg.UserData.UnderlyingType.IsAssignableFrom(VarArgsArrayType)) { object o; arg.UserData.TryGet(out o); pars[i] = o; 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.DynValueToTypedValue <object>(extraArgs[ii], null, false), ii); } pars[i] = vararg; } // else, convert it else { var arg = args.RawGet(j, false); if (!arg.IsValid) { arg = DynValue.Void; } pars[i] = ScriptToClrConversions.DynValueToTypedValue <object>(arg, parameters[i].DefaultValue, parameters[i].HasDefaultValue); j += 1; } } return(pars); }