@@ -125,6 +125,32 @@ behavior you'd like.
125125
126126More documentation can be found on the [ GitHub wiki] ( https://github.com/cowboyd/therubyracer/wiki )
127127
128+ ### Protecting Your CPU cycles
129+
130+ When running untrusted JavaScript code, you not only have to protect
131+ which functions it has access to, but also how much of your CPU it can
132+ consume. Take this simple, yet thoroughly malicious script:
133+
134+ ``` javascript
135+ while (true ) {}
136+ ```
137+
138+ It will loop forever and never return control to the calling Ruby
139+ thread. To protect against such JavaScript code that either
140+ deliberately or accidentally runs longer that it should, you can
141+ set an explicit timeout on your context. If the code runs longer that
142+ the allowed timeout, then it will throw an exception. Note that this
143+ exception could be raised at any point in the execution of the
144+ JavaScript.
145+
146+ To specify the timeout (in milliseconds), pass in the ` timeout ` option
147+ to the constructor.
148+
149+ ``` ruby
150+ cxt = V8 ::Context .new timeout: 700
151+ cxt.eval " while (true);" # = exception after 700ms!
152+ ```
153+
128154### PREREQUISITES
129155
130156The Ruby Racer requires the V8 Javascript engine, but it offloads the
@@ -173,4 +199,4 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
173199SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
174200
175201[ 1 ] : https://github.com/cowboyd/libv8
176- [ 2 ] : http://code.google.com/p/v8/wiki/BuildingWithGYP
202+ [ 2 ] : http://code.google.com/p/v8/wiki/BuildingWithGYP
0 commit comments