XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using Jint;
using Mono.Cecil;
using XFESystem.Collections;
using System.Security.Cryptography;
using System.Text.Json.Nodes;
using Patch=XFESystem.Tools.JintCleanupPatch.Program;

namespace XFESystem.JintCleanupPatch.Tests;

public sealed class WeakCollectionPatchTests
{
    [Fact]
    public void MarkedPartialOwnerBindingIsRejectedEvenWithAnUpdatedHashReceipt()
    {
        string? root=AppContext.BaseDirectory;
        while(root is not null&&!File.Exists(Path.Combine(root,"XFESystem.slnx")))root=Path.GetDirectoryName(root);
        Assert.NotNull(root);
        string directory=Path.Combine(root,"build","WeakPartial-"+Guid.NewGuid().ToString("N"),"obj","XfeJintHost");
        Directory.CreateDirectory(directory);string path=Path.Combine(directory,"Jint.dll");
        File.Copy(typeof(Engine).Assembly.Location,path);
        using(var assembly=AssemblyDefinition.ReadAssembly(path,new ReaderParameters{InMemory=true}))
        {assembly.MainModule.GetType("Jint.Engine").Interfaces.Clear();assembly.Write(path);}
        string hash=Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
        var receipt=JsonNode.Parse(File.ReadAllText(typeof(Engine).Assembly.Location+".XfeBind.json"))!;
        receipt["AfterSha256"]=hash;File.WriteAllText(path+".XfeBind.json",receipt.ToJsonString());
        Assert.Throws<InvalidOperationException>(()=>Patch.Main([path,"--bind-only"]));
        Assert.Equal(hash,Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path))));
    }
    [Fact]
    public void ActualPinnedPrivateInterpreterOwnsItsStorageDirectlyAndKeepsIntrinsicAlgorithms()
    {
        using var assembly=AssemblyDefinition.ReadAssembly(typeof(Engine).Assembly.Location);
        var module=assembly.MainModule;
        Assert.NotNull(module.GetType("XFESystem.JintOwnedWeakCollectionsV1"));
        var engine=module.GetType("Jint.Engine");
        Assert.Contains(engine.Interfaces,item=>item.InterfaceType.FullName==typeof(IWeakCollectionOwner).FullName);
        Assert.Single(engine.Fields,field=>field.Name=="_xfeWeakCollections");
        foreach(string name in new[]{"Jint.Native.JsWeakMap","Jint.Native.JsWeakSet"})
        {
            var type=module.GetType(name);var field=type.Fields.Single(item=>item.Name=="_table");
            Assert.Equal(typeof(OwnedWeakTable<,>).FullName,((GenericInstanceType)field.FieldType).ElementType.FullName);
            Assert.DoesNotContain(type.Methods.Where(method=>method.HasBody).SelectMany(method=>method.Body.Instructions),
                instruction=>instruction.Operand is MethodReference method&&method.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.ConditionalWeakTable",StringComparison.Ordinal));
        }
        Assert.NotNull(module.GetType("Jint.Native.JsWeakMap").Methods.Single(method=>method.Name=="GetOrInsertComputed"));
        var actual=new Engine();var owner=Assert.IsAssignableFrom<IWeakCollectionOwner>((object)actual);
        Assert.Same(owner.WeakCollections,owner.WeakCollections);
        Assert.NotSame(owner.WeakCollections,((IWeakCollectionOwner)(object)new Engine()).WeakCollections);
        owner.WeakCollections.Release();
    }
}