Skip to content

Commit

Permalink
Merge of #5935
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Mar 19, 2024
2 parents ed54e0c + b359acc commit ac5dcb9
Show file tree
Hide file tree
Showing 48 changed files with 932 additions and 164 deletions.
59 changes: 59 additions & 0 deletions docs/docs/02-concepts/01-preflight-and-inflight.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,65 @@ inflight () => {
};
```

### Lift qualification

Preflight objects referenced inflight are called "lifted" objects:

```js playground
let preflight_str = "hello from preflight";
inflight () => {
log(preflight_str); // `preflight_str` is "lifted" into inflight.
};
```

During the lifting process the compiler tries to figure out in what way the lifted objects are being used.
This is how Winglang generats least privilage permissions. Consider the case of lifting a [`cloud.Bucket`](../04-standard-library/cloud/bucket.md) object:

```js playground
bring cloud;
let bucket = new cloud.Bucket();
new cloud.Function(inflight () => {
bucket.put("key", "value"); // `bucket` is lifted and `put` is being used on it
});
```

In this example the compiler generates the correct _write_ access permissions for the [`cloud.Function`](../04-standard-library/cloud/function.md) on `bucket` based on the fact we're `put`ing into it. We say `bucket`'s lift is qualified with `put`.

#### Explicit lift qualification
In some cases the compiler can't figure out (yet) the lift qualifications, and therefore will report an error:

```js playground
bring cloud;
let main_bucket = new cloud.Bucket() as "main";
let secondary_bucket = new cloud.Bucket() as "backup";
let use_main = true;
new cloud.Function(inflight () => {
let var b = main_bucket;
if !use_main {
b = secondary_bucket;
}
b.put("key", "value"); // Error: the compiler doesn't know the possible values for `b` and therefore can't qualify the lift.
});
```

To explicitly qualify lifts in an inflight closure or inflight method and supress the above compiler error use the `lift()` utility function:

```js playground
bring cloud;
let main_bucket = new cloud.Bucket() as "main";
let secondary_bucket = new cloud.Bucket() as "backup";
let use_main = true;
new cloud.Function(inflight () => {
lift(main_bucket, ["put"]); // Explicitly sate the "put" may be used on `main_bucket`
lift(secondary_bucket, ["put"]); // Explicitly sate the "put" may be used on `secondary_bucket`
let var b = main_bucket;
if !use_main {
b = secondary_bucket;
}
b.put("key", "value"); // Error is supressed and all possible values of `b` were explicitly qualified with "put"
});
```

## Phase-independent code

The global functions `log`, `assert`, and `throw` can all be used in both preflight and inflight code.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/03-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ log("UTC: {t1.utc.toIso())}"); // output: 2023-02-09T06:21:03.000Z
| `assert` | checks a condition and _throws_ if evaluated to false |
| `unsafeCast` | cast a value into a different type |
| `nodeof` | obtain the [tree node](./02-concepts/02-application-tree.md) of a preflight object |
| `lift` | explicitly qualify a [lift](./02-concepts/01-preflight-and-inflight.md#explicit-lift-qualification) of a preflight object |
> ```TS
> log("Hello {name}");
Expand Down
40 changes: 40 additions & 0 deletions examples/tests/invalid/explicit_lift_qualification.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
bring cloud;

let bucket = new cloud.Bucket();

let prelight_string = "hi";

class Foo {
pub inflight mehtod1() {
let b = bucket;
lift(b, ["put"]); // Explicit qualification with inflight object, lift call as non first statement
// ^ Expected a preflight object as first argument to `lift` builtin, found inflight expression instead
//^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

lift(prelight_string, ["contains"]); // Explicit qualification on preflight non-class
// ^^^^^^^^^^^^^^^ Expected type to be "Resource", but got "str" instead
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

let inflight_qualifier = "delete";
lift(bucket, [inflight_qualifier]); // Explicit qualification with inflight qualifiers, lift call as non first statement
// ^^^^^^^^^^^^^^^^^^^^ Qualification list must not contain any inflight elements
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

let inner_closure = () => {
lift(bucket, ["get"]); // lift() call in inner closure
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight
};
class Bar {
pub inflight method() {
lift(bucket, ["get"]); // lift() call in inner class
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight
}
}
}

pub inflight method2() {
let b = bucket;
b.put("k", "v"); // With no explicit qualification this should be an error
//^ Expression of type "Bucket" references an unknown preflight object
}
}
43 changes: 43 additions & 0 deletions examples/tests/valid/explicit_lift_qualification.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
bring cloud;

let bucket = new cloud.Bucket();
bucket.addObject("k", "value");

let put_and_list = ["put", "list"];

class Foo {
pub inflight mehtod() {
lift(bucket, put_and_list); // Qualify `bucket` with a preflight expression
lift(bucket, ["delete"]); // Qualify `bucket` with `delete` via literal
let b = bucket; // Assign `bucket` to an inflight variable

// `put` should work on `b` since we explicitly qualified `bucket` with `put`
// no error generated here because of use of `lift()` in this method
b.put("k2", "value2");

// validate `put` worked and that we can also `list`
assert(b.list() == ["k", "k2"]);

// Validate `delete` works
b.delete("k2");
assert(bucket.tryGet("k2") == nil);
}
}

let foo = new Foo();

test "explicit method lift qualification" {
foo.mehtod();
}

// Similar to the above test, but using a closure
let inflight_closure = inflight () => {
lift(bucket, ["put"]);
let b = bucket;
b.put("k3", "value3"); // Use inflight expression to access explicitly qualified `bucket`
assert(bucket.get("k3") == "value3");
};

test "explicit closure lift qualification" {
inflight_closure();
}
31 changes: 0 additions & 31 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,37 +304,6 @@ pub struct Stmt {
pub idx: usize,
}

#[derive(Debug)]
pub enum UtilityFunctions {
Log,
Assert,
UnsafeCast,
Nodeof,
}

impl UtilityFunctions {
/// Returns all utility functions.
pub fn all() -> Vec<UtilityFunctions> {
vec![
UtilityFunctions::Log,
UtilityFunctions::Assert,
UtilityFunctions::UnsafeCast,
UtilityFunctions::Nodeof,
]
}
}

impl Display for UtilityFunctions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UtilityFunctions::Log => write!(f, "log"),
UtilityFunctions::Assert => write!(f, "assert"),
UtilityFunctions::UnsafeCast => write!(f, "unsafeCast"),
UtilityFunctions::Nodeof => write!(f, "nodeof"),
}
}
}

#[derive(Debug)]
pub struct ElifBlock {
pub condition: Expr,
Expand Down
11 changes: 9 additions & 2 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,8 +1855,15 @@ impl<'a> JSifier<'a> {
for (method_name, method_qual) in lift_qualifications {
bind_method.open(format!("\"{method_name}\": [",));
for (code, method_lift_qual) in method_qual {
let ops_strings = method_lift_qual.ops.iter().map(|op| format!("\"{}\"", op)).join(", ");
bind_method.line(format!("[{code}, [{ops_strings}]],",));
let ops = method_lift_qual.ops.iter().join(", ");
// To keep the code concise treat no ops, single op and multiple ops differenly here, although the multiple ops is the generic case
if method_lift_qual.ops.len() == 0 {
bind_method.line(format!("[{code}, []],"));
} else if method_lift_qual.ops.len() == 1 {
bind_method.line(format!("[{code}, {ops}],"));
} else {
bind_method.line(format!("[{code}, [].concat({ops})],"));
}
}
bind_method.close("],");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"put": [
[this.b, ["list", "put"]],
[this.b, [].concat(["put"], ["list"])],
],
"$inflight_init": [
[this.b, []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"handle": [
[b, ["list", "put"]],
[b, [].concat(["put"], ["list"])],
],
"$inflight_init": [
[b, []],
Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/src/jsify/snapshots/fail_unqualified_lift.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 7:6
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 7:6
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 6:6
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 6:6
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Bucket" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 6:6
Expression of type "Bucket" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 6:6
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: libs/wingc/src/jsify/tests.rs
---
## Errors
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 11:6
Expression of type "Queue" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 11:6
2 changes: 1 addition & 1 deletion libs/wingc/src/jsify/snapshots/preflight_object.snap
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"handle": [
[pf_obj, ["goodbye", "hello"]],
[pf_obj, [].concat(["hello"], ["goodbye"])],
],
"$inflight_init": [
[pf_obj, []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class $Root extends $stdlib.std.Resource {
get _liftMap() {
return ({
"handle": [
[b, ["list", "put"]],
[b, [].concat(["list"], ["put"])],
],
"$inflight_init": [
[b, []],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ source: libs/wingc/src/jsify/tests.rs
---
## Errors
Can't access preflight member "x" on inflight instance of type "A" 9:14
Expression of type "A" references an unknown preflight object, can't qualify its capabilities (see https://github.com/winglang/wing/issues/76 for details) 9:12
Expression of type "A" references an unknown preflight object, can't qualify its capabilities. Use `lift()` to explicitly qualify the preflight object to disable this error. 9:12
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class $Root extends $stdlib.std.Resource {
[this.s, ["length"]],
],
"bam": [
[this.b, ["get", "put"]],
[this.b, [].concat(["put"], ["get"])],
],
"$inflight_init": [
[this.b, []],
Expand Down
Loading

0 comments on commit ac5dcb9

Please sign in to comment.