From c2b908aaf7f40de1417f90f2df5148c65c7e5ce8 Mon Sep 17 00:00:00 2001 From: Udayshankar Ravikumar Date: Fri, 20 Dec 2024 17:41:54 +0530 Subject: [PATCH] Wrapper unification part 2. --- src/GlobalSuppressions.cs | 3 +- .../Collections/RuntimeEzrObjectDictionary.cs | 5 +- src/Runtime/Interpreter.cs | 6 +- .../Builtins/EzrBuiltinFunctions.cs | 1 - .../Builtins/EzrBuiltinsUtility.cs | 56 ++--- .../Attributes/WrappedMemberAttribute.cs | 17 +- .../EzrSharpCompatibilityType.cs | 60 ++++- .../Attributes/PrimaryConstructorAttribute.cs | 11 + .../EzrSharpSourceExecutableWrapper.cs | 209 ---------------- .../EzrSharpSourceFunctionWrapper.cs | 142 ----------- .../EzrSharpSourceTypeWrapper.cs | 232 ------------------ src/Runtime/Types/Collections/EzrArray.cs | 1 - .../Types/Collections/EzrDictionary.cs | 33 +-- src/Runtime/Types/Collections/EzrList.cs | 1 - .../Core/RuntimeErrors/EzrAssertionError.cs | 15 +- .../RuntimeErrors/EzrIllegalOperationError.cs | 26 +- .../Core/RuntimeErrors/EzrKeyNotFoundError.cs | 26 +- .../Types/Core/RuntimeErrors/EzrMathError.cs | 29 ++- .../EzrMissingRequiredArgumentError.cs | 26 +- .../EzrPrivateMemberOperationError.cs | 25 +- .../Core/RuntimeErrors/EzrRuntimeError.cs | 30 ++- .../RuntimeErrors/EzrUndefinedValueError.cs | 25 +- .../EzrUnexpectedArgumentError.cs | 25 +- .../RuntimeErrors/EzrUnexpectedTypeError.cs | 25 +- .../EzrUnsupportedWrappingError.cs | 25 +- .../RuntimeErrors/EzrValueOutOfRangeError.cs | 25 +- .../RuntimeErrors/EzrWrapperExecutionError.cs | 25 +- .../Types/Core/Text/EzrCharacterList.cs | 1 - src/Runtime/Types/Core/Text/EzrString.cs | 1 - .../SharpMethodParameters.cs | 72 ------ .../SharpMethodWrapperAttribute.cs | 72 ------ .../SharpTypeWrapperAttribute.cs | 56 ----- 32 files changed, 307 insertions(+), 999 deletions(-) create mode 100644 src/Runtime/Types/CSharpWrappers/CompatWrappers/ObjectMembers/Executables/Attributes/PrimaryConstructorAttribute.cs delete mode 100644 src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceExecutableWrapper.cs delete mode 100644 src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceFunctionWrapper.cs delete mode 100644 src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceTypeWrapper.cs delete mode 100644 src/Runtime/WrapperAttributes/SharpMethodParameters.cs delete mode 100644 src/Runtime/WrapperAttributes/SharpMethodWrapperAttribute.cs delete mode 100644 src/Runtime/WrapperAttributes/SharpTypeWrapperAttribute.cs diff --git a/src/GlobalSuppressions.cs b/src/GlobalSuppressions.cs index 68d4a4a..9f9fee6 100644 --- a/src/GlobalSuppressions.cs +++ b/src/GlobalSuppressions.cs @@ -11,4 +11,5 @@ [assembly: SuppressMessage("Style", "IDE0078:Use pattern matching", Justification = "Recommended fix breaks code.", Scope = "member", Target = "~M:EzrSquared.Runtime.Types.Collections.EzrList.Multiplication(EzrSquared.Runtime.Types.IEzrObject,EzrSquared.Runtime.RuntimeResult)")] [assembly: SuppressMessage("Style", "IDE0078:Use pattern matching", Justification = "Recommended fix breaks code.", Scope = "member", Target = "~M:EzrSquared.Runtime.Types.Core.Text.EzrString.Multiplication(EzrSquared.Runtime.Types.IEzrObject,EzrSquared.Runtime.RuntimeResult)")] [assembly: SuppressMessage("Style", "IDE0078:Use pattern matching", Justification = "Recommended fix breaks code.", Scope = "member", Target = "~M:EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.EzrSharpCompatibilityWrapper`1.EzrObjectToCSharp(EzrSquared.Runtime.Types.IEzrObject,System.Type,EzrSquared.Runtime.RuntimeResult)~System.Object")] -[assembly: SuppressMessage("Style", "IDE0046:Convert to conditional expression", Justification = "Too many ternary operators.", Scope = "member", Target = "~M:EzrSquared.Runtime.WrapperAttributes.SharpAutoWrapperAttribute.ValidateMethod(System.Reflection.MethodBase,System.Boolean)~System.Boolean")] +[assembly: SuppressMessage("Style", "IDE0046:Convert to conditional expression", Justification = "Too many ternary operators.", Scope = "member", Target = "~M:EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes.WrappedMemberAttribute.ValidateMethod(System.Reflection.MethodBase,System.Boolean)~System.Boolean")] +[assembly: SuppressMessage("Style", "IDE0046:Convert to conditional expression", Justification = "Too many ternary operators.", Scope = "member", Target = "~M:EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes.WrappedMemberAttribute.ValidateType(System.Type,System.Boolean)~System.Boolean")] diff --git a/src/Runtime/Collections/RuntimeEzrObjectDictionary.cs b/src/Runtime/Collections/RuntimeEzrObjectDictionary.cs index 67dbcd3..7058a77 100644 --- a/src/Runtime/Collections/RuntimeEzrObjectDictionary.cs +++ b/src/Runtime/Collections/RuntimeEzrObjectDictionary.cs @@ -2,7 +2,7 @@ using EzrSquared.Runtime.Types.Collections; using EzrSquared.Runtime.Types.Core.Errors; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; using System.Collections; using System.Collections.Generic; @@ -192,7 +192,8 @@ public Reference Get(IEzrObject key, RuntimeResult result) /// The key to be checked. /// The object for returning errors. /// if the key was found, if any error occured or the key was not found. - public bool HasKey(IEzrObject key, RuntimeResult result) + [WrappedMember] + public bool HasKey(IEzrObject key, [Runtime(Feature.ResultRef)] RuntimeResult result) { int hash = key.ComputeHashCode(result); return !result.ShouldReturn && _items.ContainsKey(hash); diff --git a/src/Runtime/Interpreter.cs b/src/Runtime/Interpreter.cs index 9efac64..02ebcd0 100644 --- a/src/Runtime/Interpreter.cs +++ b/src/Runtime/Interpreter.cs @@ -6,7 +6,7 @@ using EzrSquared.Runtime.Types.Core.Errors; using EzrSquared.Runtime.Types.Core.Numerics; using EzrSquared.Runtime.Types.Core.Text; -using EzrSquared.Runtime.Types.CSharpWrappers.SourceWrappers; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers; using EzrSquared.Runtime.Types.Executables; using System; using System.Collections.Generic; @@ -1174,13 +1174,13 @@ private void VisitTryNode(TryNode node, Context executionContext, Context callin return; IEzrObject targetObject = RuntimeResult.Reference.Object; - if (targetObject is not EzrSharpSourceTypeWrapper target || !typeof(IEzrRuntimeError).IsAssignableFrom(target.SharpType)) + if (targetObject is not EzrSharpCompatibilityType target || !typeof(IEzrRuntimeError).IsAssignableFrom(target.SharpMember)) { RuntimeResult.Failure(new EzrUnexpectedTypeError($"Expected error type, but got object of type \"{targetObject.TypeName}\"!", executionContext, errorType.StartPosition, errorType.EndPosition)); return; } - if (target.SharpType.IsAssignableFrom(error.GetType())) + if (target.SharpMember.IsAssignableFrom(error.GetType())) { if (errorVariableNode is not null) { diff --git a/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinFunctions.cs b/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinFunctions.cs index 6908142..c08786c 100644 --- a/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinFunctions.cs +++ b/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinFunctions.cs @@ -7,7 +7,6 @@ using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; -using EzrSquared.Runtime.WrapperAttributes; using System; using System.Collections.Generic; using System.Text; diff --git a/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinsUtility.cs b/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinsUtility.cs index 52e5cb1..b438a59 100644 --- a/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinsUtility.cs +++ b/src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinsUtility.cs @@ -1,7 +1,7 @@ using EzrSquared.Runtime.Types.Core; using EzrSquared.Runtime.Types.Core.Errors; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables; -using EzrSquared.Runtime.Types.CSharpWrappers.SourceWrappers; namespace EzrSquared.Runtime.Types.CSharpWrappers.Builtins; @@ -56,36 +56,36 @@ public static void AddBuiltinIOFunctions(Context context) /// Adds all built-in types to the given context. /// /// The context to add to. - public static void AddBuiltinTypes(Context context) + public static void AddBuiltinTypes(Context context ) { - EzrSharpSourceTypeWrapper runtimeError = new(typeof(EzrRuntimeError), context, Position.None, Position.None); - context.Set(null, runtimeError.SharpTypeName, ReferencePool.Get(runtimeError, AccessMod.Constant)); + EzrSharpCompatibilityType runtimeError = new(typeof(EzrRuntimeError), context, Position.None, Position.None); + context.Set(null, runtimeError.SharpMemberName, ReferencePool.Get(runtimeError, AccessMod.Constant)); - EzrSharpSourceTypeWrapper assertionError = new(typeof(EzrAssertionError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper illegalOperationError = new(typeof(EzrIllegalOperationError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper keyNotFoundError = new(typeof(EzrKeyNotFoundError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper mathError = new(typeof(EzrMathError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper missingRequiredArgumentError = new(typeof(EzrMissingRequiredArgumentError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper privateMemberOperationError = new(typeof(EzrPrivateMemberOperationError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper undefinedValueError = new(typeof(EzrUndefinedValueError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper unexpectedArgumentError = new(typeof(EzrUnexpectedArgumentError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper unexpectedTypeError = new(typeof(EzrUnexpectedTypeError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper unsupportedWrappingError = new(typeof(EzrUnsupportedWrappingError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper valueOutOfRangeError = new(typeof(EzrValueOutOfRangeError), context, Position.None, Position.None); - EzrSharpSourceTypeWrapper wrapperExecutionError = new(typeof(EzrWrapperExecutionError), context, Position.None, Position.None); + EzrSharpCompatibilityType assertionError = new(typeof(EzrAssertionError), context, Position.None, Position.None); + EzrSharpCompatibilityType illegalOperationError = new(typeof(EzrIllegalOperationError), context, Position.None, Position.None); + EzrSharpCompatibilityType keyNotFoundError = new(typeof(EzrKeyNotFoundError), context, Position.None, Position.None); + EzrSharpCompatibilityType mathError = new(typeof(EzrMathError), context, Position.None, Position.None); + EzrSharpCompatibilityType missingRequiredArgumentError = new(typeof(EzrMissingRequiredArgumentError), context, Position.None, Position.None); + EzrSharpCompatibilityType privateMemberOperationError = new(typeof(EzrPrivateMemberOperationError), context, Position.None, Position.None); + EzrSharpCompatibilityType undefinedValueError = new(typeof(EzrUndefinedValueError), context, Position.None, Position.None); + EzrSharpCompatibilityType unexpectedArgumentError = new(typeof(EzrUnexpectedArgumentError), context, Position.None, Position.None); + EzrSharpCompatibilityType unexpectedTypeError = new(typeof(EzrUnexpectedTypeError), context, Position.None, Position.None); + EzrSharpCompatibilityType unsupportedWrappingError = new(typeof(EzrUnsupportedWrappingError), context, Position.None, Position.None); + EzrSharpCompatibilityType valueOutOfRangeError = new(typeof(EzrValueOutOfRangeError), context, Position.None, Position.None); + EzrSharpCompatibilityType wrapperExecutionError = new(typeof(EzrWrapperExecutionError), context, Position.None, Position.None); - context.Set(null, assertionError.SharpTypeName, ReferencePool.Get(assertionError, AccessMod.Constant)); - context.Set(null, illegalOperationError.SharpTypeName, ReferencePool.Get(illegalOperationError, AccessMod.Constant)); - context.Set(null, keyNotFoundError.SharpTypeName, ReferencePool.Get(keyNotFoundError, AccessMod.Constant)); - context.Set(null, mathError.SharpTypeName, ReferencePool.Get(mathError, AccessMod.Constant)); - context.Set(null, missingRequiredArgumentError.SharpTypeName, ReferencePool.Get(missingRequiredArgumentError, AccessMod.Constant)); - context.Set(null, privateMemberOperationError.SharpTypeName, ReferencePool.Get(privateMemberOperationError, AccessMod.Constant)); - context.Set(null, undefinedValueError.SharpTypeName, ReferencePool.Get(undefinedValueError, AccessMod.Constant)); - context.Set(null, unexpectedArgumentError.SharpTypeName, ReferencePool.Get(unexpectedArgumentError, AccessMod.Constant)); - context.Set(null, unexpectedTypeError.SharpTypeName, ReferencePool.Get(unexpectedTypeError, AccessMod.Constant)); - context.Set(null, unsupportedWrappingError.SharpTypeName, ReferencePool.Get(unsupportedWrappingError, AccessMod.Constant)); - context.Set(null, valueOutOfRangeError.SharpTypeName, ReferencePool.Get(valueOutOfRangeError, AccessMod.Constant)); - context.Set(null, wrapperExecutionError.SharpTypeName, ReferencePool.Get(wrapperExecutionError, AccessMod.Constant)); + context.Set(null, assertionError.SharpMemberName, ReferencePool.Get(assertionError, AccessMod.Constant)); + context.Set(null, illegalOperationError.SharpMemberName, ReferencePool.Get(illegalOperationError, AccessMod.Constant)); + context.Set(null, keyNotFoundError.SharpMemberName, ReferencePool.Get(keyNotFoundError, AccessMod.Constant)); + context.Set(null, mathError.SharpMemberName, ReferencePool.Get(mathError, AccessMod.Constant)); + context.Set(null, missingRequiredArgumentError.SharpMemberName, ReferencePool.Get(missingRequiredArgumentError, AccessMod.Constant)); + context.Set(null, privateMemberOperationError.SharpMemberName, ReferencePool.Get(privateMemberOperationError, AccessMod.Constant)); + context.Set(null, undefinedValueError.SharpMemberName, ReferencePool.Get(undefinedValueError, AccessMod.Constant)); + context.Set(null, unexpectedArgumentError.SharpMemberName, ReferencePool.Get(unexpectedArgumentError, AccessMod.Constant)); + context.Set(null, unexpectedTypeError.SharpMemberName, ReferencePool.Get(unexpectedTypeError, AccessMod.Constant)); + context.Set(null, unsupportedWrappingError.SharpMemberName, ReferencePool.Get(unsupportedWrappingError, AccessMod.Constant)); + context.Set(null, valueOutOfRangeError.SharpMemberName, ReferencePool.Get(valueOutOfRangeError, AccessMod.Constant)); + context.Set(null, wrapperExecutionError.SharpMemberName, ReferencePool.Get(wrapperExecutionError, AccessMod.Constant)); } /// diff --git a/src/Runtime/Types/CSharpWrappers/CompatWrappers/Attributes/WrappedMemberAttribute.cs b/src/Runtime/Types/CSharpWrappers/CompatWrappers/Attributes/WrappedMemberAttribute.cs index 90d58fc..815d62d 100644 --- a/src/Runtime/Types/CSharpWrappers/CompatWrappers/Attributes/WrappedMemberAttribute.cs +++ b/src/Runtime/Types/CSharpWrappers/CompatWrappers/Attributes/WrappedMemberAttribute.cs @@ -6,7 +6,7 @@ namespace EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; /// /// Attribute for C# types and members to be automatically wrapped from C# types into ezr² types. /// -[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)] +[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)] public class WrappedMemberAttribute : Attribute { /// @@ -50,6 +50,21 @@ public WrappedMemberAttribute(string name, bool isReadOnly = false, bool isWrite Name = name; } + /// + /// Checks if the given type has the supported signature for wrapping. + /// + /// The type. + /// Disable exception throwing. + /// if valid, an exception or otherwise. + /// Thrown if the type is generic or has generic parameters. + public static bool ValidateType(Type type, bool dontThrow = false) + { + if (type.IsAbstract || type.IsGenericTypeDefinition) + return dontThrow ? false : throw new ArgumentException($"The \"{nameof(WrappedMemberAttribute)}\" attribute does not support abstract/generic type \"{type.Name}\".", nameof(type)); + + return true; + } + /// /// Checks if the given method has the supported signature for wrapping. /// diff --git a/src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs b/src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs index df5064a..523670e 100644 --- a/src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs +++ b/src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs @@ -2,6 +2,7 @@ using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; using EzrSquared.Util; using System; using System.Collections.Generic; @@ -13,7 +14,7 @@ namespace EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers; /// /// Class to automatically wrap C# types so that they can be used in ezr². /// -public class EzrSharpCompatibilityType : EzrSharpCompatibilityWrapper +public class EzrSharpCompatibilityType : EzrSharpCompatibilityWrapper, IEzrObject { /// public override string TypeName { get; protected internal set; } = "csharp type"; @@ -21,11 +22,15 @@ public class EzrSharpCompatibilityType : EzrSharpCompatibilityWrapper /// public override string Tag { get; protected internal set; } = "ezrSquared.CSharpType"; + /// + /// The primary wrapped constructor for the type. May be . + /// + public readonly EzrSharpCompatibilityConstructor? PrimaryConstructor; + /// /// Creates a new . /// /// The type to wrap. - /// Runtime result for carrying any errors. /// The context in which this object was created. /// The starting position of the object. /// The ending position of the object. @@ -41,15 +46,10 @@ public EzrSharpCompatibilityType( | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type sharpType, - RuntimeResult result, Context parentContext, Position startPosition, Position endPosition) : base(sharpType, null, parentContext, startPosition, endPosition) + Context parentContext, Position startPosition, Position endPosition) : base(sharpType, null, parentContext, startPosition, endPosition) { Tag = $"{Tag}.{SharpMemberName}.{UIDProvider.Get()}"; - - if (sharpType.IsAbstract || sharpType.IsGenericTypeDefinition) - { - result.Failure(new EzrUnsupportedWrappingError($"Cannot wrap generic/abstract C# type \"{SharpMember.Name}\"!", Context, StartPosition, EndPosition)); - return; - } + WrappedMemberAttribute.ValidateType(SharpMember, AutoWrapperAttribute is null); MethodInfo[] allStaticMethods = sharpType.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Dictionary duplicateNames = new(allStaticMethods.Length); @@ -109,9 +109,47 @@ public EzrSharpCompatibilityType( if (!WrappedMemberAttribute.ValidateMethod(constructor, constructorObject.AutoWrapperAttribute is null)) continue; - Context.Set(null, definedConstructors == 0 ? "make" : $"make_{definedConstructors}", ReferencePool.Get(constructorObject, AccessMod.Constant)); - definedConstructors++; + if (constructor.GetCustomAttribute() is not null) + { + if (PrimaryConstructor is not null) + throw new ArgumentException($"Type \"{SharpMember.Name}\" cannot have multiple primary constructors!", nameof(sharpType)); + + PrimaryConstructor = constructorObject; + continue; + } + + string name = definedConstructors == 0 ? "make" : $"make_{definedConstructors}"; + if (constructorObject.AutoWrapperAttribute is not WrappedMemberAttribute attr || string.IsNullOrEmpty(attr.Name)) + definedConstructors++; + else + { + if (Context.IsDefined(attr.Name)) + throw new ArgumentException($"Wrapped member with name \"{attr.Name}\" is already defined for type \"{SharpMember.Name}\".", nameof(sharpType)); + + name = attr.Name; + } + + Context.Set(null, name, ReferencePool.Get(constructorObject, AccessMod.Constant)); + } + } + + /// + public new void Update(Context context, Position startPosition, Position endPosition) + { + base.Update(context, startPosition, endPosition); + PrimaryConstructor?.Update(Context, startPosition, endPosition); + } + + /// + public override void Execute(Reference[] arguments, Interpreter interpreter, RuntimeResult result) + { + if (PrimaryConstructor is null) + { + base.Execute(arguments, interpreter, result); + return; } + + PrimaryConstructor.Execute(arguments, interpreter, result); } /// diff --git a/src/Runtime/Types/CSharpWrappers/CompatWrappers/ObjectMembers/Executables/Attributes/PrimaryConstructorAttribute.cs b/src/Runtime/Types/CSharpWrappers/CompatWrappers/ObjectMembers/Executables/Attributes/PrimaryConstructorAttribute.cs new file mode 100644 index 0000000..4ad4b0e --- /dev/null +++ b/src/Runtime/Types/CSharpWrappers/CompatWrappers/ObjectMembers/Executables/Attributes/PrimaryConstructorAttribute.cs @@ -0,0 +1,11 @@ +using System; + +namespace EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; + +/// +/// Attribute that declares a primary constructor. +/// +[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)] +public class PrimaryConstructorAttribute : Attribute +{ +} diff --git a/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceExecutableWrapper.cs b/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceExecutableWrapper.cs deleted file mode 100644 index daf9078..0000000 --- a/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceExecutableWrapper.cs +++ /dev/null @@ -1,209 +0,0 @@ -global using WrapperArgumentPopulationResult = (System.Collections.Generic.Dictionary Arguments, System.Collections.Generic.List? ExtraPositionalArguments); - -using EzrSquared.Runtime.Types.Core.Errors; -using System; -using System.Collections.Generic; -using System.Text; - -namespace EzrSquared.Runtime.Types.CSharpWrappers.SourceWrappers; - -/// -/// Parent of all wrapper classes which wrap executable members written in C# so that they can be used in ezr². -/// -/// The context in which this object was created. -/// The starting position of the object. -/// The ending position of the object. -public abstract class EzrSharpSourceExecutableWrapper(Context parentContext, Position startPosition, Position endPosition) : EzrObject(parentContext, startPosition, endPosition) -{ - /// - public override string TypeName { get; protected internal set; } = "csharp source executable wrapper"; - - /// - public override string Tag { get; protected internal set; } = "ezrSquared.CSharpSourceExecutableWrapper"; - - /// - /// The name of the executable's parameters and if they are required. - /// - public (string Name, bool IsRequired)[] Parameters = []; - - /// - /// Does the executable accept extra keyword arguments? - /// - public bool HasExtraKeywordArguments; - - /// - /// Does the executable accept extra positional arguments? - /// - public bool HasExtraPositionalArguments; - - /// - /// Converts a string from PascalCase to lowecase plain text, seperated by spaces. - /// - /// The text to convert in PascalCase. - /// The converted text in lowecase plain text. - internal protected static string PascalCaseToLowerCasePlainText(string text) - { - StringBuilder result = new(); - result.Append(char.ToLowerInvariant(text[0])); - - for (int i = 1; i < text.Length; ++i) - { - char c = text[i]; - if (char.IsUpper(c)) - result.Append(' ').Append(char.ToLowerInvariant(c)); - else - result.Append(c); - } - - return result.ToString(); - } - - /// - /// Converts an index to a power of two, so that the index can be used like an enum flag. - /// - /// - /// This function is used to check if all arguments have been provided to and types.
- /// The indices of the given arguments are converted to powers of two and bitwise-ored together, then bitwise-anded with the index of a defined parameter which is also converted to a power of two.
- /// Finally, if the result is the same as the power of two of the parameter's index, this tells the interpreter that the particular required parameter has been provided. - ///
- /// The index to be converted. - /// The power of two. - protected internal static int IndexToFlag(int index) - { - return (index++ < 3) ? index : (4 * index) - 8; - } - - /// - /// Checks and populates the arguments given by the user's ezr² code into a dictionary. - /// - /// The array of arguments. - /// Runtime result for carrying any errors. - /// The dictionary of all the arguments. - protected internal WrapperArgumentPopulationResult CheckAndPopulateArguments(Reference[] arguments, RuntimeResult result) - { - int totalRequiredArguments = 0; - foreach ((string Name, bool IsRequired) in Parameters) - { - if (IsRequired) - totalRequiredArguments++; - } - - int calculatedParameterIndex = 0; - int requiredKeywordArguments = 0; - int flaggedRequiredArguments = -1; - - Dictionary argumentReferences = new(arguments.Length); - List? extraPositionalArgumentReferences = HasExtraPositionalArguments ? new() : null; - - for (int i = 0; i < arguments.Length; i++) - { - string name; - int parameterIndex = 0; - Reference reference = arguments[i]; - - if (!string.IsNullOrEmpty(reference.Name) && !reference.IsRegistered && !reference.IsEmpty) - { - name = reference.Name; - if (argumentReferences.ContainsKey(name)) - { - result.Failure(new EzrIllegalOperationError($"Cannot override already defined argument \"{name}\"!", _executionContext, reference.Object.StartPosition, reference.Object.EndPosition)); - return (argumentReferences, extraPositionalArgumentReferences); - } - - parameterIndex = Array.FindIndex(Parameters, (param) => param.Name == name); - if (parameterIndex > -1) - { - argumentReferences.Add(name, reference); - requiredKeywordArguments++; - } - else if (HasExtraKeywordArguments) - { - argumentReferences.Add(name, reference); - parameterIndex = -1; - } - else - { - result.Failure(new EzrUnexpectedArgumentError($"Did not expect argument \"{name}\"!", _executionContext, reference.Object.StartPosition, reference.Object.EndPosition)); - return (argumentReferences, extraPositionalArgumentReferences); - } - } - else - { - IndexCheck: - if (Parameters.Length <= calculatedParameterIndex && !HasExtraPositionalArguments) - { - result.Failure(new EzrUnexpectedArgumentError( - requiredKeywordArguments > 0 - ? $"Only expected {Parameters.Length - requiredKeywordArguments} unnamed argument(s) as {requiredKeywordArguments} required argument(s) has/have been declared as (a) keyword argument(s)!" - : $"Only expected {Parameters.Length} unnamed argument(s)!", - _executionContext, StartPosition, EndPosition)); - break; - } - else if (totalRequiredArguments <= calculatedParameterIndex && HasExtraPositionalArguments) - { - extraPositionalArgumentReferences!.Add(reference); - continue; - } - - string argumentName = Parameters[calculatedParameterIndex].Name; - if (!argumentReferences.ContainsKey(argumentName)) - { - name = argumentName; - parameterIndex = calculatedParameterIndex; - - argumentReferences.Add(name, reference); - calculatedParameterIndex++; - } - else - { - calculatedParameterIndex++; - goto IndexCheck; - } - } - - if (parameterIndex > -1) - if (flaggedRequiredArguments < 0) - flaggedRequiredArguments = IndexToFlag(parameterIndex); - else - flaggedRequiredArguments |= IndexToFlag(parameterIndex); - } - - for (int i = 0; i < Parameters.Length; i++) - { - int parameterFlag = IndexToFlag(i); - if (Parameters[i].IsRequired && (flaggedRequiredArguments < 0 || (flaggedRequiredArguments & parameterFlag) != parameterFlag)) - { - result.Failure(new EzrMissingRequiredArgumentError($"Expected required argument \"{Parameters[i].Name}\"!", _executionContext, StartPosition, EndPosition)); - return (argumentReferences, extraPositionalArgumentReferences); - } - } - - return (argumentReferences, extraPositionalArgumentReferences); - } - - /// - public override void ComparisonEqual(IEzrObject other, RuntimeResult result) - { - bool equal = StrictEquals(other, result); - if (result.ShouldReturn) - return; - - result.Success(NewBooleanConstant(equal)); - } - - /// - public override void ComparisonNotEqual(IEzrObject other, RuntimeResult result) - { - bool equal = StrictEquals(other, result); - if (result.ShouldReturn) - return; - - result.Success(NewBooleanConstant(!equal)); - } - - /// - public override bool EvaluateBoolean(RuntimeResult result) - { - return true; - } -} diff --git a/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceFunctionWrapper.cs b/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceFunctionWrapper.cs deleted file mode 100644 index be1a6e5..0000000 --- a/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceFunctionWrapper.cs +++ /dev/null @@ -1,142 +0,0 @@ -using EzrSquared.Runtime.WrapperAttributes; -using EzrSquared.Util; -using System; -using System.Reflection; - -namespace EzrSquared.Runtime.Types.CSharpWrappers.SourceWrappers; - -/// -/// A class to wrap methods written in C# so that they can be used in ezr². -/// -public partial class EzrSharpSourceFunctionWrapper : EzrSharpSourceExecutableWrapper -{ - /// - public override string TypeName { get; protected internal set; } = "csharp source function wrapper"; - - /// - public override string Tag { get; protected internal set; } = "ezrSquared.CSharpSourceFunctionWrapper"; - - /// - /// The wrapped function. - /// - public readonly EzrSharpSourceWrappableMethod SharpFunction; - - /// - /// The name of the function, in snake_case. - /// - public readonly string SharpFunctionName; - - /// - /// Creates a new from a function's . - /// - /// The method to wrap. - /// The object which contains the method, if static. - /// The context in which this object was created. - /// The starting position of the object. - /// The ending position of the object. - public EzrSharpSourceFunctionWrapper(MethodInfo function, object? instance, Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition) - { - Exception? parameterException = SharpMethodWrapperAttribute.ValidateMethodParameters(function); - if (parameterException is not null) - throw parameterException; - - SharpFunction = (EzrSharpSourceWrappableMethod)function.CreateDelegate(typeof(EzrSharpSourceWrappableMethod), instance); - (SharpFunctionName, SharpMethodWrapperAttribute attribute) = GetFunctionInfo(function); - - AddParameters(attribute); - } - - /// - /// Creates a new from a function. - /// - /// The method to wrap. - /// The context in which this object was created. - /// The starting position of the object. - /// The ending position of the object. - public EzrSharpSourceFunctionWrapper(EzrSharpSourceWrappableMethod function, Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition) - { - SharpFunction = function; - (SharpFunctionName, SharpMethodWrapperAttribute attribute) = GetFunctionInfo(function.Method); - - AddParameters(attribute); - } - - /// - /// Gets the function's ezr² (snake_case) name and wrapper attribute. - /// - /// The function's . - /// The function's ezr² name and wrapper attribute - /// - /// Thrown if the attribute was not found in the function - /// or if the name given in the function's is empty. - /// - private (string, SharpMethodWrapperAttribute) GetFunctionInfo(MethodInfo function) - { - SharpMethodWrapperAttribute attribute = function.GetCustomAttribute(true) - ?? throw new ArgumentException($"No \"{nameof(SharpMethodWrapperAttribute)}\" attribute found in function \"{function.Name}\"!", nameof(function)); - - if (string.IsNullOrEmpty(attribute.Name)) - throw new ArgumentException($"Name not provided in {nameof(SharpMethodWrapperAttribute)} of function \"{function.Name}\"!", nameof(function)); - - Tag = $"{Tag}.{attribute.Name}.{UIDProvider.Get()}"; - return (attribute.Name, attribute); - } - - /// - /// Adds the parameters of the function to be wrapped to the object. - /// - /// The attribute of the function containing the details for optional, required and extra keyword parameters. - private void AddParameters(SharpMethodWrapperAttribute attribute) - { - int requiredParameters = attribute.RequiredParameters.Length; - Parameters = new (string Name, bool IsRequired)[attribute.RequiredParameters.Length + attribute.OptionalParameters.Length]; - - for (int j = 0; j < requiredParameters; j++) - Parameters[j] = new(attribute.RequiredParameters[j], true); - - for (int j = 0; j < attribute.OptionalParameters.Length; j++) - Parameters[j + requiredParameters] = new(attribute.OptionalParameters[j], false); - - HasExtraKeywordArguments = attribute.HasExtraKeywordArguments; - HasExtraPositionalArguments = attribute.HasExtraPositionalArguments; - } - - /// - public override void Execute(Reference[] arguments, Interpreter interpreter, RuntimeResult result) - { - WrapperArgumentPopulationResult argumentReferences = CheckAndPopulateArguments(arguments, result); - if (result.ShouldReturn) - return; - - SharpFunction.Invoke(new SharpMethodParameters - ( - argumentReferences.Arguments, - argumentReferences.ExtraPositionalArguments, - _executionContext, - CreationContext, - Context, - StartPosition, - EndPosition, - interpreter, - result - )); - } - - /// - public override int ComputeHashCode(RuntimeResult result) - { - return HashCode.Combine(HashTag, SharpFunction); - } - - /// - public override bool StrictEquals(IEzrObject other, RuntimeResult result) - { - return (other as EzrSharpSourceFunctionWrapper)?.SharpFunction == SharpFunction && other.HashTag == HashTag; - } - - /// - public override string ToString(RuntimeResult result) - { - return $"<{TypeName} \"{SharpFunctionName}\">"; - } -} diff --git a/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceTypeWrapper.cs b/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceTypeWrapper.cs deleted file mode 100644 index de494d3..0000000 --- a/src/Runtime/Types/CSharpWrappers/SourceWrappers/EzrSharpSourceTypeWrapper.cs +++ /dev/null @@ -1,232 +0,0 @@ -using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; -using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers; -using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables; -using EzrSquared.Runtime.WrapperAttributes; -using EzrSquared.Util; -using System; -using System.Diagnostics.CodeAnalysis; -using System.Reflection; - -namespace EzrSquared.Runtime.Types.CSharpWrappers.SourceWrappers; - -/// -/// A class to wrap types written in C# so that they can be used in ezr². -/// -public class EzrSharpSourceTypeWrapper : EzrSharpSourceExecutableWrapper -{ - /// - public override string TypeName { get; protected internal set; } = "csharp source type wrapper"; - - /// - public override string Tag { get; protected internal set; } = "ezrSquared.CSharpSourceTypeWrapper"; - - /// - /// The wrapped type. - /// - public readonly Type SharpType; - - /// - /// The name of the type, in snake_case. - /// - public readonly string SharpTypeName; - - /// - /// Reflection information about the type's constructor. - /// - public readonly ConstructorInfo Constructor; - - /// - /// Creates a new . - /// - /// The type to wrap. - /// The context in which this object was created. - /// The starting position of the object. - /// The ending position of the object. - /// - /// Thrown if the type to be wrapped is generic or if the was not found in the type. - /// - /// - /// Thrown if there are multiple methods/properties/fields with the same name. - /// - public EzrSharpSourceTypeWrapper( - - [DynamicallyAccessedMembers( - DynamicallyAccessedMemberTypes.PublicMethods - | DynamicallyAccessedMemberTypes.NonPublicMethods - | DynamicallyAccessedMemberTypes.PublicFields - | DynamicallyAccessedMemberTypes.NonPublicFields - | DynamicallyAccessedMemberTypes.PublicProperties - | DynamicallyAccessedMemberTypes.NonPublicProperties - | DynamicallyAccessedMemberTypes.PublicConstructors - | DynamicallyAccessedMemberTypes.NonPublicConstructors - )] Type type, - - Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition) - { - SharpType = type; - - // Check if type inherits from IEzrObject. - if (!typeof(IEzrObject).IsAssignableFrom(SharpType)) - throw new ArgumentException($"A source wrapper cannot be used to wrap \"{type.Name}\", as it does not inherit {nameof(IEzrObject)}!", nameof(type)); - - // Check if generic or abstract. - if (type.IsAbstract || type.IsGenericTypeDefinition) - throw new ArgumentException($"Cannot wrap generic/abstract C# type \"{type.Name}\"!", nameof(type)); - - // Check for type attribute. - SharpTypeWrapperAttribute typeAttribute = type.GetCustomAttribute(true) - ?? throw new ArgumentException($"No \"{nameof(SharpTypeWrapperAttribute)}\" attribute found for type \"{type.Name}\"!", nameof(type)); - - // Set name and tag. - SharpTypeName = typeAttribute.Name; - Tag = $"{Tag}.{SharpTypeName}.{UIDProvider.Get()}"; - - Exception? typeAttributeException = SharpTypeWrapperAttribute.ValidateMethodParameters(type, - out (ConstructorInfo Info, SharpMethodWrapperAttribute Attribute)? constructor); - - if (typeAttributeException is not null) - throw typeAttributeException; - - // Get constructor parameters. - int requiredParameters = constructor!.Value.Attribute.RequiredParameters.Length; - Parameters = new (string Name, bool IsRequired)[constructor.Value.Attribute.RequiredParameters.Length + constructor.Value.Attribute.OptionalParameters.Length]; - - // Required parameters. - for (int j = 0; j < requiredParameters; j++) - Parameters[j] = new(constructor.Value.Attribute.RequiredParameters[j], true); - - // Optional parameters. - for (int j = 0; j < constructor.Value.Attribute.OptionalParameters.Length; j++) - Parameters[j + requiredParameters] = new(constructor.Value.Attribute.OptionalParameters[j], false); - - // Set variables. - HasExtraKeywordArguments = constructor.Value.Attribute.HasExtraKeywordArguments; - HasExtraPositionalArguments = constructor.Value.Attribute.HasExtraPositionalArguments; - Constructor = constructor.Value.Info; - - // Get static methods to wrap. - MethodInfo[] staticMethods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - for (int i = 0; i < staticMethods.Length; i++) - { - MethodInfo method = staticMethods[i]; - - IEzrObject wrappedMethod; - string methodName; - - // Check if method can be wrapped. - if (method.GetCustomAttribute(false) is not null) - { - EzrSharpSourceFunctionWrapper sourceMethod = new(method, null, Context, StartPosition, EndPosition); - (wrappedMethod, methodName) = (sourceMethod, sourceMethod.SharpFunctionName); - } - else if (method.GetCustomAttribute() is not null) - { - EzrSharpCompatibilityFunction compatMethod = new(method, null, Context, StartPosition, EndPosition); - (wrappedMethod, methodName) = (compatMethod, compatMethod.SharpMemberName); - } - else - continue; - - // Check if name already defined. - if (Context.IsDefined(methodName)) - throw new AmbiguousMatchException($"Cannot wrap CSharp static method \"{methodName}\" of type {type.Name} as another member with the same name already exists!"); - - // Set in context. - Context.Set(null, methodName, ReferencePool.Get(wrappedMethod, AccessMod.Constant)); - } - - // Get public static properties. - PropertyInfo[] publicStaticProperties = type.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - for (int i = 0; i < publicStaticProperties.Length; i++) - { - PropertyInfo property = publicStaticProperties[i]; - IEzrObject wrappedProperty; - string propertyName; - - // Check if property can be wrapped. - if (property.GetCustomAttribute() is not null) - { - EzrSharpCompatibilityProperty compatWrapper = new(property, null, Context, StartPosition, EndPosition); - (wrappedProperty, propertyName) = (compatWrapper, compatWrapper.SharpMemberName); - } - else - continue; - - // Check if name already defined. - if (Context.IsDefined(propertyName)) - throw new AmbiguousMatchException($"Cannot wrap CSharp static property \"{propertyName}\" of type {type.Name} as another member with the same name already exists!"); - - Context.Set(null, propertyName, ReferencePool.Get(wrappedProperty, AccessMod.Constant)); - } - - // Get public static fields. - FieldInfo[] publicStaticFields = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - for (int i = 0; i < publicStaticFields.Length; i++) - { - FieldInfo field = publicStaticFields[i]; - IEzrObject wrappedField; - string fieldName; - - // Check if property can be wrapped. - if (field.GetCustomAttribute() is not null) - { - EzrSharpCompatibilityField compatWrapper = new(field, null, Context, StartPosition, EndPosition); - (wrappedField, fieldName) = (compatWrapper, compatWrapper.SharpMemberName); - } - else - continue; - - // Check if name already defined. - if (Context.IsDefined(fieldName)) - throw new AmbiguousMatchException($"Cannot wrap CSharp static fieldName \"{fieldName}\" of type {type.Name} as another member with the same name already exists!"); - - Context.Set(null, fieldName, ReferencePool.Get(wrappedField, AccessMod.Constant)); - } - } - - /// - public override void Execute(Reference[] arguments, Interpreter interpreter, RuntimeResult result) - { - WrapperArgumentPopulationResult argumentReferences = CheckAndPopulateArguments(arguments, result); - if (result.ShouldReturn) - return; - - IEzrObject newObject = (IEzrObject)Constructor.Invoke( - [ - new SharpMethodParameters - ( - argumentReferences.Arguments, - argumentReferences.ExtraPositionalArguments, - _executionContext, - CreationContext, - Context, - StartPosition, - EndPosition, - interpreter, - result - ) - ] - ); - - if (!result.ShouldReturn) - result.Success(ReferencePool.Get(newObject, AccessMod.PrivateConstant)); - } - - /// - public override int ComputeHashCode(RuntimeResult result) - { - return HashCode.Combine(HashTag, SharpType); - } - - /// - public override bool StrictEquals(IEzrObject other, RuntimeResult result) - { - return (other as EzrSharpSourceTypeWrapper)?.SharpType == SharpType && other.HashTag == HashTag; - } - - /// - public override string ToString(RuntimeResult result) - { - return $"<{TypeName} \"{SharpTypeName}\">"; - } -} diff --git a/src/Runtime/Types/Collections/EzrArray.cs b/src/Runtime/Types/Collections/EzrArray.cs index e16a9c9..e282b70 100644 --- a/src/Runtime/Types/Collections/EzrArray.cs +++ b/src/Runtime/Types/Collections/EzrArray.cs @@ -2,7 +2,6 @@ using EzrSquared.Runtime.Types.Core.Numerics; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers; -using EzrSquared.Runtime.WrapperAttributes; using System; using System.Collections; using System.Collections.Generic; diff --git a/src/Runtime/Types/Collections/EzrDictionary.cs b/src/Runtime/Types/Collections/EzrDictionary.cs index 6bb8f02..099ba6b 100644 --- a/src/Runtime/Types/Collections/EzrDictionary.cs +++ b/src/Runtime/Types/Collections/EzrDictionary.cs @@ -1,12 +1,9 @@ using EzrSquared.Runtime.Collections; -using EzrSquared.Runtime.Types.Core; using EzrSquared.Runtime.Types.Core.Errors; using EzrSquared.Runtime.Types.Core.Numerics; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables; -using EzrSquared.Runtime.Types.CSharpWrappers.SourceWrappers; -using EzrSquared.Runtime.WrapperAttributes; using System; using System.Collections; using System.Collections.Generic; @@ -47,7 +44,7 @@ public EzrDictionary(RuntimeEzrObjectDictionary value, Context parentContext, Po Context.Set(null, "length", ReferencePool.Get(new EzrSharpCompatibilityProperty(GetMemberInfo(nameof(Value.Count))!, Value, Context, StartPosition, EndPosition), AccessMod.Constant)); Context.Set(null, "remove_by_hash", ReferencePool.Get(new EzrSharpCompatibilityFunction(Value.RemoveHash, Context, StartPosition, EndPosition), AccessMod.Constant)); - Context.Set(null, "has_key", ReferencePool.Get(new EzrSharpSourceFunctionWrapper(DictionaryExists, Context, StartPosition, EndPosition), AccessMod.Constant)); + Context.Set(null, "has_key", ReferencePool.Get(new EzrSharpCompatibilityFunction(Value.HasKey, Context, StartPosition, EndPosition), AccessMod.Constant)); } /// @@ -91,34 +88,6 @@ IEnumerator IEnumerable.GetEnumerator() return GetEnumerator(); } - /// - /// Basic key checking function. Implements . - /// - /// - /// ezr² parameters: - /// - /// - /// key - /// () The key to check for. - /// - /// - /// - /// ezr² return type: - /// - /// - /// The constructor arguments. - [SharpMethodWrapper("has_key", RequiredParameters = ["key"])] - private void DictionaryExists(SharpMethodParameters arguments) - { - Reference reference = arguments.ArgumentReferences["key"]; - - bool hasKey = Value.HasKey(reference.Object, arguments.Result); - if (arguments.Result.ShouldReturn) - return; - - arguments.Result.Success(NewBooleanConstant(hasKey)); - } - /// public override void ComparisonEqual(IEzrObject other, RuntimeResult result) { diff --git a/src/Runtime/Types/Collections/EzrList.cs b/src/Runtime/Types/Collections/EzrList.cs index a643e94..990e4aa 100644 --- a/src/Runtime/Types/Collections/EzrList.cs +++ b/src/Runtime/Types/Collections/EzrList.cs @@ -3,7 +3,6 @@ using EzrSquared.Runtime.Types.Core.Numerics; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers; -using EzrSquared.Runtime.WrapperAttributes; using System; using System.Collections; using System.Collections.Generic; diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrAssertionError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrAssertionError.cs index d42f331..c346211 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrAssertionError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrAssertionError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -8,7 +9,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("assertion_error")] +[WrappedMember("assertion_error")] public class EzrAssertionError(Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Assertion failed", "The assertion conditions were not met!", context, startPosition, endPosition) { /// @@ -20,7 +21,11 @@ public class EzrAssertionError(Context context, Position startPosition, Position /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper] - public EzrAssertionError(SharpMethodParameters arguments) : this(arguments.ExecutionContext, arguments.StartPosition, arguments.EndPosition) { } + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrAssertionError( + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this(executionContext, wrapper.StartPosition, wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrIllegalOperationError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrIllegalOperationError.cs index e43db1d..d4a18a6 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrIllegalOperationError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrIllegalOperationError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("illegal_operation_error")] +[WrappedMember("illegal_operation_error")] public class EzrIllegalOperationError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Illegal operation", details, context, startPosition, endPosition) { /// @@ -21,13 +22,16 @@ public class EzrIllegalOperationError(string details, Context context, Position /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrIllegalOperationError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) - { } + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrIllegalOperationError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrKeyNotFoundError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrKeyNotFoundError.cs index 303691d..9564326 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrKeyNotFoundError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrKeyNotFoundError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("key_not_found_error")] +[WrappedMember("key_not_found_error")] public class EzrKeyNotFoundError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Key not found", details, context, startPosition, endPosition) { /// @@ -21,13 +22,16 @@ public class EzrKeyNotFoundError(string details, Context context, Position start /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrKeyNotFoundError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) - { } + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrKeyNotFoundError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrMathError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrMathError.cs index 1772dc2..664ecae 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrMathError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrMathError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -10,7 +11,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("math_error")] +[WrappedMember("math_error")] public class EzrMathError(string title, string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError(title, details, context, startPosition, endPosition) { /// @@ -22,14 +23,18 @@ public class EzrMathError(string title, string details, Context context, Positio /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["title", "details"])] - public EzrMathError(SharpMethodParameters arguments) : this( - GetStringArgument("title", arguments.ArgumentReferences["title"].Object, arguments.ExecutionContext, arguments.Result), - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) - { } + /// The title of the error. + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrMathError(string title, string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + title, + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrMissingRequiredArgumentError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrMissingRequiredArgumentError.cs index c2be2b9..5c4820f 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrMissingRequiredArgumentError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrMissingRequiredArgumentError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("missing_required_argument_error")] +[WrappedMember("missing_required_argument_error")] public class EzrMissingRequiredArgumentError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Missing required argument", details, context, startPosition, endPosition) { /// @@ -21,13 +22,16 @@ public class EzrMissingRequiredArgumentError(string details, Context context, Po /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrMissingRequiredArgumentError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) - { } + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrMissingRequiredArgumentError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrPrivateMemberOperationError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrPrivateMemberOperationError.cs index a49ec2e..98ea7c6 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrPrivateMemberOperationError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrPrivateMemberOperationError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("private_member_operation_error")] +[WrappedMember("private_member_operation_error")] public class EzrPrivateMemberOperationError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Private member operation", details, context, startPosition, endPosition) { /// @@ -21,13 +22,17 @@ public class EzrPrivateMemberOperationError(string details, Context context, Pos /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrPrivateMemberOperationError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrPrivateMemberOperationError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrRuntimeError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrRuntimeError.cs index f1eb24e..55f8c9f 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrRuntimeError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrRuntimeError.cs @@ -1,5 +1,6 @@ using EzrSquared.Runtime.Types.Core.Text; -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; using System; using System.Text; @@ -8,7 +9,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// /// Implementation of with some utility methods. /// -[SharpTypeWrapper("runtime_error")] +[WrappedMember("runtime_error")] public class EzrRuntimeError : EzrObject, IEzrRuntimeError { /// @@ -52,20 +53,23 @@ public EzrRuntimeError(string title, string details, Context context, Position s Context.Set(null, "details", ReferencePool.Get(new EzrString(Details, Context, StartPosition, EndPosition), AccessMod.Constant)); } - /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["title", "details"])] - public EzrRuntimeError(SharpMethodParameters arguments) : this( - GetStringArgument("title", arguments.ArgumentReferences["title"].Object, arguments.ExecutionContext, arguments.Result), - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) - { } + /// The title of the error. + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrRuntimeError(string title, string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + title, + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } /// /// Converts the given argument to a string. diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrUndefinedValueError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrUndefinedValueError.cs index 60f6dc6..cee154a 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrUndefinedValueError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrUndefinedValueError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("undefined_value_error")] +[WrappedMember("undefined_value_error")] public class EzrUndefinedValueError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Undefined value", details, context, startPosition, endPosition) { /// @@ -21,13 +22,17 @@ public class EzrUndefinedValueError(string details, Context context, Position st /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrUndefinedValueError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrUndefinedValueError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedArgumentError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedArgumentError.cs index ed00270..667989f 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedArgumentError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedArgumentError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("unexpected_argument_error")] +[WrappedMember("unexpected_argument_error")] public class EzrUnexpectedArgumentError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Unexpected argument(s)", details, context, startPosition, endPosition) { /// @@ -21,13 +22,17 @@ public class EzrUnexpectedArgumentError(string details, Context context, Positio /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrUnexpectedArgumentError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrUnexpectedArgumentError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedTypeError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedTypeError.cs index 71fc49f..6ab622a 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedTypeError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrUnexpectedTypeError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("unexpected_type_error")] +[WrappedMember("unexpected_type_error")] public class EzrUnexpectedTypeError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Unexpected type", details, context, startPosition, endPosition) { /// @@ -21,13 +22,17 @@ public class EzrUnexpectedTypeError(string details, Context context, Position st /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrUnexpectedTypeError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrUnexpectedTypeError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrUnsupportedWrappingError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrUnsupportedWrappingError.cs index 458e573..96dc5fd 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrUnsupportedWrappingError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrUnsupportedWrappingError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("unsupported_wrapping_error")] +[WrappedMember("unsupported_wrapping_error")] public class EzrUnsupportedWrappingError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Unsupported wrapping", details, context, startPosition, endPosition) { /// @@ -21,13 +22,17 @@ public class EzrUnsupportedWrappingError(string details, Context context, Positi /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrUnsupportedWrappingError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrUnsupportedWrappingError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrValueOutOfRangeError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrValueOutOfRangeError.cs index 829e5e8..80c1c30 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrValueOutOfRangeError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrValueOutOfRangeError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("value_out_of_range_error")] +[WrappedMember("value_out_of_range_error")] public class EzrValueOutOfRangeError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Value out of range", details, context, startPosition, endPosition) { /// @@ -21,13 +22,17 @@ public class EzrValueOutOfRangeError(string details, Context context, Position s /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrValueOutOfRangeError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrValueOutOfRangeError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/RuntimeErrors/EzrWrapperExecutionError.cs b/src/Runtime/Types/Core/RuntimeErrors/EzrWrapperExecutionError.cs index 811fbc3..87ff7b1 100644 --- a/src/Runtime/Types/Core/RuntimeErrors/EzrWrapperExecutionError.cs +++ b/src/Runtime/Types/Core/RuntimeErrors/EzrWrapperExecutionError.cs @@ -1,4 +1,5 @@ -using EzrSquared.Runtime.WrapperAttributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; +using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers.Executables.Attributes; namespace EzrSquared.Runtime.Types.Core.Errors; @@ -9,7 +10,7 @@ namespace EzrSquared.Runtime.Types.Core.Errors; /// The context in which the error occurred. /// The starting position of the error. /// The ending position of the error. -[SharpTypeWrapper("wrapper_execution_error")] +[WrappedMember("wrapper_execution_error")] public class EzrWrapperExecutionError(string details, Context context, Position startPosition, Position endPosition) : EzrRuntimeError("Wrapper execution error", details, context, startPosition, endPosition) { /// @@ -21,13 +22,17 @@ public class EzrWrapperExecutionError(string details, Context context, Position /// /// Wrapper constructor for creating the error object. /// - /// The constructor arguments. - [SharpMethodWrapper(RequiredParameters = ["details"])] - public EzrWrapperExecutionError(SharpMethodParameters arguments) : this( - GetStringArgument("details", arguments.ArgumentReferences["details"].Object, arguments.ExecutionContext, arguments.Result), - arguments.ExecutionContext, - arguments.StartPosition, - arguments.EndPosition - ) + /// Details on why the error happened. + /// The caller. + /// The execution context. + [WrappedMember, PrimaryConstructor] + public EzrWrapperExecutionError(string details, + [Runtime(Feature.CallerRef)] IEzrObject wrapper, + [Runtime(Feature.ExecutionRef)] Context executionContext) + : this( + details, + executionContext, + wrapper.StartPosition, + wrapper.EndPosition) { } } diff --git a/src/Runtime/Types/Core/Text/EzrCharacterList.cs b/src/Runtime/Types/Core/Text/EzrCharacterList.cs index 41c193a..16fb67f 100644 --- a/src/Runtime/Types/Core/Text/EzrCharacterList.cs +++ b/src/Runtime/Types/Core/Text/EzrCharacterList.cs @@ -3,7 +3,6 @@ using EzrSquared.Runtime.Types.Core.Numerics; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers; -using EzrSquared.Runtime.WrapperAttributes; using System; using System.Collections; using System.Collections.Generic; diff --git a/src/Runtime/Types/Core/Text/EzrString.cs b/src/Runtime/Types/Core/Text/EzrString.cs index ea2648d..931a3e8 100644 --- a/src/Runtime/Types/Core/Text/EzrString.cs +++ b/src/Runtime/Types/Core/Text/EzrString.cs @@ -3,7 +3,6 @@ using EzrSquared.Runtime.Types.Core.Numerics; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.Attributes; using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers; -using EzrSquared.Runtime.WrapperAttributes; using System; using System.Collections; using System.Collections.Generic; diff --git a/src/Runtime/WrapperAttributes/SharpMethodParameters.cs b/src/Runtime/WrapperAttributes/SharpMethodParameters.cs deleted file mode 100644 index 07e48be..0000000 --- a/src/Runtime/WrapperAttributes/SharpMethodParameters.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System.Collections.Generic; - -namespace EzrSquared.Runtime.WrapperAttributes; - -/// -/// Class for the paramters of a wrapped C# method. -/// -/// See . -/// See . -/// See . -/// See . -/// See . -/// See . -/// See . -/// See . -/// See . -public class SharpMethodParameters( - Dictionary argumentReferences, - List? extraPositionalArgumentReferences, - Context executionContext, - Context creationContext, - Context methodContext, - Position startPosition, - Position endPosition, - Interpreter interpreter, - RuntimeResult result) -{ - /// - /// The references to the actual ezr² arguments. - /// - public Dictionary ArgumentReferences = argumentReferences; - - /// - /// The references to optional extra positional ezr² arguments. - /// - public List? ExtraPositionalArgumentReferences = extraPositionalArgumentReferences; - - /// - /// The context under which the method is being executed. - /// - public Context ExecutionContext = executionContext; - - /// - /// The context under which the method wrapper was created. - /// - public Context CreationContext = creationContext; - - /// - /// The context of the method wrapper itself. - /// - public Context MethodContext = methodContext; - - /// - /// The starting position of the method wrapper object. - /// - public Position StartPosition = startPosition; - - /// - /// The ending position of the method wrapper object. - /// - public Position EndPosition = endPosition; - - /// - /// The interpreter executing the method. - /// - public Interpreter Interpreter = interpreter; - - /// - /// The runtime result to return values or to throw errors. - /// - public RuntimeResult Result = result; -} diff --git a/src/Runtime/WrapperAttributes/SharpMethodWrapperAttribute.cs b/src/Runtime/WrapperAttributes/SharpMethodWrapperAttribute.cs deleted file mode 100644 index 79898ff..0000000 --- a/src/Runtime/WrapperAttributes/SharpMethodWrapperAttribute.cs +++ /dev/null @@ -1,72 +0,0 @@ -global using EzrSharpSourceWrappableMethod = System.Action; - -using System; -using System.Reflection; - -namespace EzrSquared.Runtime.WrapperAttributes; - -/// -/// Attribute for C# methods and constructors which will be wrapped into ezr². -/// -[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, AllowMultiple = false, Inherited = true)] -public class SharpMethodWrapperAttribute : Attribute -{ - /// - /// The ezr² name for the method, optional. - /// - public readonly string Name = string.Empty; - - /// - /// The ezr² names of the required parameters of the method. - /// - public string[] RequiredParameters = []; - - /// - /// The ezr² names of the optional parameters of the method. - /// - public string[] OptionalParameters = []; - - /// - /// Does this method support additional keyword arguments (kwargs)? - /// - public bool HasExtraKeywordArguments; - - /// - /// Does this method support additional positional arguments (args)? - /// - public bool HasExtraPositionalArguments; - - /// - /// Creates a new instance of with a name. - /// - /// The ezr² name for the method. - public SharpMethodWrapperAttribute(string name) - { - Name = name; - } - - /// - /// Creates a new instance of . - /// - public SharpMethodWrapperAttribute() { } - - /// - /// Checks if the given method or constructor has the required parameters. - /// - /// The method or constructor to check. - /// if the check was successful, an otherwise. - public static Exception? ValidateMethodParameters(MethodBase methodInfo) - { - // Get the parameter types of the method - Type[] parameterTypes = Array.ConvertAll(methodInfo.GetParameters(), p => p.ParameterType); - - // Check if the number of parameters matches - if (parameterTypes.Length != 1) - return new TargetParameterCountException($"\"{methodInfo.Name}\": Method or constructor must have exactly one parameter, as it uses the {nameof(SharpMethodWrapperAttribute)} attribute."); - - // Check if the required parameter types match - return parameterTypes[0] != typeof(SharpMethodParameters) - ? new FormatException($"\"{methodInfo.Name}\": Parameter 1 must be of type {nameof(SharpMethodParameters)}, as it uses the {nameof(SharpMethodWrapperAttribute)} attribute.") - : null; - } -} diff --git a/src/Runtime/WrapperAttributes/SharpTypeWrapperAttribute.cs b/src/Runtime/WrapperAttributes/SharpTypeWrapperAttribute.cs deleted file mode 100644 index 098ca71..0000000 --- a/src/Runtime/WrapperAttributes/SharpTypeWrapperAttribute.cs +++ /dev/null @@ -1,56 +0,0 @@ -using EzrSquared.Runtime.Types; -using System; -using System.Diagnostics.CodeAnalysis; -using System.Reflection; - -namespace EzrSquared.Runtime.WrapperAttributes; - -/// -/// Attribute for C# classes which will be wrapped into ezr². -/// -/// The ezr² name for the type. -[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] -public class SharpTypeWrapperAttribute(string name) : Attribute -{ - /// - /// The ezr² name for the type. - /// - public readonly string Name = name; - - /// - /// Checks if the given type has a constructor with the attribute. - /// - /// The type to check. - /// The constructor and its attribute. - /// if the check was successful, an otherwise. - public static Exception? ValidateMethodParameters( - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] - Type typeInfo, - - out (ConstructorInfo Info, SharpMethodWrapperAttribute Attribute)? constructor) - { - constructor = null; - if (!typeof(IEzrObject).IsAssignableFrom(typeInfo)) - return new ArgumentException($"Expected type \"{typeInfo.Name}\" to inherit from {nameof(IEzrObject)}, as it uses the attribute \"{nameof(SharpTypeWrapperAttribute)}\"", nameof(typeInfo)); - - ConstructorInfo[] constructors = typeInfo.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance); - - foreach (ConstructorInfo constructorInfo in constructors) - { - SharpMethodWrapperAttribute? attribute = constructorInfo.GetCustomAttribute(false); - if (constructor is not null && attribute is not null) - return new AmbiguousMatchException($"Found multiple constructors for type \"{typeInfo.Name}\" with attribute {nameof(SharpMethodWrapperAttribute)}"); - - if (attribute is not null) - { - constructor = (constructorInfo, attribute); - if (SharpMethodWrapperAttribute.ValidateMethodParameters(constructorInfo) is Exception exception) - return exception; - } - } - - return constructor is null - ? new ArgumentException($"No constructor with attribute {nameof(SharpMethodWrapperAttribute)} found for type \"{typeInfo.Name}\"!", nameof(typeInfo)) - : null; - } -} \ No newline at end of file