Skip to content

Panic with certain uses of new Function(...) #4525

@hpp2334

Description

@hpp2334

Describe the bug

VM panics at runtime due to new Function(...) usage.

thread 'test_react_counter' (18511) panicked at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/vm/opcode/define/mod.rs:82:39:
must be declarative environment
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/f8297e351a40c1439a467bbbb6879088047f50b3/library/std/src/panicking.rs:698:5
   1: core::panicking::panic_fmt
             at /rustc/f8297e351a40c1439a467bbbb6879088047f50b3/library/core/src/panicking.rs:75:14
   2: core::panicking::panic_display
             at /rustc/f8297e351a40c1439a467bbbb6879088047f50b3/library/core/src/panicking.rs:259:5
   3: core::option::expect_failed
             at /rustc/f8297e351a40c1439a467bbbb6879088047f50b3/library/core/src/option.rs:2178:5
   4: core::option::Option<T>::expect
             at /home/a/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:965:21
   5: boa_engine::environments::runtime::EnvironmentStack::put_lexical_value
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/environments/runtime/mod.rs:278:18
   6: boa_engine::vm::opcode::define::PutLexicalValue::operation
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/vm/opcode/define/mod.rs:82:39
   7: boa_engine::vm::opcode::handle_put_lexical_value
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/vm/opcode/mod.rs:313:17
   8: boa_engine::vm::opcode::<impl boa_engine::context::Context>::execute_bytecode_instruction
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/vm/opcode/mod.rs:464:9
   9: core::ops::function::FnOnce::call_once
             at /home/a/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  10: boa_engine::vm::<impl boa_engine::context::Context>::execute_instruction
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/vm/mod.rs:639:9
  11: boa_engine::vm::<impl boa_engine::context::Context>::execute_one
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/vm/mod.rs:660:18
  12: boa_engine::vm::<impl boa_engine::context::Context>::run
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/vm/mod.rs:848:24
  13: boa_engine::script::Script::evaluate
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/script.rs:172:30
  14: boa_engine::context::Context::eval
             at /home/a/.cargo/git/checkouts/boa-126d820dff07bf57/2c1eb63/core/engine/src/context/mod.rs:204:41
  15: basic::test_react_counter
             at ./tests/basic.rs:42:8
  16: basic::test_react_counter::{{closure}}
             at ./tests/basic.rs:20:24
  17: core::ops::function::FnOnce::call_once
             at /home/a/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  18: core::ops::function::FnOnce::call_once
             at /rustc/f8297e351a40c1439a467bbbb6879088047f50b3/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test test_react_counter ... FAILED

To Reproduce

#[test]
fn test_react_counter() {
    let mut cx = boa_engine::Context::default();
    let s = boa_engine::Source::from_bytes(br#"
    const compiledCode = `
        const noop = () => {}
        function Counter() {
            const [v, setV] = [0, noop]

            const onClick = () => {
                setV(v => v + 1)
            }

            return null
        }
        return Counter()
    `
    
    const React = {}
    const f = new Function('React', compiledCode)
    f(React)
"#);

    cx.eval(s.into()).expect("Failed to eval");
    assert!(true);
}

Expected behavior
got null

Build environment

  • OS: Ubuntu 24.04.3 LTS
  • Version: 64
  • Target triple: x86_64-unknown-linux-gnu
  • Rustc version: rustc 1.91.0 (f8297e351 2025-10-28)

Additional context

  • Boa version: v0.21 (2c1eb63)
  • It can also be reproduced in playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions