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

cannot access 'i' before initialization #2760

Closed
lastmjs opened this issue Mar 29, 2023 · 8 comments
Closed

cannot access 'i' before initialization #2760

lastmjs opened this issue Mar 29, 2023 · 8 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@lastmjs
Copy link
Contributor

lastmjs commented Mar 29, 2023

Describe the bug

I am trying to use an npm package called elliptic. This is a popular library, and with that knowledge it is a safe assumption that it works well in the common JS engines. I have tested it locally in Node.js and it works fine.

Unfortunately when trying to import the most basic functionality in Boa, this error is thrown:

cannot access 'i' before initialization

I have looked through the source code, and the variable referenced in the error is always declared with var and never const or let, so I don't think this error should be thrown.

To Reproduce

Just throw this code into a Boa script and execute it: https://github.com/indutny/elliptic/blob/master/dist/elliptic.min.js

The error will be thrown.

Expected behavior

This error should not be thrown, and the code should work just fine.

Build environment (please complete the following information):

I don't believe this information is relevant.

  • OS: Ubuntu
  • Version: 20.04
  • Target triple: wasm32-unknown-unknown and probably all triples
  • Rustc version: rustc 1.66.1 (90743e729 2023-01-10)

Additional context

I believe this issue has a high likelihood of being related: #1393

@lastmjs lastmjs added the bug Something isn't working label Mar 29, 2023
@jedel1043
Copy link
Member

Huh, I tried to reproduce it in the main branch but it successfully runs. Are you maybe running it in the 0.16v?

@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 29, 2023

I've reproduced it with multiple versions, let me double check.

@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 29, 2023

I can confirm that the issue presents on the latest commit hash on main.

Here's the commit hash: cf85843

Here's the Cargo.toml that I'm using:

[package]
name = "boa_uint8array"
version = "0.0.0"
edition = "2018"

[dependencies]
boa_engine = { git = "https://github.com/boa-dev/boa", rev = "cf85843dc838c7c36e6b635b34f31d100e64c63b" }

@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 29, 2023

Did you ensure to paste this exact minified code in? https://github.com/indutny/elliptic/blob/master/dist/elliptic.min.js

@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 29, 2023

Here's the Rust without that minified code pasted in without modification:

fn main() {
    let mut context = boa_engine::Context::default();

    let exports_js_value = context
        .eval_script(boa_engine::Source::from_bytes(r#""#))
        .unwrap();

    println!("{:#?}", exports_js_value);
}

@jedel1043
Copy link
Member

Finally could reproduce the issue, I was accidentally running the unminified script instead of the minified one 😅

The upside is that elliptic.js does work without problems, it's just the minified version the one that is giving the engine some trouble. I'll put this in the pre-v1 tracker, and I'll probably debug it next week.

@jedel1043 jedel1043 self-assigned this Mar 29, 2023
@jedel1043 jedel1043 moved this to To do in Boa pre-v1 Mar 29, 2023
@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 29, 2023

I think the issue still presents when you try to use the library using the non-minified version, unfortunately.

@jedel1043
Copy link
Member

jedel1043 commented Mar 29, 2023

Yeah, and it seems to be the same issue. I tried to create an encryption context using the EC constructor with:

let script = include_bytes!("test.js");

    let context = &mut Context::default();

    match context.eval_script(Source::from_bytes(script)) {
        Ok(v) => println!("{}", v.display()),
        Err(e) => eprintln!("{}", e),
    }

    let elliptic = context
        .global_object()
        .clone()
        .get("elliptic", context)
        .unwrap()
        .as_object()
        .unwrap()
        .clone();
    let constructor = elliptic
        .get("ec", context)
        .unwrap()
        .as_object()
        .unwrap()
        .clone();

    match constructor.construct(&["curve25519".into()], None, context) {
        Ok(v) => println!(
            "{}",
            JsValue::new(v)
                .to_string(context)
                .unwrap()
                .to_std_string_escaped()
        ),
        Err(e) => eprintln!("{}", e),
    }

and it returned:

ReferenceError: cannot access 'r' before initialization

Seems to be the same problem of var binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants