edbrowse-dev - development list for edbrowse
 help / color / mirror / Atom feed
* [edbrowse-dev] Rooted
@ 2020-11-01  3:23 Karl Dahlke
  2020-11-01 22:35 ` Adam Thompson
  0 siblings, 1 reply; 4+ messages in thread
From: Karl Dahlke @ 2020-11-01  3:23 UTC (permalink / raw)
  To: edbrowse-dev

[-- Attachment #1: Type: text/plain, Size: 3676 bytes --]

Spider monkey has a rooting guide, which I bookmarked, and I need to read, but sometimes I learn more just playing withthe the software.

First the existential crisis.
I stored a pointer to the document object, swallowed startwindow.js and third.js, which is a lot of stuff,
Fetched the document object from the global object, and verified that the pointer changed out from under me.
Pointer to object is not permanent and guaranteed, as it is in duktape.
Just one of many reasons duktape is sooooo much easier to use.
We have to have reliable pointers.
When I go to a hyperlink, <a> tag, there's an object with that,
edbrowse keeps a pointer to that object, I need to be able to bounce through that pointer and look at the object,
and see if the href has changed, or if there is onclick code to run, and so on and so on.
If the garbage collector has moved that object somewhere else, or if it moved by realloc because it got bigger,
then I'm fucking screwed.
A seg fault that I'll never be able to debug.
That is the existential crisis.
And the crisis is confirmed by my hello program, wherein the document object moved to a new location because of executing 16,000 lines of js.
This of course put me into a deep depression, along with everything else that is happening in my life.

But I wanted to learn something.
Still haven't read the rooting guide, but I knew it had something to do with rooting.

There is a general Rooted class.

template <t>
class Rooted<t> { ... }

(This is where you have to learn c++.)

Lots of things can be rooted but I'm mostly interested in objects.
So specialize the template as


They have some convenient typedefs for the common ones.

typedef Rooted<Object> RootedObject;
typedef Rooted<Value> RootedValue;

A rooted thing is 12 bytes.
The first 8 bytes do the rooting, somehow, and the last 4 bytes point to the thing.
The * operator is overloaded to push the pointer out.
(Other important operators are overloaded as well.)
So if d is an object from class Rooted<Foo>, then *d returns those last 4 bytes, which is a pointer to something of type foo.
But only use that pointer in a transient way.
I thought rooting would prevent the pointer from changing, prevent the thing from moving.
But it does update the pointer in each root if it does move.
So, my document object moved, and it updated the pointer in my rooted document object.
In fact they really want you to deal with the rooted things, not the pointers at all, if you can help it.
Their functions take and return rooted things.
If objects move around in memory, all the rooted things are updated.

I have a structure htmlTag, with a member jv, javascript variable.
It's just a void *
I could point to the object and feel good about it, in duktape, because the object never moved.
In mozjs, I can't just point to the object, it might move, I have to use something rooted.
I could have RootedObject in the html tag, but then all of edbrowse has to read in jsapi.h,
and all of edbrowse has to know about RootedObject, and all of edbrowse has to be processed by g++.
There's no more encapsulation, keeping it all within jseng-moz.cpp.
That would piss me off!
(Yeah, that's how we handled it many years ago.)
Or I could just say ok, jv is an opaque 12 bytes that I don't know anything about, but that's hardly portable.
What if it's 16 bytes on some other machine, or even 24?
I could point to or index an array of rooted things that is only known inside jseng-moz.cpp.
I sort of like that idea but not sure how to implement it in practice.

Holy shit this stuff is complicated.

Karl Dahlke

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-11-09 22:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-01  3:23 [edbrowse-dev] Rooted Karl Dahlke
2020-11-01 22:35 ` Adam Thompson
2020-11-01 23:27   ` Karl Dahlke
2020-11-09 22:58     ` Adam Thompson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).