Skip to content

add JavaScriptCoreSharp#274

Draft
lytico wants to merge 21 commits intoGtkSharp:developfrom
lytico:lytico/additional_apis
Draft

add JavaScriptCoreSharp#274
lytico wants to merge 21 commits intoGtkSharp:developfrom
lytico:lytico/additional_apis

Conversation

@lytico
Copy link
Copy Markdown
Member

@lytico lytico commented Jul 20, 2021

No description provided.

@lytico lytico marked this pull request as draft July 21, 2021 11:46
@jwosty
Copy link
Copy Markdown

jwosty commented Dec 17, 2021

I've taken this branch and lytico:lytico/webkit2 and merged them into one branch (jwosty:jwosty/frankenbranch) to see how they are working together - it builds. WebView.RunJavascript(string) is working fine, which is super cool.

I'm trying to get script message handlers to work, but getting this runtime error:

Marshaling script-message-received signal
Exception in Gtk# callback delegate
  Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
System.Exception: Unknown type WebKitJavascriptResult
   at GLib.Value.ToBoxed()
   at GLib.Value.get_Val()
   at GLib.SignalClosure.MarshalCallback(IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data)
   at GLib.ExceptionManager.RaiseUnhandledException(Exception e, Boolean is_terminal)
   at GLib.SignalClosure.MarshalCallback(IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data)
   at Gtk.Application.Run()
   at Examples.Gtk.WebKit.Main.main(String[] _arg1) in /home/jwostenberg/Code/Interstellar/Examples/Examples.Gtk.WebKit/Program.fs:line 23

The program '[7152] Examples.Gtk.WebKit.dll' has exited with code 0 (0x0).

What does this mean? I'd love to try to fix this, but I don't have experience working with GLib->.NET bindings. JavascriptResult.cs is being generated.

Here's the code that registers the handler (it's F#):

webView.Settings.EnableDeveloperExtras <- true
webView.Settings.EnableWriteConsoleMessagesToStdout <- true
webView.UserContentManager.ScriptMessageReceived.Add (fun e ->
    printfn "%A" e
    ()
)
if not (webView.UserContentManager.RegisterScriptMessageHandler("interstellarBridge")) then
    eprintfn "Failed to register script message handler; JS bridge will not work"

And the JS I used to trigger the handler (just execute via the JS console in the inspector in the app):

window.webkit.interstellarBridge.postMessage('hello')

Any pointers on what needs fixing?

@jwosty
Copy link
Copy Markdown

jwosty commented Dec 17, 2021

FWIW, this is my use case:

  1. I have a .NET desktop app running on macOS and Windows which I'm attempting to port to Linux
  2. It uses web frontend tech for the UI
  3. On Windows, it uses WinForms + CefSharp (Chromium) to embed the UI in a native window (via Interstellar)
  4. On macOS, it uses Xamarin.Mac + Apple WebKit to embed the UI (also via Interstellar)
  5. On both platforms, it relies on injecting JS into the page, and installing custom message handlers to facilitate communication between the JS and F#. Those should be the only two things it needs to work.

Your two PRs seem to enable most of this to work (great job so for); the script message handler seems to be the last thing holding it back.

Do these parts already work in the mono library? I'd be happy to use that for now, but I'd want to switch back to this fork ASAP, since .NET Core compatibility is a huge plus.

@lytico
Copy link
Copy Markdown
Member Author

lytico commented Sep 15, 2022

@lytico
Copy link
Copy Markdown
Member Author

lytico commented Sep 15, 2022

seems to be a problem with namespaces in WebkitGtkSharp-api.xml

missing type is generated in
namespace WebKit
struct JavascriptResult

in WebkitGtkSharp-api.xml it's typed as WebKitJavascriptResult and
marked as missing

it's never registered with GLib.GType.Register (...)

@3D-aero-sbahr
Copy link
Copy Markdown

Happy to see you looking into this again. BlazorWebView for GTK would be so great!

@lytico
Copy link
Copy Markdown
Member Author

lytico commented Sep 20, 2022

the problem seems to be in JavaScriptCore.Result
as soon as its referenced, the programm crashes:

test is Samples.WebviewSection

generated Code in JavascriptResult (slightly changed for debugging):

		public JavaScriptCore.Value JsValue {
			get {
				IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(this));
				System.Runtime.InteropServices.Marshal.StructureToPtr(this, this_as_native, false);
				IntPtr raw_ret = webkit_javascript_result_get_js_value(this_as_native);
				var glibobj = GLib.Object.GetObject(raw_ret);
				JavaScriptCore.Value ret = glibobj != null ? glibobj as JavaScriptCore.Value : default;

				ReadNative(this_as_native, ref this);
				System.Runtime.InteropServices.Marshal.FreeHGlobal(this_as_native);

				return ret;
			}
		}

then, in
GLib.Object.GetObject(raw_ret)
GLib.Object.GetObject(raw_ret,false)

it crashes calling g_object_is_floating(o)


			if (!owned_ref && !g_object_is_floating(o))
				g_object_ref (o);
			else if (owned_ref && g_object_is_floating(o))
				unexpected_owned_floating = true;

@lytico
Copy link
Copy Markdown
Member Author

lytico commented Mar 1, 2023

@lytico
Copy link
Copy Markdown
Member Author

lytico commented Jun 12, 2023

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

Successfully merging this pull request may close these issues.

4 participants