JSFiddle - React, Tailwind, and code Playground

HTML

<pre></pre>

JavaScript

function make_fn(expr) {
  const whitelist = ['Math', 'Array'];
  let scope = {};
  for (let obj = this; obj; obj = Object.getPrototypeOf(obj)) {
    Object.getOwnPropertyNames(obj).forEach(name => scope[name] = undefined);
  }
  whitelist.forEach(name => scope[name] = this[name]);
  return (x,y) => Function("scope", "x", "y", "with (scope) return " + expr).bind({})(scope,x,y);
}
        

let s = ``;
for (let expr of ['x+y', 'Math.max(x,y)', 'this', 'window', 'trace((function(){return this}()))']) {
  const fn = make_fn(expr);
  s += `\neval "${expr}" with x=3 y=5 --> `;
  try { r = fn(3,5); s += `${typeof r == 'object' ? `[object {${Object.keys(r)}}]` : r}\n`; }
  catch (e) { s += e; }
}
document.getElementsByTagName('pre')[0].innerText = s;