-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: refactor platforms to expose global class factory (#6099)
[Wing Platforms](https://www.winglang.io/docs/concepts/platforms) allow users to substitute in their own class definitions whenever a class is used in Wing code. To generate classes in JavaScript that extend a class defined by a Wing platform, the Wing compiler needs to generate code shaped something like this: ``` class Child extends getParentType() { ... } ``` where `getParentType()` is just some expression that obtains the parent class type from the Wing platform system. Surprisingly, JavaScript allows for putting expressions like function calls here (which is cool). The compiler currently generates some code that no-ops, so classes defined by Wing platforms are never injected. We would like to instead generate code that accesses the current Wing platform, but it isn't possible because the only ways to obtain the platform are through the `App`. To make a fix for this possible, this PR refactors our platform-related code so that we're now exposing the dependency injection factory through the [JavaScript global object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis). Wing generated code can obtain the target-specific version of any class it needs (if an implementation is available) by calling `globalThis.$ClassFactory.new(fqn, scope, id, ...args)`. The unfortunate drawback of this change is that it pollutes the global namespace, and it means only a single Wing platform can be used for synthesizing apps in a given Node process. (If you want to compile apps to two different platforms, you'll likely need multiple node processes). It's possible we could avoid this by making Wing-generated JavaScript exports parameterized -- however this may negatively affect the Wing TypeScript experience by introducing more steps for importing Wing libraries: ``` import { main, cloud } from "@wingcloud/framework"; import initPostgres from "@winglibs/postgres"; main((root) => { const postgres = initPostgres(root); new cloud.Bucket(root, "Bucket"); new postgres.Database(root, "Database"); }); ``` But such an alternative could cause problems if we want to eventually [make Wing libraries into JSII libraries](#6655). BREAKING CHANGE: Several platform-related APIs have been changed. `new()`, `newAbstract()`, `typeForFqn()`, and `tryNew()` methods have been removed from `core.App`. Instead, you can obtain a `ClassFactory` through `ClassFactory.of(scope)`, which has methods like `new()`, `tryNewInstance()` and `resolveType()`. `AppProps` now does not expect `newInstanceOverrides` anymore, and instead expects a `classFactory`. ## Checklist - [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/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 [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
Showing
352 changed files
with
1,944 additions
and
1,947 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
examples/tests/doc_examples/valid/00-cloud-oriented-programming.md_example_1/main.w
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
examples/tests/doc_examples/valid/01-cli.md_example_1/main.w
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
examples/tests/doc_examples/valid/03-platforms.md_example_1/main.w
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
examples/tests/doc_examples/valid/80-singletons.md_example_1/main.w
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
bring cloud; | ||
|
||
let t = new cloud.Topic(); | ||
let q = new cloud.Queue(); | ||
|
||
q.setConsumer(inflight (msg: str) => { | ||
t.publish("msg"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ let q = new cloud.Queue(); | |
|
||
q.setConsumer(inflight (msg: str) => { | ||
b.put("file.txt", msg); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ClassFactory } from "@winglang/sdk/lib/core"; | ||
|
||
declare global { | ||
var $ClassFactory: ClassFactory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.