<commit_before>var engine = null;

/**
 * Compile the script in the editor, and run the preview in the canvas
 */
compileAndRun = function(parent, fpsLabel) {
    // If we need to change the version, don't do this
    if (parent.settingsPG.mustModifyBJSversion()) return;

    try {
        parent.menuPG.hideWaitDiv();

        if (!BABYLON.Engine.isSupported()) {
            parent.utils.showError("Your browser does not support WebGL. Please, try to update it, or install a compatible one.", null);
            return;
        }

        var showInspector = false;
        parent.menuPG.showBJSPGMenu();
        parent.monacoCreator.JsEditor.updateOptions({ readOnly: false });

        if (BABYLON.Engine.LastCreatedScene && BABYLON.Engine.LastCreatedScene.debugLayer.isVisible()) {
            showInspector = true;
        }

        if (engine) {
            try {
                engine.dispose();
            }
            catch (ex) { }
            engine = null;
        }

        var canvas = document.getElementById("renderCanvas");
        document.getElementById("errorZone").style.display = 'none';
        document.getElementById("errorZone").innerHTML = "";
        document.getElementById("statusBar").innerHTML = "Loading assets... Please wait.";
        var checkCamera = true;
        var checkSceneCount = true;
        var createEngineFunction = "createDefaultEngine";
        var createSceneFunction;

        if (!navigator.gpu) {
            parent.utils.showError("WebGPU is not supported on your platform.");
            return;
        }

        parent.monacoCreator.getRunCode().then(async code => {
            try {
                var createDefaultEngine = function () {
                    return new BABYLON.WebGPUEngine(canvas);
                };

                var scene;
                var defaultEngineZip = "new BABYLON.WebGPUEngine(canvas)";

                if (code.indexOf("createEngine") !== -1) {
                    createEngineFunction = "createEngine";
                }

                // Check for different typos
                if (code.indexOf("delayCreateScene") !== -1) { // delayCreateScene
                    createSceneFunction = "delayCreateScene";
                    checkCamera = false;
                } else if (code.indexOf("createScene") !== -1) { // createScene
                    createSceneFunction = "createScene";
                } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
                    createSceneFunction = "CreateScene";
                } else if (code.indexOf("createscene") !== -1) { // createscene
                    createSceneFunction = "createscene";
                }

                if (!createSceneFunction) {
                    // Just pasted code.
                    engine = createDefaultEngine();
                    await engine.initAsync(window.glslangOptions).catch((e) => {
                        showparent.utils.showErrorError(e);
                
                        // Also log error in console to help debug playgrounds
                        console.error(e);
                    });
                    scene = new BABYLON.Scene(engine);
                    var runScript = null;
                    eval("runScript = function(scene, canvas) {" + code + "}");
                    runScript(scene, canvas);

                    parent.zipTool.ZipCode = "var engine = " + defaultEngineZip + ";\r\nvar scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
                } else {
                    var __createScene = null;
                    if (parent.settingsPG.ScriptLanguage == "JS") {
                        code += "\n" + "__createScene = " + createSceneFunction + ";";
                    }
                    else {
                        __createScene = createSceneFunction;
                        var startCar = code.search('var ' + createSceneFunction);
                        code = code.substr(0, startCar) + code.substr(startCar + 4);
                    }

                    // Execute the code
                    eval(code);

                    // Create engine
                    eval("engine = " + createEngineFunction + "()");
                    if (!engine) {
                        parent.utils.showError("createEngine function must return an engine.", null);
                        return;
                    }
                    await engine.initAsync(window.glslangOptions).catch((e) => {
                        parent.utils.showError(e);
                
                        // Also log error in console to help debug playgrounds
                        console.error(e);
                    });

                    // Create scene
                    eval("scene = " + __createScene + "()");

                    if (!scene) {
                        parent.utils.showError(createSceneFunction + " function must return a scene.", null);
                        return;
                    }

                    // if scene returns a promise avoid checks
                    if (scene.then) {
                        checkCamera = false;
                        checkSceneCount = false;
                    }

                    var createEngineZip = (createEngineFunction === "createEngine")
                        ? "createEngine()"
                        : defaultEngineZip;

                    parent.zipTool.zipCode =
                        code + "\r\n\r\n" +
                        "var engine = " + createEngineZip + ";\r\n" +
                        "var scene = " + createSceneFunction + "();";

                }

                engine = engine;
                engine.runRenderLoop(function () {
                    if (engine.scenes.length === 0) {
                        return;
                    }

                    if (canvas.width !== canvas.clientWidth) {
                        engine.resize();
                    }

                    var scene = engine.scenes[0];

                    if (scene.activeCamera || scene.activeCameras.length > 0) {
                        scene.render();
                    }

                    fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
                }.bind(this));

                if (checkSceneCount && engine.scenes.length === 0) {
                    parent.utils.showError("You must at least create a scene.", null);
                    return;
                }

                if (checkCamera && engine.scenes[0].activeCamera == null) {
                    parent.utils.showError("You must at least create a camera.", null);
                    return;
                } else if (scene.then) {
                    scene.then(function () {
                        document.getElementById("statusBar").innerHTML = "";
                    });
                } else {
                    engine.scenes[0].executeWhenReady(function () {
                        document.getElementById("statusBar").innerHTML = "";
                    });
                }
<commit_msg>also for webgpu (future safe)<commit_after>var engine = null;

/**
 * Compile the script in the editor, and run the preview in the canvas
 */
compileAndRun = function(parent, fpsLabel) {
    // If we need to change the version, don't do this
    if (parent.settingsPG.mustModifyBJSversion()) return;

    try {
        parent.menuPG.hideWaitDiv();

        if (!BABYLON.Engine.isSupported()) {
            parent.utils.showError("Your browser does not support WebGL. Please, try to update it, or install a compatible one.", null);
            return;
        }

        var showInspector = false;
        parent.menuPG.showBJSPGMenu();
        parent.monacoCreator.JsEditor.updateOptions({ readOnly: false });

        if (BABYLON.Engine.LastCreatedScene && BABYLON.Engine.LastCreatedScene.debugLayer.isVisible()) {
            showInspector = true;
        }

        if (engine) {
            try {
                engine.dispose();
            }
            catch (ex) { }
            engine = null;
        }

        var canvas = document.getElementById("renderCanvas");
        document.getElementById("errorZone").style.display = 'none';
        document.getElementById("errorZone").innerHTML = "";
        document.getElementById("statusBar").innerHTML = "Loading assets... Please wait.";
        var checkCamera = true;
        var checkSceneCount = true;
        var createEngineFunction = "createDefaultEngine";
        var createSceneFunction;

        if (!navigator.gpu) {
            parent.utils.showError("WebGPU is not supported on your platform.");
            return;
        }

        parent.monacoCreator.getRunCode().then(async code => {
            try {
                var createDefaultEngine = function () {
                    return new BABYLON.WebGPUEngine(canvas);
                };

                var scene;
                var defaultEngineZip = "new BABYLON.WebGPUEngine(canvas)";

                if (code.indexOf("createEngine") !== -1) {
                    createEngineFunction = "createEngine";
                }

                // Check for different typos
                if (code.indexOf("delayCreateScene") !== -1) { // delayCreateScene
                    createSceneFunction = "delayCreateScene";
                    checkCamera = false;
                } else if (code.indexOf("createScene") !== -1) { // createScene
                    createSceneFunction = "createScene";
                } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
                    createSceneFunction = "CreateScene";
                } else if (code.indexOf("createscene") !== -1) { // createscene
                    createSceneFunction = "createscene";
                }

                if (!createSceneFunction) {
                    // Just pasted code.
                    engine = createDefaultEngine();
                    await engine.initAsync(window.glslangOptions).catch((e) => {
                        showparent.utils.showErrorError(e);
                
                        // Also log error in console to help debug playgrounds
                        console.error(e);
                    });
                    scene = new BABYLON.Scene(engine);
                    var runScript = null;
                    eval("runScript = function(scene, canvas) {" + code + "}");
                    runScript(scene, canvas);

                    parent.zipTool.ZipCode = "var engine = " + defaultEngineZip + ";\r\nvar scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
                } else {
                    var __createScene = null;
                    if (parent.settingsPG.ScriptLanguage == "JS") {
                        code += "\n" + "__createScene = " + createSceneFunction + ";";
                    }
                    else {
                        __createScene = createSceneFunction;
                        var startCar = code.search('var ' + createSceneFunction);
                        code = code.substr(0, startCar) + code.substr(startCar + 4);
                    }

                    // Execute the code
                    eval(code);

                    // Create engine
                    eval("engine = " + createEngineFunction + "()");
                    if (!engine) {
                        parent.utils.showError("createEngine function must return an engine.", null);
                        return;
                    }
                    await engine.initAsync(window.glslangOptions).catch((e) => {
                        parent.utils.showError(e);
                
                        // Also log error in console to help debug playgrounds
                        console.error(e);
                    });

                    // Create scene
                    eval("scene = " + __createScene + "()");

                    if (!scene) {
                        parent.utils.showError(createSceneFunction + " function must return a scene.", null);
                        return;
                    }

                    // if scene returns a promise avoid checks
                    if (scene.then) {
                        checkCamera = false;
                        checkSceneCount = false;
                    }

                    var createEngineZip = (createEngineFunction === "createEngine")
                        ? "createEngine()"
                        : defaultEngineZip;

                    parent.zipTool.zipCode =
                        code + "\r\n\r\n" +
                        "var engine = " + createEngineZip + ";\r\n" +
                        "var scene = " + createSceneFunction + "();";

                }

                engine = engine;
                engine.runRenderLoop(function () {
                    if (engine.scenes.length === 0) {
                        return;
                    }

                    if (canvas.width !== canvas.clientWidth) {
                        engine.resize();
                    }

                    var scene = engine.scenes[0];

                    if (scene.activeCamera || scene.activeCameras.length > 0) {
                        scene.render();
                    }

                    // Update FPS if camera is not a webxr camera
                    if(!(sceneToRender.activeCamera && 
                        sceneToRender.activeCamera.getClassName && 
                        sceneToRender.activeCamera.getClassName() === 'WebXRCamera')) {
                        fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
                    }
                }.bind(this));

                if (checkSceneCount && engine.scenes.length === 0) {
                    parent.utils.showError("You must at least create a scene.", null);
                    return;
                }

                if (checkCamera && engine.scenes[0].activeCamera == null) {
                    parent.utils.showError("You must at least create a camera.", null);
                    return;
                } else if (scene.then) {
                    scene.then(function () {
                        document.getElementById("statusBar").innerHTML = "";
                    });
                } else {
                    engine.scenes[0].executeWhenReady(function () {
                        document.getElementById("statusBar").innerHTML = "";
                    });
                }

{'fraction_non_alphanumeric': 0.10417244247296922, 'fraction_numerical': 0.0018020515663986693, 'mean_word_length': 1.2208711713098352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4121540', 'n_tokens_mistral': 3506, 'n_tokens_neox': 3187, 'n_words': 725}