Infinite recursion error with Reflect.has

by Joe Pea

JavaScript

"use strict";

function multiple(...classes) {
    if (classes.length === 0)
        return Object;
    if (classes.length === 1) {
        const result = classes[0];
        return (typeof result === 'function' ? result : Object);
    }

    const FirstClass = classes.shift();
    const __instances__ = new WeakMap();
    const getInstances = (inst) => {
        let result = __instances__.get(inst);
        if (!result)
            __instances__.set(inst, (result = []));
        return result;
    };

    class MultiClass extends FirstClass {
        constructor(...args) {
            super(...args);

            const protoBeforeMultiClassProto = findPrototypeBeforeMultiClassPrototype(this, MultiClass.prototype);
            if (protoBeforeMultiClassProto)
                Object.setPrototypeOf(protoBeforeMultiClassProto, newMultiClassPrototype);

            const instances = getInstances(this);

            for (const Ctor of classes)
                instances.push(new Ctor(...args));
        }
    }

    let count = 0;
    const newMultiClassPrototype = new Proxy({
        __proto__: MultiClass.prototype,
    }, {
        get(target, key, self) {
            if (count++ < 500) console.log(' --------------- get', key);

            if (Reflect.has(MultiClass.prototype, key))
                return Reflect.get(MultiClass.prototype, key, self);

            for (const instance of getInstances(self))
                if (Reflect.has(instance, key))
                    return Reflect.get(instance, key, self);

            return undefined;
        },

        set(target, key, value, self) {
            console.log(' ----- set1', key, value);

            // PROXY INFINITE RECURSION HERE:
            console.log('hmmmmmmmmmmmmmmmm?', Reflect.has(MultiClass.prototype, key));

            console.log(' ----- set1.5', key, value);

            if (Reflect.has(MultiClass.prototype, key)) {
                return Reflect.set(target, key, value, self);
            }

     ...