Skip to content

Commit

Permalink
fix(compiler): cannot use inflight methods from inflight closures def…
Browse files Browse the repository at this point in the history
…ined in class initializer (#3580)

Fixes #3577

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/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 [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
Chriscbr committed Jul 24, 2023
1 parent c132000 commit f7ca799
Show file tree
Hide file tree
Showing 248 changed files with 712 additions and 429 deletions.
15 changes: 15 additions & 0 deletions examples/tests/valid/use_inflight_method_inside_init_closure.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bring cloud;

class Foo {
init() {
new cloud.Function(inflight () => {
this.bar();
});
}

inflight bar() {

}
}

new Foo();
16 changes: 9 additions & 7 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,16 +1003,14 @@ impl<'a> JSifier<'a> {
false
};

let mut body_code = CodeMaker::default();

// we always need a super() call because even if the class doesn't have an explicit parent, it
// will inherit from core.Resource.
if !super_called {
code.line("super(scope, id);");
body_code.line("super(scope, id);");
}

// We must jsify the statements in the constructor before adding any additional code blew
// this is to ensure if there are calls to super constructor within the statements,
// they will be jsified before any attempts to call `this` are made.
code.add_code(self.jsify_scope_body(&init_statements, ctx));
body_code.add_code(self.jsify_scope_body(&init_statements, ctx));

let inflight_fields = class.inflight_fields();
let inflight_methods = class.inflight_methods(true);
Expand All @@ -1030,9 +1028,13 @@ impl<'a> JSifier<'a> {
.chain(inflight_field_names.iter())
.map(|name| format!("\"{}\"", name))
.join(", ");
code.line(format!("this._addInflightOps({inflight_ops_string});"));

// insert as the first statement after the super() call
body_code.insert_line(1, format!("this._addInflightOps({inflight_ops_string});"));
}

code.add_code(body_code);

code.close("}");
code
}
Expand Down
25 changes: 25 additions & 0 deletions libs/wingc/src/jsify/codemaker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ impl CodeMaker {
self.indent += 1;
}

/// Insert a line at the given index.
pub fn insert_line<S: Into<String>>(&mut self, index: usize, line: S) {
// get the indent of the current line at that index
let indent = self.lines.get(index).map(|(indent, _)| *indent).unwrap_or(self.indent);
self.lines.insert(index, (indent, line.into()));
}

pub fn one_line<S: Into<String>>(s: S) -> CodeMaker {
let mut code = CodeMaker::default();
code.line(s);
Expand Down Expand Up @@ -145,4 +152,22 @@ mod tests {
"#}
);
}

#[test]
fn codemaker_insert_line() {
let mut code = CodeMaker::default();
code.open("if true {");
code.line("let b = 2;");
code.close("}");
code.insert_line(1, "let a = 1;");
assert_eq!(
code.to_string(),
indoc! {r#"
if true {
let a = 1;
let b = 2;
}
"#}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class $Root extends $stdlib.std.Resource {
class Base extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.b = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket");
this._addInflightOps("put", "$inflight_init");
this.b = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket");
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class $Root extends $stdlib.std.Resource {
class Base extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.f = "hello";
this._addInflightOps("$inflight_init");
this.f = "hello";
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand All @@ -88,8 +88,8 @@ class $Root extends $stdlib.std.Resource {
class Derived extends Base {
constructor(scope, id, ) {
super(scope, id);
this.g = "world";
this._addInflightOps("$inflight_init");
this.g = "world";
}
foo() {
this.f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class $Root extends $stdlib.std.Resource {
class Base extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.b = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket");
this._addInflightOps("$inflight_init");
this.b = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket");
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class $Root extends $stdlib.std.Resource {
class Base extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.f = x;
this._addInflightOps("$inflight_init");
this.f = x;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/src/jsify/snapshots/builtins.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand All @@ -84,8 +84,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure2 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class $Root extends $stdlib.std.Resource {
class Foo extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.b = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket");
this._addInflightOps("$inflight_init");
this.b = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket");
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand All @@ -89,8 +89,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/src/jsify/snapshots/capture_token.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class $Root extends $stdlib.std.Resource {
class $Closure1 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this.display.hidden = true;
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
Expand Down
Loading

0 comments on commit f7ca799

Please sign in to comment.