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

feat: allow referencing properties of constructs #2909

Closed
wants to merge 48 commits into from
Closed

Conversation

eladb
Copy link
Contributor

@eladb eladb commented Jun 13, 2023

It is now possible to reference properties of Terraform CDK constructs from inflight context. For example:

bring "@cdktf/provider-aws" as aws;
let b = new aws.s3Bucket.S3Bucket();

inflight () => {
  log(b.arn);
};

In order to support this, we need the lifting and binding mechanism to have information about which properties are being accessed. To that end, I've extended this API to accept an additional props array with the names of properties being bound/lifted.

I changed how FieldReferenceVisitor so that if it finds a property reference (e.g. this.bucket.arn or bucket.arn), it will treat arn as the op, and we merge this information with the list of captured variables/fields.

Tweaked the registerBind() method to allow any op that matches a preflight property.

  • Fixed a bug in how we lift tokens. CDKTF produces a new token every time a tokenized property is retrieved (e.g. bucket.arn). And since we used this token string every time. Since we use the token ID to produce an environment variable, this created a situation where lift() and bind() had a different environment variable name.
  • Update the Debug Wing Compiler launch configuration to use the currently open file as the source for debugging.
  • Remove the self_client_path from the generated _toInflightType() methods and just embed the path directly into the inline code.

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 Monada Contribution License.

It is now possible to reference properties of Terraform CDK constructs from inflight context. For example:

```js
bring "@cdktf/provider-aws" as aws;
let b = new aws.s3Bucket.S3Bucket();

inflight () => {
  log(b.arn);
};
```

In order to support this, we need the lifting and binding mechanism to have information about which properties are being accessed. To that end, I've extended this API to accept an additional `props` array with the names of properties being bound/lifted.

I changed how `FieldReferenceVisitor` so that if it finds a property reference (e.g. `this.bucket.arn` or `bucket.arn`), it will treat `arn` as the `op`, and we merge this information with the list of captured variables/fields.

Tweaked the `registerBind()` method to allow any `op` that matches a preflight property.

* Fixed a bug in how we lift tokens. CDKTF produces a new token every time a tokenized property is retrieved (e.g. `bucket.arn`). And since we used this token string every time. Since we use the token ID to produce an environment variable, this created a situation where `lift()` and `bind()` had a different environment variable name.
* Update the `Debug Wing Compiler` launch configuration to use the currently open file as the source for debugging.
* Remove the `self_client_path` from the generated `_toInflightType()` methods and just embed the path directly into the inline code.
@eladb eladb requested a review from a team as a code owner June 13, 2023 19:24
Comment on lines +10 to +12
inflight getBucketArn(): str {
return this.b2.arn;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the user accesses the arn in a different way? For example:

Suggested change
inflight getBucketArn(): str {
return this.b2.arn;
}
inflight getBucketArn(): str {
let bucket = this.b2;
return bucket.arn;
}

or:

class MyClass {
  arn: str;
  init() {
    let b2 = new aws.s3Bucket.S3Bucket();
    this.arn = b2.arn;
  }
  inflight getBucketArn(): str {
    return this.arn;
  }
}

@@ -2,13 +2,31 @@

## stdout.log
```log
pass ─ add_object.wsim » root/env0/test:addObject
fail ┌ add_object.wsim » root/env0/test:addObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i have some work to do here.

Comment on lines 48 to 51
private envNameForToken(value: any) {
const resolved = Tokenization.resolve(value, { scope: this.stack, resolver: new DefaultTokenResolver(new StringConcat()) });
return this.envName(resolved);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think there's a way to unit test this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i'll look into this.

@@ -224,7 +225,7 @@ export abstract class Resource extends Construct implements IResource {
}

throw new Error(
`unable to serialize immutable data object of type ${obj.constructor?.name}`
`objects of type '${obj.constructor?.name}' cannot be referenced from an inflight scope`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case it's not an object

Suggested change
`objects of type '${obj.constructor?.name}' cannot be referenced from an inflight scope`
`objects of type '${obj.constructor?.name ?? typeof obj}' cannot be referenced from an inflight scope`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Chriscbr
Copy link
Contributor

Let me know when this is ready for another review

@eladb
Copy link
Contributor Author

eladb commented Jun 22, 2023

Will do

ekeren and others added 18 commits June 22, 2023 16:42
## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
## Description

Added `util.sleep(Duration)` inflight api

## Leftover
#2907

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Due to the jsii type lazy loading, completions only show types that were used in the current file. We now eagerly load namespaces when referenced (even if incomplete).

For example:

```
bring "aws-cdk-lib" as awscdk;

new awscdk.aws_lambda.
                    //^ completions will show here
```

https://github.com/winglang/wing/assets/1237390/d6edeee2-8c64-428d-a2ef-0aa51b5e3b96

## Notes
  - I couldn't think of a reasonable way to test this, the lsp tests need a good jsii fixture system
  - refactored some code into a `reference_to_udt` and as part of it, allowed a udt to basically refer to a namespace
  - We also now also eagerly load jsii types that don't belong to a namespace, just to cover that base

Fixes #2639

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [ ] Tests added (always)
  - See notes above 
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
…2908)

This PR introduces another debugging utility for our compiler. Given code like this:

```js
bring cloud;

class A {
  f: num;
  init(b: cloud.Bucket) {
    this.f = 5;
    🗺️;

    let n = b.node.id;
  }
}

new A(new cloud.Bucket());
```

Compiling this will print to the terminal:

```
[symbol environment at example.w:7:5]
level 0: { __parent_this => A, b => Bucket, this => A }
level 1: { A => A [type], assert => (condition: bool): void, cloud => cloud [namespace], log => (message: str): void, panic => (message: str): void, std => std [namespace], throw => (message: str): void }
```

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
## Description

add `util.wait`

```ts
test "returns false takes a minute" {
  if util.wait(inflight (): bool => { return false; }) {
     assert(false);
    } else {
    assert(JSHelper.getTime() - start > 60 * 1000);
  }
}
```
fixes #2621 



## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
## Checklist

- [x] Title matches [Winglang's style
guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] 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 [Monada Contribution
License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
There were two copies of the Wing SDK spec, so I removed the duplicate.

I also cleaned up some internal naming in the SDK to consolidate some of the code around the API name change from `addConsumer` to `setConsumer`. (This is sort of a breaking change, but in practice I doubt anyone's using the interface name directly).

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Initial spec for v0 of equality in the Wing language. Many of the relationships described already hold, though some of them are not implemented yet. All feedback is welcome.

[Rendered doc](https://github.com/winglang/wing/blob/rybickic/equality-spec/docs/03-language-guide/14-equality.md)

Closes #2756 

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
…#2944)

Time Save: ~17 minutes
Time Save (pr/e2e-full): ~30 minutes

This is a low-effort change, but saves us quite a bit of time.
This adds quite a bit of duplication due to the fact that testing in a new job effectively requires us to redo a lot of work, but at the cost of wasted workflow time we gain overall speed. This could be possible to avoid by saving the entire build steps as an asset, but that would be multiple GBs of data so I'm not sure it's worth it. 
It would also be solved by fully figuring out Nx caching, but tbh I think that'll be a nightmare.

Additionally, it turns out that the test sharding for e2e tests weren't working. So we were just running all tests twice 😭 . Fixing that shaved another ~7 minutes from non-full e2e load.

Misc Change: Decided also to remove the auto-approve workflow, since we're not using it and it adds a useless "skipped" status to all PRs.

Closes #2820

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [ ] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
`post-compile` requires `compile` to run, so we might as well run the whole build step.

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
We were running esbuild through a spawned node process, which is very slow. See evanw/esbuild#2927 for why this was needed. In that thread, it was mentioned that some changes to esbuild avoided the issue. 

### Change

Added a patch with the previously mentioned workaround to "fix" this (with a small additional change to handle the newly piped stderr). This greatly improves bundling time (saves several hundred ms per inflight).

As far as I can tell, there are no bad side effects for this change (leaks, etc.)

### Root Cause (?)

After doing research on this, I still haven't quite figured out the real issue. esbuild-wasm creates both a worker thread and a subprocess that stays alive "forever". The thread and subprocess are `.unref()`ed, which means that node will not wait for them to finish before the parent process ends. The child process has a handle on stderr, so it seems like when the parent process finishes at a bad time then something in that fd gets messed up. This change removes that handle, which I suppose is good enough?

### Misc

- Started running into an issue that looked like vitest wasn't properly mocking an azure dep in the SDK tests. Not sure why it suddenly started happening. Updating vitest fixed it.
- Updated esbuild-wasm to latest. This wasn't necessary for the fix to work, I just figured it made sense to do.
- I accidentally ran `npm audit fix` and decided that instead of undoing it I would just keep it in, so this PR also updates aws-sdk packages.

### Upstreaming this change

I don't understand esbuild's architecture to know if this change is best for everyone, so maybe we hang on to this for a while before thinking about offering it as a PR upstream. Not sure though, I'd be fine with also doing it right away to see what the author says.

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Updating our tests to run on Node 20 instead of Node 19 since Node 19 is past its end-of-life date (https://endoflife.date/nodejs).

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
…utJson` (#2901)

Changed `MutJson` methods `.set()`, `.setAt()` to accept `MutJson`  and not
Fixes issue #2767  

## Checklist

- [ ] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
verify that sleep work without guarantee precision

*By submitting this pull request, I confirm that my contribution is made
under the terms of the [Monada Contribution
License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
## Checklist

- [ ] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
…2870)

Adding support to days, months and years to Duration

| syntax | description | in seconds |
|-|-|-|
| 1ms | 1 millisecond | 1/1000 |
| 1s | 1 second | 1 |
| 1m | 1 minute | 60 |
| 1h | 1 hour | 60 * 60 |
| 1d | 1 day | 60 * 60 * 24 |
| 1mo | 1 month | (60 * 60 * 24 * 365) / 12 ➡️ (30.4166667 days) |
| 1y | 1 year | 60 * 60 * 24 * 365 |

Months and years are a bit complicated as their duration varies, so I am assuming that 1 month (30,4166667 days) is equal to 2,592,000 seconds and 1 year corresponds to 31,104,000 seconds.

Closes #2243

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Access `duration` functions without prefixing with `std`

Fixes #2935 

## Checklist

- [X] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [X] Description explains motivation and solution
- [X] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
hasanaburayyan and others added 19 commits June 22, 2023 16:42
Adding core concept entry for Wing compiler targets


## Checklist

- [ ] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
…#2925)

Fixes #2747

https://github.com/winglang/wing/assets/1237390/253e892c-0680-4bba-ab7c-3a0b065ba686

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
* testing website's initiation, `addJson`, and `url`
* add external `fetch` and `readFile` methods (js)

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
I love https://github.com/winglang/console/tree/main/tools/conventional-release and it effectively has the same goal as the ad-hoc script in https://github.com/winglang/wing/blob/main/scripts/changelog.mjs. 
To ease the movement of the console into the monorepo, I went ahead and moved that project into this repo. 
Unfortunately, a lot of changes were needed due to repo differences so I ended up using more of the original script than I intended, but the end result is still easier to use than what I implemented previously.

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
refactored to make the logic simpler as well

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Another followup to #2992

Switched back to using PROJEN_BUMP_VERSION, which is what the original script used. The issue with the last change probably was that we were trying to regenerate the version/changelog and it was failing. Using the existing variable should work and it's faster anyways.

Also explicitly added a little log, which shouldn't hurt.
* Adding a second argument (event type) for bucket.onEvent, so now it's aligned to the spec (fixes #192, but not systemically)
* Improving `bucket/events.w`  test
* Adding an example for functions that return functions
* Exposing a severe error in aws events- #2724

This pull request adds and updates several examples and tests for Wing, especially related to bucket events and functions as values. It also modifies the `convertBetweenHandlers` function and the `BucketEventHandlerClient` class to support passing additional arguments to handler clients. It updates the snapshots to reflect the changes in the examples, tests, and compilation output.

___

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
*By submitting this pull request, I confirm that my contribution is made under the terms of the [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Regression from #2784

2 things:

Raised the local node version for developing wing to the latest 18 (18.16.0) to make sure user versions support fetch
On windows the path for the Website construct is a windows path, so any matching needs to account for that.
Certain errors were not available in the lsp because we didn't run the jsify phase. This PR changes that and also avoids many panics.

So far it seems the perf overhead of running jsify in the lsp is minimal. On my PC, it adds between 1ms and 8ms. This time scales with classes/inflight/externs.

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
)

## Checklist

- [ ] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Adds support for calling super()  constructor within derived class initializer.

Closes: #1123

- fixed latent bug where inflight client super constructor calls were not wrapping args as an object
- some minor refactors and cleanups

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
Signed-off-by: monada-bot[bot] <[email protected]>
@eladb eladb changed the title feat: allow referencing properties of CDK constructs feat: allow referencing properties of constructs Jun 22, 2023
@eladb
Copy link
Contributor Author

eladb commented Jun 28, 2023

Superseded by #3125

@eladb eladb closed this Jun 28, 2023
eladb added a commit that referenced this pull request Jul 3, 2023
## TODO

- [ ] Still failing tests
- [ ] Determine how to pass lifting tokens to super classes

Refactor the lifting system to work on expressions instead of references and merge lifting and capturing into a unified model.

*Lifting* is the concept of referencing a preflight expression from an inflight scope, while *capturing* is the concept of referencing an object defined in a parent scope (albeit not another phase).

The mechanism works like this: we now have another "lifting" compilation phase which runs immediately after type checking. This is a transform ("fold") which does the following:

1. Lifting: for all inflight methods, it scans all the expressions. If it finds and expression marked as a preflight expression, it transforms it into an `Expr::Lift` which is a placeholder that can be used later to replace it with a lifted token.
2. Capturing: for each reference (both type refs and object refs), it checks looks up in which symbol environment it is defined. It then checks if that environment is a parent of the current method environment. If it is, is transforms it into a `Reference::Capture` to indicate downstream that this is a captured reference.

Now we move to the jsification phase. When the jsifier hits a *lifted expression* or a *captured reference*, to jsifies it as if it was in a preflight scope and registers it in a centralized token registry maintained at the class level during jsification. This registry maintains a mapping between *inflight tokens* and *preflight expressions* (both for lifting and for capturing purposes).

The token registry is consulted when the class is jsified in order to emit the code that lifts expressions from preflight to inflight (both at the object level and at the type level).

## Fixes

* #1878 is fixed because now we are able to lift complete expressions. So `bucket.bucketArn` is a simple string and there is no need to lift the bucket.

This is the 3rd attempt at this refactor, and hopefully the last for now. It supersedes #3125 and #2909.
@eladb eladb mentioned this pull request Jul 3, 2023
5 tasks
mergify bot pushed a commit that referenced this pull request Jul 6, 2023
Refactor the lifting system to work on expressions instead of references and merge lifting and capturing into a unified model.

*Lifting* is the concept of referencing a preflight expression from an inflight scope, while *capturing* is the concept of referencing an object defined in a parent scope (albeit not another phase).

The mechanism works like this: we now have another "lifting" phase which runs immediately after type checking. This phase does the following:

1. **Lifting**: for inflight methods, it scans all the expressions. If it finds an expression marked as a preflight expression, it uses the jsifier to render it's preflight code and obtains a *token* from a lifts registry that's maintained at the class *type* level.

2. **Capturing**: for each expression that is a reference (both type refs and object refs), it checks looks up in which symbol environment it is defined. It then checks if that environment is a parent of the current method environment. If it is, is adds it to the lifts registry as well.

For each class, the lifting phase returns a new class object with the lift registry updated. So `class.lifts` has all the information about lifting and capturing for this class, later to be used downstream.

Now we move to the jsification phase. For each expression, the jsifier checks the `lifts` registry and if there's a token for this expression (by id), it emits the token instead (only in inflight of course).

The token registry is also consulted when the class machinery is jsified in order to emit the code that lifts expressions from preflight to inflight (both at the object level and at the type level) and binds them for access management.

When jsifying inflight base classes, we need to resolve the base class type and extract the lifting tokens from it.

## Misc

* The simulator failed to start certain test apps due to unresolvable attributes. This is because there was no mechanism to handle implicit dependencies on startup. A popular solution to this problem is to use an *init queue*. We basically pop a resource from the queue and try to start it. If we can't resolve a reference to other resources, we retry by returning it to the queue. An attempt count is decremented to avoid an infinite loop in case of a dependency cycle or unresolvable attribute (cc @Chriscbr).
* Additionally, the simulator did not call `cleanup()` during the shutdown sequence. This resulted in leaking ports in the `cloud.Api` resource that prevented the program from exiting (not exactly sure how this have worked). @tsuf239, any idea?


## Fixes

* #1878 is fixed because now we are able to lift complete expressions. So `bucket.bucketArn` is a simple string and there is no need to lift the bucket.
* #2729 is fixed with the following error: `Unable to reassign a captured variable`
* #3253

This is the 3rd attempt at this refactor, and hopefully the last for now. It supersedes #3125 and #2909.

## Follow ups

* #3244
* #3249
* #3189

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributing/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
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.