Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:Instead of:
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 —
httpServewas the odd one out.Implementation
Changes to
src/codegen/llvm-generator.tsgenerateHttpServemethod: Now accepts both:variablenodes (existing behavior):httpServe(port, myHandler)arrow_functionnodes (new):httpServe(port, (req) => app.handle(req))New helper
inferArrowHandlerReturnType: Analyzes the arrow function body to determine its TypeScript return type:app.handle(req)), looks up the method's return type viasymbolTable.getConcreteClass()+getMethodReturnType()"HttpResponse"for proper interface detection later"i8*"(opaque pointer) if inference failsArrow 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 alwaysHttpRequest(passed as pointer)returnType— inferred from body, or"i8*"as fallbackEnhanced
hasHeaders/hasBodyLendetection: Previously only searchedast.functionsfor the handler. Now also searchesliftedFunctionsto correctly identify interface fields (e.g.,headers) on arrow function handlers.Changes to
chadscript.d.tsAdded three
declare moduleblocks so TypeScript knows about imports fromchadscript/*:Provides IDE autocomplete and type checking for
import { Router } from "chadscript/router"etc.Changes to
examples/http-server.tsSimplified from a pre-declared wrapper function to the idiomatic inline form.
How It Works
ArrowFunctionNodefrom(req: HttpRequest) => app.handle(req)generateHttpServedetects it's an arrow functioninferArrowHandlerReturnTypewalks the body:app.handle(req)Router.handle()return type →"HttpResponse"__lambda_0withreturnType: "HttpResponse"liftedFunctions[]hasHeaders/hasBodyLendetection finds the lifted function and correctly identifies thatHttpResponsehas headers@__lws_http_handleris generated with correct struct field accessError Handling
Non-function arguments are rejected with a clear error:
Testing
examples/http-server.tscompiles to native binary successfullyHttpResponseheaders via the C bridgeBenefits
Express.js,http.createServer())