Skip to content

[RFC]: set constructor names only where the runtime allows it #16859

Backlog

Description

@batpigandme

Description

This RFC proposes an @stdlib/assert/has-*-support check for whether a function's name can be redefined, and setting each constructor's name read-only only where that check passes, so that stdlib's constructors load on ES5 engines whose function name is fixed.

Currently, stdlib pins each custom constructor's name with setReadOnly( Ctor, 'name', value ). On ES5 engines whose function name is neither writable nor configurable, that call throws whenever value differs from the declared name, so the package fails at load.

ES5 doesn't define a name property for functions, so an engine that provides one chooses its attributes, and ES2015 made it configurable (19.2.4.2). ES5's [[DefineOwnProperty]] accepts a redefinition that changes nothing (§8.12.9, step 6) and rejects a new value only when the property is both non-writable and non-configurable (step 10.a.ii). With a declared function and the descriptor setReadOnly passes:

  • lv5 and otto: a different name throws, in strict and sloppy code. On lv5, an esbuild bundle that includes @stdlib/array/float16 throws changing [[Value]] of readonly property not allowed at setReadOnly( Float16Array2, "name", "Float16Array" ).
  • Node.js 0.10.48 (V8 3.14): a different name throws only for sloppy functions. A strict function's name is writable, and stdlib's files are all 'use strict', so stdlib's oldest supported Node.js (package.json) isn't affected.
  • Duktape 2.7.0, JerryScript 3.0.0, current Rhino, MicroQuickJS, and ES2015 engines accept it. The same name is accepted everywhere.

A different name has three sources. Six calls set one in the source itself (marked in the list below). Bundlers rename on a collision: esbuild turned function Float16Array into Float16Array2, and its --keep-names is unavailable for targets that don't support ES6 (docs). And uglify-js at its defaults, as bundlify calls it (L179), turned function Float16Array into function o in a small test module.

Proposed change

Two parts, each following a pattern stdlib already uses.

  1. A support check, as an @stdlib/assert/has-*-support package. Like has-define-property-support, it tries the operation inside a try and returns a boolean, with the built-in captured in a private define_property.js so that tests can replace it. The function is declared inside the check, because a successful define fixes its name:

    var defineProperty = require( './define_property.js' );
    
    function hasSupport() {
        function foo() {} // eslint-disable-line no-empty-function
        try {
            defineProperty( foo, 'name', {
                'configurable': false,
                'enumerable': false,
                'writable': false,
                'value': 'bar'
            });
        } catch ( err ) { // eslint-disable-line no-unused-vars
            return false;
        }
        return ( foo.name === 'bar' );
    }

    It tries the define rather than reading configurable, as has-define-property-support does. On Node.js 0.10, a strict function's name is non-configurable but writable, so the redefinition succeeds and a flag test would skip calls that work today. On an engine without Object.getOwnPropertyDescriptor, such as MicroQuickJS, the flag can't be read at all. The existing has-function-name-support tests whether functions have a name, not whether it can be redefined, so it doesn't answer this.

  2. Callers decide once, at load, and define only where the check passes. As in dstructs/named-typed-tuple, the result is kept as a module constant (L58) and the property is defined only if it's true, with nothing recorded when it isn't:

    var HAS_SUPPORT = hasSupport();
    
    if ( HAS_SUPPORT ) {
        setReadOnly( Float16Array, 'name', 'Float16Array' );
    }

    Wrapping that in a helper that replaces setReadOnly( Ctor, 'name', value ) would keep each site to one line. stdlib has no conditional definer yet, so whether to add one is question 1. If it is added, the choice belongs in its index.js at load, as the barrel in utils/define-property chooses, not in a check on every call.

The change applies at every call site. There are 28 setReadOnly( …, 'name', … ) calls under lib/ (test/, benchmark/, examples/, and docs/ excluded). One is bench/harness's on a benchmark instance, which isn't a function, so 27 would change:

The 27 call sites

Found with:

grep -rnE "set[A-Za-z]*(ReadOnly|Configurable|Writable)[A-Za-z]*\( *[A-Za-z0-9_.]+, *'name'" lib/node_modules/@stdlib --include='*.js' | grep -vE '/(test|benchmark|examples|docs)/'

What the brand checks lose where the name can't be set.
Where the test fails, a constructor keeps its declared name, or whatever a bundler or minifier left. 13 @stdlib/assert packages fall back to the constructor's name after instanceof (for example, is-float16array), and constructorName returns ctor.name (L63). Same-realm values still pass through instanceof. A cross-realm value from a renamed build would fail, but such builds can't load on these engines today. The six different-name sites have no name-based brand check, so there the cost is the name alone.

Tests would follow the existing ones, without touching globals: the support check by stubbing its define_property.js with a function that throws (utils/define-property's test.has_support.js), and each caller by stubbing the check, as utils/function-name's tests stub has-function-name-support.

Related Issues

Related issues:

Questions

  1. A helper, or the pattern inline at each site?
    No define-* helper defines a property conditionally today, so a helper would be the first. As a barrel, its fallback would do nothing, where other barrels' fallbacks implement the behaviour.
  2. Naming. What are the check and the helper called, and where do they live?
    hasSupport above is a placeholder, and the name should keep it apart from has-function-name-support. define-* helpers are named by the attributes they set (define-nonenumerable-read-only-property), so a helper named for its purpose would also be new.

Other

Reproduction, in current Node.js, with one function's name fixed first to simulate an engine like lv5 or otto:

Important

This is only a reproduction, not a testing approach.
Redefining name on a function by hand is a quick way to show the failure without building an ES5-era engine. It isn't how the change should be tested, here or anywhere else.

// repro.js
'use strict';

var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );

// Simulate an engine whose function `name` is neither writable nor configurable (e.g., lv5 or otto):
function Foo() {}
Object.defineProperty( Foo, 'name', {
    'configurable': false,
    'enumerable': false,
    'writable': false,
    'value': 'Foo2' // the name a bundler might leave
});

try {
    setReadOnly( Foo, 'name', 'Foo' );
    console.log( 'ok:', Foo.name );
} catch ( err ) {
    console.log( err.name + ': ' + err.message );
}

Output at fed67ca5 (Node.js v24.22.3, run from the repository root with NODE_PATH=lib/node_modules node repro.js):

TypeError: Cannot redefine property: name

lv5 throws the same way, with its own message, changing [[Value]] of readonly property not allowed.

The engine results above came from a probe that defines a declared function's name with the same value and with a different one, in sloppy and strict code. The source-level renames were not run on lv5 or otto; that they throw there follows from the strict-mode result.

Checklist

  • I have read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
  • The issue name begins with RFC:.

Activity

  1. added
    RFC
    Feature
    Needs Discussion
    JavaScript
    on Nov 26, 2026
  2. changed the title [-][RFC]:[/-] [+][RFC]: set constructor names only where the runtime allows it[/+] on Nov 26, 2026
  3. ZunedKhan07 commented on Nov 28, 2026

    @ZunedKhan07

    Hello @batpigandme,

    On question 1, I think a helper makes more sense. The 27 calls are spread across different packages, not one file, so without a helper, every package would carry its own copy of the HAS_SUPPORT check. If the helper runs the check once when it loads, each call site only needs a one-line change.

    One small thing: if the helper is structured as a barrel like utils/define-property, its fallback would just be a no-op, whereas other barrels use the fallback to do the actual work. Might be worth confirming if that convention fits before finalizing the helper's name.

    On naming, has-function-name-redefinition-support is clear, though a bit long and close to has-function-name-support. Maybe something like has-configurable-function-name-support is a bit easier to tell apart, though I'm fine with whatever fits stdlib naming style best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureJavaScriptNeeds DiscussionRFC

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions