diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index 21cf7b0be0..331706190f 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -941,7 +941,7 @@ function lowerExpression( } case "OptionalCallExpression": { const expr = exprPath as NodePath; - const optional = expr.get("optional"); + const optional = expr.node.optional; const calleePath = expr.get("callee"); if ( calleePath.isMemberExpression() || diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index 93912893aa..48e0cc034a 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -90,7 +90,8 @@ export type ReactiveValue = | InstructionValue | ReactiveLogicalValue | ReactiveSequenceValue - | ReactiveTernaryValue; + | ReactiveTernaryValue + | ReactiveOptionalCallValue; export type ReactiveLogicalValue = { kind: "LogicalExpression"; @@ -116,6 +117,14 @@ export type ReactiveSequenceValue = { loc: SourceLocation; }; +export type ReactiveOptionalCallValue = { + kind: "OptionalCall"; + id: InstructionId; + call: ReactiveValue; + optional: boolean; + loc: SourceLocation; +}; + export type ReactiveTerminal = | ReactiveBreakTerminal | ReactiveContinueTerminal diff --git a/compiler/forget/src/HIR/PrintHIR.ts b/compiler/forget/src/HIR/PrintHIR.ts index 766192ca08..c602e7701e 100644 --- a/compiler/forget/src/HIR/PrintHIR.ts +++ b/compiler/forget/src/HIR/PrintHIR.ts @@ -440,6 +440,10 @@ export function printInstructionValue(instrValue: ReactiveValue): string { value = `Global ${instrValue.name}`; break; } + case "OptionalCall": { + value = `OptionalCall ${printInstructionValue(instrValue.call)}`; + break; + } default: { assertExhaustive( instrValue, diff --git a/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts index 81499ce6e7..65c063b3df 100644 --- a/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts @@ -29,7 +29,6 @@ import { ReactiveValue, Terminal, } from "../HIR/HIR"; -import todo from "../Utils/todo"; import { assertExhaustive } from "../Utils/utils"; /** @@ -457,6 +456,7 @@ class Driver { break; } + case "optional-call": case "ternary": case "logical": { const fallthroughId = terminal.fallthrough; @@ -482,9 +482,6 @@ class Driver { this.visitBlock(this.cx.ir.blocks.get(fallthroughId)!, blockValue); break; } - case "optional-call": { - todo("BuildReactiveFunction: support optional-call terminal"); - } case "goto": { switch (terminal.variant) { case GotoVariant.Break: { @@ -655,6 +652,31 @@ class Driver { id: InstructionId; } { switch (terminal.kind) { + case "optional-call": { + const test = this.visitValueBlock(terminal.test, terminal.loc); + const testBlock = this.cx.ir.blocks.get(test.block)!; + invariant( + testBlock.terminal.kind === "branch", + "Unexpected terminal kind '%s' for optional call test block", + testBlock.terminal.kind + ); + const consequent = this.visitValueBlock( + testBlock.terminal.consequent, + terminal.loc + ); + return { + place: { ...consequent.place }, + value: { + kind: "OptionalCall", + optional: terminal.optional, + call: consequent.value, + id: terminal.id, + loc: terminal.loc, + }, + fallthrough: terminal.fallthrough, + id: terminal.id, + }; + } case "logical": { const test = this.visitValueBlock(terminal.test, terminal.loc); const testBlock = this.cx.ir.blocks.get(test.block)!; diff --git a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 5fffea9e81..c8a132fd47 100644 --- a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -589,6 +589,20 @@ function codegenInstructionValue( value = createCallExpression(instrValue.loc, callee, args); break; } + case "OptionalCall": { + const call = codegenInstructionValue(cx, instrValue.call); + invariant(call.type === "CallExpression", "Expected a call expression"); + invariant( + t.isExpression(call.callee), + "v8 intrinsics are validated during lowering" + ); + value = t.optionalCallExpression( + call.callee, + call.arguments, + instrValue.optional + ); + break; + } case "MethodCall": { const memberExpr = codegenPlace(cx, instrValue.property); invariant( diff --git a/compiler/forget/src/ReactiveScopes/MergeOverlappingReactiveScopes.ts b/compiler/forget/src/ReactiveScopes/MergeOverlappingReactiveScopes.ts index 9f8ee4cf56..cd96967867 100644 --- a/compiler/forget/src/ReactiveScopes/MergeOverlappingReactiveScopes.ts +++ b/compiler/forget/src/ReactiveScopes/MergeOverlappingReactiveScopes.ts @@ -124,7 +124,8 @@ class Visitor extends ReactiveFunctionVisitor { ): void { if ( instruction.value.kind === "ConditionalExpression" || - instruction.value.kind === "LogicalExpression" + instruction.value.kind === "LogicalExpression" || + instruction.value.kind === "OptionalCall" ) { state.enter(() => { super.visitInstruction(instruction, state); diff --git a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts index d010d94c17..7e6fc285b9 100644 --- a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts +++ b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -444,6 +444,12 @@ function visitReactiveValue( value: ReactiveValue ): void { switch (value.kind) { + case "OptionalCall": { + context.enterConditional(() => { + visitReactiveValue(context, id, value.call); + }); + break; + } case "LogicalExpression": { visitReactiveValue(context, id, value.left); context.enterConditional(() => { diff --git a/compiler/forget/src/ReactiveScopes/PruneNonEscapingScopes.ts b/compiler/forget/src/ReactiveScopes/PruneNonEscapingScopes.ts index 32f36ce364..4a9a997ebe 100644 --- a/compiler/forget/src/ReactiveScopes/PruneNonEscapingScopes.ts +++ b/compiler/forget/src/ReactiveScopes/PruneNonEscapingScopes.ts @@ -508,6 +508,7 @@ function computeMemoizationInputs( rvalues: [value.value], }; } + case "OptionalCall": case "FunctionExpression": case "TaggedTemplateExpression": case "CallExpression": diff --git a/compiler/forget/src/ReactiveScopes/visitors.ts b/compiler/forget/src/ReactiveScopes/visitors.ts index 73bc814667..a7687cf266 100644 --- a/compiler/forget/src/ReactiveScopes/visitors.ts +++ b/compiler/forget/src/ReactiveScopes/visitors.ts @@ -41,6 +41,10 @@ export class ReactiveFunctionVisitor { } traverseValue(id: InstructionId, value: ReactiveValue, state: TState): void { switch (value.kind) { + case "OptionalCall": { + this.visitValue(id, value.call, state); + break; + } case "LogicalExpression": { this.visitValue(id, value.left, state); this.visitValue(id, value.right, state); @@ -277,6 +281,10 @@ export function* eachReactiveValueOperand( instrValue: ReactiveValue ): Iterable { switch (instrValue.kind) { + case "OptionalCall": { + yield* eachReactiveValueOperand(instrValue.call); + break; + } case "LogicalExpression": { yield* eachReactiveValueOperand(instrValue.left); yield* eachReactiveValueOperand(instrValue.right); diff --git a/compiler/forget/src/SSA/LeaveSSA.ts b/compiler/forget/src/SSA/LeaveSSA.ts index d065cd8bd3..26c3b1af10 100644 --- a/compiler/forget/src/SSA/LeaveSSA.ts +++ b/compiler/forget/src/SSA/LeaveSSA.ts @@ -336,7 +336,11 @@ export function leaveSSA(fn: HIRFunction): void { const update = fn.body.blocks.get(terminal.update)!; pushPhis(update); } - if (terminal.kind === "logical" || terminal.kind === "ternary") { + if ( + terminal.kind === "logical" || + terminal.kind === "ternary" || + terminal.kind === "optional-call" + ) { const fallthrough = fn.body.blocks.get(terminal.fallthrough)!; pushPhis(fallthrough); } diff --git a/compiler/forget/yarn.lock b/compiler/forget/yarn.lock index 62d377b5f2..8001987f45 100644 --- a/compiler/forget/yarn.lock +++ b/compiler/forget/yarn.lock @@ -83,12 +83,12 @@ "@jridgewell/gen-mapping" "^0.3.2" jsesc "^2.5.1" -"@babel/generator@^7.21.1": - version "7.21.1" - resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.21.1.tgz#951cc626057bc0af2c35cd23e9c64d384dea83dd" - integrity sha512-1lT45bAYlQhFn/BHivJs43AiW2rg3/UbLyShGfF3C0KmHvO5fSghWd5kBJy30kpRRucGzXStvnnCFniCR2kXAA== +"@babel/generator@^7.21.3": + version "7.21.3" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.21.3.tgz#232359d0874b392df04045d72ce2fd9bb5045fce" + integrity sha512-QS3iR1GYC/YGUnW7IdggFeN5c1poPUurnGttOV/bZgPGV+izC/D8HnD6DLwod0fsatNyVn1G3EVWMYIF0nHbeA== dependencies: - "@babel/types" "^7.21.0" + "@babel/types" "^7.21.3" "@jridgewell/gen-mapping" "^0.3.2" "@jridgewell/trace-mapping" "^0.3.17" jsesc "^2.5.1" @@ -269,11 +269,16 @@ resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.19.3.tgz#8dd36d17c53ff347f9e55c328710321b49479a9a" integrity sha512-pJ9xOlNWHiy9+FuFP09DEAFbAn4JskgRsVcc169w2xRBC3FRGuQEwjeIMMND9L2zc0iEhO/tGv4Zq+km+hxNpQ== -"@babel/parser@^7.20.7", "@babel/parser@^7.21.2": +"@babel/parser@^7.20.7": version "7.21.2" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.21.2.tgz#dacafadfc6d7654c3051a66d6fe55b6cb2f2a0b3" integrity sha512-URpaIJQwEkEC2T9Kn+Ai6Xe/02iNaVCuT/PtoRz3GPVJVDpPd7mLo+VddTbhCRU9TXqW5mSrQfXZyi8kDKOVpQ== +"@babel/parser@^7.21.3": + version "7.21.3" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.21.3.tgz#1d285d67a19162ff9daa358d4cb41d50c06220b3" + integrity sha512-lobG0d7aOfQRXh8AyklEAgZGvA4FShxo6xQbUrrT/cNBPUdIDojlokwJsQyCC/eKia7ifqM0yP+2DRZ4WKw2RQ== + "@babel/plugin-syntax-async-generators@^7.8.4": version "7.8.4" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-generators/-/plugin-syntax-async-generators-7.8.4.tgz#a983fb1aeb2ec3f6ed042a210f640e90e786fe0d" @@ -502,18 +507,18 @@ lodash "^4.17.10" "@babel/traverse@^7.19.1": - version "7.21.2" - resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.21.2.tgz#ac7e1f27658750892e815e60ae90f382a46d8e75" - integrity sha512-ts5FFU/dSUPS13tv8XiEObDu9K+iagEKME9kAbaP7r0Y9KtZJZ+NGndDvWoRAYNpeWafbpFeki3q9QoMD6gxyw== + version "7.21.3" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.21.3.tgz#4747c5e7903d224be71f90788b06798331896f67" + integrity sha512-XLyopNeaTancVitYZe2MlUEvgKb6YVVPXzofHgqHijCImG33b/uTurMS488ht/Hbsb2XK3U2BnSTxKVNGV3nGQ== dependencies: "@babel/code-frame" "^7.18.6" - "@babel/generator" "^7.21.1" + "@babel/generator" "^7.21.3" "@babel/helper-environment-visitor" "^7.18.9" "@babel/helper-function-name" "^7.21.0" "@babel/helper-hoist-variables" "^7.18.6" "@babel/helper-split-export-declaration" "^7.18.6" - "@babel/parser" "^7.21.2" - "@babel/types" "^7.21.2" + "@babel/parser" "^7.21.3" + "@babel/types" "^7.21.3" debug "^4.1.0" globals "^11.1.0" @@ -561,7 +566,7 @@ "@babel/helper-validator-identifier" "^7.19.1" to-fast-properties "^2.0.0" -"@babel/types@^7.21.0", "@babel/types@^7.21.2": +"@babel/types@^7.21.0": version "7.21.2" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.21.2.tgz#92246f6e00f91755893c2876ad653db70c8310d1" integrity sha512-3wRZSs7jiFaB8AjxiiD+VqN5DTG2iRvJGQ+qYFrs/654lg6kGTQWIOFjlBo5RaXuAZjBmP3+OQH4dmhqiiyYxw== @@ -570,6 +575,15 @@ "@babel/helper-validator-identifier" "^7.19.1" to-fast-properties "^2.0.0" +"@babel/types@^7.21.3": + version "7.21.3" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.21.3.tgz#4865a5357ce40f64e3400b0f3b737dc6d4f64d05" + integrity sha512-sBGdETxC+/M4o/zKC0sl6sjWv62WFR/uzxrJ6uYyMLZOUlPnwzw0tKgVHOXxaAd5l2g8pEDM5RZ495GPQI77kg== + dependencies: + "@babel/helper-string-parser" "^7.19.4" + "@babel/helper-validator-identifier" "^7.19.1" + to-fast-properties "^2.0.0" + "@bcoe/v8-coverage@^0.2.3": version "0.2.3" resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"