/*
 * Copyright 2019 The CodeWorld Authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

function addMessage(err, str) {
    // Catch exceptions to protect against cross-domain access errors.
    try {
        if (window.parent && window.parent.addToMessage) {
            window.parent.addToMessage(str);

            if (err) {
                let message = window.parent.document.getElementById('message');
                message.classList.add('error');
            }

            return;
        }
    } catch (e) {}

    console.log(str);
}

function showCanvas() {
    // Catch exceptions to protect against cross-domain access errors.
    // If the frame is cross-domain, then it's embedded, in which case
    // there is no need to show it.
    try {
        if (!window.parent) {
            return;
        }

        let runner = window.parent.document.getElementById('runner');
        if (!runner) {
            return;
        }

        runner.style.display = '';
        runner.focus();
        runner.contentWindow.focus();
    } catch (e) {}
}

function start() {
    h$base_writeStdout = (fd, fdo, buf, buf_offset, n, c) => {
        addMessage(false, h$decodeUtf8(buf, n, buf_offset));
        c(n);
    };
    h$base_writeStderr = (fd, fdo, buf, buf_offset, n, c) => {
        addMessage(false, h$decodeUtf8(buf, n, buf_offset));
        c(n);
    };
    h$log = () => {
        let s = '';
        for (let i = 0; i < arguments.length; i++) {
            s = s + arguments[i];
        }
        addMessage(false, s + '\n');
    };
    h$errorMsg = str => {
        for (let i = 1; i < arguments.length; i++) {
            str = str.replace(/%s/, arguments[i]);
        }
        addMessage(true, '\n' + str);
    };
    h$base_stdout_fd.write = h$base_writeStdout;
    h$base_stderr_fd.write = h$base_writeStderr;

    h$run(h$mainZCZCMainzimain);
}

@@ -1918,16 +1918,23 @@
 $log = (
+...args
 ) =%3E %7B%0A 
@@ -1975,37 +1975,32 @@
 t i = 0; i %3C arg
-ument
 s.length; i++) %7B
@@ -2019,29 +2019,24 @@
  s = s + arg
-ument
 s%5Bi%5D;%0A      
@@ -2100,19 +2100,30 @@
 orMsg = 
+(
 str
+, ...args)
  =%3E %7B%0A  
@@ -2141,17 +2141,17 @@
 let i = 
-1
+0
 ; i %3C ar
@@ -2151,21 +2151,16 @@
  i %3C arg
-ument
 s.length
@@ -2207,21 +2207,16 @@
 %25s/, arg
-ument
 s%5Bi%5D);%0A 

Fix some uses of arguments in arrow functions.