Emscripten Javascript接口实现

时间:2015-10-09 07:22:39

标签: 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::unordered_map<std::string, std::string> & headers, const std::shared_ptr<HttpCallback> & callback) = 0;
};

我知道以下内容将让我开始,但我如何实现构造函数等。

var osHttp = {
    constructor: function(){}
    request: function(verb, url, body, headers, callback) {
        console.log('OsHttp with: ' + verb);
    }
};

var OsHttpObject = Module.OsHttp.implement(osHttp);

1 个答案:

答案 0 :(得分:2)

如果我了解您之后的内容,那么您需要在Javascript和C ++世界之间进行一些沟通。另外,我认为如果你想在C ++中使用一个实现这个接口的对象,那么为了使它能够编译和运行,必须在C ++中有一个具体的接口实现。然后,该接口的实现将调用Javascript。

为此,您可以在实现接口的类中使用EM_ASM_* macros

SQLITE

如果你想在Javascript中实际存在一个与C ++对象相对应的对象,你可能需要在Javascript中进行一些手动管理来在某种工厂中创建/存储/删除对象。具体来说,它需要将它们存储在某处,以便C ++可以通过某种键访问正确的密钥。指向&#34;这个&#34;可能很方便:

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());
    }
};

你在Javascript中实现OsHttpFactory有很多自由。你还没有提到如果你想在浏览器中使用它,但如果你这样做,并且正在使用XMLHttpRequests,你可能会有类似的东西

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());
    }
};

然后在C ++中,您可以将它用作标准类:

(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);

我不得不说,这有点过分了!