ModuleBuilder.cs 4.2 KB

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