<html>
<head>
    <script src="/js-test-resources/js-test-pre.js"></script>
    <script src="resources/cross-frame-access.js"></script>
    <script>
        jsTestIsAsync = true;

        data = null;

        // Set up listener for message from iframe
        addEventListener('message', function(event) {
            if (event.data == "finishedLoad")
                doTest();
        }, false);

        function turnLeakedException(object) {
            try {
                object.setter = {toString: function() { return {} } };
            } catch (e) {
                let crossOriginFunctionConstructor = e.constructor.constructor;
                data = crossOriginFunctionConstructor(`
                    var element = document.getElementById("password");
                    if (!element)
                        return null;
                    return element.textContent;`)();
            }
            shouldNotBeEqualToString(`data`, `null`);
            shouldNotBeEqualToString(`data`, "Cocoa");
            data = null;
        }

        doTest = function()
        {
            let targetWindow = document.getElementById("target").contentWindow;

            // putInlineSlow
            turnLeakedException({__proto__: targetWindow.location});

            // putToPrimitive
            num = 1337;
            num.__proto__.__proto__ = targetWindow.location;
            turnLeakedException(num);

            finishJSTest();
        }
    </script>
</head>
<body>
    <div>This tests that you can't get cross-origin object during [[Put]] operation.</div>
    <iframe id="target" src="http://localhost:8000/security/resources/cross-frame-iframe-for-object-put-optimization-test.html"></iframe>
    <pre id="console"></pre>
    <script src="/js-test-resources/js-test-post.js"></script>
</body>
</html>
