Calling Clr object methods on the Ui thread #937
Replies: 4 comments
-
Poke |
Beta Was this translation helpful? Give feedback.
-
We do no. 1 - works fine here. We need to produce typings for the Javascript editor anyway, so there's a code generation step no matter what. We don't wrap the objects themselves, we only add wrapped methods (all classes are partial), so this has no effect on memory consumption. |
Beta Was this translation helpful? Give feedback.
-
Can you explain this a bit more? |
Beta Was this translation helpful? Give feedback.
-
Our codegen runs as part of our build system. It's built in C#, and just uses reflection to perform the following functions:
All JS accessible classes are defined as partial in our code. So our codegen can just create one big generated file where we add the methods. This may be overkill for the particular solution you have in mind though, but it works well for us since it allows us to do a bunch of stuff. I personally wouldn't recommend doing proxy objects, primarily since you'd be allocating twice the memory. Your no. 3 sounds pretty clean to me. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We are using jint in a xamarin mobile application. The engine runs on a background thread since we don't want any potentially long-running operations tying up the UI thread. We need to be able to call some methods in objects exposed to jint on the Ui thread, or the underlying OS throws exceptions.
I wanted to get your thoughts about the best way to do this. I have 4 ideas:
Create proxy objects that handle this completely outside of Jint. These have the same methods as the target, but just do a blocking thread switch before calling the real method. Good: Doesn't require any modification to Jint. Bad: Creating and keeping the proxy objects up to date is a hassle. It could be automated, but it is a pain.
Update MethodInfoFunctionInstance so the actual call to the method is done in a virtual function that can be overridden to make the thread switching call. This also requires creating a custom ObjectWrapper that knows about this subclass somehow. Create a custom converter and register it to do this. Good: Is dynamic and could potentially work with any object. Affects the creation process less. Bad: Requires some mods to ObjectWrapper, MethodInfoFunctionInstance.
Update ObjectWrapper to see if the class and/or methods are decorated with a special attribute that specifies an alternative MethodInfoFunctionInstance to use to wrap the method. Good: less intrusive. Bad: Would only work for classes that have been decorated. Seems to do something the converter system was intended to do.
Update JsValue.FromObject, and ObjectWrapper to leverage the converter system that's already in place. Good: Leverages the existing system. Bad: Greater amount of changes to the object conversion process.
What are your thoughts?
Beta Was this translation helpful? Give feedback.
All reactions