Hi again, Finally, I ended up with this after my experiments to get something back from js side (which in my case will be a string storing a json object from a mongodb query result). This way I don't need that many function pointers, just one for a function that acts as a constructor and by which the js db handler object gets created:
js_db::js_db(){ mongodb_init_fptr = EM_ASM_INT({ mongodb_init = Runtime.addFunction(function(objid) { console.log('objid:'+objid); function mongodb(objid) { var self = this; this.id = objid; this.MongoClient=null; this.assert=null; this.init = function () { console.log('mongoinst init'); self.MongoClient = require('mongodb').MongoClient; self.assert = require('assert'); }; this.exec_query = function () { console.log('mongoinst query'); var test = 'test'; var ptr = allocate(intArrayFromString(test),'i8',ALLOC_NORMAL); return ptr; }; } if(typeof mongoinst === 'undefined') { mongoinst=[]; } mongoinst[objid] = new mongodb(); mongoinst[objid].init(); }); return mongodb_init; },0); void (*mongodb_init)(int) = reinterpret_cast<void (*)(int)>(mongodb_init_fptr); mongodb_init(1);//instance number (here: 1) could be handled by the c++ class, not that I'd need more than one instance for a db handler } js_db::~js_db(){ //TODO: don't forget Runtime.removeFunction($0); } void js_db::exec_query(const std::string& query){ int addr = EM_ASM_INT({ var addr = mongoinst[$0].exec_query(); return addr; },1);//instance number needs to be passed here as well and TODO: pass in query as well:) std::cout<<std::string(reinterpret_cast<char*>(addr))<<std::endl; } My question would be now, who manages the memory allocated in the exec_query() js object method? If I got it right on the wiki, I'd need to call _free on the js side AND afterwards free() on the c++ side as well. Am I right? Thanks®ards, r0ller 2016. október 10., hétfő 20:24:35 UTC+2 időpontban jj a következőt írta: > > These types of activities are best done directly in JS side, with e.g. a > --js-library myMongoLibrary.js integration, rather than attempting to adapt > each JS code line to be called from C++ (in a JNI-style fashion). The JS > libraries allow storing both state and functions on JS side, which makes it > easier to store database objects and other things that don't need to be > marshalled over to C++ side, but can be pointed to via using some kind of > custom opaque handle. See the Emscripten src/library_xxx.js files for > example usage, they use this facility. > > 2016-10-10 17:24 GMT+03:00 <r0l...@freemail.hu <javascript:>>: > >> Hi All, >> >> I'm trying to instantiate a mongodb client and open it via >> emscripten::val but I'm already stuck at the beginning:( What I'm trying >> achieve first, is step 3 of this mongodb doc: >> https://docs.mongodb.com/getting-started/node/client/ which is this js >> oneliner: "var MongoClient = require('mongodb').MongoClient;" >> >> In the consturctor of my c++ class I have: >> >> js_db::js_db(){ >> emscripten::val require = emscripten::val::global("require"); >> emscripten::val mongoModule = require(std::string("mongodb")); >> emscripten::val mongoClient = mongoModule.MongoClient(); >> //TODO: register objIDs in global objectTracker table; >> } >> >> The error I get is: "no member named 'MongoClient' in 'emscripten::val' >> ". What am I doing wrong? Why don't I get it in case of mongoModule? >> >> What I'd like to achieve then, based on this earlier suggestion of this >> group ( >> https://groups.google.com/d/msg/emscripten-discuss/7-i5pRK7edQ/-MrCNVAuCAAJ >> ) is to call mongoClient.connect() like: >> >> void js_db::open(const std::string& db_uri){ >> this->db_uri=db_uri; >> EM_ASM_({ >> var db_uri = Module.Pointer_stringify($0); >> var mongoClient = objectTracker[$1]; >> //TODO: MongoClient.connect(db_uri, function(err, db) >> },db_uri.c_str(),mongoClientObjId); >> } >> >> Thanks for any help in advance! >> >> Best regards, >> r0ller >> >> -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.