Siubaak / sval

A javascript interpreter written in javascript
https://jsbin.com/kehahiqono/edit?js,console
MIT License
379 stars 50 forks source link

Using atob returns TypeError: Illegal invocation #65

Closed imolorhe closed 4 years ago

imolorhe commented 4 years ago

When running code within the sval interpreter that uses window.atob or window.btoa (those are the ones I've experienced the issue with so far), I get a TypeError: Illegal invocation error.

Siubaak commented 4 years ago

Just fixed. Keep it open until next release.

imolorhe commented 4 years ago

@Siubaak thanks for the speedy implementation. When will the next release be?

Siubaak commented 4 years ago

@imolorhe 0.4.8 released. try it

imolorhe commented 4 years ago

Thanks, it works!

dprentis commented 2 years ago

@Siubaak using 0.4.8, calling some window methods (including atob and btoa) directly (not through window.) throws the Illegal invocation error for me. Not sure if I could be doing something wrong - I'm using your library in a chrome extension content script.

Anyway, the following workaround works for me:

const bindings = [
  'fetch',
  'setInterval',
  'clearInterval',
  'setTimeout',
  'clearTimeout',
  'atob',
  'btoa'
]
  .map((method) => `window.${method} = window.${method}.bind(null);\n`).join('');

const createSval = (options?: SvalOptions) => {
  const interpreter = new Sval(options);
  interpreter.run(bindings);
  return interpreter;
};

Setting the this arg to null is counterintuitive, but it seems to fix the issue. Also, not all methods are affected by the issue eg. localStorage appears to work correctly without the fix.

Siubaak commented 2 years ago

@Siubaak using 0.4.8, calling some window methods (including atob and btoa) directly (not through window.) throws the Illegal invocation error for me. Not sure if I could be doing something wrong - I'm using your library in a chrome extension content script.

Anyway, the following workaround works for me:

const bindings = [
  'fetch',
  'setInterval',
  'clearInterval',
  'setTimeout',
  'clearTimeout',
  'atob',
  'btoa'
]
  .map((method) => `window.${method} = window.${method}.bind(null);\n`).join('');

const createSval = (options?: SvalOptions) => {
  const interpreter = new Sval(options);
  interpreter.run(bindings);
  return interpreter;
};

Setting the this arg to null is counterintuitive, but it seems to fix the issue. Also, not all methods are affected by the issue eg. localStorage appears to work correctly without the fix.

It seems to be a bug. Please create an issue, thanks. Or, pr is welcomed, anytime. :D