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

TextMateSharp with Wasm not working? #57

Open
HendrikMennen opened this issue Oct 26, 2023 · 29 comments
Open

TextMateSharp with Wasm not working? #57

HendrikMennen opened this issue Oct 26, 2023 · 29 comments

Comments

@HendrikMennen
Copy link

HendrikMennen commented Oct 26, 2023

I saw the package TextmateSharp.Wasm.
Is it possible to run with AvaloniaEdit?
For me AvaloniaEdit alone works in Wasm. As soon as I add some highlighting it breaks, with or without the TextMateSharp.Wasm package.
Maybe I am not doing it right, since I didn't see any demo project for this

@HendrikMennen HendrikMennen changed the title TextMateSharp with Wasm TextMateSharp with Wasm not working? Oct 26, 2023
@danipen
Copy link
Owner

danipen commented Oct 26, 2023

TextMateSharp should support WASM but it's possible that the AvaloniaEdit TextMate integration may not be fully prepared to work with WASM yet.

@SKProCH
Copy link

SKProCH commented Mar 29, 2024

Hello, @danipen

Is there any progress on this problem? Seems like TextMate.Wasm package exists, but installing it doesn't change anything - app still hangs while applying coloring to AvaloniaEdit.

@danipen
Copy link
Owner

danipen commented Mar 29, 2024

It worked some time ago but I didn't try it recently. Any clue of what the issue is?

@danipen danipen reopened this Mar 29, 2024
@SKProCH
Copy link

SKProCH commented Mar 29, 2024

Actually I think this is something related to TextMate library calls or coloring the AvaloniaEdit (perhaps a multithreading or something like that). I've actually use your library for ShowMeTheXaml editor: https://github.com/AvaloniaUtils/ShowMeTheXaml.Avalonia/blob/master/ShowMeTheXaml.Avalonia.AvaloniaEdit/XamlDisplayAvaloniaEditThemeBehavior.cs

And seems like initialization was completed successfully:

https://github.com/AvaloniaUtils/ShowMeTheXaml.Avalonia/blob/53c1c0c7e3702072a2b77560a67d4cb1abab95cf/ShowMeTheXaml.Avalonia.AvaloniaEdit/XamlDisplayAvaloniaEditThemeBehavior.cs#L27-L34

But after that apps hangs out.

Actual issue is reproduceable in Material.Avalonia demo app for browser. Here is the link: https://avaloniacommunity.github.io/Material.Avalonia/ (aim on code icons on the bottom right side of controls on any page).

Also, this issue only reproduceable in browser WASM, since NAOT app on desktop works fine.

I'd be grateful for any help to diagnose this issue.

@Numpsy
Copy link
Contributor

Numpsy commented Apr 4, 2024

It worked some time ago but I didn't try it recently. Any clue of what the issue is?

I was never able to get WasmEnableTheads to work properly in Avalonia 11.0 (various problems occuring before it even got to the TextMateSharp bit), but I just had another try with the Avalonia 11.1 nightly and it seems to have worked:

image

(that's both highlighting Json and doing folding at the same time)

@SKProCH
Copy link

SKProCH commented Apr 4, 2024

Hmm, I should give it a try.

@SKProCH
Copy link

SKProCH commented Apr 7, 2024

Seems like my case still doesn't work properly with nightly avalonia. @Numpsy, can you, please, share your project?

@Numpsy
Copy link
Contributor

Numpsy commented Apr 8, 2024

The screen shot I posted is from a project at work that I can't post all the code for, but I'll try to extract a sample out of it

@danipen
Copy link
Owner

danipen commented Aug 10, 2024

Guys, you should try v1.0.61. It ships #70, so TextMateSharp now supports WASM and includes native libs for each dotnet (6.0, 7.0, 8.0).

@Numpsy
Copy link
Contributor

Numpsy commented Aug 11, 2024

I had a go at a minimal test project using Avalonia.FuncUI at https://github.com/Numpsy/FuncUITextMateSharpWasmTest and the syntax highlighting for Json seems ok (tried on Edge and Firefox on Windows).

My first go at using TextMateSharp 1.0.61 rather than TextMateSharp.Wasm didn't work. I haven't had time to investigate that in detail, though it might be that the condition at https://github.com/aikawayataro/Onigwrap/blob/a62e9cae00b8cc134f807f59c995ae14b9f969ed/src/Onigwrap/buildTransitive/Onigwrap.targets#L3 doesn't include the native assets when using Avalonia.Browser (with no Blazor)

@aikawayataro
Copy link
Contributor

@Numpsy thanks for the example! I'll have a look at it

@HendrikMennen
Copy link
Author

HendrikMennen commented Aug 12, 2024

Once I enable "WasmEnableThreads" my app won't start anymore with:

MONO_WASM: Assert failed: Expect to have one js-module-threads asset in resources
Error: Assert failed: Expect to have one js-module-threads asset in resources
at Te (http://127.0.0.1:53299/_framework/dotnet.js:3:18520)
at J (http://127.0.0.1:53299/_framework/dotnet.js:3:5850)
at http://127.0.0.1:53299/_framework/dotnet.js:3:27682
at http://127.0.0.1:53299/_framework/dotnet.js:3:28704
at async Object.create (http://127.0.0.1:53299/_framework/dotnet.js:3:34625)
at async http://127.0.0.1:53299/main.js:6:23

Probably doesn't have anything to do with TextMateSharp though, but without it won't work aswell
https://github.com/one-ware/OneWare/tree/main/studio/OneWare.Studio.Browser

@Numpsy
Copy link
Contributor

Numpsy commented Aug 12, 2024

It might be that it needs different onigwrap builds for single/multi threaded consumer builds, though my knowledge of what actually changes in the native build part of that is rather slim

@aikawayataro
Copy link
Contributor

aikawayataro commented Aug 12, 2024

@Numpsy I was working on pthread build and adjusted the condition, but unfortunately it doesn't change anything (I verified that the resulting Wasm links onigwrap). I've noticed that when I start deleting text from your example, I get a deadlock. This deadlock is very similar to the one I get with the new version of the package, but the new one starts right after the app is launched. I'll investigate this further...

@Numpsy
Copy link
Contributor

Numpsy commented Aug 12, 2024

I was just doing some tests with the 1.0.5 onigwrap build, and had this odd situation on Firefox where the behavior varies depending if I have the browser dev tools are open:
ehhhhh

(I'm not yet clear if my sample app is doing something wrong, or if there is a timing issue somewhere)

I also sometimes get this in the diagnostics:
image

Reading the linked article, it mentions setting PTHREAD_POOL_SIZE to help with this, and I see that the project linked by @HendrikMennen has _WasmPThreadPoolSize set in the project file, so maybe it could be a configuration thing?

Why any of this is different to the previous TextMateSharp.Wasm package, I don't know :-(

@Numpsy
Copy link
Contributor

Numpsy commented Aug 12, 2024

I've noticed that when I start deleting text from your example, I get a deadlock.

Additional note: The app I posted the screen shot from on April 4th is actually running AvaloniaEdit in read only mode, so I didn't previously test editing the json after display.

@aikawayataro
Copy link
Contributor

Reading the linked article, it mentions setting PTHREAD_POOL_SIZE to help with this, and I see that the project linked by @HendrikMennen has _WasmPThreadPoolSize set in the project file, so maybe it could be a configuration thing?

I can see an idle worker in dev tools, probably not the reason... Worth trying though

Why any of this is different to the previous TextMateSharp.Wasm package, I don't know :-(

Maybe because it was built with older emsdk. I will try to swap the asset with freshly built to confirm.

Judging by deadlock it should be a threading problem, dont know where to dig though

@aikawayataro
Copy link
Contributor

Maybe because it was built with older emsdk. I will try to swap the asset with freshly built to confirm.

Not confirmed. It works but still hangs with the same deadlock after text is erased.

I have a wild guess. The only behavior from original Onigwrap I changed was dropping IDisposable in favor of just finalizer. This was supposed to ease freeing of native resources (pattern I follow with small memory resources), but maybe mono's GC has some problems with this. I'll try to understand current state of mono threading in Wasm and see if I can do anything.

@aikawayataro
Copy link
Contributor

I have a wild guess. ...

Not confirmed. Replaced Onigwrap with a dummy implementation and still hangs.

@HendrikMennen
Copy link
Author

I guess we will need to wait a little bit for things to work
dotnet/runtime#68162

@Numpsy
Copy link
Contributor

Numpsy commented Aug 12, 2024

The hang that sometimes happens when changing the highlighted text does appear to work better when running on .NET 9.0 - https://github.com/Numpsy/FuncUITextMateSharpWasmTest/tree/net9.0

@aikawayataro
Copy link
Contributor

The hang that sometimes happens when changing the highlighted text does appear to work better when running on .NET 9.0

Your example with dotnet 9 works for me without any freeze. I've released a new version with a workaround that links the latest available version of the binding with dotnet 9 (at this moment the emsdk used in 8 and 9 is the same).

Honestly, I would like to wait for dotnet 9 release, it looks very promising for Wasm support. I couldn't set up a debug environment to debug Wasm though, seems like documentation in veryyy early stages 😁

@danipen
Copy link
Owner

danipen commented Aug 13, 2024

I bumped the new onigwrap version in a new TextMateSharp v1.0.61. Thanks guys.

@Numpsy
Copy link
Contributor

Numpsy commented Sep 30, 2024

The hang that sometimes happens when changing the highlighted text does appear to work better when running on .NET 9.0

Your example with dotnet 9 works for me without any freeze. I've released a new version with a workaround that links the latest available version of the binding with dotnet 9 (at this moment the emsdk used in 8 and 9 is the same).

Honestly, I would like to wait for dotnet 9 release, it looks very promising for Wasm support. I couldn't set up a debug environment to debug Wasm though, seems like documentation in veryyy early stages 😁

According to
mono/SkiaSharp#3010
dotnet/runtime#100334

.NET 9.0 has moved to emscripten 3.1.56.
Does that require any updates over here to match?

@aikawayataro
Copy link
Contributor

.NET 9.0 has moved to emscripten 3.1.56.
Does that require any updates over here to match?

Yes, it has to be. Could you please check the value of the EmscriptenVersion property on the latest dotnet 9 preview?

@Numpsy
Copy link
Contributor

Numpsy commented Sep 30, 2024

If I run dotnet build --getProperty:EmscriptenVersion with the .NET 9.0 RC-1 SDK then it says 3.1.34, but when I did a dotnet workload update it installed a set of 3.1.56 versions:

image

@aikawayataro
Copy link
Contributor

I will add functionality to use the appropriate version when EmscriptenVersion is defined, thanks for your investigation

@Numpsy
Copy link
Contributor

Numpsy commented Nov 15, 2024

for the record:
I've had the minimal sample working on .NET 9, using Avalonia 11.2.1 and SkiaSharp 2.88.9, though there still seems to be a few issues with all the new versions of things (e.g. dotnet/runtime#109289) and it only presently works for me in release builds.

Also the shipping .NET 9 apparently has the wrong value for EmscriptenVersion - dotnet/runtime#108972

@danipen
Copy link
Owner

danipen commented Nov 15, 2024

There is a workaround for dotnet/runtime#109289 here: AvaloniaUI/Avalonia#17507

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

5 participants