ModuleBuilder.cs 4.3 KB

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