ModuleBuilder.cs 4.2 KB

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