ModuleBuilder.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using Esprima;
  5. using Esprima.Ast;
  6. using Jint.Native;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Interop;
  9. using Jint.Runtime.Modules;
  10. namespace Jint;
  11. public sealed class ModuleBuilder
  12. {
  13. private readonly Engine _engine;
  14. private readonly string _specifier;
  15. private readonly List<string> _sourceRaw = new();
  16. private readonly Dictionary<string, JsValue> _exports = new();
  17. private readonly ParserOptions _options;
  18. internal ModuleBuilder(Engine engine, string specifier)
  19. {
  20. _engine = engine;
  21. _specifier = specifier;
  22. _options = new ParserOptions(specifier);
  23. }
  24. public ModuleBuilder AddSource(string code)
  25. {
  26. _sourceRaw.Add(code);
  27. return this;
  28. }
  29. public ModuleBuilder ExportValue(string name, JsValue value)
  30. {
  31. _exports.Add(name, value);
  32. return this;
  33. }
  34. public ModuleBuilder ExportObject(string name, object value)
  35. {
  36. _exports.Add(name, JsValue.FromObject(_engine, value));
  37. return this;
  38. }
  39. public ModuleBuilder ExportType<T>()
  40. {
  41. ExportType<T>(typeof(T).Name);
  42. return this;
  43. }
  44. public ModuleBuilder ExportType<T>(string name)
  45. {
  46. _exports.Add(name, TypeReference.CreateTypeReference<T>(_engine));
  47. return this;
  48. }
  49. public ModuleBuilder ExportType(Type type)
  50. {
  51. ExportType(type.Name, type);
  52. return this;
  53. }
  54. public ModuleBuilder ExportType(string name, Type type)
  55. {
  56. _exports.Add(name, TypeReference.CreateTypeReference(_engine, type));
  57. return this;
  58. }
  59. public ModuleBuilder ExportFunction(string name, Func<JsValue[], JsValue> fn)
  60. {
  61. _exports.Add(name, new ClrFunctionInstance(_engine, name, (_, args) => fn(args)));
  62. return this;
  63. }
  64. public ModuleBuilder ExportFunction(string name, Func<JsValue> fn)
  65. {
  66. _exports.Add(name, new ClrFunctionInstance(_engine, name, (_, _) => fn()));
  67. return this;
  68. }
  69. public ModuleBuilder ExportFunction(string name, Action<JsValue[]> fn)
  70. {
  71. _exports.Add(name, new ClrFunctionInstance(_engine, name, (_, args) =>
  72. {
  73. fn(args);
  74. return JsValue.Undefined;
  75. }));
  76. return this;
  77. }
  78. public ModuleBuilder ExportFunction(string name, Action fn)
  79. {
  80. _exports.Add(name, new ClrFunctionInstance(_engine, name, (_, _) =>
  81. {
  82. fn();
  83. return JsValue.Undefined;
  84. }));
  85. return this;
  86. }
  87. public ModuleBuilder WithOptions(Action<ParserOptions> configure)
  88. {
  89. configure(_options);
  90. return this;
  91. }
  92. internal Module Parse()
  93. {
  94. if (_sourceRaw.Count <= 0)
  95. {
  96. return new Module(NodeList.Create(Array.Empty<Statement>()));
  97. }
  98. var javaScriptParser = new JavaScriptParser(_sourceRaw.Count == 1 ? _sourceRaw[0] : string.Join(Environment.NewLine, _sourceRaw), _options);
  99. try
  100. {
  101. return javaScriptParser.ParseModule();
  102. }
  103. catch (ParserException ex)
  104. {
  105. ExceptionHelper.ThrowSyntaxError(_engine.Realm, $"Error while loading module: error in module '{_specifier}': {ex.Error}", new Location(new Position(ex.LineNumber, ex.Column), new Position(ex.LineNumber, ex.Column), _specifier));
  106. return null!;
  107. }
  108. }
  109. internal void BindExportedValues(BuilderModuleRecord module)
  110. {
  111. foreach (var export in _exports)
  112. {
  113. module.BindExportedValue(export.Key, export.Value);
  114. }
  115. }
  116. }