ModuleBuilder.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Diagnostics.CodeAnalysis;
  2. using Esprima;
  3. using Esprima.Ast;
  4. using Jint.Native;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Runtime.Modules;
  7. public sealed class ModuleBuilder
  8. {
  9. private readonly Engine _engine;
  10. private readonly string _specifier;
  11. private global::Esprima.Ast.Module? _module;
  12. private readonly List<string> _sourceRaw = new();
  13. private readonly Dictionary<string, JsValue> _exports = new(StringComparer.Ordinal);
  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. RegexTimeout = engine.Options.Constraints.RegexTimeout
  22. };
  23. }
  24. public ModuleBuilder AddSource(string code)
  25. {
  26. if (_module != null)
  27. {
  28. throw new InvalidOperationException("Cannot have both source text and pre-compiled.");
  29. }
  30. _sourceRaw.Add(code);
  31. return this;
  32. }
  33. public ModuleBuilder AddModule(global::Esprima.Ast.Module module)
  34. {
  35. if (_sourceRaw.Count > 0)
  36. {
  37. throw new InvalidOperationException("Cannot have both source text and pre-compiled.");
  38. }
  39. if (_module != null)
  40. {
  41. throw new InvalidOperationException("pre-compiled module already exists.");
  42. }
  43. _module = module;
  44. return this;
  45. }
  46. public ModuleBuilder ExportValue(string name, JsValue value)
  47. {
  48. _exports.Add(name, value);
  49. return this;
  50. }
  51. public ModuleBuilder ExportObject(string name, object value)
  52. {
  53. _exports.Add(name, JsValue.FromObject(_engine, value));
  54. return this;
  55. }
  56. public ModuleBuilder ExportType<[DynamicallyAccessedMembers(InteropHelper.DefaultDynamicallyAccessedMemberTypes)] T>()
  57. {
  58. ExportType<T>(typeof(T).Name);
  59. return this;
  60. }
  61. public ModuleBuilder ExportType<[DynamicallyAccessedMembers(InteropHelper.DefaultDynamicallyAccessedMemberTypes)] T>(string name)
  62. {
  63. _exports.Add(name, TypeReference.CreateTypeReference<T>(_engine));
  64. return this;
  65. }
  66. public ModuleBuilder ExportType([DynamicallyAccessedMembers(InteropHelper.DefaultDynamicallyAccessedMemberTypes)] Type type)
  67. {
  68. ExportType(type.Name, type);
  69. return this;
  70. }
  71. public ModuleBuilder ExportType(string name, [DynamicallyAccessedMembers(InteropHelper.DefaultDynamicallyAccessedMemberTypes)] Type type)
  72. {
  73. _exports.Add(name, TypeReference.CreateTypeReference(_engine, type));
  74. return this;
  75. }
  76. public ModuleBuilder ExportFunction(string name, Func<JsValue[], JsValue> fn)
  77. {
  78. _exports.Add(name, new ClrFunction(_engine, name, (_, args) => fn(args)));
  79. return this;
  80. }
  81. public ModuleBuilder ExportFunction(string name, Func<JsValue> fn)
  82. {
  83. _exports.Add(name, new ClrFunction(_engine, name, (_, _) => fn()));
  84. return this;
  85. }
  86. public ModuleBuilder ExportFunction(string name, Action<JsValue[]> fn)
  87. {
  88. _exports.Add(name, new ClrFunction(_engine, name, (_, args) =>
  89. {
  90. fn(args);
  91. return JsValue.Undefined;
  92. }));
  93. return this;
  94. }
  95. public ModuleBuilder ExportFunction(string name, Action fn)
  96. {
  97. _exports.Add(name, new ClrFunction(_engine, name, (_, _) =>
  98. {
  99. fn();
  100. return JsValue.Undefined;
  101. }));
  102. return this;
  103. }
  104. public ModuleBuilder WithOptions(Action<ParserOptions> configure)
  105. {
  106. configure(_options);
  107. return this;
  108. }
  109. internal global::Esprima.Ast.Module Parse()
  110. {
  111. if (_module != null) return _module;
  112. if (_sourceRaw.Count <= 0)
  113. {
  114. return new global::Esprima.Ast.Module(NodeList.Create(Array.Empty<Statement>()));
  115. }
  116. var javaScriptParser = new JavaScriptParser(_options);
  117. try
  118. {
  119. var source = _sourceRaw.Count == 1 ? _sourceRaw[0] : string.Join(Environment.NewLine, _sourceRaw);
  120. return javaScriptParser.ParseModule(source, _specifier);
  121. }
  122. catch (ParserException ex)
  123. {
  124. 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));
  125. return null!;
  126. }
  127. }
  128. internal void BindExportedValues(BuilderModule module)
  129. {
  130. foreach (var export in _exports)
  131. {
  132. module.BindExportedValue(export.Key, export.Value);
  133. }
  134. }
  135. }