Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructuring module imports with const throw a redeclaration error #411

Open
grob opened this issue Nov 3, 2020 · 1 comment
Open

Destructuring module imports with const throw a redeclaration error #411

grob opened this issue Nov 3, 2020 · 1 comment

Comments

@grob
Copy link
Member

grob commented Nov 3, 2020

Some modules in RingoJS (binary, io, ringo/events) use defineClass() which modifies the global scope. In itself that's imho quite intransparent and thus not a good idea, but alas the wrong implementation of const in Rhino leads to another problem: requiring one of these modules in conjunction with const and desctructuring assignments fails with eg. TypeError: redeclaration of var ByteArray.

Module test-a.js:

require("binary");
require("test-b");

Module test-b.js:

const {ByteArray} = require("binary");

Afais Rhino walks up the prototype chain (the global scope is the prototype of all module scopes) and tries to re-declare the const ByteArray in there, which correctly fails - but i couldn't figure out why the const isn't declared in the module scope.

Workaround: use const binary = require("binary"); in modules. Adds verbosity, but works.

@grob grob added the 3.0 label Nov 3, 2020
@botic botic added this to the 3.0.0 milestone Dec 11, 2020
@botic
Copy link
Member

botic commented Mar 19, 2021

Looks like the origins of the bug are somewhere here in combination with Rhino's ScriptableObject

public ModuleScope(String moduleId, Trackable source,
                       Scriptable prototype, RingoWorker worker) {
        setParentScope(null);
        setPrototype(prototype);
        // ...

I found no way around this issue. So maybe we should add this to a known issues list, @grob / @oberhamsi ?

@botic botic removed this from the 3.0.0 milestone Mar 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants