title: How to pass strings between C++ and javascript via emscripten
body: <p>I am learning emscripten, and I can't even get the most basic string manipulation working, when passing strings between C++ and JS.</p>
  
  <p>For example, I would like to write a string length function. In C++:</p>
  
  <pre><code>extern "C" int stringLen(std::string p)
  {
      return p.length();
  }
  </code></pre>
  
  <p>Called from javascript as:</p>
  
  <pre><code>var len = _stringLen("hi.");
  </code></pre>
  
  <p>This yields <code>0</code> for me. How do I make this work as expected? Which string type should I use here? <code>char const*</code>? <code>std::wstring</code>? <code>std::string</code>? None seem to work; I always get pretty random values.</p>
  
  <p>This is only the beginning... How do I then <em>return</em> a string from C++ like this?</p>
  
  <pre><code>extern "C" char *stringTest()
  {
      return "...";
  }
  </code></pre>
  
  <p>And in JS:</p>
  
  <pre><code>var str = _stringTest();
  </code></pre>
  
  <p>Again, I cannot find a way to make this work; I always get garbage in JS.</p>
  
  <p>So my question is clearly: How do I marshal string types between JS and C++ via Emscripten?</p>