-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to map obj[Symbol.dispose] to obj.Dispose() #533
Comments
Hi @DavidBal, Host objects in ClearScript currently don't support Anyway, assuming that dynamic setupFunc = engine.Evaluate(@"(
function (host, IDisposable) {
Object.defineProperty(Object.getPrototypeOf(host), Symbol.dispose, {
get() {
const disposable = host.asType(IDisposable, this);
return disposable ? () => disposable.Dispose() : undefined;
}
});
}
)");
setupFunc(new HostFunctions(), typeof(IDisposable).ToHostType(engine)); With this setup in place, most host objects will support We'll keep an eye on the proposal and add full support if and when it's incorporated into the JavaScript standard. Good luck! |
Hi again, Here's a more complete solution: dynamic setupFunc = engine.Evaluate(@"(
function (obj, host, IDisposable) {
Object.defineProperty(Object.getPrototypeOf(obj), Symbol.dispose, {
get() {
const disposable = host.asType(IDisposable, this);
return disposable ? () => disposable.Dispose() : undefined;
}
});
}
)");
var host = new HostFunctions();
var disposable = typeof(IDisposable).ToHostType(engine);
setupFunc(host, host, disposable);
setupFunc(new Action(() => {}), host, disposable);
setupFunc(new PropertyBag(), host, disposable); This should set up Cheers! |
Hi, thank you that works great. Here is my full implementation, should someone else have the same problem: dynamic setupDisposeFunc = scriptEngine.Evaluate("""
(
function (obj, host, IDisposable) {
Symbol.dispose ??= Symbol("Symbol.dispose");
Object.defineProperty(Object.getPrototypeOf(obj), Symbol.dispose, {
get() {
const disposable = host.asType(IDisposable, this);
return disposable ? () => disposable.Dispose() : undefined;
}
});
}
)
""");
dynamic setupAsyncDisposeFunc = scriptEngine.Evaluate("""
(
function (obj, host, IAsyncDisposable) {
Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");
Object.defineProperty(Object.getPrototypeOf(obj), Symbol.asyncDispose, {
get() {
const disposable = host.asType(IAsyncDisposable, this);
return disposable ? () => disposable.DisposeAsync() : undefined;
}
});
}
)
""");
HostFunctions host = new HostFunctions();
object disposable = typeof(IDisposable).ToHostType(scriptEngine);
object asyncDisposable = typeof(IAsyncDisposable).ToHostType(scriptEngine);
setupDisposeFunc(host, host, disposable);
setupDisposeFunc(new Action(() => { }), host, disposable);
setupDisposeFunc(new PropertyBag(), host, disposable);
setupAsyncDisposeFunc(host, host, asyncDisposable);
setupAsyncDisposeFunc(new Action(() => { }), host, asyncDisposable);
setupAsyncDisposeFunc(new PropertyBag(), host, asyncDisposable); Best regards, |
Hi ClearScript Team,
in the recent update of TypeScript the using statement was added.
https://devblogs.microsoft.com/typescript/announcing-typescript-5-2/#using-declarations-and-explicit-resource-management
The JavaScript for this feature looks something like this:
Is there any way we can map this call to CSharp?
Thanks in advance.
Best regards,
David
The text was updated successfully, but these errors were encountered: