Title: feat: Add experimental closures support with feature flag by BlobMaster41 · Pull Request #2964 · AssemblyScript/assemblyscript · GitHub
Open Graph Title: feat: Add experimental closures support with feature flag by BlobMaster41 · Pull Request #2964 · AssemblyScript/assemblyscript
X Title: feat: Add experimental closures support with feature flag by BlobMaster41 · Pull Request #2964 · AssemblyScript/assemblyscript
Description: Fixes #798. Related: #173, #563, #2054, #2753. Changes proposed in this pull request: ⯈ Added experimental closures feature - Closures can now capture variables from their enclosing scope. This includes support for: Capturing parameters and local variables (both let and var) Capturing this directly in class methods Read and write access to captured variables (reference semantics) Multiple closures sharing the same environment Deeply nested closures (capturing from grandparent scopes) Higher-order functions (map, filter, reduce patterns) Factory patterns, memoization, and other functional programming idioms ⯈ Implemented as an opt-in feature flag - Closures are disabled by default to maintain backwards compatibility and expected behavior. Users must explicitly enable the feature with --enable closures. This ensures: No changes to existing code behavior No additional runtime overhead for code that doesn't use closures Indirect calls without closures enabled use simpler codegen ⯈ Added compile-time constant ASC_FEATURE_CLOSURES - Allows conditional compilation based on whether closures are enabled ⯈ Added comprehensive test suites: closure.ts - Basic closure patterns (captures, mutations, shared environments) closure-stress.ts - Stress tests covering many edge cases (616 lines) closure-class.ts - Complex class patterns with closures (1000+ lines) including: State management (BankAccount, Counter) Design patterns (Builder, Factory, Observer, State Machine, Iterator) Inheritance with closures Callback-based "async" patterns Tree traversal with recursive closures Implementation Details The implementation follows the approach discussed in #798: Closure Environment: Captured variables are stored in a heap-allocated environment structure Multiple closures in the same scope share a single environment (reference semantics) Nested closures maintain a chain of environments via parent pointers Environment structures are managed by the runtime (GC-compatible) First-Class Functions: Function references contain both a table index and an _env pointer When calling through a function reference, the _env is loaded and made available Non-closure functions have _env = 0, avoiding overhead when closures aren't used Core changes in src/compiler.ts: Pre-scan phase (prescanForClosures) identifies closures and captured variables before compilation Environment allocation creates heap-allocated storage for captured variables Closure load/store operations route through the environment Dynamic Function objects are created with environment pointers for closures Feature checks gate closure functionality at detection points Indirect calls only set up closure environment handling when the feature is enabled Supporting changes: std/assembly/shared/feature.ts - Added Feature.Closures enum value src/common.ts - Added ASC_FEATURE_CLOSURES constant name src/program.ts - Registered compile-time constant src/index-wasm.ts - Exported FEATURE_CLOSURES for CLI src/flow.ts - Added flow tracking for captured variables Limitations This is an experimental implementation. Known limitations: Performance: Each closure creation involves a heap allocation for the environment Usage # Closures disabled (default) - captures produce an error asc myfile.ts # Enable closures explicitly asc myfile.ts --enable closures // Example closure usage (requires --enable closures) function makeCounter(): () => i32 { let count = 0; return (): i32 => { count += 1; return count; }; } let counter = makeCounter(); counter(); // 1 counter(); // 2 counter(); // 3 I've read the contributing guidelines I've added my name and email to the NOTICE file
Open Graph Description: Fixes #798. Related: #173, #563, #2054, #2753. Changes proposed in this pull request: ⯈ Added experimental closures feature - Closures can now capture variables from their enclosing scope. This inc...
X Description: Fixes #798. Related: #173, #563, #2054, #2753. Changes proposed in this pull request: ⯈ Added experimental closures feature - Closures can now capture variables from their enclosing scope. This inc...
Opengraph URL: https://github.com/AssemblyScript/assemblyscript/pull/2964
X: @github
Domain: patch-diff.githubusercontent.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:40e33eed-bdce-671d-72ef-be7b58a108ee |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | ECEA:3406D0:787D79B:9FE7B2E:696DF484 |
| html-safe-nonce | 304ff19fd113a7e0bd7b6044fccabead5378e728aebd7a65bfcadfc1762f3655 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQ0VBOjM0MDZEMDo3ODdENzlCOjlGRTdCMkU6Njk2REY0ODQiLCJ2aXNpdG9yX2lkIjoiMTA3Njc0ODg1MjYzMTgyNzU4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e8ed401285740c0b3ce1da0de61b952c0c415d15b9f524f6872b38f43a49a767 |
| hovercard-subject-tag | pull_request:3091551973 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/AssemblyScript/assemblyscript/pull/2964/checks |
| twitter:image | https://avatars.githubusercontent.com/u/96896824?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/96896824?s=400&v=4 |
| og:image:alt | Fixes #798. Related: #173, #563, #2054, #2753. Changes proposed in this pull request: ⯈ Added experimental closures feature - Closures can now capture variables from their enclosing scope. This inc... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4922b452d03cd8dbce479d866a11bc25b59ef6ee2da23aa9b0ddefa6bd4d0064 |
| turbo-cache-control | no-preview |
| go-import | github.com/AssemblyScript/assemblyscript git https://github.com/AssemblyScript/assemblyscript.git |
| octolytics-dimension-user_id | 28916798 |
| octolytics-dimension-user_login | AssemblyScript |
| octolytics-dimension-repository_id | 105138356 |
| octolytics-dimension-repository_nwo | AssemblyScript/assemblyscript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 105138356 |
| octolytics-dimension-repository_network_root_nwo | AssemblyScript/assemblyscript |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7e5ae23c70136152637ceee8d6faceb35596ec46 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width