[webkit-gtk] Memory management for JavaScriptCore objects

Carlos Garcia Campos cgarcia at igalia.com
Mon Jan 2 01:13:32 PST 2017


El lun, 02-01-2017 a las 11:47 +1100, Michael Gratton escribió:
> Hey all,

Hi, 

> As part of porting Geary to WK2 (I'm writing this from it now!) I
> have 
> been developing a Vala VAPI[0] for JSC, so I can work with the JS 
> objects returned by JavascriptResult and Frame directly from Vala, 
> without dropping back to C. However since they are not GObjects and
> not 
> ref counted, I need to get the momeory management right still.

Yes, we plan to have GObject bindings of JSC that would make your life
easier. There's a wip patch in [1] that is waiting review (I've been
quite busy, but it's still in my TODO). Memory management is indeed the
main challenge, because it requires to have a good knowledge of JSC
internals and how the garbage collector works. It's also important to
understand how apps will use the API to try to make the lib as
convenient as possible, so your feedback would be really useful.

> I've been trying to work out when to use functions like 
> JSGlobalContextRelease, JSStringRelease, and JSValueUnprotect, but 
> haven't been able to find any useful documentation about it. Also, 
> examples on the web seem to conflict: E.g. The WebKitGTK docs and 
> Cookbook[1] uses JSStringRelease on a string copied from a
> JSValueRef, 
> whereas others (e.g. [2]) suggest that JSStringRelease only needs to
> be 
> used in certain instances, or that it needs to be used all the time. 
> Any pointers to canonical docs and examples would be appreciated.

JavaScript objects are garbage collected, that affects JSValueRef,
JSObjectRef, but not JSGlobalContext or JSString. You should always
release those. JSValueUnprotectc is not comparable with
JSGlobalContextRelease or JSStringRelease, Release methods do actually
release/free/unrefs the objects, but Unprotect tells the garbage
collector that it's ok to release that object. It only makes sense if
the value has been protected before, note that values are not protected
by default.

> I assume I never need to release a JSGlobalContextRef obtained from 
> JavascriptResult or Frame, since the web process will effectively 
> retain it, but what about the others? Would it hurt to call 
> JSGlobalContextRelease and JSStringRelease even if not needed? What 
> about JSValueRefs?

Yes, the global context returned by
webkit_frame_get_javascript_global_context() is owned by WebKit, it's
transfer-none. You shouldn't Unprotect values returned by
WebKitJavaScriptResult either, unless you explicitly protect them, of
course. But you should release strings returned by JSValueRef.

> Also, my assumption that JSC objects should be treated as Vala
> simple 
> types, i.e. they should be passed as copies (not by reference),
> that's 
> correct isn't it?

I'm not sure, because I don't know anything about vala, but JSValueRef
is a pointer. 

Most of the questions are not specific to WebKitGTK+, but to
JavaScriptCore C API which is cross-platform, so I would ask in webkit-
dev mailing list, since JSC developers can answer the questions much
better than me :-)

> Cheers,
> //Mike
> 
> [0] - 
> <https://git.gnome.org/browse/geary/tree/bindings/vapi/javascriptcore
> -4.0.vapi?h=wip/728002-webkit2>
> [1] - 
> <https://wiki.gnome.org/Projects/WebKitGtk/ProgrammingGuide/Cookbook>
> [2] - 
> <http://parmanoir.com/Taming_JavascriptCore_within_and_without_WebVie
> w>
> 

[1] https://bugs.webkit.org/show_bug.cgi?id=164061
-- 
Carlos Garcia Campos
http://pgp.rediris.es:11371/pks/lookup?op=get&search=0xF3D322D0EC4582C3
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 224 bytes
Desc: This is a digitally signed message part
URL: <https://lists.webkit.org/pipermail/webkit-gtk/attachments/20170102/74d5e784/attachment.sig>


More information about the webkit-gtk mailing list