edbrowse-dev - development list for edbrowse
 help / color / mirror / Atom feed
From: Karl Dahlke <eklhad@comcast.net>
To: edbrowse-dev@edbrowse.org
Subject: [edbrowse-dev] Rooted
Date: Sat, 31 Oct 2020 23:23:49 -0400	[thread overview]
Message-ID: <20200931232349.eklhad@comcast.net> (raw)

[-- 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

Rooted<Object>

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.
WRONG!
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

             reply	other threads:[~2020-11-01  3:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-01  3:23 Karl Dahlke [this message]
2020-11-01 22:35 ` Adam Thompson
2020-11-01 23:27   ` Karl Dahlke
2020-11-09 22:58     ` Adam Thompson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200931232349.eklhad@comcast.net \
    --to=eklhad@comcast.net \
    --cc=edbrowse-dev@edbrowse.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).