TextMateSharp icon indicating copy to clipboard operation
TextMateSharp copied to clipboard

TextMateSharp with Wasm not working?

Open hendrikmennen opened this issue 2 years ago • 29 comments

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 avatar Oct 26 '23 11:10 hendrikmennen

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

danipen avatar Oct 26 '23 12:10 danipen

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.

SKProCH avatar Mar 29 '24 16:03 SKProCH

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

danipen avatar Mar 29 '24 17:03 danipen

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.

SKProCH avatar Mar 29 '24 17:03 SKProCH

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)

Numpsy avatar Apr 04 '24 17:04 Numpsy

Hmm, I should give it a try.

SKProCH avatar Apr 04 '24 18:04 SKProCH

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

SKProCH avatar Apr 07 '24 13:04 SKProCH

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

Numpsy avatar Apr 08 '24 11:04 Numpsy

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).

danipen avatar Aug 10 '24 10:08 danipen

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)

Numpsy avatar Aug 11 '24 23:08 Numpsy

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

aikawayataro avatar Aug 12 '24 06:08 aikawayataro

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

hendrikmennen avatar Aug 12 '24 07:08 hendrikmennen

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

Numpsy avatar Aug 12 '24 10:08 Numpsy

@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...

aikawayataro avatar Aug 12 '24 11:08 aikawayataro

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 avatar Aug 12 '24 11:08 Numpsy

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.

Numpsy avatar Aug 12 '24 11:08 Numpsy

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 avatar Aug 12 '24 11:08 aikawayataro

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 avatar Aug 12 '24 11:08 aikawayataro

I have a wild guess. ...

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

aikawayataro avatar Aug 12 '24 12:08 aikawayataro

I guess we will need to wait a little bit for things to work https://github.com/dotnet/runtime/issues/68162

hendrikmennen avatar Aug 12 '24 12:08 hendrikmennen

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

Numpsy avatar Aug 12 '24 13:08 Numpsy

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 :grin:

aikawayataro avatar Aug 13 '24 11:08 aikawayataro

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

danipen avatar Aug 13 '24 12:08 danipen