VirtualHandler fundamental traps default values

# David Bruant (13 years ago)

Direct proxies landed in Firefox a couple of days ago, so I thought I'd rewrite every sample I had written to the new API. I have started with the emulation of native arrays with proxies (and "normal objects") [1]. Most of my tests related to the proxyArray length fail because I only override the defineProperty trap (but not the get trap). That's fine, that's what the VirtualHandler API [2] is here for. This one is not implemented yet in Firefox [3], but I thought a bit more about how I would use it.

Line 53 [4], I use "this.delete" (this refers to the handler). Since I don't override the delete trap, this will throw an error (by definition of the VirtualHandler API as currently specified). In my case, I will have to override the delete trap with Reflect.delete (to respect ES5.1 specification of an array). Likewise for all fundamental traps. An alternative to throwing for missing trap is using the corresponding method in the Reflect module as mention in the wiki page open issue. I think it would be a good idea here.

To go further with this idea, VirtualHandler.prototype, instead of inheriting from Object.prototype could inherit from Reflect. And a virtual handler instance prototype chain would then look like: instance --> VirtualHandler.prototype --> Reflect --> Object.prototype --> null

VirtualHandler would just be one (convenient) way to override Reflect to generate handlers.

David

[1] DavidBruant/HarmonyProxyLab/tree/master/ProxyArray [2] harmony:virtual_object_api [3] bugzilla.mozilla.org/show_bug.cgi?id=787713 [4] DavidBruant/HarmonyProxyLab/blob/master/ProxyArray/ProxyArray.js#L53

# Tom Van Cutsem (13 years ago)

I just updated the "virtual object API" wiki page to reflect the proposed changes from the July meeting.

I agree that having Handler.prototype inherit from Reflect is actually sensible. As currently specified, Handler.prototype simply delegates all fundamental traps to the Reflect object. We have a tool to automate such code, and it's called prototypal inheritance :-)

Note that in ES6, Reflect should probably be represented as a module instance, not as a global object. So then Handler.prototype would inherit from a module instance object. I don't see anything necessarily wrong with that, but I just want to point it out.

Cheers, Tom

2012/9/1 David Bruant <bruant.d at gmail.com>