Skip to content

Commit

Permalink
refactor(compiler): support safe function calls (#51100)
Browse files Browse the repository at this point in the history
Adds support for safe function calls in template pipeline compiler

PR Close #51100
  • Loading branch information
mmalerba authored and alxhub committed Aug 1, 2023
1 parent 42caeee commit 2d52d5e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@
"files": [
{
"expected": "safe_call_template.js",
"templatePipelineExpected": "safe_call_template.pipeline.js",
"generated": "safe_call.js"
}
],
"failureMessage": "Incorrect template"
}
],
"skipForTemplatePipeline": true
]
},
{
"description": "should handle non-null assertions after a safe access",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
template: function MyApp_Template(rf, $ctx$) {
if (rf & 1) {
$i0$.ɵɵelementStart(0, "span", 0);
$i0$.ɵɵtext(1);
$i0$.ɵɵelementEnd();
}
if (rf & 2) {
let $tmp_4_0$;
let $tmp_5_0$;
$i0$.ɵɵproperty("title", "Your last name is " + (($tmp_4_0$ = $ctx$.person.getLastName == null ? null : $ctx$.person.getLastName()) !== null && $tmp_4_0$ !== undefined ? $tmp_4_0$ : "unknown"));
$i0$.ɵɵadvance(1);
$i0$.ɵɵtextInterpolate2(" Hello, ", $ctx$.person.getName == null ? null : $ctx$.person.getName(), "! You are a Balrog: ", ($ctx$.person.getSpecies == null ? null : ($tmp_5_0$ = $ctx$.person.getSpecies()) == null ? null : ($tmp_5_0$ = $tmp_5_0$()) == null ? null : ($tmp_5_0$ = $tmp_5_0$()) == null ? null : ($tmp_5_0$ = $tmp_5_0$()) == null ? null : $tmp_5_0$()) || "unknown", " ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ function safeTernaryWithTemporary(
}

function isSafeAccessExpression(e: o.Expression): e is ir.SafePropertyReadExpr|
ir.SafeKeyedReadExpr {
return e instanceof ir.SafePropertyReadExpr || e instanceof ir.SafeKeyedReadExpr;
ir.SafeKeyedReadExpr|ir.SafeInvokeFunctionExpr {
return e instanceof ir.SafePropertyReadExpr || e instanceof ir.SafeKeyedReadExpr ||
e instanceof ir.SafeInvokeFunctionExpr;
}

function isUnsafeAccessExpression(e: o.Expression): e is o.ReadPropExpr|o.ReadKeyExpr|
Expand All @@ -128,7 +129,7 @@ function isUnsafeAccessExpression(e: o.Expression): e is o.ReadPropExpr|o.ReadKe
}

function isAccessExpression(e: o.Expression): e is o.ReadPropExpr|ir.SafePropertyReadExpr|
o.ReadKeyExpr|ir.SafeKeyedReadExpr|o.InvokeFunctionExpr {
o.ReadKeyExpr|ir.SafeKeyedReadExpr|o.InvokeFunctionExpr|ir.SafeInvokeFunctionExpr {
return isSafeAccessExpression(e) || isUnsafeAccessExpression(e);
}

Expand All @@ -146,11 +147,6 @@ function deepestSafeTernary(e: o.Expression): ir.SafeTernaryExpr|null {
// TODO: When strict compatibility with TemplateDefinitionBuilder is not required, we can use `&&`
// instead to save some code size.
function safeTransform(e: o.Expression, ctx: SafeTransformContext): o.Expression {
if (e instanceof ir.SafeInvokeFunctionExpr) {
// TODO: Implement safe function calls in a subsequent commit.
return new o.InvokeFunctionExpr(e.receiver, e.args);
}

if (!isAccessExpression(e)) {
return e;
}
Expand All @@ -170,6 +166,10 @@ function safeTransform(e: o.Expression, ctx: SafeTransformContext): o.Expression
dst.expr = dst.expr.key(e.index);
return e.receiver;
}
if (e instanceof ir.SafeInvokeFunctionExpr) {
dst.expr = safeTernaryWithTemporary(dst.expr, (r: o.Expression) => r.callFn(e.args), ctx);
return e.receiver;
}
if (e instanceof ir.SafePropertyReadExpr) {
dst.expr = safeTernaryWithTemporary(dst.expr, (r: o.Expression) => r.prop(e.name), ctx);
return e.receiver;
Expand All @@ -179,6 +179,9 @@ function safeTransform(e: o.Expression, ctx: SafeTransformContext): o.Expression
return e.receiver;
}
} else {
if (e instanceof ir.SafeInvokeFunctionExpr) {
return safeTernaryWithTemporary(e.receiver, (r: o.Expression) => r.callFn(e.args), ctx);
}
if (e instanceof ir.SafePropertyReadExpr) {
return safeTernaryWithTemporary(e.receiver, (r: o.Expression) => r.prop(e.name), ctx);
}
Expand Down

0 comments on commit 2d52d5e

Please sign in to comment.