Skip to content

Commit

Permalink
compilerEs: refactor code generation for once methods
Browse files Browse the repository at this point in the history
Initialize once methods to undefined instead of "_once_". The compiler is generating
synthetic code for the handling of once fields/methods so we trap the few areas where the
literal "_once_" is being used and change it to undefined.
  • Loading branch information
Matthew Giannini authored and Matthew Giannini committed Jul 12, 2024
1 parent 16ba22f commit 0d84906
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/compilerEs/fan/ast/JsExpr.fan
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,19 @@ class JsExpr : JsNode
leave := x.leave
isAssign := x.assignTarget != null

// convert binary expressions assigning or testing for equivalence to
// synthetic str "_once_" to undefined
if (lhs.expr is FieldExpr)
{
fe := (FieldExpr)lhs.expr
if (rhs.expr.id === ExprId.strLiteral && rhs.expr->val == "_once_")
{
lhs.write
js.w(" ${symbol} undefined", loc)
return
}
}

if (isAssign && lhs.expr is FieldExpr)
{
fe := (FieldExpr)lhs.expr
Expand Down
3 changes: 3 additions & 0 deletions src/compilerEs/fan/ast/JsType.fan
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ class JsType : JsNode

private static Str fieldDefVal(FieldDef f)
{
// initialize once fields to undefined
if (f.isOnce) return "undefined"

defVal := "null"
fieldType := f.fieldType
if (!fieldType.isNullable)
Expand Down

0 comments on commit 0d84906

Please sign in to comment.