From d77bd0fac70acd6e9ada7d37bf43ce1843b7179d Mon Sep 17 00:00:00 2001 From: R-unic Date: Sun, 5 Jan 2025 22:21:17 -0500 Subject: [PATCH] fix: restrict object.GetType() usage This is due to not having access to types during runtime. --- RobloxCS.Luau/AST/TableInitializer.cs | 6 +- RobloxCS.Luau/AstUtility.cs | 116 +++++++++++++------------- RobloxCS.Luau/Macros.cs | 11 +-- RobloxCS.Shared/Logger.cs | 8 +- RobloxCS/LuauGenerator.cs | 2 +- 5 files changed, 67 insertions(+), 76 deletions(-) diff --git a/RobloxCS.Luau/AST/TableInitializer.cs b/RobloxCS.Luau/AST/TableInitializer.cs index 572e49f..4c55337 100644 --- a/RobloxCS.Luau/AST/TableInitializer.cs +++ b/RobloxCS.Luau/AST/TableInitializer.cs @@ -1,6 +1,6 @@ namespace RobloxCS.Luau { - public class TableInitializer(List? values = null, List? keys = null) + public class TableInitializer(List? values = null, List? keys = null, bool treatIdentifiersAsKeyNames = false) : Expression { public List Values { get; } = values ?? []; @@ -23,12 +23,12 @@ public override void Render(LuauWriter luau) var key = Keys.ElementAtOrDefault(index); if (key != null) { - if (key is not IdentifierName) + if (!(key is IdentifierName && !treatIdentifiersAsKeyNames)) { luau.Write('['); } key.Render(luau); - if (key is not IdentifierName) + if (!(key is IdentifierName && !treatIdentifiersAsKeyNames)) { luau.Write(']'); } diff --git a/RobloxCS.Luau/AstUtility.cs b/RobloxCS.Luau/AstUtility.cs index 8e34199..877b386 100644 --- a/RobloxCS.Luau/AstUtility.cs +++ b/RobloxCS.Luau/AstUtility.cs @@ -35,64 +35,64 @@ public static Expression SubtractOne(Expression expression) public static Parenthesized CreateTypeInfo(Type type) { List keys = [ - new Literal("\"Name\""), - new Literal("\"FullName\""), - new Literal("\"Namespace\""), - new Literal("\"AssemblyQualifiedName\""), - new Literal("\"TypeInitializer\""), - new Literal("\"ReflectedType\""), - new Literal("\"IsAbstract\""), - new Literal("\"IsAnsiClass\""), - new Literal("\"IsArray\""), - new Literal("\"IsSealed\""), - new Literal("\"IsInterface\""), - new Literal("\"IsGenericTypeParameter\""), - new Literal("\"IsGenericTypeDefinition\""), - new Literal("\"IsGenericType\""), - new Literal("\"IsGenericMethodParameter\""), - new Literal("\"IsConstructedGenericType\""), - new Literal("\"IsImport\""), - new Literal("\"IsClass\""), - new Literal("\"IsCollectible\""), - new Literal("\"IsByRef\""), - new Literal("\"IsByRefLike\""), - new Literal("\"IsAutoClass\""), - new Literal("\"IsAutoLayout\""), - new Literal("\"IsCOMObject\""), - new Literal("\"IsContextful\""), - new Literal("\"IsEnum\""), - new Literal("\"IsExplicitLayout\""), - new Literal("\"IsPointer\""), - new Literal("\"IsFunctionPointer\""), - new Literal("\"IsUnmanagedFunctionPointer\""), - new Literal("\"IsLayoutSequential\""), - new Literal("\"IsMarshalByRef\""), - new Literal("\"IsNested\""), - new Literal("\"IsNestedAssembly\""), - new Literal("\"IsNestedFamily\""), - new Literal("\"IsNestedFamANDAssem\""), - new Literal("\"IsNestedFamORAssem\""), - new Literal("\"IsNestedPrivate\""), - new Literal("\"IsNestedPublic\""), - new Literal("\"IsNotPublic\""), - new Literal("\"IsPublic\""), - new Literal("\"IsSZArray\""), - new Literal("\"IsSecurityCritical\""), - new Literal("\"IsSecuritySafeCritical\""), - new Literal("\"IsSecurityTransparent\""), - new Literal("\"IsSignatureType\""), - new Literal("\"IsSpecialName\""), - new Literal("\"IsTypeDefinition\""), - new Literal("\"IsUnicodeClass\""), - new Literal("\"IsValueType\""), - new Literal("\"IsVariableBoundArray\""), - new Literal("\"IsVisible\""), - new Literal("\"UnderlyingSystemType\""), - new Literal("\"BaseType\""), - new Literal("\"DeclaringType\""), - new Literal("\"ContainsGenericParameters\""), - new Literal("\"GenericTypeArguments\""), - new Literal("\"GUID\""), + new IdentifierName("Name"), + new IdentifierName("FullName"), + new IdentifierName("Namespace"), + new IdentifierName("AssemblyQualifiedName"), + new IdentifierName("TypeInitializer"), + new IdentifierName("ReflectedType"), + new IdentifierName("IsAbstract"), + new IdentifierName("IsAnsiClass"), + new IdentifierName("IsArray"), + new IdentifierName("IsSealed"), + new IdentifierName("IsInterface"), + new IdentifierName("IsGenericTypeParameter"), + new IdentifierName("IsGenericTypeDefinition"), + new IdentifierName("IsGenericType"), + new IdentifierName("IsGenericMethodParameter"), + new IdentifierName("IsConstructedGenericType"), + new IdentifierName("IsImport"), + new IdentifierName("IsClass"), + new IdentifierName("IsCollectible"), + new IdentifierName("IsByRef"), + new IdentifierName("IsByRefLike"), + new IdentifierName("IsAutoClass"), + new IdentifierName("IsAutoLayout"), + new IdentifierName("IsCOMObject"), + new IdentifierName("IsContextful"), + new IdentifierName("IsEnum"), + new IdentifierName("IsExplicitLayout"), + new IdentifierName("IsPointer"), + new IdentifierName("IsFunctionPointer"), + new IdentifierName("IsUnmanagedFunctionPointer"), + new IdentifierName("IsLayoutSequential"), + new IdentifierName("IsMarshalByRef"), + new IdentifierName("IsNested"), + new IdentifierName("IsNestedAssembly"), + new IdentifierName("IsNestedFamily"), + new IdentifierName("IsNestedFamANDAssem"), + new IdentifierName("IsNestedFamORAssem"), + new IdentifierName("IsNestedPrivate"), + new IdentifierName("IsNestedPublic"), + new IdentifierName("IsNotPublic"), + new IdentifierName("IsPublic"), + new IdentifierName("IsSZArray"), + new IdentifierName("IsSecurityCritical"), + new IdentifierName("IsSecuritySafeCritical"), + new IdentifierName("IsSecurityTransparent"), + new IdentifierName("IsSignatureType"), + new IdentifierName("IsSpecialName"), + new IdentifierName("IsTypeDefinition"), + new IdentifierName("IsUnicodeClass"), + new IdentifierName("IsValueType"), + new IdentifierName("IsVariableBoundArray"), + new IdentifierName("IsVisible"), + new IdentifierName("UnderlyingSystemType"), + new IdentifierName("BaseType"), + new IdentifierName("DeclaringType"), + new IdentifierName("ContainsGenericParameters"), + new IdentifierName("GenericTypeArguments"), + new IdentifierName("GUID"), ]; List values = [ diff --git a/RobloxCS.Luau/Macros.cs b/RobloxCS.Luau/Macros.cs index 709f60c..7de4f0b 100644 --- a/RobloxCS.Luau/Macros.cs +++ b/RobloxCS.Luau/Macros.cs @@ -186,15 +186,8 @@ private bool ObjectMethod(Func visit, MemberAccessExpressionS { expanded = null; switch (memberAccess.Name.Identifier.Text) { - case "GetType": { - var typeSymbol = _semanticModel.GetTypeInfo(memberAccess.Expression).Type; - if (typeSymbol == null) - throw Logger.CodegenError(memberAccess.Expression, "Unable to resolve type symbol of the expression that GetType() was called on"); - - var type = StandardUtility.GetRuntimeType(_semanticModel, memberAccess.Expression, typeSymbol); - expanded = AstUtility.CreateTypeInfo(type); - break; - } + case "GetType": + throw Logger.UnsupportedError(memberAccess.Name, "Object.GetType()", useIs: true, useYet: false); } expanded?.MarkExpanded(MacroKind.ObjectMethod); diff --git a/RobloxCS.Shared/Logger.cs b/RobloxCS.Shared/Logger.cs index 3a7ca80..ad265d0 100644 --- a/RobloxCS.Shared/Logger.cs +++ b/RobloxCS.Shared/Logger.cs @@ -40,7 +40,7 @@ public static void CodegenWarning(SyntaxToken token, string message) public static Exception UnsupportedError(SyntaxNode node, string subject, bool useIs = false, bool useYet = true) { - return CodegenError(node, $"{subject} {(useIs == true ? "is" : "are")} not {(useYet ? "yet " : "")} supported, sorry!"); + return CodegenError(node, $"{subject} {(useIs == true ? "is" : "are")} not {(useYet ? "yet " : "")}supported, sorry!"); } public static Exception CodegenError(SyntaxNode node, string message) @@ -107,9 +107,7 @@ private static void Log(string message, ConsoleColor color, string level) Console.ForegroundColor = originalColor; } - private static string FormatLocation(FileLinePositionSpan lineSpan) - { - return $"{(lineSpan.Path == "" ? "" : lineSpan.Path)}:{lineSpan.StartLinePosition.Line + 1}:{lineSpan.StartLinePosition.Character + 1}"; - } + private static string FormatLocation(FileLinePositionSpan lineSpan) => + $"{(lineSpan.Path == "" ? "" : lineSpan.Path)}:{lineSpan.StartLinePosition.Line + 1}:{lineSpan.StartLinePosition.Character + 1}"; } } \ No newline at end of file diff --git a/RobloxCS/LuauGenerator.cs b/RobloxCS/LuauGenerator.cs index 8559f0b..14d964a 100644 --- a/RobloxCS/LuauGenerator.cs +++ b/RobloxCS/LuauGenerator.cs @@ -967,7 +967,7 @@ public override Luau.Node VisitExpressionStatement(ExpressionStatementSyntax nod : expressionNode; } - public override Node? VisitInterpolatedStringExpression(InterpolatedStringExpressionSyntax node) + public override Luau.InterpolatedString VisitInterpolatedStringExpression(InterpolatedStringExpressionSyntax node) { var parts = node.Contents.Select(Visit).ToList(); return new Luau.InterpolatedString(parts);