"use strict" ;
var check = ( function ( glob) {
return function ( code) {
var contextProps = { } ;
for ( var key in glob) {
console.log ( "guarding property: " + key) ;
( function ( key) {
contextProps[ key] = { get : function ( ) { throw new Error( "Trying to access global variable: " + key) ; } } ;
} ) ( key) ;
}
var context = { } ;
Object .defineProperties ( context, contextProps) ;
var func = new Function ( 'context' , 'with(context) {' + code + '};' ) ;
func( context) ;
} ;
} ) ( global) ;
console.log ( "test" ) ;
check( "setTimeout(function() { console.log('error'); }, 0);" ) ;
InVzZSBzdHJpY3QiOwoKdmFyIGNoZWNrID0gKGZ1bmN0aW9uKGdsb2IpIHsKICByZXR1cm4gZnVuY3Rpb24oY29kZSkgewogICAgdmFyIGNvbnRleHRQcm9wcyA9IHt9OwogICAgZm9yICh2YXIga2V5IGluIGdsb2IpIHsKICAgICAgY29uc29sZS5sb2coImd1YXJkaW5nIHByb3BlcnR5OiAiICsga2V5KTsKICAgICAgKGZ1bmN0aW9uKGtleSkgewogICAgICAgIGNvbnRleHRQcm9wc1trZXldID0geyBnZXQ6IGZ1bmN0aW9uKCkgeyB0aHJvdyBuZXcgRXJyb3IoIlRyeWluZyB0byBhY2Nlc3MgZ2xvYmFsIHZhcmlhYmxlOiAiICsga2V5KTsgfSB9OwogICAgICB9KShrZXkpOwogICAgfQogICAgdmFyIGNvbnRleHQgPSB7fTsKICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0aWVzKGNvbnRleHQsIGNvbnRleHRQcm9wcyk7CiAgICB2YXIgZnVuYyA9IG5ldyBGdW5jdGlvbignY29udGV4dCcsICd3aXRoKGNvbnRleHQpIHsnICsgY29kZSArICd9OycpOwogICAgZnVuYyhjb250ZXh0KTsKICB9Owp9KShnbG9iYWwpOwoKY29uc29sZS5sb2coInRlc3QiKTsKY2hlY2soInNldFRpbWVvdXQoZnVuY3Rpb24oKSB7IGNvbnNvbGUubG9nKCdlcnJvcicpOyB9LCAwKTsiKTs=
stdout
test
guarding property: ArrayBuffer
guarding property: Int8Array
guarding property: Uint8Array
guarding property: Uint8ClampedArray
guarding property: Int16Array
guarding property: Uint16Array
guarding property: Int32Array
guarding property: Uint32Array
guarding property: Float32Array
guarding property: Float64Array
guarding property: DataView
guarding property: global
guarding property: process
guarding property: GLOBAL
guarding property: root
guarding property: Buffer
guarding property: setTimeout
guarding property: setInterval
guarding property: clearTimeout
guarding property: clearInterval
guarding property: console
stderr
Error: Trying to access global variable: setTimeout
at Object.contextProps.(anonymous function).get (/home/0Ngcnl/prog.js:9:55)
at eval (eval at <anonymous> (/home/0Ngcnl/prog.js:14:16))
at /home/0Ngcnl/prog.js:15:5
at Object.<anonymous> (/home/0Ngcnl/prog.js:20:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)