JintExportNamedDeclaration.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #nullable enable
  2. using Esprima.Ast;
  3. using Jint.Native;
  4. using Jint.Runtime.Interpreter.Expressions;
  5. namespace Jint.Runtime.Interpreter.Statements;
  6. internal sealed class JintExportNamedDeclaration : JintStatement<ExportNamedDeclaration>
  7. {
  8. private JintExpression? _declarationExpression;
  9. private JintStatement? _declarationStatement;
  10. public JintExportNamedDeclaration(ExportNamedDeclaration statement) : base(statement)
  11. {
  12. }
  13. protected override void Initialize(EvaluationContext context)
  14. {
  15. if (_statement.Declaration != null)
  16. {
  17. switch (_statement.Declaration)
  18. {
  19. case Expression e:
  20. _declarationExpression = JintExpression.Build(context.Engine, e);
  21. break;
  22. case Statement s:
  23. _declarationStatement = Build(s);
  24. break;
  25. default:
  26. ExceptionHelper.ThrowNotSupportedException($"Statement {_statement.Declaration.Type} is not supported in an export declaration.");
  27. break;
  28. }
  29. }
  30. }
  31. /// <summary>
  32. /// https://tc39.es/ecma262/#sec-exports-runtime-semantics-evaluation
  33. /// </summary>
  34. protected override Completion ExecuteInternal(EvaluationContext context)
  35. {
  36. if (_declarationStatement != null)
  37. {
  38. _declarationStatement.Execute(context);
  39. return NormalCompletion(Undefined.Instance);
  40. }
  41. if (_declarationExpression != null)
  42. {
  43. // Named exports don't require anything more since the values are available in the lexical context
  44. return _declarationExpression.GetValue(context);
  45. }
  46. return NormalCompletion(Undefined.Instance);
  47. }
  48. }