Created
November 4, 2018 19:20
-
-
Save wilforlan/760562fbc20e1fc7b124e18bdcb0f7f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global._pathfinder = require('./utils/path-finder'); | |
const Sandbox = require(_pathfinder.join(_pathfinder.services, 'code-sandbox/main')); | |
/** | |
* Example 1 | |
* Using a single function. | |
* | |
*/ | |
var x = (v) => { | |
return v; | |
}; | |
var y = () => { | |
return process.env; | |
} | |
var u = (new Sandbox()).run(x.toString()); | |
var t = (new Sandbox()).run(y.toString()); | |
u(5) // should return 5 | |
t(); // should throw error process undefined. | |
/** | |
* Example 2 | |
* Using a Class. | |
* This will pass {this} into the context for use within the function. | |
* | |
*/ | |
// Create a sample DB | |
var db = {}; | |
var Funt = function (name) { | |
this.name = name; | |
} | |
Funt.prototype.die = function () { | |
return this.name; | |
} | |
Funt.prototype.wake = function(fb) { | |
db['method'] = { func: fb.toString() }; | |
}; | |
Funt.prototype.show = function () { | |
this.wake(this.die) | |
} | |
Funt.prototype.run = function () { | |
this.show(); | |
var func = db['method'].func; | |
var u = (new Sandbox()).run(func, this); // should return Whale | |
var k = (new Sandbox()).run(func); // should return undefined | |
console.log(u()); | |
console.log(k()); | |
} | |
var f = new Funt('Whale'); | |
f.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment