{"size": 959, "ext": "js", "max_stars_repo_path": "superset/assets/javascripts/modules/sandbox.js", "max_stars_repo_name": "neilchencn/incubator-superset", "max_stars_repo_stars_event_min_datetime": "2018-04-10T00:40:57.000Z", "max_stars_repo_stars_event_max_datetime": "2018-04-10T00:40:57.000Z", "max_issues_repo_path": "superset/assets/javascripts/modules/sandbox.js", "max_issues_repo_name": "neilchencn/incubator-superset", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "superset/assets/javascripts/modules/sandbox.js", "max_forks_repo_name": "neilchencn/incubator-superset", "max_forks_repo_forks_event_min_datetime": "2022-01-21T00:19:53.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-23T02:17:39.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 34.25, "max_line_length": 88, "alphanum_fraction": 0.7184567258}
// A safe alternative to JS's eval
import vm from 'vm';
import _ from 'underscore';
import * as colors from './colors';

// Objects exposed here should be treated like a public API
// if `underscore` had backwards incompatible changes in a future release, we'd
// have to be careful about bumping the library as those changes could break user charts
const GLOBAL_CONTEXT = {
  console,
  _,
  colors,
};

// Copied/modified from https://github.com/hacksparrow/safe-eval/blob/master/index.js
export default function sandboxedEval(code, context, opts) {
  const sandbox = {};
  const resultKey = 'SAFE_EVAL_' + Math.floor(Math.random() * 1000000);
  sandbox[resultKey] = {};
  const codeToEval = resultKey + '=' + code;
  const sandboxContext = { ...GLOBAL_CONTEXT, ...context };
  Object.keys(sandboxContext).forEach(function (key) {
    sandbox[key] = sandboxContext[key];
  });
  vm.runInNewContext(codeToEval, sandbox, opts);
  return sandbox[resultKey];
}
