Skip to content

Commit

Permalink
fix: restrict object.GetType() usage
Browse files Browse the repository at this point in the history
This is due to not having access to types during runtime.
  • Loading branch information
R-unic committed Jan 6, 2025
1 parent 5d15239 commit d77bd0f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 76 deletions.
6 changes: 3 additions & 3 deletions RobloxCS.Luau/AST/TableInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace RobloxCS.Luau
{
public class TableInitializer(List<Expression>? values = null, List<Expression>? keys = null)
public class TableInitializer(List<Expression>? values = null, List<Expression>? keys = null, bool treatIdentifiersAsKeyNames = false)
: Expression
{
public List<Expression> Values { get; } = values ?? [];
Expand All @@ -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(']');
}
Expand Down
116 changes: 58 additions & 58 deletions RobloxCS.Luau/AstUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,64 +35,64 @@ public static Expression SubtractOne(Expression expression)
public static Parenthesized CreateTypeInfo(Type type)
{
List<Expression> 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<Expression> values = [
Expand Down
11 changes: 2 additions & 9 deletions RobloxCS.Luau/Macros.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,8 @@ private bool ObjectMethod(Func<SyntaxNode, Node?> 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);
Expand Down
8 changes: 3 additions & 5 deletions RobloxCS.Shared/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 == "" ? "<anonymous>" : lineSpan.Path)}:{lineSpan.StartLinePosition.Line + 1}:{lineSpan.StartLinePosition.Character + 1}";
}
private static string FormatLocation(FileLinePositionSpan lineSpan) =>
$"{(lineSpan.Path == "" ? "<anonymous>" : lineSpan.Path)}:{lineSpan.StartLinePosition.Line + 1}:{lineSpan.StartLinePosition.Character + 1}";
}
}
2 changes: 1 addition & 1 deletion RobloxCS/LuauGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Luau.Expression>).ToList();
return new Luau.InterpolatedString(parts);
Expand Down

0 comments on commit d77bd0f

Please sign in to comment.