about summary refs log tree commit diff
path: root/src/librustc_binaryen/BinaryenWrapper.cpp
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2018-02-07 08:30:51 -0800
committerGitHub <noreply@github.com>2018-02-07 08:30:51 -0800
commitd2f7febe9330664041d8d7790aa497bd7f9b8d5c (patch)
tree6ce398e858635f23b0124c923fc70ca285c80b28 /src/librustc_binaryen/BinaryenWrapper.cpp
parent185f258801984637e508ea54ae211e6a62c02911 (diff)
parentcfe53c066646857a19046ebaf0cdc410c3d9f034 (diff)
downloadrust-d2f7febe9330664041d8d7790aa497bd7f9b8d5c.tar.gz
rust-d2f7febe9330664041d8d7790aa497bd7f9b8d5c.zip
Rollup merge of #47883 - yurydelendik:wasm-map, r=alexcrichton
Export wasm source map when debug information is enabled

We use binaryen's linker to produce a wasm file (via s2wasm). The wasm writer has capabilities to export source maps. The pilot support for source maps is added to Firefox.

The produced source map contains references to the original file, that might require additional source map file processing to include / package original files with it.

/cc @alexcrichton
Diffstat (limited to 'src/librustc_binaryen/BinaryenWrapper.cpp')
-rw-r--r--src/librustc_binaryen/BinaryenWrapper.cpp34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/librustc_binaryen/BinaryenWrapper.cpp b/src/librustc_binaryen/BinaryenWrapper.cpp
index d1095a7819d..55f11665f6d 100644
--- a/src/librustc_binaryen/BinaryenWrapper.cpp
+++ b/src/librustc_binaryen/BinaryenWrapper.cpp
@@ -14,6 +14,7 @@
 
 #include <stdint.h>
 #include <string>
+#include <sstream>
 #include <stdlib.h>
 
 #include "s2wasm.h"
@@ -24,6 +25,7 @@ using namespace wasm;
 
 struct BinaryenRustModule {
   BufferWithRandomAccess buffer;
+  std::string sourceMapJSON;
 };
 
 struct BinaryenRustModuleOptions {
@@ -36,6 +38,7 @@ struct BinaryenRustModuleOptions {
   bool ignoreUnknownSymbols;
   bool debugInfo;
   std::string startFunction;
+  std::string sourceMapUrl;
 
   BinaryenRustModuleOptions() :
     globalBase(0),
@@ -46,7 +49,8 @@ struct BinaryenRustModuleOptions {
     importMemory(false),
     ignoreUnknownSymbols(false),
     debugInfo(false),
-    startFunction("")
+    startFunction(""),
+    sourceMapUrl("")
   {}
 
 };
@@ -74,6 +78,12 @@ BinaryenRustModuleOptionsSetStart(BinaryenRustModuleOptions *options,
 }
 
 extern "C" void
+BinaryenRustModuleOptionsSetSourceMapUrl(BinaryenRustModuleOptions *options,
+                                         char *sourceMapUrl) {
+  options->sourceMapUrl = sourceMapUrl;
+}
+
+extern "C" void
 BinaryenRustModuleOptionsSetStackAllocation(BinaryenRustModuleOptions *options,
                                             uint64_t stack) {
   options->stackAllocation = stack;
@@ -106,12 +116,20 @@ BinaryenRustModuleCreate(const BinaryenRustModuleOptions *options,
   {
     WasmBinaryWriter writer(&linker.getOutput().wasm, ret->buffer, options->debug);
     writer.setNamesSection(options->debugInfo);
-    // FIXME: support source maps?
-    // writer.setSourceMap(sourceMapStream.get(), sourceMapUrl);
+
+    std::unique_ptr<std::ostringstream> sourceMapStream = nullptr;
+    {
+      sourceMapStream = make_unique<std::ostringstream>();
+      writer.setSourceMap(sourceMapStream.get(), options->sourceMapUrl);
+    }
 
     // FIXME: support symbol maps?
     // writer.setSymbolMap(symbolMap);
     writer.write();
+
+    if (sourceMapStream) {
+      ret->sourceMapJSON = sourceMapStream->str();
+    }
   }
   return ret.release();
 }
@@ -126,6 +144,16 @@ BinaryenRustModuleLen(const BinaryenRustModule *M) {
   return M->buffer.size();
 }
 
+extern "C" const char*
+BinaryenRustModuleSourceMapPtr(const BinaryenRustModule *M) {
+  return M->sourceMapJSON.data();
+}
+
+extern "C" size_t
+BinaryenRustModuleSourceMapLen(const BinaryenRustModule *M) {
+  return M->sourceMapJSON.length();
+}
+
 extern "C" void
 BinaryenRustModuleFree(BinaryenRustModule *M) {
   delete M;