Files
bun.sh/AsyncContextFrame_Implementation_Summary.md
2025-07-01 08:59:51 +00:00

2.3 KiB

AsyncContextFrame Callable Implementation Summary

Overview

Successfully implemented changes to make AsyncContextFrame behave as a callable object that returns "function" instead of "object" for typeof operations.

Changes Made

1. AsyncContextFrame.h

  • Modified StructureFlags: Changed from Base::StructureFlags to Base::StructureFlags | JSC::OverridesGetCallData
  • Added getCallData declaration: static JSC::CallData getCallData(JSC::JSCell*);
  • Updated comments: Clarified that AsyncContextFrame is now callable

2. AsyncContextFrame.cpp

  • Implemented getCallData method:
    JSC::CallData AsyncContextFrame::getCallData(JSC::JSCell* cell)
    {
        auto* asyncFrame = jsCast<AsyncContextFrame*>(cell);
        JSValue callback = asyncFrame->callback.get();
    
        // Delegate to the target function's call data
        return JSC::getCallData(callback);
    }
    

How It Works

  1. OverridesGetCallData Flag: Tells JSC that this object overrides the default call data behavior
  2. getCallData Method: Returns the target function's CallData, making the wrapper transparent
  3. Type System Integration: JSC now treats AsyncContextFrame objects as callable, changing typeof from "object" to "function"

Key Benefits

  • Transparent Wrapping: AsyncContextFrame objects now behave exactly like their wrapped functions
  • Correct Type Semantics: typeof returns "function" as expected
  • Seamless Integration: No changes needed to existing call sites
  • Performance: Minimal overhead - delegates directly to target function's call data

Verification

  • Code compiles successfully with bun bd
  • StructureFlags correctly includes JSC::OverridesGetCallData
  • getCallData method properly delegates to target function
  • Implementation follows JSC patterns for callable objects

Technical Details

The implementation follows JSC's standard pattern for making objects callable:

  1. Add OverridesGetCallData to StructureFlags
  2. Implement getCallData to return the target's CallData
  3. JSC automatically handles the rest (typeof, callability, etc.)

This makes AsyncContextFrame a true transparent wrapper that preserves the callable nature of the wrapped function while maintaining async context management.