-
Notifications
You must be signed in to change notification settings - Fork 200
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: generate types for extern
declarations
#5967
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
32026f9
wip
MarkMcCulloh f1ebab4
Merge branch 'main' into mark/ts-inflight
MarkMcCulloh 7cc17e8
Merge branch 'main' into mark/ts-inflight
monadabot 39d3271
chore: self mutation (build.diff)
monadabot fc9c05b
add missing extern
MarkMcCulloh 4e49c60
Merge branch 'main' into mark/ts-inflight
monadabot 953227a
chore: self mutation (build.diff)
monadabot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,58 @@ | ||
--- | ||
title: Using JavaScript | ||
title: Using JavaScript/TypeScript | ||
id: using-javascript | ||
keywords: [Wing example] | ||
keywords: [example, javascript, extern, typescript, js, ts] | ||
--- | ||
|
||
Calling a Javascript function from Wing requires two steps. | ||
Calling a Javascript function from Wing requires two steps. | ||
|
||
First, export the function from Javascript. | ||
|
||
This examples exports `isValidUrl` from a file named`url_utils.js`: | ||
1. Create a .js file that exports some functions | ||
|
||
```js | ||
exports.isValidUrl = function(url) { | ||
try { | ||
new URL(url); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
// util.js | ||
|
||
exports.isValidUrl = function (url) { | ||
return URL.canParse(url); | ||
}; | ||
``` | ||
|
||
### Preflight function | ||
|
||
To call this in preflight code, define the function as an `extern` in a class. | ||
|
||
**Note:** Extern functions must be `static.` | ||
In preflight, this file must be a CommonJS module written in Javascript. Inflight, it may be CJS/ESM and either JavaScript or TypeScript. | ||
|
||
If you want to use the function outside of the class, be sure to declare it as `pub`. | ||
2. Use the `extern` keyword in a class to expose the function to Wing. Note that this must be `static`. It may also be `inflight` | ||
|
||
```ts | ||
class JsExample { | ||
// preflight static | ||
pub extern "./url_utils.js" static isValidUrl(url: str): bool; | ||
```ts | ||
class JsExample { | ||
pub extern "./util.js" static isValidUrl(url: str): bool; | ||
} | ||
|
||
assert(JsExample.isValidUrl("http://www.google.com")); | ||
assert(!JsExample.isValidUrl("X?Y")); | ||
``` | ||
|
||
### Inflight | ||
### Type-safe `extern` | ||
|
||
To call the function inflight, add the `inflight` modifier. | ||
Running `wing compile` will generate a corresponding `.d.ts` file for each `extern`. This file can be imported into the extern file itself to ensure the extern is type-safe. Either your IDE or a separate usage of the TypeScript compiler can provide type-checking. | ||
|
||
```ts | ||
class JsExample { | ||
// inflight static method | ||
extern "./url_utils.js" static inflight isValidUrl(url: str): bool; | ||
} | ||
// util.ts | ||
import type extern from "./util.extern"; | ||
|
||
test "main" { | ||
assert(JsExample.isValidUrl("http://www.google.com")); | ||
assert(!JsExample.isValidUrl("X?Y")); | ||
} | ||
export const isValidUrl: extern["isValidUrl"] = (url) => { | ||
// url is known to be a string and that we must return a boolean | ||
return URL.canParse(url); | ||
}; | ||
``` | ||
|
||
The .d.ts file can also be used in JavaScript via JSDoc comments and can even be applied at a module export level. | ||
|
||
```js | ||
// util.js | ||
/** @type {import("./util.extern").default} */ | ||
module.exports = { | ||
isValidUrl: (url) => { | ||
return URL.canParse(url); | ||
}, | ||
}; | ||
``` | ||
|
||
Coming Soon: The ability to use resources inside an `inflight extern`. See [this issue](https://github.com/winglang/wing/issues/76) for more information. |
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,3 @@ | ||
export default interface extern { | ||
logging: () => Promise<void>, | ||
} |
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,10 @@ | ||
export default interface extern { | ||
createServer: (body: string) => Promise<IHttpServer$Inflight>, | ||
} | ||
export interface Address { | ||
readonly port: number; | ||
} | ||
export interface IHttpServer$Inflight { | ||
readonly address: () => Promise<Address>; | ||
readonly close: () => Promise<void>; | ||
} |
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,3 @@ | ||
export default interface extern { | ||
platform: () => Promise<string>, | ||
} |
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,3 @@ | ||
export default interface extern { | ||
validateUUIDv4: (uuidv4: string) => Promise<boolean>, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default interface extern { | ||
_getItem: (tableName: string, key: Readonly<any>) => Promise<Readonly<any>>, | ||
_putItem: (tableName: string, item: Readonly<any>) => Promise<void>, | ||
} |
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MarkMcCulloh Just checking, so we've decided to officially support *.ts files? Should we note this in the documentation or language spec somewhere? (I'm cool with the choice, just wanna check if we're tracking it somewhere :^))