Skip to content

Worktree staged singing lighthouse#81

Merged
cs01 merged 2 commits intomainfrom
worktree-staged-singing-lighthouse
Mar 2, 2026
Merged

Worktree staged singing lighthouse#81
cs01 merged 2 commits intomainfrom
worktree-staged-singing-lighthouse

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 2, 2026

httpServe Ergonomics: Support Inline Arrow Functions

Summary

Allow passing arrow functions directly to httpServe() instead of requiring a pre-declared named function. Users can now write:

httpServe(port, (req: HttpRequest) => app.handle(req));

Instead of:

function handleRequest(req: HttpRequest): HttpResponse {
  return app.handle(req);
}
httpServe(port, handleRequest);

Motivation

The pre-declared function pattern is needless boilerplate. Other async/callback-based APIs in ChadScript (Router, Map handlers, etc.) already support inline arrow functions — httpServe was the odd one out.

Implementation

Changes to src/codegen/llvm-generator.ts

  1. generateHttpServe method: Now accepts both:

    • variable nodes (existing behavior): httpServe(port, myHandler)
    • arrow_function nodes (new): httpServe(port, (req) => app.handle(req))
  2. New helper inferArrowHandlerReturnType: Analyzes the arrow function body to determine its TypeScript return type:

    • If body is a method call (e.g., app.handle(req)), looks up the method's return type via symbolTable.getConcreteClass() + getMethodReturnType()
    • Returns type name like "HttpResponse" for proper interface detection later
    • Falls back to "i8*" (opaque pointer) if inference fails
  3. Arrow function lifting: When an arrow is passed, it's lifted to a top-level function via arrowFunctionGen.generateArrowFunction() with type hints:

    • paramTypes: ["i8*"] — handler param is always HttpRequest (passed as pointer)
    • returnType — inferred from body, or "i8*" as fallback
  4. Enhanced hasHeaders/hasBodyLen detection: Previously only searched ast.functions for the handler. Now also searches liftedFunctions to correctly identify interface fields (e.g., headers) on arrow function handlers.

Changes to chadscript.d.ts

Added three declare module blocks so TypeScript knows about imports from chadscript/*:

declare module "chadscript/argparse" { ... }
declare module "chadscript/router" { ... }
declare module "chadscript/http-utils" { ... }

Provides IDE autocomplete and type checking for import { Router } from "chadscript/router" etc.

Changes to examples/http-server.ts

Simplified from a pre-declared wrapper function to the idiomatic inline form.

How It Works

  1. Parser creates ArrowFunctionNode from (req: HttpRequest) => app.handle(req)
  2. generateHttpServe detects it's an arrow function
  3. inferArrowHandlerReturnType walks the body:
    • Finds method call app.handle(req)
    • Looks up Router.handle() return type → "HttpResponse"
  4. Arrow is lifted to a top-level function __lambda_0 with returnType: "HttpResponse"
  5. Lifted function is added to liftedFunctions[]
  6. Later, hasHeaders/hasBodyLen detection finds the lifted function and correctly identifies that HttpResponse has headers
  7. Wrapper @__lws_http_handler is generated with correct struct field access

Error Handling

Non-function arguments are rejected with a clear error:

error: httpServe() handler must be a function reference or arrow function

Testing

  • Updated examples/http-server.ts compiles to native binary successfully
  • All existing tests pass (pre-existing failures unaffected)
  • Arrow function handler correctly reads/writes HttpResponse headers via the C bridge

Benefits

  • Idiomatic: Matches JavaScript conventions (e.g., Express.js, http.createServer())
  • Less boilerplate: No wrapper function needed
  • Consistent: Router already supports inline handlers

@cs01 cs01 merged commit ffe158c into main Mar 2, 2026
12 checks passed
@cs01 cs01 deleted the worktree-staged-singing-lighthouse branch March 2, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant