Skip to content

Scope issues with eval() #8854

@ButzYung

Description

@ButzYung
  • Electron version: 1.6.2
  • Operating system: Windows 10 64-bit

Expected behavior

eval() should run in the current scope and be able to access global variables. The following script, which is executed on the main process (global scope), should produce no error (in fact, it does work properly in Electron v1.4.15).

eval(
  "const app = electron.app;\n"
+ "const BrowserWindow = electron.BrowserWindow;\n"
+ "app.on('ready', function() {\n"
+ "  mainWindow = new BrowserWindow({ ... });\n"
+ "});"
);

Actual behavior

Error occurs on the "new BrowserWindow" part saying BrowserWindow is not defined. The defined global variable BrowserWindow is not accessible inside the function.

As a workaround, I have to enclose the script inside an anonymous self-invoking function so that a local scope is created to avoid the problem.

eval (
  "(function () {\n"
......
+ "})();"
);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions