-
-
Notifications
You must be signed in to change notification settings - Fork 55
Open
Description
Problem
The current inheritance hierarchy between JSFunction and JSObject creates several design issues:
- Mismatch with JavaScript's dynamic callability: In JavaScript, any object can potentially be callable (through Proxy traps or other mechanisms), but our type system assumes objects and functions are mutually exclusive categories
- Complex Inheritance: The inheritance structure makes the codebase unnecessarily complex to maintain
- Inconsistent Closure Hierarchy:
- JSClosure inherits from JSFunction
- JSOneshotClosure inherits from JSObject
- This inconsistency creates confusion about which base class to use
- Type System Gap:
JSObject.global.Function.function\!.new
returns a JSObject that should be callable, but it's not in our type system
Current Architecture
JSFunction
inherits fromJSObject
JSClosure
inherits fromJSFunction
JSOneshotClosure
inherits fromJSObject
- JSValue enum has separate cases for
.function
and.object
Proposed Solution
Unify JSFunction and JSObject by:
- Merge functionality: Move all JSFunction functionality into JSObject
- Add callability detection: Provide runtime checks for callable objects
- Simplify closures: Make JS(Oneshot)Closure composition-based rather than inheritance-based
- Update JSValue: Remove separate function case, use single object case
Benefits
- Aligns type system with JavaScript's dynamic callability
- Reduces inheritance complexity
- Provides consistent closure architecture
- Simplifies API surface with unified object model
Migration Strategy
- Add deprecated JSFunction class that forwards to JSObject
- Update all JSFunction usage to JSObject
- Provide migration guide and tooling
- Remove deprecated JSFunction in next major version
References
- Async test failures already hint at this issue:
Tests/JavaScriptEventLoopTests/JSClosure+AsyncTests.swift:61-64
- Current TODO comments acknowledge the need for this unification
Metadata
Metadata
Assignees
Labels
No labels