samedi 1 décembre 2018

How to define property in object which passed to mergeInto LibraryManager.library ? Emscripten --js-library

I have myLibrary.js and hello.cpp files.

myLibrary.js

mergeInto(LibraryManager.library, {
 fd: -1,
 create_socket: function (a1, a2, a3){
   console.log(this.fd); // will print this.fd is undefined
 },
 close_socket: function(a1){
      console.log(a1);
 },
 send_buffer: function(a1, a2, a3, a4, a5, a6){
     console.log("sendBuffer", a1, Pointer_stringify(a2), a3, a4, a5, a6);
}});

hello.cpp

    #include <stdio.h>
    #include <emscripten/emscripten.h>
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <cstring>
    #include <sys/socket.h>
    #include <unistd.h>

    #ifdef __cplusplus
    extern "C" {
    #endif

    int socketHandle = -1;

    extern int create_socket(int a1, int a2, int a3);
    extern void close_socket(int a1);
    extern void send_buffer(int a1, const char* a2, int a3, int a4, std::string a5, int a6);



    int main() {
        create_socket(AF_INET, SOCK_DGRAM, 0);
        return 0;
    }

    void EMSCRIPTEN_KEEPALIVE socket_created(int fd) {
        socketHandle = fd;
        printf("%d\n", socketHandle);
        std::cout<<"asdasd";
    //    char buf[512];
    //    strcpy(buf,"Message to send");
        std::string buf("Message to send");
        std::string destAddres = "127.0.0.1:3000";
        send_buffer(socketHandle, buf.c_str(), (int)buf.length(), 0, destAddres, (int)destAddres.length());
    }

    void EMSCRIPTEN_KEEPALIVE buffer_sent(int fd) {
        close_socket(socketHandle);
    }

    #ifdef __cplusplus
    }
    #endif

When I build this in this way:

em++ webassembly/hello.cpp -s WASM=1 -o webassembly/hello.js --js-library webassembly/myLibrary.js

and when I trying to console.log this.fd in myLibrary.js its print undefined. How to properly define property in the passed object? I also tried to add extern fd to my hello.cpp file and no result.




Aucun commentaire:

Enregistrer un commentaire