Skip to content

Commit

Permalink
chore: Implement an implicit operator for creating normal completions
Browse files Browse the repository at this point in the history
Whenever the spec tells us to return a value for a function that returns
a completion, it wants us to wrap that value in a normal completion.

Before we had to manually make a normal completion using the
NormalCompletion static functions.

However, it is much better to have an implicit operator that will call
NormalCompletion for us and makes the code a lot more readable.
  • Loading branch information
PrestonLTaylor committed Apr 3, 2024
1 parent 8242014 commit ea1573d
Show file tree
Hide file tree
Showing 46 changed files with 129 additions and 126 deletions.
2 changes: 1 addition & 1 deletion JSS.Lib/AST/BitwiseNotExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ override public Completion Evaluate(VM vm)
{
// a. Return Number::bitwiseNOT(oldValue).
var asNumber = oldValue.Value.AsNumber();
return Completion.NormalCompletion(Number.BitwiseNOT(asNumber));
return Number.BitwiseNOT(asNumber);
}
// 4. Else,
else
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/CallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private Completion ArgumentListEvaluation(VM vm)
precedingArgs.Add(arg.Value);
}

return Completion.NormalCompletion(precedingArgs);
return precedingArgs;
}

public IExpression Lhs { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/ComputedPropertyExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private Completion EvaluatePropertyAccessWithExpressionKey(VM vm, Value baseValu

// 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: EMPTY }.
var propertyString = propertyKey.Value.AsString();
return Completion.NormalCompletion(Reference.Resolvable(baseValue, propertyString.Value, Empty.The));
return Reference.Resolvable(baseValue, propertyString.Value, Empty.The);
}

public IExpression Lhs { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/ConstDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ override public Completion Evaluate(VM vm)
MUST(asReference.InitializeReferencedBinding(value.Value));

// 6. Return EMPTY.
return Completion.NormalCompletion(Empty.The);
return Empty.The;
}

public string Identifier { get; }
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/AST/DebuggerStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public override Completion Evaluate(VM vm)
Debugger.Break();

// b. Return a new implementation-defined Completion Record.
return Completion.NormalCompletion(Empty.The);
return Empty.The;
}

// 2. Else,
// a. Return EMPTY.
return Completion.NormalCompletion(Empty.The);
return Empty.The;
}
}
2 changes: 1 addition & 1 deletion JSS.Lib/AST/DoWhileStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ override public Completion Evaluate(VM vm)
// f. If ToBoolean(exprValue) is false, return V.
if (!exprValue.Value.ToBoolean().Value)
{
return Completion.NormalCompletion(V);
return V;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/EmptyStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ internal sealed class EmptyStatement : INode
override public Completion Evaluate(VM vm)
{
// 1. Return EMPTY.
return Completion.NormalCompletion(Empty.The);
return Empty.The;
}
}
2 changes: 1 addition & 1 deletion JSS.Lib/AST/ForStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private Completion ForBodyEvaluation(VM vm)
// iii. If ToBoolean(testValue) is false, return V.
if (!testValue.Value.ToBoolean().Value)
{
return Completion.NormalCompletion(V);
return V;
}
}

Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/FunctionDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FunctionObject InstantiateFunctionObject(Environment env)
override public Completion Evaluate(VM vm)
{
// 1. Return EMPTY.
return Completion.NormalCompletion(Empty.The);
return Empty.The;
}

public string Identifier { get; }
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/AST/GreaterThanEqualsExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ override public Completion Evaluate(VM vm)
if (r.IsAbruptCompletion()) return r;

// 6. If r is either true or undefined, return false. Otherwise, return true.
if (r.Value.IsUndefined()) return Completion.NormalCompletion(new Boolean(false));
if (r.Value.IsUndefined()) return new Boolean(false);

var rAsBoolean = r.Value.AsBoolean();
return Completion.NormalCompletion(new Boolean(!rAsBoolean.Value));
return new Boolean(!rAsBoolean.Value);
}

public IExpression Lhs { get; }
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/AST/GreaterThanExpreesion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ override public Completion Evaluate(VM vm)
if (r.IsAbruptCompletion()) return r;

// 6. If r is undefined, return false. Otherwise, return r.
if (r.Value.IsUndefined()) return Completion.NormalCompletion(new Boolean(false));
if (r.Value.IsUndefined()) return new Boolean(false);

var rAsBoolean = r.Value.AsBoolean();
return Completion.NormalCompletion(new Boolean(rAsBoolean.Value));
return new Boolean(rAsBoolean.Value);
}

public IExpression Lhs { get; }
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/AST/IExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static public Completion ApplyStringOrNumericBinaryOperator(Value lval, BinaryOp
var lstrValue = lstr.Value.AsString();
var rstrValue = rstr.Value.AsString();
var concatenation = lstrValue.Value + rstrValue.Value;
return Completion.NormalCompletion(new String(concatenation));
return new String(concatenation);
}

// d. Set lval to lprim.
Expand Down Expand Up @@ -99,7 +99,7 @@ static public Completion ApplyStringOrNumericBinaryOperator(Value lval, BinaryOp
};

var result = operation(lnum.Value.AsNumber(), rnum.Value.AsNumber());
return Completion.NormalCompletion(result);
return result;
}

// 13.15.4 EvaluateStringOrNumericBinaryExpression ( leftOperand, opText, rightOperand )
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/IfStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private Completion EvaluateWithoutElse(VM vm)
if (!exprValue.Value)
{
// a. Return undefined.
return Completion.NormalCompletion(Undefined.The);
return Undefined.The;
}
// 4. Else,
else
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/AST/LessThanEqualsExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ override public Completion Evaluate(VM vm)
if (r.IsAbruptCompletion()) return r;

// 6. If r is either true or undefined, return false. Otherwise, return true.
if (r.Value.IsUndefined()) return Completion.NormalCompletion(new Boolean(false));
if (r.Value.IsUndefined()) return new Boolean(false);

var rAsBoolean = r.Value.AsBoolean();
return Completion.NormalCompletion(new Boolean(!rAsBoolean.Value));
return new Boolean(!rAsBoolean.Value);
}

public IExpression Lhs { get; }
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/AST/LessThanExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ override public Completion Evaluate(VM vm)
if (r.IsAbruptCompletion()) return r;

// 6. If r is undefined, return false. Otherwise, return r.
if (r.Value.IsUndefined()) return Completion.NormalCompletion(new Boolean(false));
if (r.Value.IsUndefined()) return new Boolean(false);

var rAsBoolean = r.Value.AsBoolean();
return Completion.NormalCompletion(new Boolean(rAsBoolean.Value));
return new Boolean(rAsBoolean.Value);
}

public IExpression Lhs { get; }
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/AST/LetDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private Completion EvaluateWithoutInitializer(VM vm)
MUST(asReference.InitializeReferencedBinding(Undefined.The));

// 3. Return EMPTY.
return Completion.NormalCompletion(Empty.The);
return Empty.The;
}

private Completion EvaluateWithInitializer(VM vm)
Expand All @@ -69,7 +69,7 @@ private Completion EvaluateWithInitializer(VM vm)
MUST(asReference.InitializeReferencedBinding(value.Value));

// 6. Return EMPTY.
return Completion.NormalCompletion(Empty.The);
return Empty.The;
}

public string Identifier { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/Literal/BooleanLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ override public Completion Evaluate(VM _)
{
// 1. If BooleanLiteral is the token false, return false.
// 2. If BooleanLiteral is the token true, return true.
return Completion.NormalCompletion(_value);
return _value;
}

public bool Value
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/Literal/NullLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ internal sealed class NullLiteral : IExpression
override public Completion Evaluate(VM vm)
{
// 1. Return null.
return Completion.NormalCompletion(Null.The);
return Null.The;
}
}
2 changes: 1 addition & 1 deletion JSS.Lib/AST/Literal/NumericLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public NumericLiteral(double value)
override public Completion Evaluate(VM _)
{
// 1. Return the NumericValue of NumericLiteral as defined in 12.9.3.
return Completion.NormalCompletion(_value);
return _value;
}

public double Value
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/Literal/ObjectLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ override public Completion Evaluate(VM vm)
{
// FIXME: Implement the rest of the evaluation when we parse property definitions
// 1. Return OrdinaryObjectCreate(%Object.prototype%).
return Completion.NormalCompletion(new Object(ObjectPrototype.The));
return new Object(ObjectPrototype.The);
}
}
2 changes: 1 addition & 1 deletion JSS.Lib/AST/Literal/StringLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public StringLiteral(string value)
override public Completion Evaluate(VM _)
{
// 1. Return the SV of StringLiteral as defined in 12.9.4.2.
return Completion.NormalCompletion(_value);
return _value;
}

public string Value
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/LogicalNotExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ override public Completion Evaluate(VM vm)

// 3. If oldValue is true, return false.
// 4. Return true.
return Completion.NormalCompletion(new Boolean(!oldValue.Value));
return new Boolean(!oldValue.Value);
}

public IExpression Expression { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/LooseInequalityExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ override public Completion Evaluate(VM vm)

// 6. If r is true, return false. Otherwise, return true.
var rAsBoolean = r.Value.AsBoolean().Value;
return Completion.NormalCompletion(new Boolean(!rAsBoolean));
return new Boolean(!rAsBoolean);
}

public IExpression Lhs { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/PrefixDecrementExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ override public Completion Evaluate(VM vm)
if (putResult.IsAbruptCompletion()) return putResult;

// 6. Return newValue.
return Completion.NormalCompletion(newValue);
return newValue;
}

public IExpression Expression { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/PrefixIncrementExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override Completion Evaluate(VM vm)
if (putResult.IsAbruptCompletion()) return putResult;

// 6. Return newValue.
return Completion.NormalCompletion(newValue);
return newValue;
}

public IExpression Expression { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/PropertyExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ override public Completion Evaluate(VM vm)
// FIXME: 3. If the source text matched by this MemberExpression is strict mode code, let strict be true; else let strict be false.

// 4. Return EvaluatePropertyAccessWithIdentifierKey(baseValue, IdentifierName, strict).
return Completion.NormalCompletion(EvaluatePropertyAccessWithIdentifierKey(baseValue.Value));
return EvaluatePropertyAccessWithIdentifierKey(baseValue.Value);
}

// 13.3.4 EvaluatePropertyAccessWithIdentifierKey, https://tc39.es/ecma262/#sec-evaluate-property-access-with-identifier-key
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/StatementList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public StatementList(List<INode> statements)
override public Completion Evaluate(VM vm)
{
// 1. Let sl be ? Evaluation of StatementList.
Completion completion = Completion.NormalCompletion(Empty.The);
Completion completion = Empty.The;
foreach (var statement in Statements)
{
// 2. Let s be Completion(Evaluation of StatementListItem).
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/StrictEqualityExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ override public Completion Evaluate(VM vm)
if (rval.IsAbruptCompletion()) return rval;

// 5. Return IsStrictlyEqual(rval, lval).
return Completion.NormalCompletion(Value.IsStrictlyEqual(rval.Value, lval.Value));
return Value.IsStrictlyEqual(rval.Value, lval.Value);
}

public IExpression Lhs { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/StrictInequalityExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ override public Completion Evaluate(VM vm)
var r = Value.IsStrictlyEqual(rval.Value, lval.Value);

// 6. If r is true, return false. Otherwise, return true.
return Completion.NormalCompletion(new Boolean(!r.Value));
return new Boolean(!r.Value);
}

public IExpression Lhs { get; }
Expand Down
20 changes: 10 additions & 10 deletions JSS.Lib/AST/TypeOfExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override Completion Evaluate(VM vm)
var asReference = val.Value.AsReference();
if (asReference.IsUnresolvableReference())
{
return Completion.NormalCompletion(new String("undefined"));
return new String("undefined");
}
}

Expand All @@ -39,43 +39,43 @@ public override Completion Evaluate(VM vm)
// 4. If val is undefined, return "undefined".
if (val.Value.IsUndefined())
{
return Completion.NormalCompletion(new String("undefined"));
return new String("undefined");
}

// 5. If val is null, return "object".
if (val.Value.IsNull())
{
return Completion.NormalCompletion(new String("object"));
return new String("object");
}

// 6. If val is a String, return "string".
if (val.Value.IsString())
{
return Completion.NormalCompletion(new String("string"));
return new String("string");
}

// 7. If val is a Symbol, return "symbol".
if (val.Value.IsSymbol())
{
return Completion.NormalCompletion(new String("symbol"));
return new String("symbol");
}

// 8. If val is a Boolean, return "boolean".
if (val.Value.IsBoolean())
{
return Completion.NormalCompletion(new String("boolean"));
return new String("boolean");
}

// 9. If val is a Number, return "number".
if (val.Value.IsNumber())
{
return Completion.NormalCompletion(new String("number"));
return new String("number");
}

// 10. If val is a BigInt, return "bigint".
if (val.Value.IsBigInt())
{
return Completion.NormalCompletion(new String("bigint"));
return new String("bigint");
}

// 11. Assert: val is an Object.
Expand All @@ -86,11 +86,11 @@ public override Completion Evaluate(VM vm)
// 13. If val has a [[Call]] internal slot, return "function".
if (val.Value.HasInternalCall())
{
return Completion.NormalCompletion(new String("function"));
return new String("function");
}

// 14. Return "object".
return Completion.NormalCompletion(new String("object"));
return new String("object");
}

public IExpression Expression { get; }
Expand Down
2 changes: 1 addition & 1 deletion JSS.Lib/AST/UnaryMinusExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ override public Completion Evaluate(VM vm)
{
// a. Return Number::unaryMinus(oldValue).
var asNumber = oldValue.Value.AsNumber();
return Completion.NormalCompletion(Number.UnaryMinus(asNumber));
return Number.UnaryMinus(asNumber);
}
// 4. Else,
else
Expand Down
Loading

0 comments on commit ea1573d

Please sign in to comment.