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 use inflight methods from inflight closures defined in class initializer #3580

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading