Skip to content

Commit

Permalink
support Type.IsAbstract, #433
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Oct 7, 2023
1 parent 0a00dd1 commit 467585d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
5 changes: 2 additions & 3 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,11 @@ local function setBase(cls, kind)
cls.__index = cls
cls.__call = new

local object = kind ~= "S" and Object or ValueType
if extends then
local base = extends[1]
if not base then error(cls.__name__ .. "'s base is nil") end
if base.class == "I" then
setmetatable(cls, object)
setmetatable(cls, kind ~= "S" and Object or ValueType)
setInterface(cls, extends)
else
setmetatable(cls, base)
Expand All @@ -250,7 +249,7 @@ local function setBase(cls, kind)
end
end
else
setmetatable(cls, object)
setmetatable(cls, kind ~= "S" and Object or ValueType)
end
end

Expand Down
46 changes: 35 additions & 11 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,23 @@ local function getName(this)
return this.name
end

local function isAccessibility(memberInfo, kind)
local metadata = memberInfo.metadata
if not metadata then
throwNoMatadata(memberInfo.c.__name__ .. "." .. memberInfo.name)
end
local function isAccessibility(this, kind)
local metadata = this:getMetadata()
return band(metadata[2], 0x7) == kind
end

local MemberInfo = define("System.Reflection.MemberInfo", {
getName = getName,
getCls = function (this)
return this.c
end,
getMetadata = function (this)
local metadata = this.metadata
if not metadata then
throwNoMatadata(this:getCls().__name__ .. "." .. this.name)
end
return metadata
end,
EqualsObj = function (this, obj)
if getmetatable(this) ~= getmetatable(obj) then
return false
Expand All @@ -102,14 +109,15 @@ local MemberInfo = define("System.Reflection.MemberInfo", {
return this.memberType
end,
getDeclaringType = function (this)
return typeof(this.c)
return typeof(this:getCls())
end,
getIsAbstract = function (this)
local metadata = this:getMetadata()
return band(metadata[2], 0x10) ~= 0
end,
getIsStatic = function (this)
local metadata = this.metadata
if not metadata then
throwNoMatadata(this.c.__name__ .. "." .. this.name)
end
return band(metadata[2], 0x8) == 1
local metadata = this:getMetadata()
return band(metadata[2], 0x8) ~= 0
end,
getIsPrivate = function (this)
return isAccessibility(this, 1)
Expand Down Expand Up @@ -853,8 +861,24 @@ function System.GetExecutingAssembly(assembly)
return setmetatable(assembly, Assembly)
end

setmetatable(Type, MemberInfo)
Type.getAssembly = getAssembly

Type.memberType = 32

Type.getCls = function (this)
return this[1]
end

Type.getMetadata = function (this)
local cls = this[1]
local metadata = rawget(cls, "__metadata__")
if not metadata then
throwNoMatadata(cls.__name__)
end
return metadata.class
end

function Type.getAssemblyQualifiedName(this)
return this:getName() .. ', ' .. getName(assembly)
end
Expand Down
1 change: 1 addition & 0 deletions CSharp.lua/LuaSyntaxNodeTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ private void CheckTypeDeclaration(INamedTypeSymbol typeSymbol, LuaTypeDeclaratio

if (IsCurTypeExportMetadataAll || attributes.Count > 0 || typeDeclaration.IsExportMetadata) {
var data = new LuaTableExpression { IsSingleLine = true };
data.Add(new LuaStringLiteralExpressionSyntax(typeSymbol.Name));
data.Add(typeSymbol.GetMetaDataAttributeFlags());
data.AddRange(typeDeclaration.TypeParameterExpressions);
data.AddRange(attributes);
Expand Down
14 changes: 12 additions & 2 deletions CSharp.lua/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,8 +1384,12 @@ public static string GetMetaDataAttributeFlags(this ISymbol symbol, PropertyMeth
const int kParametersMaxCount = 256;

int accessibility = (int)symbol.DeclaredAccessibility;
int isStatic = symbol.IsStatic ? 1 : 0;
int flags = accessibility | (isStatic << 3);
int flags = accessibility;
if (symbol.IsStatic) {
flags |= 1 << 3;
} else if (symbol.IsAbstract) {
flags |= 1 << 4;
}
switch (symbol.Kind) {
case SymbolKind.Method: {
var methodSymbol = (IMethodSymbol)symbol;
Expand All @@ -1406,6 +1410,12 @@ public static string GetMetaDataAttributeFlags(this ISymbol symbol, PropertyMeth
}
case SymbolKind.NamedType: {
var nameType = (INamedTypeSymbol)symbol;
if (nameType.IsStatic) {
flags |= 1 << 4;
flags |= 1 << 5;
} else if (nameType.IsSealed) {
flags |= 1 << 5;
}
if (nameType.IsGenericType) {
int typeCount = nameType.TypeParameters.Length;
Contract.Assert(typeCount < kParametersMaxCount);
Expand Down

0 comments on commit 467585d

Please sign in to comment.