An idea to extend the functionality of Proxy objects.

# Ranando King (7 months ago)

I have an idea I’d like to propose with to Proxy objects. I would like to make a change to spec section 6.1.7.3 (as seen in the latest draft). The change is as follows:

The Internal Methods of Objects of an ECMAScript engine must conform to the list of invariants specified below. Ordinary ECMAScript Objects as well as all standard exotic objects in this specification maintain these invariants. ECMAScript Proxy objects maintain these invariants by means of runtime checks on the result of traps invoked on the [[ProxyHandler]] object using either the corresponding traps on the [[ProxyHandler]] should the needed trap be defined, or the default internal method otherwise.

Put simply, the change that I’m requesting would have the runtime checks verify the selfconsistency of the results returned by the [[ProxyHandler]] object instead of testing the [[ProxyHandler]] results against the target. The rationale behind this change is to allow Proxy objects the freedom to behave in a manner that is inconsistent with the behavior that would have been should the target have been accessed directly, while still requiring that the consistency of the behavior of the essential internal methods be upheld. In this way, a Proxy object would be able to project new properties for a proxied target even if the target object is not extensible, or even completely hide non-configurable properties. The requirement to do so would be implementation of the appropriate [[ProxyHandler]] methods so as to satisfy all of the corresponding invariants for all implemented handlers. The ECMAScript engine would then see results consistent with expectations, despite the fact that the results are inconsistent with the actual nature of the proxied target object.

An example might be the case of a library to mock objects.

/* SourceFile.js */
var obj = { bar: "The real bar. Accept no imitations!", fubar: "Always has
been." };
obj.seal();

export default obj;

/* TestFile.js */
import testObj from "SourceFile";

var mock = new Proxy(testObj, {
  has: function(target, key) {
    var retval = false;
    if (key == "foo") { //add the foo property
      retval = true;
    }
    else if (key != "bar") { //hide the bar property
      retval = (key in target);
    }

    return retval;
  },
  ownKeys: function(target) {
    var retval = Reflect.ownKeys(target);
    var barIndex = retval.indexOf(bar);

    if (barIndex != -1)
      retval.splice(barIndex, 1);

    retval.push("foo");
    return retval;
  },
  defineProperty: function(target, key, descriptor) {
    var retval = true;
    if ((key == "foo") || (key == "bar")) {
      retval = false;
    }
    else {
      Reflect.defineProperty(target, key, descriptor);
    }

    return retval;
  },
  get: function(target, key) {
    var retval = undefined;

    if (key == "foo") {
      retval = "You got the fake property!"
    }
    else if (key != "bar") {
      retval = Reflect.deleteProperty(target, key);
    }

    return retval;
  },
  set: function(target, key, value) {
    var retval = false;
    if ((key != "foo") && (key != "bar"))
      retval = Reflect.set(target, key);
    }
    return retval;
  },
  deleteProperty: function(target, key) {
    var retval = false;
    if ((key != "foo") && (key != "bar"))
      retval = Reflect.deleteProperty(target, key);
    }
    return retval;
  },
  getOwnPropertyDescriptor: function(target, key) {
    var retval;
    if (key == "foo") {
      retval = {
        enumerable: true,
        writable: false,
        configurable: false,
        value: "You got the fake property!"
      };
    }
    else if (key != "bar") {
      retval = Reflect.getOwnPropertyDescriptor(target, key);
    }
    return retval;
  }
});

console.log(mock.fubar); // "Always has been"
console.log(mock.foo);   // #1
console.log(mock.bar);   // #2

Currently, if the above code were run, an error would be thrown at comment .#1. Even if that line was commented out, an error would be thrown at comment #2. With my proposed change, the code would run successfully. Comment #1 would be "You got the fake property!" and comment #2 would be undefined. As a matter of completeness, if the handler only contained the "get" and "set" methods, this code would throw an error just as it currently would. The reason that it works with all of the handlers this that these handlers ensure that a consistent description of the is being presented to the interpreter since the interpreter would rely on the methods of the proxy object's handler to supply the data needed to validate the response of the original call.

# Isiah Meadows (7 months ago)

Make sure to compare your changes to the invariants of the essential internal methods, since changes to them would require some significant justification. And yes, your proposal would require changes to them. (I'm not a TC39 member, but I've read up enough about their decisions to make some educated guesses, and core changes tend to be met with extreme skepticism.)

Also, note that you probably meant to, in SourceFile.js, use Object.seal(obj), not obj.seal() (it's a static method, not an instance method).


Isiah Meadows me at isiahmeadows.com

Looking for web consulting? Or a new website? Send me an email and we can get started. www.isiahmeadows.com

# Ranando King (7 months ago)

You're right about the mistake with Object.seal. I wasn't too focused on that part when writing the example code. So are you suggesting that I also prepare a fully detailed rewrite of the invariant rules for comparison?

# Ranando King (7 months ago)

---------- Forwarded message ----------
From: Ranando King <kingmph at gmail.com>
Date: Fri, Jan 12, 2018 at 7:41 PM
Subject: Re: An idea to extend the functionality of Proxy objects.
To: Isiah Meadows <isiahmeadows at gmail.com>

You're right about the mistake with Object.seal. I wasn't too focused on that part when writing the example code. So are you suggesting that I also prepare a fully detailed rewrite of the invariant rules for comparison?

# Ranando King (7 months ago)

---------- Forwarded message ----------
From: Isiah Meadows <isiahmeadows at gmail.com>
Date: Fri, Jan 12, 2018 at 9:07 PM
Subject: Re: An idea to extend the functionality of Proxy objects.
To: Ranando King <kingmph at gmail.com>

Not full, but changes of some kind will likely be required, and you should address and document how those would be changed.

On Fri, Jan 12, 2018, 20:41 Ranando King <kingmph at gmail.com> wrote:

You're right about the mistake with Object.seal. I wasn't too focused on that part when writing the example code. So are you suggesting that I also prepare a fully detailed rewrite of the invariant rules for comparison?

# Ranando King (7 months ago)

---------- Forwarded message ----------
From: Ranando King <kingmph at gmail.com>
Date: Sat, Jan 13, 2018 at 2:23 PM
Subject: Re: An idea to extend the functionality of Proxy objects.
To: Isiah Meadows <isiahmeadows at gmail.com>

While I understand what you're getting at, The invariants would not change under my idea. In fact, I've thought about it carefully and crafted the wording change from my suggestion specifically so that the invariants wouldn't change. My goal is simply to change this process:

some action on proxied target ->
[[ProxyHandler]] method called ->
[[ProxyHandler]] method results checked by testing against
Reflect.<methods>(target...) ->
error on failure ->
return result

into

some action on proxied target ->
[[ProxyHandler]] method called ->
[[ProxyHandler]] method results checked by testing against
[[ProxyHandler]].<methods>(target...) ->
error on failure ->
return result

so that the developers proxy handler implementation is solely responsible for ensuring that the invariant requirements are maintained. The <methods> in question are exactly those described by the invariants. The only thing that is changing is that [[ProxyHandler]] would be the authority for the information used to ensure that the invariants have not been violated.

The gist is that while currently, given a caller, a proxy, and a target, the target constrains what the caller can do with the proxy, what I want is to reverse it so that the proxy constrains what the caller can do with the target, and all still without violating the existing invariant requirements. Does that make what I'm after a little clearer?

# Ranando King (7 months ago)

---------- Forwarded message ----------
From: Ranando King <kingmph at gmail.com>
Date: Sat, Jan 13, 2018 at 2:32 PM
Subject: Re: An idea to extend the functionality of Proxy objects.
To: Isiah Meadows <isiahmeadows at gmail.com>

After thinking about it a bit more, maybe you mean something like this:

Original:

[[SetPrototypeOf]] (V)

  • The Type of the return value must be Boolean.
  • If target is non-extensible, [[SetPrototypeOf]] must return false, unless V is the SameValue as the target's observed [[GetPrototypeOf]] value.

Modified:

[[SetPrototypeOf]] (V)

  • The Type of the return value must be Boolean.
  • If [[IsExtensible]] returns false, [[SetPrototypeOf]] must return false, unless V is the SameValue as that returned by [[GetPrototypeOf]].
# Ranando King (7 months ago)

---------- Forwarded message ----------
From: Ranando King <kingmph at gmail.com>
Date: Sat, Jan 13, 2018 at 2:35 PM
Subject: Re: An idea to extend the functionality of Proxy objects.
To: Isiah Meadows <isiahmeadows at gmail.com>

Sorry clicked send before editing. The first version is the original, the second the modified version. This this the kind of thing you were referring to? I admit that doing this makes it more clear that the prototype handler methods are being called, and I can see the value in doing so, but this would indeed require a fairly large rewording to the invariant requirements. Hopefully, however, it is clear that such a rewording doesn't change the intent of the requirement.

# Ranando King (7 months ago)

---------- Forwarded message ----------
From: Ranando King <kingmph at gmail.com>
Date: Mon, Jan 15, 2018 at 12:34 AM
Subject: Re: An idea to extend the functionality of Proxy objects.
To: Isiah Meadows <isiahmeadows at gmail.com>

I'm only including the altered verbiage for each internal method. The altered invariants would be as follows, with changes in red, with a black background for removed bits. Sorry if this looks too much like a redacted government document. The point of these changes is to remove any references to "the target" in favor of "the target as described by the essential internal methods, superseded by the corresponding methods of a [[ProxyHandler]] should one exist".

Definitions:

  • The target of an internal method is the object upon which the internal method is called.
  • A target is non-extensible if it has been observed to return false from its [[IsExtensible]] internal method, or true from its [[PreventExtensions]] internal method.
  • A non-existent property is a property that does not exist as an own property on a non-extensible target.
  • All references to SameValue are according to the definition of the SameValue algorithm.
  • Each reference to an essential internal method is presumed to refer to the corresponding trap on the [[ProxyHandler]] object should both the object and the trap exist.

[[GetPrototypeOf]] ( )

  • If [[IsExtensible]] returns false, and [[GetPrototypeOf]] returns a value v, then any future calls to [[GetPrototypeOf]] should return the SameValue as v.

[[SetPrototypeOf]] (V)

  • If [[IsExtensible]] returns false, [[SetPrototypeOf]] must return false, unless V is the SameValue as the target's observed [[GetPrototypeOf]] value.

[[PreventExtensions]] ( )

  • If [[PreventExtensions]] returns true, all future calls to [[IsExtensible]] on the target must return false and the target is now considered non-extensible.

[[GetOwnProperty]] (P)

  • If [[IsExtensible]] returns false and P is non-existent, then all future calls to [[GetOwnProperty]] (P) on the target must describe P as non-existent (i.e. [[GetOwnProperty]] (P) must return undefined).

[[DefineOwnProperty]] (P, Desc)

  • [[DefineOwnProperty]] must return false if [[GetOwnProperty]] (P) has previously been observed as a non-configurable own property of the target, unless either:
  • [[DefineOwnProperty]] (P, Desc) must return false if [[IsExtensible]] returns false and P is a non-existent own property. That is, if [[IsExtensible]] returns false, [[DefineOwnProperty]] (P, Desc) cannot be used to create new properties.

[[HasProperty]] ( P )

  • If [[GetOwnProperty]] (P) was previously observed as a non-configurable data or accessor own property of the target, [[HasProperty]] must return true.

[[Get]] (P, Receiver)

  • If [[GetOwnProperty]] (P) was previously observed as a non-configurable, non-writable own data property of the target with value v, then [[Get]] must return the SameValue.
  • If [[GetOwnProperty]] (P) was previously observed as a non-configurable own accessor property of the target, and the [[Get]] attribute of P is undefined, the [[Get]] operation must return undefined.

[[Set]] ( P, V, Receiver)

  • If [[GetOwnProperty]] (P) was previously observed as a non-configurable, non-writable own data property of the target, then [[Set]] must return false unless V is the SameValue as P's [[Value]] attribute.
  • If [[GetOwnProperty]] (P) was previously observed as a non-configurable own accessor property of the target, and the [[Set]] attribute of P is undefined, the [[Set]] operation must return false.

[[Delete]] ( P )

  • If [[GetOwnProperty]] (P) was previously observed to be a non-configurable own data or accessor property of the target, [[Delete]] must return false.

[[OwnPropertyKeys]] ( )

  • If [[IsExtensible]] returns false, the returned List must contain only the keys of all own properties of the object that are observable using [[GetOwnProperty]].
# Oriol _ (7 months ago)

The problem is enforcing the invariants. For example, if [[GetOwnProperty]] returns a non-configurable non-writable descriptor, then all future calls must also return a non-configurable non-writable descriptor with the same value. But you can't check this by just calling some traps. Instead, you need to store the property value value somewhere so that you can compare in all future calls. And the target object is the perfect place to store it.

If the invariants prevent your traps from behaving like you want, just use an extensible object with no non-configurable property as the target.