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

Todo: Add documentation for JsRuntime.cs #1

Open
MHDante opened this issue Dec 23, 2021 · 2 comments
Open

Todo: Add documentation for JsRuntime.cs #1

MHDante opened this issue Dec 23, 2021 · 2 comments
Assignees

Comments

@MHDante
Copy link
Contributor

MHDante commented Dec 23, 2021

No description provided.

@MHDante MHDante self-assigned this Dec 23, 2021
@marcusx2
Copy link

How can I use this library? How does it compare with this one?

@MHDante
Copy link
Contributor Author

MHDante commented Nov 12, 2022

Hahaha! Sorry I haven't had the time (in almost a year) to document this lib.

The purpose of this library is to write c# code to access browser functions:

For example

// csharp code.

var window = JsRuntime.GetGlobalValue("window");
var location = window.GetProp("location").GetProp("href");

var console = JsRuntime.GetGlobalValue("console");
console.Call("log", "Outputting our location", location);

While this library is highly optimized to reduce allocations in the unity heap, it's still slower than direct interop and something like tinyutils.

The primary objective of the library is to allow you to write algorithms in C# that may need access Javascript libraries.

I used this to implement https://github.com/transformsai/UnityWebGLHttpHandler

I decided to do this since it would facilitate writing a C# HTTP handler while degating calls to JS Fetch.

One of the main benefits of using this library is the ability to hold references to JS objects in C#, for example:

// We're still in C#
var element =  JSRuntime.GetGlobalValue("document").Call("getElementById", "some-element-id");
var element2 =  JSRuntime.GetGlobalValue("document").Call("getElementById", "some-other-element-id");

element2.Call("appendChild", element2);

There's also some cool stuff in this library like:

Support for callbacks
Support for awaiting JS promises from inside Csharp (!!!)
Avoiding garbage collection (and segfaults )for long-lived callbacks.
Two-way garbage collection synchronization.
Support for sharing memory across C# and JS.

That said, I've moved to a different project at TransformsAI and haven't had time to continue working on this stack. Hopefully this comment helps :)

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