瀏覽代碼

Improve PG performance (no more eval)

David Catuhe 5 年之前
父節點
當前提交
f6e0935e6a
共有 2 個文件被更改,包括 25 次插入14 次删除
  1. 21 14
      Playground/js/main.js
  2. 4 0
      Playground/js/utils.js

+ 21 - 14
Playground/js/main.js

@@ -1,4 +1,6 @@
 var engine = null;
+var canas = null;
+var scene = null;
 
 handleException = function(parent, e) {
     parent.utils.showError(e.message, e);
@@ -6,6 +8,16 @@ handleException = function(parent, e) {
     console.error(e);
 }
 
+fastEval = function(code) {
+    var head = document.getElementsByTagName('head')[0];
+    var script = document.createElement('script');
+    script.setAttribute('type', 'text/javascript');
+
+    script.innerHTML = code;
+
+    head.appendChild(script);
+}
+
 /**
  * Compile the script in the editor, and run the preview in the canvas
  */
@@ -37,7 +49,7 @@ compileAndRun = function(parent, fpsLabel) {
             engine = null;
         }
 
-        var canvas = document.getElementById("renderCanvas");
+        canvas = document.getElementById("renderCanvas");
         document.getElementById("errorZone").style.display = 'none';
         document.getElementById("errorZone").innerHTML = "";
         document.getElementById("statusBar").innerHTML = "Loading assets... Please wait.";
@@ -47,11 +59,10 @@ compileAndRun = function(parent, fpsLabel) {
         var createSceneFunction;
 
         parent.monacoCreator.getRunCode().then(code => {
-            var createDefaultEngine = function () {
+            createDefaultEngine = function () {
                 return new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
             }
 
-            var scene;
             var defaultEngineZip = "new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true })";
 
             if (code.indexOf("createEngine") !== -1) {
@@ -75,33 +86,29 @@ compileAndRun = function(parent, fpsLabel) {
                 engine = createDefaultEngine();
                 scene = new BABYLON.Scene(engine);
                 var runScript = null;
-                eval("runScript = function(scene, canvas) {" + code + "}");
+                fastEval("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 + ";";
+                    code += "\n" + "scene = " + createSceneFunction + "();";
                 }
                 else {
-                    __createScene = createSceneFunction;
                     var startCar = code.search('var ' + createSceneFunction);
                     code = code.substr(0, startCar) + code.substr(startCar + 4);
+                    code += "\n" + "scene = " + createSceneFunction + "();";
                 }
 
-                // Execute the code
-                eval(code);
-
                 // Create engine
-                eval("engine = " + createEngineFunction + "()");
+                fastEval("engine = " + createEngineFunction + "()");
                 if (!engine) {
                     parent.utils.showError("createEngine function must return an engine.", null);
                     return;
-                }
+                }                
 
-                // Create scene
-                eval("scene = " + __createScene + "()");
+                // Execute the code
+                fastEval(code);
 
                 if (!scene) {
                     parent.utils.showError(createSceneFunction + " function must return a scene.", null);

+ 4 - 0
Playground/js/utils.js

@@ -19,6 +19,10 @@ class Utils {
     };
 
     toLocationError(errorMessage, errorEvent) {
+        if (!errorEvent) {
+            return null;
+        }
+
         // Do we have any location info?
         if (errorEvent.hasOwnProperty('lineNumber') && errorEvent.hasOwnProperty('columnNumber'))
             return errorEvent;