Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to isolate a function from global variables

Tags:

javascript

I'm building a little coding game. For this game, each player submits a javascript function. The game runs each of these functions a number of times in succession, and I collect the values the functions return. These returned values are what's important for the sake of the game. So if playerFunc is a player-submitted function, my game might do something like this:

 var values = []
 for(var i = 0; i < 1000; i++){
   values.push(playerFunc(i))
 }
 doSomething(values)

The problem is, I want to prevent players from passing data from one invocation to the next. For example, I wouldn't want there to be any way for an invocation of playerFunc to figure out if it had already been called with an argument of 0. To do this, I figure I need to prevent the player-submitted functions from accessing closures and global variables.

I know I can get rid of the closures by creating each function with the Function constructor, so I think I've got that figured out. Blocking access to global variables is what I'm having trouble with.

I can totally isolate each function call by running it in a web worker, but I've read that web workers take around 40ms to setup, and I may need to be running these functions up to 1000 times a second, so that's to slow.

Is there any other way I could prevent a function from accessing global variables, or of somehow resetting the global scope after each function call?

This game will only be played with friends for now, so I'm not worried about the players doing anything malicious, but I do think they might try to cheat.

like image 974
Michael Avatar asked Aug 29 '15 02:08

Michael


People also ask

Can you use global variables in a function?

Global variables can be used by everyone, both inside of functions and outside.

How do you get around a global variable?

The simplest way to avoid globals all together is to simply pass your variables using function arguments. As you can see, the $productData array from the controller (via HTTP request) goes through different layer: The controller receives the HTTP request. The parameters are passed to the model.

Can C++ function access global variables?

Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the lifetime of your program. A global variable can be accessed by any function.

How can you access a global variable inside the function if function has a variable with same name?

To access a global variable in a function, if the function has a local variable with the same name, we use the global keyword before the variable name.


1 Answers

You could try something like this:

var createFunction = function(fnBody) {
  var f = new Function('obj', 'window', 'document', '"use strict";' + fnBody);
  return f.bind({}, {}, {}, {});
}

Any access to window or document will use the parameters instead of the global variables. You are welcome to add more global variables to restrict access. With "use strict";, you'll stop the supplied function from accessing the global scope with undefined variables.

var createFunction = function(fnBody) {
  var f = new Function('obj', 'window', 'document', '"use strict";' + fnBody);
  return f.bind({}, {}, {}, {});
}
   
createFunction('window.hello = "test"')();
console.log(window.test); //undefined
createFunction('hello = "test";')(); //throws error
console.log(hello);

This throws an error.

like image 120
MinusFour Avatar answered Sep 28 '22 08:09

MinusFour