1 |
3a515b92
|
cagy
|
# vm-browserify
|
2 |
|
|
|
3 |
|
|
emulate node's vm module for the browser
|
4 |
|
|
|
5 |
|
|
[![Build Status](https://travis-ci.org/browserify/vm-browserify.svg?branch=master)](https://travis-ci.org/browserify/vm-browserify)
|
6 |
|
|
|
7 |
|
|
# example
|
8 |
|
|
|
9 |
|
|
Just write some client-side javascript:
|
10 |
|
|
|
11 |
|
|
``` js
|
12 |
|
|
var vm = require('vm');
|
13 |
|
|
|
14 |
|
|
window.addEventListener('load', function () {
|
15 |
|
|
var res = vm.runInNewContext('a + 5', { a : 100 });
|
16 |
|
|
document.querySelector('#res').textContent = res;
|
17 |
|
|
});
|
18 |
|
|
```
|
19 |
|
|
|
20 |
|
|
compile it with [browserify](http://github.com/substack/node-browserify):
|
21 |
|
|
|
22 |
|
|
```
|
23 |
|
|
browserify entry.js -o bundle.js
|
24 |
|
|
```
|
25 |
|
|
|
26 |
|
|
then whip up some html:
|
27 |
|
|
|
28 |
|
|
``` html
|
29 |
|
|
<html>
|
30 |
|
|
<head>
|
31 |
|
|
<script src="/bundle.js"></script>
|
32 |
|
|
</head>
|
33 |
|
|
<body>
|
34 |
|
|
result = <span id="res"></span>
|
35 |
|
|
</body>
|
36 |
|
|
</html>
|
37 |
|
|
```
|
38 |
|
|
|
39 |
|
|
and when you load the page you should see:
|
40 |
|
|
|
41 |
|
|
```
|
42 |
|
|
result = 105
|
43 |
|
|
```
|
44 |
|
|
|
45 |
|
|
# methods
|
46 |
|
|
|
47 |
|
|
## vm.runInNewContext(code, context={})
|
48 |
|
|
|
49 |
|
|
Evaluate some `code` in a new iframe with a `context`.
|
50 |
|
|
|
51 |
|
|
Contexts are like wrapping your code in a `with()` except slightly less terrible
|
52 |
|
|
because the code is sandboxed into a new iframe.
|
53 |
|
|
|
54 |
|
|
# install
|
55 |
|
|
|
56 |
|
|
This module is depended upon by browserify, so you should just be able to
|
57 |
|
|
`require('vm')` and it will just work. However if you want to use this module
|
58 |
|
|
directly you can install it with [npm](http://npmjs.org):
|
59 |
|
|
|
60 |
|
|
```
|
61 |
|
|
npm install vm-browserify
|
62 |
|
|
```
|
63 |
|
|
|
64 |
|
|
# license
|
65 |
|
|
|
66 |
|
|
MIT
|