I want to use javascript's Proxy objects to augment a data structure with a bit of additional logging. While doing so, I noticed a problem:
simpleProxy = new Proxy(new Number(4), {})
simpleProxy.valueOf(); // TypeError: valueOf method called on incompatible Proxy
The error makes sense to me. valueOf is invoked with the this object set to the Proxy instead of the Number object thus leading to a type error.
The problem to me now is that in user code it is actually useful to propagate the Proxy as the this value into function calls so that access to properties is done through the proxy. Only when calling "low-level" routines like the above do I get in trouble. The question: How can I reliably detect that I'd be calling such a Proxy incompatible function, possibly assuming for a moment that user defined functions are safe to call?
This is not asking if the called function is a native function. Only certain native functions can not handle Proxy values. Besides, the detection in the linked question is rather unsound and would require different special cases here. Consider a function linked from a native interface. It may very well be able to operate on a proxy value, thus detecting it as a native function would be a false positive.
Currently, I believe the best approach is to find out if the target in get is primitive by calling target.valueOf() first instead of infering anything from whether the function is native or not. As such, this is not a duplicate.