using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Runtime.Remoting; using System.Security.Permissions; using AspClassic.Scripting.Runtime; using AspClassic.Scripting.Utils; namespace AspClassic.Scripting.Hosting; public sealed class ObjectOperations : MarshalByRefObject { private readonly DynamicOperations _ops; private readonly ScriptEngine _engine; public ScriptEngine Engine => _engine; internal ObjectOperations(DynamicOperations ops, ScriptEngine engine) { _ops = ops; _engine = engine; } public bool IsCallable(object obj) { return _ops.IsCallable(obj); } public dynamic Invoke(object obj, params object[] parameters) { return _ops.Invoke(obj, parameters); } public dynamic InvokeMember(object obj, string memberName, params object[] parameters) { return _ops.InvokeMember(obj, memberName, parameters); } public dynamic CreateInstance(object obj, params object[] parameters) { return _ops.CreateInstance(obj, parameters); } public dynamic GetMember(object obj, string name) { return _ops.GetMember(obj, name); } public T GetMember(object obj, string name) { return _ops.GetMember(obj, name); } public bool TryGetMember(object obj, string name, out object value) { return _ops.TryGetMember(obj, name, out value); } public bool ContainsMember(object obj, string name) { return _ops.ContainsMember(obj, name); } public void RemoveMember(object obj, string name) { _ops.RemoveMember(obj, name); } public void SetMember(object obj, string name, object value) { _ops.SetMember(obj, name, value); } public void SetMember(object obj, string name, T value) { _ops.SetMember(obj, name, value); } public dynamic GetMember(object obj, string name, bool ignoreCase) { return _ops.GetMember(obj, name, ignoreCase); } public T GetMember(object obj, string name, bool ignoreCase) { return _ops.GetMember(obj, name, ignoreCase); } public bool TryGetMember(object obj, string name, bool ignoreCase, out object value) { return _ops.TryGetMember(obj, name, ignoreCase, out value); } public bool ContainsMember(object obj, string name, bool ignoreCase) { return _ops.ContainsMember(obj, name, ignoreCase); } public void RemoveMember(object obj, string name, bool ignoreCase) { _ops.RemoveMember(obj, name, ignoreCase); } public void SetMember(object obj, string name, object value, bool ignoreCase) { _ops.SetMember(obj, name, value, ignoreCase); } public void SetMember(object obj, string name, T value, bool ignoreCase) { _ops.SetMember(obj, name, value, ignoreCase); } public T ConvertTo(object obj) { return _ops.ConvertTo(obj); } public object ConvertTo(object obj, Type type) { ContractUtils.RequiresNotNull(type, "type"); return _ops.ConvertTo(obj, type); } public bool TryConvertTo(object obj, out T result) { return _ops.TryConvertTo(obj, out result); } public bool TryConvertTo(object obj, Type type, out object result) { return _ops.TryConvertTo(obj, type, out result); } public T ExplicitConvertTo(object obj) { return _ops.ExplicitConvertTo(obj); } public object ExplicitConvertTo(object obj, Type type) { ContractUtils.RequiresNotNull(type, "type"); return _ops.ExplicitConvertTo(obj, type); } public bool TryExplicitConvertTo(object obj, out T result) { return _ops.TryExplicitConvertTo(obj, out result); } public bool TryExplicitConvertTo(object obj, Type type, out object result) { return _ops.TryExplicitConvertTo(obj, type, out result); } public T ImplicitConvertTo(object obj) { return _ops.ImplicitConvertTo(obj); } public object ImplicitConvertTo(object obj, Type type) { ContractUtils.RequiresNotNull(type, "type"); return _ops.ImplicitConvertTo(obj, type); } public bool TryImplicitConvertTo(object obj, out T result) { return _ops.TryImplicitConvertTo(obj, out result); } public bool TryImplicitConvertTo(object obj, Type type, out object result) { return _ops.TryImplicitConvertTo(obj, type, out result); } public dynamic DoOperation(ExpressionType operation, object target) { return _ops.DoOperation(operation, target); } public TResult DoOperation(ExpressionType operation, TTarget target) { return _ops.DoOperation(operation, target); } public dynamic DoOperation(ExpressionType operation, object target, object other) { return _ops.DoOperation(operation, target, other); } public TResult DoOperation(ExpressionType operation, TTarget target, TOther other) { return _ops.DoOperation(operation, target, other); } public dynamic Add(object self, object other) { return DoOperation(ExpressionType.Add, self, other); } public dynamic Subtract(object self, object other) { return DoOperation(ExpressionType.Subtract, self, other); } public dynamic Power(object self, object other) { return DoOperation(ExpressionType.Power, self, other); } public dynamic Multiply(object self, object other) { return DoOperation(ExpressionType.Multiply, self, other); } public dynamic Divide(object self, object other) { return DoOperation(ExpressionType.Divide, self, other); } public dynamic Modulo(object self, object other) { return DoOperation(ExpressionType.Modulo, self, other); } public dynamic LeftShift(object self, object other) { return DoOperation(ExpressionType.LeftShift, self, other); } public dynamic RightShift(object self, object other) { return DoOperation(ExpressionType.RightShift, self, other); } public dynamic BitwiseAnd(object self, object other) { return DoOperation(ExpressionType.And, self, other); } public dynamic BitwiseOr(object self, object other) { return DoOperation(ExpressionType.Or, self, other); } public dynamic ExclusiveOr(object self, object other) { return DoOperation(ExpressionType.ExclusiveOr, self, other); } public bool LessThan(object self, object other) { return ConvertTo(_ops.DoOperation(ExpressionType.LessThan, self, other)); } public bool GreaterThan(object self, object other) { return ConvertTo(_ops.DoOperation(ExpressionType.GreaterThan, self, other)); } public bool LessThanOrEqual(object self, object other) { return ConvertTo(_ops.DoOperation(ExpressionType.LessThanOrEqual, self, other)); } public bool GreaterThanOrEqual(object self, object other) { return ConvertTo(_ops.DoOperation(ExpressionType.GreaterThanOrEqual, self, other)); } public bool Equal(object self, object other) { return ConvertTo(_ops.DoOperation(ExpressionType.Equal, self, other)); } public bool NotEqual(object self, object other) { return ConvertTo(_ops.DoOperation(ExpressionType.NotEqual, self, other)); } [Obsolete("Use Format method instead.")] public string GetCodeRepresentation(object obj) { return obj.ToString(); } public string Format(object obj) { return _ops.Format(obj); } public IList GetMemberNames(object obj) { return _ops.GetMemberNames(obj); } public string GetDocumentation(object obj) { return _ops.GetDocumentation(obj); } public IList GetCallSignatures(object obj) { return _ops.GetCallSignatures(obj); } [Obsolete("Use Invoke instead")] public object Call(object obj, params object[] parameters) { return _ops.Invoke(obj, parameters); } [Obsolete("Use the ExpressionType overload instead")] public object DoOperation(Operators op, object target) { ExpressionType linqOp = GetLinqOp(op); return _ops.DoOperation(linqOp, target); } [Obsolete("Use ExpressionType overload instead")] public TResult DoOperation(Operators op, TTarget target) { return _ops.DoOperation(GetLinqOp(op), target); } [Obsolete] private static ExpressionType GetLinqOp(Operators op) { ExpressionType? expressionType = null; return (op switch { Operators.Positive => ExpressionType.UnaryPlus, Operators.Negate => ExpressionType.Negate, Operators.OnesComplement => ExpressionType.OnesComplement, Operators.IsFalse => ExpressionType.IsFalse, Operators.Decrement => ExpressionType.Decrement, Operators.Increment => ExpressionType.Increment, _ => throw new InvalidOperationException($"Unrecognized shared operation: {op}"), }); } [Obsolete("Use Modulo instead")] public object Modulus(object self, object other) { return Modulo(self, other); } [Obsolete("Use ExpressionType overload instead")] public object DoOperation(Operators op, object target, object other) { return _ops.DoOperation(GetLinqBinaryOp(op), target, other); } [Obsolete] private static ExpressionType GetLinqBinaryOp(Operators op) { ExpressionType? expressionType = null; return (op switch { Operators.Add => ExpressionType.Add, Operators.BitwiseAnd => ExpressionType.And, Operators.Divide => ExpressionType.Divide, Operators.ExclusiveOr => ExpressionType.ExclusiveOr, Operators.Mod => ExpressionType.Modulo, Operators.Multiply => ExpressionType.Multiply, Operators.BitwiseOr => ExpressionType.Or, Operators.Power => ExpressionType.Power, Operators.RightShift => ExpressionType.RightShift, Operators.LeftShift => ExpressionType.LeftShift, Operators.Subtract => ExpressionType.Subtract, Operators.Equals => ExpressionType.Equal, Operators.GreaterThan => ExpressionType.GreaterThan, Operators.GreaterThanOrEqual => ExpressionType.GreaterThanOrEqual, Operators.LessThan => ExpressionType.LessThan, Operators.LessThanOrEqual => ExpressionType.LessThanOrEqual, Operators.NotEquals => ExpressionType.NotEqual, _ => throw new InvalidOperationException($"Unrecognized shared operation: {op}"), }); } [Obsolete("Use the ExpressionType overload instead")] public TResult DoOperation(Operators op, TTarget target, TOther other) { return _ops.DoOperation(GetLinqBinaryOp(op), target, other); } [Obsolete("Use Invoke instead")] public ObjectHandle Call(ObjectHandle obj, params ObjectHandle[] parameters) { return Invoke(obj, parameters); } [Obsolete("Use Invoke instead")] public ObjectHandle Call(ObjectHandle obj, params object[] parameters) { return Invoke(obj, parameters); } [Obsolete("Use the ExpressionType overload instead")] public object DoOperation(Operators op, ObjectHandle target) { return DoOperation(op, GetLocalObject(target)); } [Obsolete("Use the ExpressionType enum instead")] public ObjectHandle DoOperation(Operators op, ObjectHandle target, ObjectHandle other) { return new ObjectHandle(DoOperation(op, GetLocalObject(target), GetLocalObject(other))); } [Obsolete("Use Modulo instead")] public ObjectHandle Modulus(ObjectHandle self, ObjectHandle other) { return Modulo(self, other); } public bool IsCallable([NotNull] ObjectHandle obj) { return IsCallable(GetLocalObject(obj)); } public ObjectHandle Invoke([NotNull] ObjectHandle obj, params ObjectHandle[] parameters) { ContractUtils.RequiresNotNull(parameters, "parameters"); return new ObjectHandle((object)Invoke(GetLocalObject(obj), GetLocalObjects(parameters))); } public ObjectHandle Invoke([NotNull] ObjectHandle obj, params object[] parameters) { return new ObjectHandle((object)Invoke(GetLocalObject(obj), parameters)); } public ObjectHandle Create([NotNull] ObjectHandle obj, [NotNull] params ObjectHandle[] parameters) { return new ObjectHandle((object)CreateInstance(GetLocalObject(obj), GetLocalObjects(parameters))); } public ObjectHandle Create([NotNull] ObjectHandle obj, params object[] parameters) { return new ObjectHandle((object)CreateInstance(GetLocalObject(obj), parameters)); } public void SetMember([NotNull] ObjectHandle obj, string name, [NotNull] ObjectHandle value) { SetMember(GetLocalObject(obj), name, GetLocalObject(value)); } public void SetMember([NotNull] ObjectHandle obj, string name, T value) { SetMember(GetLocalObject(obj), name, value); } public ObjectHandle GetMember([NotNull] ObjectHandle obj, string name) { return new ObjectHandle((object)GetMember(GetLocalObject(obj), name)); } public T GetMember([NotNull] ObjectHandle obj, string name) { return GetMember(GetLocalObject(obj), name); } public bool TryGetMember([NotNull] ObjectHandle obj, string name, out ObjectHandle value) { if (TryGetMember(GetLocalObject(obj), name, out var value2)) { value = new ObjectHandle(value2); return true; } value = null; return false; } public bool ContainsMember([NotNull] ObjectHandle obj, string name) { return ContainsMember(GetLocalObject(obj), name); } public void RemoveMember([NotNull] ObjectHandle obj, string name) { RemoveMember(GetLocalObject(obj), name); } public ObjectHandle ConvertTo([NotNull] ObjectHandle obj) { return new ObjectHandle(ConvertTo(GetLocalObject(obj))); } public ObjectHandle ConvertTo([NotNull] ObjectHandle obj, Type type) { return new ObjectHandle(ConvertTo(GetLocalObject(obj), type)); } public bool TryConvertTo([NotNull] ObjectHandle obj, out ObjectHandle result) { if (TryConvertTo(GetLocalObject(obj), out var result2)) { result = new ObjectHandle(result2); return true; } result = null; return false; } public bool TryConvertTo([NotNull] ObjectHandle obj, Type type, out ObjectHandle result) { if (TryConvertTo(GetLocalObject(obj), type, out var result2)) { result = new ObjectHandle(result2); return true; } result = null; return false; } public ObjectHandle ExplicitConvertTo([NotNull] ObjectHandle obj) { return new ObjectHandle(_ops.ExplicitConvertTo(GetLocalObject(obj))); } public ObjectHandle ExplicitConvertTo([NotNull] ObjectHandle obj, Type type) { ContractUtils.RequiresNotNull(type, "type"); return new ObjectHandle(_ops.ExplicitConvertTo(GetLocalObject(obj), type)); } public bool TryExplicitConvertTo([NotNull] ObjectHandle obj, out ObjectHandle result) { T result2; bool flag = _ops.TryExplicitConvertTo(GetLocalObject(obj), out result2); if (flag) { result = new ObjectHandle(obj); } else { result = null; } return flag; } public bool TryExplicitConvertTo([NotNull] ObjectHandle obj, Type type, out ObjectHandle result) { object result2; bool flag = _ops.TryExplicitConvertTo(GetLocalObject(obj), type, out result2); if (flag) { result = new ObjectHandle(obj); } else { result = null; } return flag; } public ObjectHandle ImplicitConvertTo([NotNull] ObjectHandle obj) { return new ObjectHandle(_ops.ImplicitConvertTo(GetLocalObject(obj))); } public ObjectHandle ImplicitConvertTo([NotNull] ObjectHandle obj, Type type) { ContractUtils.RequiresNotNull(type, "type"); return new ObjectHandle(_ops.ImplicitConvertTo(GetLocalObject(obj), type)); } public bool TryImplicitConvertTo([NotNull] ObjectHandle obj, out ObjectHandle result) { T result2; bool flag = _ops.TryImplicitConvertTo(GetLocalObject(obj), out result2); if (flag) { result = new ObjectHandle(obj); } else { result = null; } return flag; } public bool TryImplicitConvertTo([NotNull] ObjectHandle obj, Type type, out ObjectHandle result) { object result2; bool flag = _ops.TryImplicitConvertTo(GetLocalObject(obj), type, out result2); if (flag) { result = new ObjectHandle(obj); } else { result = null; } return flag; } public T Unwrap([NotNull] ObjectHandle obj) { return ConvertTo(GetLocalObject(obj)); } public ObjectHandle DoOperation(ExpressionType op, [NotNull] ObjectHandle target) { return new ObjectHandle((object)DoOperation(op, GetLocalObject(target))); } public ObjectHandle DoOperation(ExpressionType op, ObjectHandle target, ObjectHandle other) { return new ObjectHandle((object)DoOperation(op, GetLocalObject(target), GetLocalObject(other))); } public ObjectHandle Add([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)Add(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle Subtract([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)Subtract(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle Power([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)Power(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle Multiply([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)Multiply(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle Divide([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)Divide(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle Modulo([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)Modulo(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle LeftShift([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)LeftShift(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle RightShift([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)RightShift(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle BitwiseAnd([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)BitwiseAnd(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle BitwiseOr([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)BitwiseOr(GetLocalObject(self), GetLocalObject(other))); } public ObjectHandle ExclusiveOr([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return new ObjectHandle((object)ExclusiveOr(GetLocalObject(self), GetLocalObject(other))); } public bool LessThan([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return LessThan(GetLocalObject(self), GetLocalObject(other)); } public bool GreaterThan([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return GreaterThan(GetLocalObject(self), GetLocalObject(other)); } public bool LessThanOrEqual([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return LessThanOrEqual(GetLocalObject(self), GetLocalObject(other)); } public bool GreaterThanOrEqual([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return GreaterThanOrEqual(GetLocalObject(self), GetLocalObject(other)); } public bool Equal([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return Equal(GetLocalObject(self), GetLocalObject(other)); } public bool NotEqual([NotNull] ObjectHandle self, [NotNull] ObjectHandle other) { return NotEqual(GetLocalObject(self), GetLocalObject(other)); } public string Format([NotNull] ObjectHandle obj) { return Format(GetLocalObject(obj)); } public IList GetMemberNames([NotNull] ObjectHandle obj) { return GetMemberNames(GetLocalObject(obj)); } public string GetDocumentation([NotNull] ObjectHandle obj) { return GetDocumentation(GetLocalObject(obj)); } public IList GetCallSignatures([NotNull] ObjectHandle obj) { return GetCallSignatures(GetLocalObject(obj)); } private static object GetLocalObject([NotNull] ObjectHandle obj) { ContractUtils.RequiresNotNull(obj, "obj"); return obj.Unwrap(); } private static object[] GetLocalObjects(ObjectHandle[] ohs) { object[] array = new object[ohs.Length]; for (int i = 0; i < array.Length; i++) { array[i] = GetLocalObject(ohs[i]); } return array; } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)] public override object InitializeLifetimeService() { return null; } }