Skip to content

runtime: throw TypeError for new Symbol and new BigInt #2883

Description

@andrewtdiz

Summary

Symbol and BigInt are callable factory/coercion functions in Node, but neither is constructable. Perry implements call lowering for both, yet new Symbol(...) and new BigInt(...) appear to fall through the generic built-in new placeholder path instead of throwing.

Node behavior

Observed with local Node v25.9.0:

console.log(typeof Symbol("x")); // "symbol"
console.log(typeof BigInt("42"), BigInt("42").toString()); // "bigint", "42"

try { new Symbol("x"); } catch (e) { console.log(e.name, e.message); }
// TypeError Symbol is not a constructor

try { new BigInt("1"); } catch (e) { console.log(e.name, e.message); }
// TypeError BigInt is not a constructor

Perry evidence

  • crates/perry-hir/src/lower/expr_call/globals.rs lowers Symbol() / Symbol(description) to Expr::SymbolNew(...) and BigInt(value) to Expr::BigIntCoerce(...), so ordinary calls are covered.
  • crates/perry-runtime/src/symbol.rs implements js_symbol_new_empty, js_symbol_new, js_symbol_for, and related runtime support. crates/perry-runtime/src/bigint.rs implements the BigInt allocation/coercion runtime.
  • crates/perry-runtime/src/object/global_this.rs::GLOBAL_THIS_BUILTIN_CONSTRUCTORS and crates/perry-codegen/src/expr/helpers.rs::is_global_this_builtin_name() include Symbol and BigInt as function-valued globals.
  • crates/perry-hir/src/lower/expr_new.rs has no special class_name == "Symbol" or class_name == "BigInt" rejection path.
  • crates/perry-codegen/src/lower_call/new.rs::lower_new() calls lower_builtin_new() first; there is no Symbol / BigInt arm there. For built-in/native class names without a dedicated handler and no user class, it lowers args for side effects and allocates an empty object placeholder via js_object_alloc(0, 0). That means new Symbol("x") / new BigInt("1") can produce an ordinary object instead of throwing.

Expected fix direction

Add a new-expression rejection path for these non-constructable built-ins so both direct and obvious rebound constructor forms throw a TypeError rather than allocating placeholders. Ordinary Symbol(...), Symbol.for(...), Symbol.keyFor(...), and BigInt(...) call behavior should remain unchanged.

Related but not duplicate: #2852 tracks missing well-known Symbol.* constants, not new Symbol construction semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions