Index: native_client_sdk/src/doc/devguide/coding/message-system.rst |
diff --git a/native_client_sdk/src/doc/devguide/coding/message-system.rst b/native_client_sdk/src/doc/devguide/coding/message-system.rst |
index f77b9bd4d704864b2bc5ecbf45f2a7dff5dacecd..8a96c8f3a047b817cfb30171cb0746ada129ce32 100644 |
--- a/native_client_sdk/src/doc/devguide/coding/message-system.rst |
+++ b/native_client_sdk/src/doc/devguide/coding/message-system.rst |
@@ -358,3 +358,45 @@ end of the Native Client module's ``HandleMessage()`` function: |
.. naclcode:: |
PostMessage(var_reply); |
+ |
+ |
+Sending and receiving other ``pp::Var`` types |
+--------------------------------------------- |
+ |
+Besides strings, ``pp::Var`` can represent other types of JavaScript |
+objects. For example, messages can be JavaScript dictionaries. |
binji
2013/09/24 21:56:26
JavaScript doesn't really have dictionaries, just
jvoung (off chromium)
2013/09/24 22:35:53
Done.
|
+These richer types can make it easier to implement an application's |
+messaging protocol. |
+ |
+To send a dictionary from the NaCl module to JavaScript simply |
+create a ``pp::VarDictionary`` and then call ``PostMessage`` with |
+the dictionary. |
+ |
+.. naclcode:: |
+ |
+ pp::VarDictionary dictionary; |
+ dictionary.Set(pp::Var("command"), pp::Var(next_command)); |
+ dictionary.Set(pp::Var("param_int"), pp::Var(123)); |
+ pp::VarArray an_array; |
+ an_array.Set(0, pp::Var("string0")); |
+ an_array.Set(1, pp::Var("string1")) |
+ dictionary.Set(pp::Var("param_array"), an_array); |
+ PostMessage(dictionary); |
binji
2013/09/24 21:56:26
Is it worth mentioning what this will look like in
jvoung (off chromium)
2013/09/24 22:35:53
Seems worth doing -- Done.
|
+ |
+ |
+To receive a dictionary-typed message in the NaCl module, test that |
+the message is truly a dictionary type, then convert the message |
+with the ``pp::VarDictionary`` class. |
+ |
+.. naclcode:: |
+ |
+ virtual void HandleMessage(const pp::Var& var) { |
+ if (var.is_dictionary()) { |
+ pp::VarDictionary dictionary(var); |
+ // Use the dictionary |
+ pp::VarArray keys = dictionary.GetKeys(); |
+ // ... |
+ } else { |
+ // ... |
+ } |
+ } |