Skip to content

Commit

Permalink
Added disposable resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Sep 12, 2023
1 parent 9057ff5 commit 66a1d5f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
56 changes: 52 additions & 4 deletions YantraJS.Core.Tests/ClrObjects/DisposableTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using YantraJS.Core.Clr;
using YantraJS.Utils;
Expand Down Expand Up @@ -30,26 +32,72 @@ public void Dispose()
}
}

class AsyncDisposableFile : IAsyncDisposable
{
public bool Open = true;

public string Value = "";

[JSExport("add")]
public void Add(string text)
{
this.Value += text + "\r\n";
}

public async ValueTask DisposeAsync()
{
await Task.Delay(10);
this.Open = false;
}
}

[TestMethod]
public void TestNamingConvention()
public void SyncDispose()
{
var c = new JSTestContext();

c["DisposableFile"] = ClrType.From(typeof(CustomObject2));
c["DisposableFile"] = ClrType.From(typeof(DisposableFile));


var a = c.Eval(@"
function use(d) {
using f = d;
f.add('a');
}
var a = new DisposableFile('b');
var a = new DisposableFile();
use(a);
return a;
");

a.ConvertTo<DisposableFile>(out var d);
Assert.IsFalse(d.Open);
}

[TestMethod]
public void AsyncDispose()
{
AsyncPump.Run(async () =>
{
var c = new JSTestContext();
c["AsyncDisposableFile"] = ClrType.From(typeof(AsyncDisposableFile));
var a = await c.ExecuteAsync(@"
(async function() {
async function use(d) {
await using f = d;
f.add('a');
}
var a = new AsyncDisposableFile();
await use(a);
return a;
})();
");
a.ConvertTo<AsyncDisposableFile>(out var d);
Assert.IsFalse(d.Open);
});
}
}
}
54 changes: 28 additions & 26 deletions YantraJS.Core/Core/Clr/ClrType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,32 +241,34 @@ internal void Generate(JSObject target, Type type, bool isStatic)
clrPrototype.SetElementAt = indexSetter;

// setup disposables...
//if (type.GetInterfaceMap(typeof(IDisposable))
// .InterfaceMethods
// .Any())
//{
// target.FastAddValue(JSSymbol.dispose, new JSFunction((in Arguments a) =>
// {
// if (a.This is ClrProxy p && p.value is IDisposable d)
// {
// d.Dispose();
// }
// return JSUndefined.Value;
// }), JSPropertyAttributes.ConfigurableValue);
//}
//if (type.GetInterfaceMap(typeof(IAsyncDisposable))
// .InterfaceMethods
// .Any())
//{
// target.FastAddValue(JSSymbol.dispose, new JSFunction((in Arguments a) =>
// {
// if (a.This is ClrProxy p && p.value is IAsyncDisposable d)
// {
// return ClrProxy.From(d.DisposeAsync().AsTask());
// }
// return JSUndefined.Value;
// }), JSPropertyAttributes.ConfigurableValue);
//}
var disposableType = typeof(IDisposable);
var asyncDisposableType = typeof(IAsyncDisposable);
if (disposableType.IsAssignableFrom(type) && type.GetInterfaceMap(disposableType)
.InterfaceMethods
.Any())
{
target.FastAddValue(JSSymbol.dispose, new JSFunction((in Arguments a) =>
{
if (a.This is ClrProxy p && p.value is IDisposable d)
{
d.Dispose();
}
return JSUndefined.Value;
}), JSPropertyAttributes.ConfigurableValue);
}
if (asyncDisposableType.IsAssignableFrom(type) && type.GetInterfaceMap(typeof(IAsyncDisposable))
.InterfaceMethods
.Any())
{
target.FastAddValue(JSSymbol.asyncDispose, new JSFunction((in Arguments a) =>
{
if (a.This is ClrProxy p && p.value is IAsyncDisposable d)
{
return ClrProxy.From(d.DisposeAsync().AsTask());
}
return JSUndefined.Value;
}), JSPropertyAttributes.ConfigurableValue);
}
}

private ClrType(
Expand Down

0 comments on commit 66a1d5f

Please sign in to comment.