Hi All,
Just as a feedback: I managed to get things done or at least have something
that works when it comes to calling js from c++ but without using val and
currently without using node modules. The c++ constructor gets the js
functionpointers like this (actually this is just a test now but I'm
planning to put all js functions I need there):
js_db::js_db(){
id = EM_ASM_INT({// id is a private int member variable
jsdb = Runtime.addFunction(function() {
console.log('I was called from C world!');
gtext = 'This is global.';//testing if global variables can be stored this
way
});
return jsdb;
},0);
}
void js_db::open(){
void (*f)(void) = reinterpret_cast<void (*)(void)>(id); //this pointer
should be stored in the constructor as private member
f();//test call
EM_ASM({
console.log(gtext);//testing if global gtext set in the constructor is
still available
});
}
So the call works for now, even the global variable stored on js side is
preserved:) The question is if it's possible to get back something from the
js functions as well and if yes, then how complex types can be returned?
Any hints are welcome! By the way, all this stuff is done on my NetBSD
build of emscripten so it's kind of a good test for that as well;)
Best regards,
r0ller
2016. október 17., hétfő 15:20:17 UTC+2 időpontban [email protected] a
következőt írta:
>
> Hi JJ,
>
> Well, I tried to create a wrapper object just as an experiment like this:
>
> var jsdb = {
> id : 123,
> open : function() {
> console.log("opening db");
> }
> };
>
> and access it via emscripten::val like:
>
> jsdb = emscripten::val::global("jsdb");
>
> where jsdb is a private member variable of the c++ class. In that case I
> get a compile time error saying "call to deleted constructor of
> 'emscripten::val' ". What am I doing wrong here?
>
> If I just work it around by using a local variable like:
>
> emscripten::val jsdb = emscripten::val::global("jsdb");
>
> it works, but then any of the two lines fail during runtime:
>
> jsdb.call<void>("open");
>
> which fails with "cannot read property 'open' of undefined"
>
> or if I try:
>
> emscripten::val jsdbobj = jsdb.new_();
>
> then it fails with "constructor is not a function".
>
> Any hint is welcome:)
>
> 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 <[email protected]>:
>>
>>> 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 [email protected].
For more options, visit https://groups.google.com/d/optout.