2015-10-09 58 views
0

我需要更多關於如何在JavaScript中實現emscripten生成的類的信息。我在C++中有以下接口,但需要在JavaScript端實現它。Emscripten 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); 

回答

2

如果我理解你以後,那麼你需要有一些什麼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()); 
    } 
}; 

如果你想在那裏實際上是在Javascript中的對象,對應於C++對象,你可能需要:

要做到這一點,你可以在實現接口的類使用EM_ASM_* macros在Javascript中進行一些手動管理以在某種工廠中創建/存儲/刪除對象。具體來說,它需要將它們存儲在某個地方,以便C++可以通過某種鍵訪問正確的一個。指針「這」可能是方便爲:

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

您對OsHttpFactory的在Javascript的執行有很大的自由度。如果你想在瀏覽器中你還沒有提到,但如果這樣做,並使用XMLHttpRequest的,你可以有那麼用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); 

++,你可以使用它作爲一個標準類:

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

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

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

我不得不說,這一切都有點兒缺憾!

+0

謝謝@MichalCharemza,這是偉大的建議! –