ModuleBuilder.cs 4.1 KB

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