Emscripten Javascript接口实现

Emscripten Javascript接口实现,javascript,c++,emscripten,Javascript,C++,Emscripten,我需要更多关于如何在javascript中实现emscripten生成的类的信息。我在C++中有以下接口,但是需要在JavaScript方面实现它。 class OsHttp { public: virtual ~OsHttp() {} virtual void request(const std::string & verb, const std::string & url, const std::string & body, const std::u

我需要更多关于如何在javascript中实现emscripten生成的类的信息。我在C++中有以下接口,但是需要在JavaScript方面实现它。
class OsHttp {
public:
    virtual ~OsHttp() {}

    virtual void request(const std::string & verb, const std::string & url, const std::string & body, const std::unordered_map<std::string, std::string> & headers, const std::shared_ptr<HttpCallback> & callback) = 0;
};

<>如果我了解你在做什么,那么你需要在JavaScript和C++世界之间进行某种沟通。另外,我认为如果你想使用C++中实现这个接口的对象,然后让它编译和运行,那么C++中的接口必须有一个具体的实现。这个接口的实现将调用Javascript

为此,可以在实现接口的类中使用:

class OsHttpImplementation : public OsHttp {
public:
    ~OsHttp()
    {
       EM_ASM({
         // Cleanup anything in Javascript context
       });
    }

    void request(const std::string & verb, const std::string & url, const std::string & body, const std::unordered_map<std::string, std::string> & headers, const std::shared_ptr<HttpCallback> & callback)
    {
        // Probably a good idea to save any shared pointers as members in C++
        // so the objects they point to survive as long as you need them

        int returnValue = EM_ASM_INT_V({
            // Example ways of accessing data from C++
            var verb = Pointer_stringify($0);
            var url = Pointer_stringify($1);
            var body = Pointer_stringify($2);
            var callbackFunctionPointer = $3;

            // Something here that makes an HTTP request, creates any objects, etc.

           return 0;

        }, verb.c_str(), url.c_str(), body.c_str(), callback.get());
    }
};

在C++中,可以使用它作为标准类:

// Run constructors
auto osHttp = new OsHttpImplementation();

// Make request
osHttp->request(....);

// Run destructors, and remove object from the Javascript store
delete osHttp;

我得说,这都是些假货

谢谢@MichalCharemza,这是一个很好的建议!!
class OsHttpImplementation : public OsHttp {
public:
    OsHttp()
    {
       EM_ASM_V({
         var thisPointer = $0;
         OsHttpFactory.construct(thisPointer);
       }, this);
    }

    ~OsHttp()
    {
       EM_ASM({
         var thisPointer = $0;
         OsHttpFactory.destruct(thisPointer);
       }, this);
    }

    void request(const std::string & verb, const std::string & url, const std::string & body, const std::unordered_map<std::string, std::string> & headers, const std::shared_ptr<HttpCallback> & callback)
    {
        int returnValue = EM_ASM_INT_V({
           var thisPointer = $0;
           OsHttpFactory.get(thisPointer).request($1, $2, $3, $4);
        }, this, verb.c_str(), url.c_str(), body.c_str(), callback.get());
    }
};
(function(context) {

  var store = {}

  function OsHttp() {
    this.request = null;
  }

  OsHttp.prototype.request = function(verb, url, body, callbackPointer) {
    var request = this.request = new XMLHttpRequest();
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        // Might need other arguments if you want to pass something back to C++
        Module.Runtime.dynCall('v', callbackPointer, []);
      }
    });
    this.request.open(verb, url, true);
    this.request.send();
  };

  OsHttp.prototype.cleanup = function() {
    // Do something to cleanup in-progress requests etc.
  }

  context.OsHttpFactory = {
    construct: function(thisPointer) {
      store[thisPointer] = new OsHttp();
    },
    destruct: function(thisPointer) {
      store[thisPointer].cleanup();
      delete store[thisPointer];
    },
    get: function(thisPointer) {
      return store[thisPointer];
    }
  };

})(window);
// Run constructors
auto osHttp = new OsHttpImplementation();

// Make request
osHttp->request(....);

// Run destructors, and remove object from the Javascript store
delete osHttp;