public virtual void RegisterType(Type t, string name) { if (t == null) { throw new ArgumentNullException(nameof(t)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } lock (this) { var n = new LuaString(name); ILuaValue o = GlobalsTable.GetItemRaw(n); if (o != LuaNil.Nil) { throw new ArgumentException(string.Format(Resources.AlreadyRegistered, name)); } else { GlobalsTable.SetItemRaw(n, new LuaType(t)); } } }
public virtual void RegisterDelegate(Delegate d, string name) { if (d == null) { throw new ArgumentNullException(nameof(d)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } lock (this) { object o = GlobalsTable.GetItemRaw(_runtime.CreateValue(name)); if (o != LuaNil.Nil) { LuaOverloadFunction meth = o as LuaOverloadFunction; if (meth == null) { throw new ArgumentException(string.Format(Resources.AlreadyRegistered, name)); } meth.AddOverload(d); } else { GlobalsTable.SetItemRaw( _runtime.CreateValue(name), new LuaOverloadFunction(name, new[] { d.Method }, new[] { d.Target })); } } }
public override bool TrySetMember(SetMemberBinder binder, object value) { lock (this) { GlobalsTable.SetItemRaw(Runtime.CreateValue(binder.Name), Runtime.CreateValue(value)); } return(true); }
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value) { if (indexes != null && indexes.Length == 1) { lock (this) { GlobalsTable.SetItemRaw( _runtime.CreateValue(indexes[0]), _runtime.CreateValue(value)); } return(true); } else { return(base.TrySetIndex(binder, indexes, value)); } }
/// <summary> /// Provides the implementation for operations that get member values. Classes /// derived from the System.Dynamic.DynamicObject class can override this method /// to specify dynamic behavior for operations such as getting a value for a /// property. /// </summary> /// <param name="binder">Provides information about the object that called the dynamic operation. /// The binder.Name property provides the name of the member on which the dynamic /// operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) /// statement, where sampleObject is an instance of the class derived from the /// System.Dynamic.DynamicObject class, binder.Name returns "SampleProperty". /// The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param> /// <param name="result">The result of the get operation. For example, if the method is called for /// a property, you can assign the property value to result.</param> /// <returns>true if the operation is successful; otherwise, false. If this method returns /// false, the run-time binder of the language determines the behavior. (In most /// cases, a run-time exception is thrown.)</returns> public override bool TryGetMember(GetMemberBinder binder, out object result) { ILuaValue o; lock (this) o = GlobalsTable.GetItemRaw(_runtime.CreateValue(binder.Name)); MethodInfo asMethod = typeof(ILuaValue).GetMethod(nameof(ILuaValue.As)).MakeGenericMethod(binder.ReturnType); result = asMethod.Invoke(o, null); if (result is double) { result = new NumberProxy((double)result); } return(true); }
/// <summary> /// Provides the implementation for operations that get a value by index. Classes /// derived from the System.Dynamic.DynamicObject class can override this method /// to specify dynamic behavior for indexing operations. /// </summary> /// <param name="binder">Provides information about the operation.</param> /// <param name="indexes">The indexes that are used in the operation. For example, for the sampleObject[3] /// operation in C# (sampleObject(3) in Visual Basic), where sampleObject is /// derived from the DynamicObject class, indexes[0] is equal to 3.</param> /// <param name="result">The result of the index operation.</param> /// <returns>true if the operation is successful; otherwise, false. If this method returns /// false, the run-time binder of the language determines the behavior. (In most /// cases, a run-time exception is thrown.)</returns> public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { if (indexes != null && indexes.Length == 1) { ILuaValue o; lock (this) o = GlobalsTable.GetItemRaw(_runtime.CreateValue(indexes[0])); MethodInfo asMethod = typeof(ILuaValue).GetMethod(nameof(ILuaValue.As)).MakeGenericMethod(binder.ReturnType); result = asMethod.Invoke(o, null); if (result is double) { result = new NumberProxy((double)result); } return(true); } else { return(base.TryGetIndex(binder, indexes, out result)); } }