Browse Source

Merge pull request #146 from rsredsq/RED-EDITOR-SMAPS

Editor will give a real message error through using a source maps
JoshEngebretson 10 years ago
parent
commit
0ae8e51d3e

+ 8 - 1
Resources/EditorData/AtomicEditor/Script/jsutils.js

@@ -1,5 +1,6 @@
 __atomic_acorn = require('./acorn');
 __atomic_beautify = require('./beautify');
+__atomic_sourcemap = require('./source-map');
 
 exports.parseToJSON = function (source) {
 
@@ -37,7 +38,6 @@ exports.parseErrorCheck = function(source) {
 
 
 	try {
-
 		__atomic_acorn.parse( source, {
 	    	ranges: true,
 	    	locations: true,
@@ -65,3 +65,10 @@ exports.jsBeautify = function (source) {
 	return __atomic_beautify.js_beautify(source);
 
 }
+
+exports.getRealLineNumber = function (map, line) {
+    var jsonMap = JSON.parse(map);
+    var smc = new __atomic_sourcemap.SourceMapConsumer(jsonMap);
+    var pos = smc.originalPositionFor({line: line, column: 100000});
+    return pos.line;
+}

File diff suppressed because it is too large
+ 1 - 0
Resources/EditorData/AtomicEditor/Script/source-map.js


+ 1 - 1
Script/AtomicEditor/.gitignore

@@ -1 +1 @@
-*.js
+out/*

+ 3 - 1
Script/AtomicEditor/tsconfig.json

@@ -5,7 +5,9 @@
         "declaration": false,
         "noImplicitAny": false,
         "removeComments": true,
-        "noLib": false
+        "noLib": false,
+        "outDir": "out/",
+        "sourceMap": true
     },
     "filesGlob": [
         "./**/*.ts"

+ 1 - 3
Source/Atomic/IO/File.cpp

@@ -535,11 +535,9 @@ void File::ReadText(String& text)
     if (!size_)
         return;
 
-    text.Resize(size_ + 1);
+    text.Resize(size_);
 
     Read((void*)text.CString(), size_);
-
-    text[size_] = '\0';
 }
 
 // ATOMIC BEGIN

+ 7 - 7
Source/AtomicEditor/Application/AEEditorApp.cpp

@@ -47,7 +47,7 @@ void AEEditorApp::Start()
 
     context_->RegisterSubsystem(new EditorMode(context_));
 
-    vm_->SetModuleSearchPaths("AtomicEditor");
+    vm_->SetModuleSearchPaths("AtomicEditor/out");
 
     // Do not create bone structure by default when in the editor
     // this can be toggled temporarily, for example to setup an animation preview
@@ -67,17 +67,17 @@ void AEEditorApp::Start()
     jsapi_init_toolcore(vm_);
     jsapi_init_editor(vm_);
 
-    SharedPtr<File> file (GetSubsystem<ResourceCache>()->GetFile("AtomicEditor/main.js"));
+    SharedPtr<File> file (GetSubsystem<ResourceCache>()->GetFile("AtomicEditor/out/main.js"));
 
     if (file.Null())
     {
-        ErrorExit("Unable to load AtomicEditor/main.js");
+        ErrorExit("Unable to load AtomicEditor/out/main.js");
         return;
     }
 
     if (!vm_->ExecuteFile(file))
     {
-        ErrorExit("Error executing AtomicEditor/main.js");
+        ErrorExit("Error executing AtomicEditor/out/main.js");
         return;
     }
 
@@ -157,10 +157,10 @@ void AEEditorApp::HandleJSError(StringHash eventType, VariantMap& eventData)
     String errMessage = eventData[P_ERRORMESSAGE].GetString();
     String errFilename = eventData[P_ERRORFILENAME].GetString();
     //String errStack = eventData[P_ERRORSTACK].GetString();
-    int errLineNumber = eventData[P_ERRORLINENUMBER].GetInt();
+    int errLineNumber = vm_->GetRealLineNumber("AtomicEditor/out/" + errFilename, eventData[P_ERRORLINENUMBER].GetInt());
+    
+    String errorString = ToString("%s - %s - Line: %i", errFilename.CString(), errMessage.CString(), errLineNumber);
 
-    String errorString = ToString("%s - %s - Line: %i",
-                                  errFilename.CString(), errMessage.CString(), errLineNumber);
 
     ErrorExit(errorString);
 

+ 1 - 0
Source/AtomicJS/Javascript/JSRequire.cpp

@@ -133,6 +133,7 @@ namespace Atomic
             vm->SetLastModuleSearchFile(jsfile->GetFullPath());
             String source;
             jsfile->ReadText(source);
+            source.Append('\n');
             duk_push_string(ctx, source.CString());
             return 1;
         }

+ 46 - 0
Source/AtomicJS/Javascript/JSVM.cpp

@@ -247,6 +247,50 @@ void JSVM::SendJSErrorEvent(const String& filename)
 
 }
 
+int JSVM::GetRealLineNumber(const String& fileName, const int lineNumber) {
+    int realLineNumber = lineNumber;
+    String path = fileName;
+    if (!path.EndsWith(".js.map"))
+        path += ".js.map";
+    if (path.EndsWith(".js")) {
+        return realLineNumber;
+    }
+    SharedPtr<File> mapFile(GetSubsystem<ResourceCache>()->GetFile(path));
+    //if there's no source map file, maybe you use a pure js, so give an error, or maybe forgot to generate source-maps :(
+    if (mapFile.Null()) 
+    {
+        return realLineNumber;
+    }    
+    String map;
+    mapFile->ReadText(map);
+    int top = duk_get_top(ctx_);
+    duk_get_global_string(ctx_, "require");
+    duk_push_string(ctx_, "AtomicEditor/Script/jsutils");
+    if (duk_pcall(ctx_, 1))
+    {
+        printf("Error: %s\n", duk_safe_to_string(ctx_, -1));
+        duk_set_top(ctx_, top);
+        return false;
+    }
+
+    duk_get_prop_string(ctx_, -1, "getRealLineNumber");
+    duk_push_string(ctx_, map.CString());
+    duk_push_int(ctx_, lineNumber);
+    bool ok = true;
+    if (duk_pcall(ctx_, 2))
+    {
+        ok = false;
+        printf("Error: %s\n", duk_safe_to_string(ctx_, -1));
+    }
+    else
+    {
+        realLineNumber = duk_to_int(ctx_, -1);
+    }
+    duk_set_top(ctx_, top);
+
+    return realLineNumber;
+}
+
 bool JSVM::ExecuteScript(const String& scriptPath)
 {
     String path = scriptPath;
@@ -266,6 +310,7 @@ bool JSVM::ExecuteScript(const String& scriptPath)
     String source;
 
     file->ReadText(source);
+    source.Append('\n');
 
     duk_push_string(ctx_, file->GetFullPath().CString());
     if (duk_eval_raw(ctx_, source.CString(), 0,
@@ -291,6 +336,7 @@ bool JSVM::ExecuteFile(File *file)
     String source;
 
     file->ReadText(source);
+    source.Append('\n');
 
     duk_push_string(ctx_, file->GetFullPath().CString());
     if (duk_eval_raw(ctx_, source.CString(), 0,

+ 2 - 0
Source/AtomicJS/Javascript/JSVM.h

@@ -162,6 +162,8 @@ public:
 
     void SendJSErrorEvent(const String& filename = String::EMPTY);
 
+    int GetRealLineNumber(const String& fileName, const int lineNumber);
+
 private:
 
     void SubscribeToEvents();

+ 32 - 0
THIRDPARTY_LICENSE.md

@@ -562,3 +562,35 @@ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+#### Source-Map license
+----------------
+
+Copyright (c) 2009-2011, Mozilla Foundation and contributors
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+  this list of conditions and the following disclaimer in the documentation
+  and/or other materials provided with the distribution.
+
+* Neither the names of the Mozilla Foundation nor the names of project
+  contributors may be used to endorse or promote products derived from this
+  software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Some files were not shown because too many files changed in this diff