title: How to get source code of a QML method in C++?
body: <p>I'm writing a serialization of objects to QML, and want to be able to get source code of functions defined in QML object. Suppose I have the following example in QML (<code>test.qml</code>):</p>
  
  <pre><code>import QtQml 2.2
  
  QtObject {
      function foo() {
          return 42;
      }
  }
  </code></pre>
  
  <p>I created a <code>QObject</code>: <code>obj</code> from that.</p>
  
  <p><strong>Is there any way (can be hacky) to get the source code of <code>obj</code>'s method <code>foo</code> without parsing the QML file <code>obj</code> was created from?</strong></p>
  
  <p>It's okay to use <code>QQmlComponent</code> <code>obj</code> was created from or any other Qt classes, as long as I don't have to parse it myself. Alternatively, how to get the function's source code from the <code>test.qml</code> file without writing my own parser? I don't want to assume anything special about <code>test.qml</code> (e.g. it can be different than the one above and it doesn't have to be simple enough to use a regexp or other not full-fledged QML parser).</p>
  
  <p>Assuming this works like JavaScript, I tried something like:</p>
  
  <pre><code>QQmlExpression expr(engine.rootContext(), obj, "foo.toString()");
  QVariant sourceCode = expr.evaluate();
  </code></pre>
  
  <p>However, it doesn't work.</p>
  
  <p>Edit: According to <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.2" rel="nofollow">http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.2</a> the <code>toString</code> method of a function object prototype is implementation-defined. In case of QML I get the result:</p>
  
  <pre><code>QVariant(QString, "function() { [code] }")
  </code></pre>
  
  <p>Since there doesn't seem to be a way to get the code by JS or C++, I'm not limiting myself to public Qt API anymore.</p>