Skip to content
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

ClrProxy documentation #71

Closed
adiamante opened this issue Mar 30, 2023 · 2 comments
Closed

ClrProxy documentation #71

adiamante opened this issue Mar 30, 2023 · 2 comments

Comments

@adiamante
Copy link

The documentation on ClrProxy was outdated but I was able to figure out how use it. The following is an example.

using YantraJS.Core;
using YantraJS.Core.Clr;

var synchronizationContext = new SynchronizationContext();
var context = new JSContext(synchronizationContext);

var calculator = new Calculator();
context["calculator"] = ClrProxy.From(calculator);

var a0 = await context.ExecuteAsync("calculator.add(1,1)");                 // a0 = {2} JSNumber
var a1 = await context.ExecuteAsync("await calculator.addAsync(1,1);");     // a1 = {2} JSNumber

public class Calculator
{
    public int Add(int x, int y)
    {
        return x + y;
    }

    public Task<int> AddAsync(int x, int y)
    {
        return Task.FromResult(x + y);
    }
}
@ackava
Copy link
Contributor

ackava commented Mar 30, 2023

@adiamante ClrProxy might be removed or will be changed, the correct way to supply Clr object would be to implement IJavaScriptObject or inheirt JavaScriptObject class. You can control what to export by using JSExport attribute. ClrProxy exports everything. I will be updating documentation soon.

Also using signature JSValue Method(in Arguments a) will be faster compared to using Clr types as arguments as there will be extra checks to convert JSValue to clr types. You can check this unit test on how to use it.

https://github.com/yantrajs/yantra/blob/main/YantraJS.Core.Tests/ClrObjects/CustomObject.cs

@adiamante
Copy link
Author

@ackava Thanks. That was informative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants