Skip to content

Commit ebfcbad

Browse files
committed
add options object
1 parent 7bcec16 commit ebfcbad

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# safe-eval
1+
# safe-eval [![npm version](https://badge.fury.io/js/safe-eval.svg)](https://badge.fury.io/js/safe-eval)
22

33
## What is this?
44

@@ -18,45 +18,47 @@ npm install safe-eval --save
1818
var safeEval = require('safe-eval')
1919
```
2020

21-
**safeEval(code, [context])**
21+
**safeEval(code, [context], options)**
2222

2323
`code` is the JavaScript code you want to execute.
2424

2525
`context` is an object of methods and properties, these methods and properties are interpreted as global objects in `code`. Be careful about the objects you are passing to the context API, because they can completely defeat the purpose of `safe-eval`.
2626

27+
`options` is the [options object](https://nodejs.org/api/vm.html) for the vm executing the code.
28+
2729
### Examples
2830

29-
```
31+
```js
3032
// string concatenation
3133
var code = '"app" + "le"'
3234
var evaluated = safeEval(code) // "apple"
3335
```
3436

35-
```
37+
```js
3638
// math
3739
var code = 'Math.floor(22/7)'
3840
var evaluated = safeEval(code) // 3.142857142857143
3941
```
4042

41-
```
43+
```js
4244
// JSON
4345
var code = '{name: "Borat", hobbies: ["disco dance", "sunbathing"]}'
4446
var evaluated = safeEval(code) // {name: "Borat", hobbies: ["disco dance", "sunbathing"]}
4547
```
4648

47-
```
49+
```js
4850
// function expression
4951
var code = '(function square(b) { return b * b; })(5)'
5052
var evaluated = safeEval(code) // 25
5153
```
5254

53-
```
55+
```js
5456
// no access to Node.js objects
5557
var code = 'process'
5658
safeEval(code) // THROWS!
5759
```
5860

59-
```
61+
```js
6062
// your own context API - access to Node's process object and a custom function
6163
var code = '{pid: process.pid, apple: a()}'
6264
var context = {
@@ -66,9 +68,15 @@ var context = {
6668
var evaluated = safeEval(code, context) // { pid: 16987, apple: 'APPLE' }
6769
```
6870

71+
```js
72+
// pass an options object to the vm
73+
var code = 'process'
74+
safeEval(code, {}, { filename: 'myfile.js'}) // myfile.js can be seen in the stacktrace
75+
```
76+
6977
## License (MIT)
7078

71-
Copyright (c) 2015 Hage Yaapa
79+
Copyright (c) 2016 Hage Yaapa
7280

7381
Permission is hereby granted, free of charge, to any person obtaining a copy
7482
of this software and associated documentation files (the "Software"), to deal

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "safe-eval",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Safer version of eval()",
55
"main": "index.js",
66
"scripts": {

test/test.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var assert = require('assert')
44
var safeEval = require(__dirname + '/..')
55

66
describe('safe-eval', function () {
7-
87
it('should perform string concatenation', function () {
98
var code = '"app" + "le"'
109
var evaluated = safeEval(code)
@@ -55,4 +54,12 @@ describe('safe-eval', function () {
5554
assert(evaluated.apple === 'APPLE')
5655
})
5756

57+
it('should include vm options', function () {
58+
var code = 'foo'
59+
try {
60+
safeEval(code, {}, { filename: 'bar.js', timeout: 1 })
61+
} catch (e) {
62+
assert(e.stack.indexOf('bar.js') > 0)
63+
}
64+
})
5865
})

0 commit comments

Comments
 (0)