Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Listen to any changes to an Array

25 views
Skip to first unread message

James Stortz

unread,
Aug 5, 2018, 2:13:35 PM8/5/18
to dev-tech-...@lists.mozilla.org
Hello,

This is my first time using the mailing list, so I apologize if I am not in
the right place.

Is there a way to listen for any changes to an array in SpiderMonkey?

>From what I understand,

1.) JS_PropertyOp/DeletePropertyOp : only hooks an individual property,
i.e. index, (and only on Objects, but you can inherit Array prototype.)

2.) Array.prototype/array instance's methods : only hooks those methods,
not accessors/setters.

3.) Proxy Objects: hooks anything, but there is no MDN documentation, and
it seems a little daunting to derive from BaseProxyHandler.


If I could get Proxy Objects to work, I would use that, but I am trying to
understand how it works under the hood (to see if I could implement
something more simple on my array.) I was hoping to see some clues in
Proxy.cpp about how "set" works to listen for any changes, but didn't
figure anything out. (I feel like Proxies were added to the ES6 spec to
keep JS natives the same while augmenting them, and it seems like overkill.)


Can anybody help me?

Thanks!

Boris Zbarsky

unread,
Aug 6, 2018, 5:26:50 PM8/6/18
to
On 8/5/18 2:13 PM, James Stortz wrote:
> Is there a way to listen for any changes to an array in SpiderMonkey?

Not really, no. When the JIT sees access on an array it will compile it
down to direct machine code access, which can't really be interposed.

> 3.) Proxy Objects: hooks anything, but there is no MDN documentation, and
> it seems a little daunting to derive from BaseProxyHandler.

If you have the option of wrapping all the arrays you care about in
proxies, this might be the way to go, fwiw.

-Boris

James Stortz

unread,
Aug 7, 2018, 3:24:52 AM8/7/18
to dev-tech-...@lists.mozilla.org
Thanks, that is good news. So, proxies intercept calls/accessors and relay
it over I assume. That's a good approach, but I was initially surprised
that such functionality wasn't commonly available, or at least as robust,
until Proxies were adopted in the JS spec. It makes sense now.

I was able to get my array wrapped in a proxy easily without having to use
the C++ js:: API. It was just a matter of evaluating a script during init.
(The script creates the proxy and calls JSNative counterparts in each of
its methods. I suppose you could also set JSNative functions as its
methods.)

Curious how Proxies work, if anybody knows a good read.

-James

Boris Zbarsky

unread,
Aug 7, 2018, 9:50:29 AM8/7/18
to
On 8/7/18 3:24 AM, James Stortz wrote:
> Curious how Proxies work, if anybody knows a good read.

ES Proxies, or the more generic C++ Proxy API SpiderMonkey exposes?

-Boris
0 new messages