Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): cannot lift a class that extends a cloud resource #6490

Merged
merged 39 commits into from
Jul 24, 2024

Conversation

Chriscbr
Copy link
Contributor

@Chriscbr Chriscbr commented May 15, 2024

This PR aims to fix an issue where unexpected bugs happen when you try extending a class from the Wing SDK, like cloud.Bucket. The root cause is related to how Wings' inheritance is implemented in JavaScript.

Each cloud resource has a JavaScript class modeling its inflight API. The class needs to be constructed with target-specific arguments. For example, for the implementation of cloud.Bucket, there's a class definition BucketClient for each of the supported cloud targets (tf-aws, tf-gcp, and tf-azure). What's important to note is that each class needs to be instantiated at runtime with value that are specific to that cloud target. For example, new aws.BucketClient(...) expects an S3 Bucket name, while new azure.BucketClient(...) expects an Azure container name and an Azure storage account name.

How does Wing ensure the right values are passed when the class is constructed? Currently, each preflight class has an _toInflight() method which returns a string of JavaScript code. That code instantiates the class and hardcodes in the necessary arguments.
This method is code-generated by the compiler for most Wing classes.

But suppose we have a class MyBucket that extends Bucket. To instantiate such a class, we'd need to instantiate MyBucket with both the arguments needed by MyBucket AND the arguments needed by Bucket, and ensure that the super() call in MyBucket's constructor passes the appropriate arguments to Bucket's constructor.

To support this, we're now exposing a new method in the TypeScript source code named _liftedState() which can be overridden by classes inside our TypeScript SDK implementation to specify what arguments they need when they're instantiated. When _toInflight() is called, it will automatically call _liftedState() to obtain the constructor arguments and instantiate the class. But if you just call _toInflightType(), the class will be returned without instantiating an instance of it. These internal changes should not have any user-facing effects.

Fixes #4203
Fixes #6093
Fixes #6851

Checklist

  • Title matches Winglang's style guide
  • Description explains motivation and solution
  • Tests added (always)
  • Docs updated (only required for features)
  • Added pr/e2e-full label if this feature requires end-to-end testing

By submitting this pull request, I confirm that my contribution is made under the terms of the Wing Cloud Contribution License.

Copy link

Thanks for opening this pull request! 🎉
Please consult the contributing guidelines for details on how to contribute to this project.
If you need any assistance, don't hesitate to ping the relevant owner over Discord.

Topic Owner
Wing SDK and utility APIs @chriscbr
Wing Console @ainvoner, @skyrpex, @polamoros
JSON, structs, primitives and collections @hasanaburayyan
Platforms and plugins @hasanaburayyan
Frontend resources (website, react, etc) @tsuf239
Language design @chriscbr
VSCode extension and language server @markmcculloh
Compiler architecture, inflights, lifting @yoav-steinberg
Wing Testing Framework @tsuf239
Wing CLI @markmcculloh
Build system, dev environment, releases @markmcculloh
Library Ecosystem @chriscbr
Documentation @hasanaburayyan
SDK test suite @tsuf239
Examples @hasanaburayyan
Wing Playground @eladcon

@monadabot
Copy link
Contributor

monadabot commented May 15, 2024

Console preview environment is available at https://wing-console-pr-6490.fly.dev 🚀

Last Updated (UTC) 2024-07-24 19:14

@monadabot
Copy link
Contributor

monadabot commented May 15, 2024

Benchmarks

Comparison to Baseline ⬜⬜⬜⬜⬜⬜⬜🟥🟥⬜⬜⬜⬜
Benchmark Before After Change
version 57ms±0.55 58ms±0.54 +1ms (+1.19%)⬜
empty.test.w -t sim 380ms±19.7 376ms±4.81 -4ms (-1.1%)⬜
empty.test.w -t tf-aws 612ms±3.62 626ms±6.16 +15ms (+2.37%)⬜
hello_world.test.w -t sim 412ms±8.51 422ms±10.66 +10ms (+2.39%)⬜
hello_world.test.w -t tf-aws 1496ms±6.7 1532ms±9.8 +36ms (+2.38%)⬜
jsii_big.test.w -t sim 2951ms±7.86 3007ms±12.74 +56ms (+1.9%)⬜
jsii_big.test.w -t tf-aws 3167ms±10.82 3227ms±12.81 +61ms (+1.91%)⬜
functions_1.test.w -t sim 406ms±5.21 419ms±5.96 +13ms (+3.09%)🟥
functions_1.test.w -t tf-aws 851ms±4.7 877ms±5.05 +26ms (+3.01%)🟥
functions_10.test.w -t sim 506ms±4.81 519ms±9.3 +13ms (+2.55%)⬜
functions_10.test.w -t tf-aws 2195ms±8.99 2222ms±20.69 +27ms (+1.25%)⬜
jsii_small.test.w -t sim 376ms±3.15 379ms±6.42 +2ms (+0.61%)⬜
jsii_small.test.w -t tf-aws 618ms±4.08 619ms±4.39 +0ms (+0.08%)⬜

⬜ Within 1.5 standard deviations
🟩 Faster, Above 1.5 standard deviations
🟥 Slower, Above 1.5 standard deviations

Benchmarks may vary outside of normal expectations, especially when running in GitHub Actions CI.

Results
name mean min max moe sd
version 58ms 57ms 59ms 1ms 1ms
empty.test.w -t sim 376ms 364ms 388ms 5ms 7ms
empty.test.w -t tf-aws 626ms 615ms 641ms 6ms 9ms
hello_world.test.w -t sim 422ms 407ms 460ms 11ms 15ms
hello_world.test.w -t tf-aws 1532ms 1514ms 1556ms 10ms 14ms
jsii_big.test.w -t sim 3007ms 2980ms 3031ms 13ms 18ms
jsii_big.test.w -t tf-aws 3227ms 3201ms 3256ms 13ms 18ms
functions_1.test.w -t sim 419ms 407ms 433ms 6ms 8ms
functions_1.test.w -t tf-aws 877ms 862ms 885ms 5ms 7ms
functions_10.test.w -t sim 519ms 500ms 542ms 9ms 13ms
functions_10.test.w -t tf-aws 2222ms 2168ms 2260ms 21ms 29ms
jsii_small.test.w -t sim 379ms 362ms 395ms 6ms 9ms
jsii_small.test.w -t tf-aws 619ms 610ms 629ms 4ms 6ms
Last Updated (UTC) 2024-07-24 19:20

libs/wingc/src/jsify.rs Outdated Show resolved Hide resolved
libs/wingsdk/src/core/inflight.ts Outdated Show resolved Hide resolved
libs/wingsdk/src/target-sim/counter.ts Outdated Show resolved Hide resolved
libs/wingsdk/src/target-tf-aws/counter.ts Outdated Show resolved Hide resolved
libs/wingsdk/src/target-tf-aws/function.ts Outdated Show resolved Hide resolved
@yoav-steinberg
Copy link
Contributor

excited about this one :) nice work.

@monadabot monadabot added the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Jul 23, 2024
@Chriscbr Chriscbr added 🧪 pr/e2e-full and removed ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! labels Jul 23, 2024
@Chriscbr Chriscbr marked this pull request as ready for review July 23, 2024 20:47
@Chriscbr Chriscbr requested a review from a team as a code owner July 23, 2024 20:47
@Chriscbr
Copy link
Contributor Author

@Chriscbr Chriscbr requested a review from skyrpex July 23, 2024 22:12
libs/awscdk/src/api.ts Show resolved Hide resolved
Copy link
Contributor

mergify bot commented Jul 24, 2024

Thanks for contributing, @Chriscbr! This PR will now be added to the merge queue, or immediately merged if rybickic/extend-resources is up-to-date with main and the queue is empty.

@mergify mergify bot merged commit c901bf6 into main Jul 24, 2024
25 checks passed
@mergify mergify bot deleted the rybickic/extend-resources branch July 24, 2024 20:06
@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.79.13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants