ModuleBuilder.cs 3.5 KB

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