Messages
In this tutorial we show how to send messages to and from a target process.
Setting up the experiment
Create a file hello.c
:
Compile with:
Start the program and make note of the address of f()
(0x400544
in the
following example):
Sending messages from a target process
The following script shows how to send a message back to the Python process. You can send any JavaScript value which is serializable to JSON.
Create a file send.py
containing:
When you run this script:
it should print the following message:
This means that the JavaScript code send(1337)
has been executed inside the
hello
process. Terminate the script with Ctrl-D
.
Handling runtime errors from JavaScript
If the JavaScript script throws an uncaught exception, this will be propagated
from the target process into the Python script. If you replace send(1337)
with send(a)
(an undefined variable), the following message will be received
by Python:
Note the type
field (error
vs send
).
Receiving messages in a target process
It is possible to send messages from the Python script to the JavaScript
script. Create the file pingpong.py
:
Running the script:
produces the output:
The mechanics of recv()
The recv() method itself is async (non-blocking). The registered callback (onMessage) will receive exactly one message. To receive the next message, the callback must be reregistered with recv().
Blocking receives in the target process
It is possible to wait for a message to arrive (a blocking receive) inside your
JavaScript script. Create a script rpc.py
:
The program hello
should be running, and you should make a note of the address
printed at its beginning (e.g. 0x400544
). Run:
then observe the change in the terminal where hello
is running:
The hello
program should start writing “doubled” values until you stop the
Python script (Ctrl-D
).