Skip to content

self is undefined under --platform bun, and the member-object collapse hides it for builtin names #10306

Closed

Description

@proggeramlug

Under --platform bun, self is not defined at all. bun defines it as a globalThis self-reference on the main thread, as browsers and Web Workers do.

Repro

// @ts-nocheck
console.log("typeof self      ", typeof self)          // bun "object"   perry "undefined"
console.log("self === gT      ", self === globalThis)  // bun true       perry false
self.x = 1                                             // bun ok         perry TypeError:
                                                       //   Cannot set properties of null or undefined (setting 'x')
console.log("self.Math.max    ", self.Math.max(1, 4))  // bun 4          perry 4   ← still works

perry compile main.ts --platform bun.

Two things worth separating

  1. self is missing. is_builtin_global_value_name lists it (with the comment "Web Worker scope aliases … installed on the worker thread's global object before its compiled entry runs"), but nothing installs it on the main thread. bun installs it on both. Node deliberately does not define self on the main thread, so if the node platform is meant to match Node this is a bun-platform-only gap — worth confirming which behaviour each platform should have rather than installing it unconditionally.

  2. self.Math.max(1, 4) returns 4 anyway. The member-object collapse in lower/expr_member/member_tail.rs rewrites self.<builtin> to the intrinsic GlobalGet(0).<builtin> surface, so it never dereferences the (undefined) receiver. The result is that self.<builtin> silently works while self.<anything else> throws — the receiver is only consulted for the names that are not built in. Same mechanism as Reads through Node's global alias miss every user-installed property (global.x = v; global.x → undefined) #10303, where global.<x> collapsed and answered undefined for user-installed properties; the self case just has no receiver at all behind the collapse.

Found while checking whether self shared #10303's shape — it does, and it has an extra problem underneath.

Impact

Not currently blocking OpenCode (#10107); it does not use self. Filed because the collapse masking a missing global is the kind of thing that surfaces later as a confusing partial-success.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions