2
0

ModuleFactory.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Jint.Native;
  2. using Jint.Native.Json;
  3. namespace Jint.Runtime.Modules;
  4. /// <summary>
  5. /// Factory which creates a single runtime <see cref="Module"/> from a given source.
  6. /// </summary>
  7. public static class ModuleFactory
  8. {
  9. /// <summary>
  10. /// Creates a <see cref="Module"/> for the usage within the given <paramref name="engine"/>
  11. /// from the provided javascript <paramref name="code"/>.
  12. /// </summary>
  13. /// <remarks>
  14. /// The returned modules location (see <see cref="Module.Location"/>) points to
  15. /// <see cref="Uri.LocalPath"/> if <see cref="ResolvedSpecifier.Uri"/> is not null. If
  16. /// <see cref="ResolvedSpecifier.Uri"/> is null, the modules location source will be null as well.
  17. /// </remarks>
  18. /// <exception cref="ParseErrorException">Is thrown if the provided <paramref name="code"/> can not be parsed.</exception>
  19. /// <exception cref="JavaScriptException">Is thrown if an error occured when parsing <paramref name="code"/>.</exception>
  20. public static Module BuildSourceTextModule(Engine engine, ResolvedSpecifier resolved, string code, ModuleParsingOptions? parsingOptions = null)
  21. {
  22. var source = resolved.Uri?.LocalPath ?? resolved.Key;
  23. var parserOptions = (parsingOptions ?? ModuleParsingOptions.Default).GetParserOptions();
  24. var parser = new Parser(parserOptions);
  25. var module = parser.ParseModuleGuarded(engine, code, source);
  26. return BuildSourceTextModule(engine, new Prepared<AstModule>(module, parserOptions));
  27. }
  28. /// <summary>
  29. /// Creates a <see cref="Module"/> for the usage within the given <paramref name="engine"/>
  30. /// from the parsed <paramref name="preparedModule"/>.
  31. /// </summary>
  32. /// <remarks>
  33. /// The returned modules location (see <see cref="Module.Location"/>) will be set
  34. /// to <see cref="SourceLocation.SourceFile"/> of the <paramref name="preparedModule"/>.
  35. /// </remarks>
  36. public static Module BuildSourceTextModule(Engine engine, in Prepared<AstModule> preparedModule)
  37. {
  38. if (!preparedModule.IsValid)
  39. {
  40. Throw.InvalidPreparedModuleArgumentException(nameof(preparedModule));
  41. }
  42. return new SourceTextModule(engine, engine.Realm, preparedModule, preparedModule.Program!.Location.SourceFile, async: false);
  43. }
  44. /// <summary>
  45. /// Creates a <see cref="Module"/> for the usage within the given <paramref name="engine"/> for the
  46. /// provided JSON module <paramref name="jsonString"/>.
  47. /// </summary>
  48. /// <remarks>
  49. /// The returned modules location (see <see cref="Module.Location"/>) points to
  50. /// <see cref="Uri.LocalPath"/> if <see cref="ResolvedSpecifier.Uri"/> is not null. If
  51. /// <see cref="ResolvedSpecifier.Uri"/> is null, the modules location source will be null as well.
  52. /// </remarks>
  53. /// <exception cref="JavaScriptException">Is thrown if an error occured when parsing <paramref name="jsonString"/>.</exception>
  54. public static Module BuildJsonModule(Engine engine, ResolvedSpecifier resolved, string jsonString)
  55. {
  56. var source = resolved.Uri?.LocalPath;
  57. JsValue module;
  58. try
  59. {
  60. module = new JsonParser(engine).Parse(jsonString);
  61. }
  62. catch (Exception)
  63. {
  64. Throw.JavaScriptException(engine, $"Could not load module {source}", AstExtensions.DefaultLocation);
  65. module = null;
  66. }
  67. return BuildJsonModule(engine, module, resolved.Uri?.LocalPath);
  68. }
  69. /// <summary>
  70. /// Creates a <see cref="Module"/> for the usage within the given <paramref name="engine"/>
  71. /// from the parsed JSON provided in <paramref name="parsedJson"/>.
  72. /// </summary>
  73. /// <remarks>
  74. /// The returned modules location (see <see cref="Module.Location"/>) will be set
  75. /// to <paramref name="location"/>.
  76. /// </remarks>
  77. public static Module BuildJsonModule(Engine engine, JsValue parsedJson, string? location)
  78. {
  79. return new SyntheticModule(engine, engine.Realm, parsedJson, location);
  80. }
  81. }