Search code examples
javascriptcgoogle-nativeclient

NativeClient : How to use Messaging_HandleMessage in C


I've got a problem with my C code and the lack of informations about nacl in C is really painful... So I use the earth example to get my .png loaded from my javascript and send it back to my C module but when I get the message I don't know how to convert it into PPB_VarDictionary, anyone got an idea ?

Here my C method :

static void Messaging_HandleMessage(PP_Instance instance, struct PP_Var message)
{
    fprintf(stdout, "%i\n", message.type);
    if(message.type == PP_VARTYPE_DICTIONARY)
    {
        PPB_VarDictionary *dictionary = NULL;
        dictionary->Create();
    } 
}

Thanks in advance for your reply.


Solution

  • Unlike C++, you can just pass your PP_Var to the PPB_VarDictionary interface directly. If it is the correct type, it will work. If not, it will give you an error.

    You can take a look at the nacl_io_demo example (examples/demo/nacl_io in the NaCl SDK) to see how this works. In your case, you can do something like this:

    PPB_Var* g_ppb_var = NULL;
    PPB_VarDictionary* g_ppb_var_dictionary = NULL;
    
    PP_EXPORT int32_t PPP_InitializeModule(PP_Module a_module_id,
                                           PPB_GetInterface get_browser) {
      g_ppb_var = (PPB_Var*)(get_browser(PPB_VAR_INTERFACE));
      g_ppb_var_dictionary = 
          (PPB_VarDictionary*)(get_browser(PPB_VAR_DICTIONARY_INTERFACE));
      return PP_OK;
    }
    
    static void Messaging_HandleMessage(PP_Instance instance, struct PP_Var message)
    {
        fprintf(stdout, "%i\n", message.type);
        if(message.type == PP_VARTYPE_DICTIONARY)
        {
            // Get the value that maps to key "foo".
            PP_Var key_var = g_ppb_var->VarFromUtf8("foo", 3);
            PP_Var value_var = g_ppb_var_dictionary->Get(message, key_var);
            g_ppb_var->Release(key_var);
    
            // Do something with value_var...
        } 
    }