Skip to content

Commit

Permalink
Merge branch 'main' into mark/turbo-2
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMcCulloh authored Jul 21, 2024
2 parents f23940f + c94661c commit 3c9d4f7
Show file tree
Hide file tree
Showing 285 changed files with 1,864 additions and 366 deletions.
14 changes: 11 additions & 3 deletions apps/wing/src/commands/pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,18 @@ describe("wing pack", () => {
await extractTarball(join(outdir, tarballPath), outdir);

// symlink node_modules/@winglang/sdk to our version of the sdk so the import works
await fs.mkdir(join(outdir, "package", "node_modules", "@winglang"), { recursive: true });
await fs.mkdir(join(outdir, "package", "node_modules", "@winglang", "sdk", "lib"), {
recursive: true,
});

await fs.symlink(
require.resolve("@winglang/sdk/lib/index.js"),
join(outdir, "package", "node_modules", "@winglang", "sdk", "index.js")
);

await fs.symlink(
require.resolve("@winglang/sdk"),
join(outdir, "package", "node_modules", "@winglang", "sdk")
require.resolve("@winglang/sdk/lib/macros.js"),
join(outdir, "package", "node_modules", "@winglang", "sdk", "lib", "macros.js")
);

const packagePath = join(outdir, "package");
Expand Down
4 changes: 2 additions & 2 deletions docs/api/04-standard-library/std/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ String.
| <code><a href="#@winglang/sdk.std.String.endsWith">endsWith</a></code> | Does this string end with the given searchString? |
| <code><a href="#@winglang/sdk.std.String.indexOf">indexOf</a></code> | Returns the index of the first occurrence of searchString found. |
| <code><a href="#@winglang/sdk.std.String.lowercase">lowercase</a></code> | Returns this string in lower case. |
| <code><a href="#@winglang/sdk.std.String.replace">replace</a></code> | Replaces the first occurence of a substring within a string. |
| <code><a href="#@winglang/sdk.std.String.replace">replace</a></code> | Replaces the first occurrence of a substring within a string. |
| <code><a href="#@winglang/sdk.std.String.replaceAll">replaceAll</a></code> | Replaces all occurrences of a substring within a string. |
| <code><a href="#@winglang/sdk.std.String.split">split</a></code> | Splits string by separator. |
| <code><a href="#@winglang/sdk.std.String.startsWith">startsWith</a></code> | Does this string start with the given searchString? |
Expand Down Expand Up @@ -126,7 +126,7 @@ Returns this string in lower case.
replace(searchString: str, replaceString: str): str
```

Replaces the first occurence of a substring within a string.
Replaces the first occurrence of a substring within a string.

###### `searchString`<sup>Required</sup> <a name="searchString" id="@winglang/sdk.std.String.replace.parameter.searchString"></a>

Expand Down
4 changes: 2 additions & 2 deletions examples/tests/sdk_tests/function/aws-function.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let fn = new cloud.Function(inflight (msg: Json?) => {

let fnInfo = getFunctionInfo(fn);

test "AWS Function" {
new std.Test(inflight () => {
if let info = fnInfo {
if target == "tf-aws" {
assert(info.get("functionArn").contains("arn:aws:lambda:"));
Expand Down Expand Up @@ -73,4 +73,4 @@ test "AWS Function" {
msg = err;
}
expect.ok(msg.contains("fake error"), "Expected fake error message");
}
}, timeout: 3m) as "AWS Function";
91 changes: 91 additions & 0 deletions examples/tests/valid/chaining_macros.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
bring expect;
bring math;

struct Result {
item: Json?;
items: Array<Json>?;
mapItem: Map<str>?;
mapItems: Array<Map<str>>?;
setItem: Set<num>?;
setItems: Array<Set<num>>?;
structItem: Result?;
structItems: Array<Result>?;
}

let result = Result {};

expect.equal(result.item?.tryGet("id"), nil);
expect.equal(result.item?.has("id"), nil);

expect.equal(result.items?.tryAt(0)?.tryGet("id"), nil);
expect.equal(result.items?.tryAt(0)?.has("id"), nil);

expect.equal(result.mapItem?.tryGet("a"), nil);
expect.equal(result.mapItem?.has("id"), nil);

expect.equal(result.mapItems?.tryAt(0)?.tryGet("id"), nil);
expect.equal(result.mapItems?.tryAt(0)?.has("id"), nil);

expect.equal(result.setItem?.size, nil);
expect.equal(result.setItem?.has(6), nil);

expect.equal(result.setItems?.tryAt(0)?.size, nil);
expect.equal(result.setItems?.tryAt(0)?.has(6), nil);

expect.equal(result.structItem?.item, nil);
expect.equal(result.structItems?.tryAt(0)?.item, nil);

let var calls = 0;

let makeArray = (): Array<num> => {
calls = calls + 1;
return [1, 2, 3];
};

expect.ok(makeArray()?.contains(2) ?? false);
expect.equal(calls, 1);

test "optional chaining macros" {
let result = Result {};

expect.equal(result.item?.tryGet("id"), nil);
expect.equal(result.item?.has("id"), nil);

expect.equal(result.items?.tryAt(0)?.tryGet("id"), nil);
expect.equal(result.items?.tryAt(0)?.has("id"), nil);

expect.equal(result.mapItem?.tryGet("a"), nil);
expect.equal(result.mapItem?.has("id"), nil);

expect.equal(result.mapItems?.tryAt(0)?.tryGet("id"), nil);
expect.equal(result.mapItems?.tryAt(0)?.has("id"), nil);

expect.equal(result.setItem?.size, nil);
expect.equal(result.setItem?.has(6), nil);

expect.equal(result.setItems?.tryAt(0)?.size, nil);
expect.equal(result.setItems?.tryAt(0)?.has(6), nil);

expect.equal(result.structItem?.item, nil);
expect.equal(result.structItems?.tryAt(0)?.item, nil);

let var calls = 0;

let makeArray = (): Array<num> => {
calls = calls + 1;
return [1, 2, 3];
};

expect.ok(makeArray()?.contains(2) ?? false);
expect.equal(calls, 1);
}


test "nesting and chaining" {
let characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let randomChar = characters.at(math.floor(math.random() * characters.length));
// could not assert a real random in snapshot test so duplicated with fixed value
let fixedChar = characters.at(math.floor(0.67 * characters.length));
expect.equal(fixedChar, "Y");

}
5 changes: 5 additions & 0 deletions libs/wingc/src/dtsify/snapshots/declarations.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub class Child extends ParentClass impl ClassInterface {}
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ $ParentClass }) {
class Child extends $ParentClass {
constructor({ }) {
Expand All @@ -65,6 +66,7 @@ module.exports = function({ $ParentClass }) {
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ }) {
class InflightClass {
async somethingFun() {
Expand All @@ -80,6 +82,7 @@ module.exports = function({ }) {
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ $InflightClass }) {
class ParentClass {
constructor({ }) {
Expand All @@ -102,6 +105,7 @@ module.exports = function({ $InflightClass }) {
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const std = $stdlib.std;
const $helpers = $stdlib.helpers;
const $extern = $helpers.createExternRequire(__dirname);
Expand All @@ -122,6 +126,7 @@ export * from "./preflight.lib-1.cjs"
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const std = $stdlib.std;
const $helpers = $stdlib.helpers;
const $extern = $helpers.createExternRequire(__dirname);
Expand Down
3 changes: 3 additions & 0 deletions libs/wingc/src/dtsify/snapshots/optionals.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub class ParentClass impl ClassInterface {
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ }) {
class ParentClass {
constructor({ }) {
Expand All @@ -43,6 +44,7 @@ module.exports = function({ }) {
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const std = $stdlib.std;
const $helpers = $stdlib.helpers;
const $extern = $helpers.createExternRequire(__dirname);
Expand All @@ -63,6 +65,7 @@ export * from "./preflight.lib-1.cjs"
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const std = $stdlib.std;
const $helpers = $stdlib.helpers;
const $extern = $helpers.createExternRequire(__dirname);
Expand Down
33 changes: 28 additions & 5 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const ENV_WING_IS_TEST: &str = "$wing_is_test";
const OUTDIR_VAR: &str = "$outdir";
const PLATFORMS_VAR: &str = "$platforms";
const HELPERS_VAR: &str = "$helpers";
const MACROS_VAR: &str = "$macros";
const EXTERN_VAR: &str = "$extern";

const ROOT_CLASS: &str = "$Root";
Expand Down Expand Up @@ -173,6 +174,7 @@ impl<'a> JSifier<'a> {
output.line("\"use strict\";");

output.line(format!("const {STDLIB} = require('{STDLIB_MODULE}');"));
output.line(format!("const {MACROS_VAR} = require(\"@winglang/sdk/lib/macros\");"));

if is_entrypoint {
output.line(format!(
Expand Down Expand Up @@ -890,14 +892,20 @@ impl<'a> JSifier<'a> {
args_text_string = args_text_string[1..args_text_string.len() - 1].to_string();
}
let args_text_string = escape_javascript_string(&args_text_string);
let mut is_optional = false;

if let Some(function_sig) = function_sig {
if let Some(js_override) = &function_sig.js_override {
let self_string = match callee {
CalleeKind::Expr(expr) => match &expr.kind {
// for "loose" macros, e.g. `print()`, $self$ is the global object
ExprKind::Reference(Reference::Identifier(_)) => "global".to_string(),
ExprKind::Reference(Reference::InstanceMember { object, .. }) => {
ExprKind::Reference(Reference::InstanceMember {
object,
optional_accessor,
..
}) => {
is_optional = *optional_accessor;
self.jsify_expression(&object, ctx).to_string()
}
ExprKind::Reference(Reference::TypeMember { property, .. }) => {
Expand All @@ -915,10 +923,24 @@ impl<'a> JSifier<'a> {
"this".to_string()
}
};
let patterns = &[MACRO_REPLACE_SELF, MACRO_REPLACE_ARGS, MACRO_REPLACE_ARGS_TEXT];
let replace_with = &[self_string, args_string, args_text_string];
let ac = AhoCorasick::new(patterns).expect("Failed to create macro pattern");
return new_code!(expr_span, ac.replace_all(js_override, replace_with));
if function_sig.is_macro {
return new_code!(
expr_span,
format!(
"{}.{}({}, {}, {})",
MACROS_VAR,
js_override,
is_optional.to_string(),
self_string,
args_string
)
);
} else {
let patterns = &[MACRO_REPLACE_SELF, MACRO_REPLACE_ARGS, MACRO_REPLACE_ARGS_TEXT];
let replace_with = &[self_string, args_string, args_text_string];
let ac = AhoCorasick::new(patterns).expect("Failed to create macro pattern");
return new_code!(expr_span, ac.replace_all(js_override, replace_with));
}
}

// If this function requires an implicit scope argument, we need to add it to the args string
Expand Down Expand Up @@ -2045,6 +2067,7 @@ impl<'a> JSifier<'a> {

code.line("\"use strict\";");
code.line(format!("const {HELPERS_VAR} = require(\"@winglang/sdk/lib/helpers\");"));
code.line(format!("const {MACROS_VAR} = require(\"@winglang/sdk/lib/macros\");"));
code.open(format!("module.exports = function({{ {inputs} }}) {{"));
code.add_code(inflight_class_code);
code.line(format!("return {name};"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ source: libs/wingc/src/jsify/tests.rs
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ $x, $y }) {
class $Closure1 {
constructor({ }) {
Expand All @@ -29,7 +30,7 @@ module.exports = function({ $x, $y }) {
}
async handle() {
$helpers.assert($helpers.eq($x.length, 3), "x.length == 3");
$helpers.assert($helpers.eq(((arr, index) => { if (index < 0 || index >= arr.length) throw new Error("Index out of bounds"); return arr[index]; })($y, 0), "hello"), "y.at(0) == \"hello\"");
$helpers.assert($helpers.eq($macros.__Array_at(false, $y, 0), "hello"), "y.at(0) == \"hello\"");
}
}
return $Closure1;
Expand All @@ -42,6 +43,7 @@ module.exports = function({ $x, $y }) {
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const $platforms = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLATFORMS);
const $outdir = process.env.WING_SYNTH_DIR ?? ".";
const $wing_is_test = process.env.WING_IS_TEST === "true";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ source: libs/wingc/src/jsify/tests.rs
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ $s }) {
class $Closure1 {
constructor({ }) {
Expand All @@ -39,6 +40,7 @@ module.exports = function({ $s }) {
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const $platforms = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLATFORMS);
const $outdir = process.env.WING_SYNTH_DIR ?? ".";
const $wing_is_test = process.env.WING_IS_TEST === "true";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ source: libs/wingc/src/jsify/tests.rs
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ $s }) {
class $Closure1 {
constructor({ }) {
Expand All @@ -26,7 +27,7 @@ module.exports = function({ $s }) {
return $obj;
}
async handle() {
$helpers.assert($helpers.eq(((obj, key) => { if (!(key in obj)) throw new Error(`Map does not contain key: "${key}"`); return obj[key]; })($s, "hello").length, 3), "s.get(\"hello\").length == 3");
$helpers.assert($helpers.eq($macros.__Map_get(false, $s, "hello").length, 3), "s.get(\"hello\").length == 3");
}
}
return $Closure1;
Expand All @@ -39,6 +40,7 @@ module.exports = function({ $s }) {
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const $platforms = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLATFORMS);
const $outdir = process.env.WING_SYNTH_DIR ?? ".";
const $wing_is_test = process.env.WING_IS_TEST === "true";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ source: libs/wingc/src/jsify/tests.rs
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ $Foo }) {
class Bar extends $Foo {
constructor({ }) {
Expand All @@ -38,6 +39,7 @@ module.exports = function({ $Foo }) {
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ }) {
class Baz {
constructor({ }) {
Expand All @@ -53,6 +55,7 @@ module.exports = function({ }) {
```js
"use strict";
const $helpers = require("@winglang/sdk/lib/helpers");
const $macros = require("@winglang/sdk/lib/macros");
module.exports = function({ }) {
class Foo {
constructor({ }) {
Expand All @@ -68,6 +71,7 @@ module.exports = function({ }) {
```js
"use strict";
const $stdlib = require('@winglang/sdk');
const $macros = require("@winglang/sdk/lib/macros");
const $platforms = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLATFORMS);
const $outdir = process.env.WING_SYNTH_DIR ?? ".";
const $wing_is_test = process.env.WING_IS_TEST === "true";
Expand Down
Loading

0 comments on commit 3c9d4f7

Please sign in to comment.