JintImportExpression.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. using Jint.Native.Promise;
  4. namespace Jint.Runtime.Interpreter.Expressions;
  5. internal sealed class JintImportExpression : JintExpression
  6. {
  7. private JintExpression _importExpression;
  8. public JintImportExpression(ImportExpression expression) : base(expression)
  9. {
  10. _initialized = false;
  11. _importExpression = null!;
  12. }
  13. protected override void Initialize(EvaluationContext context)
  14. {
  15. var expression = ((ImportExpression) _expression).Source;
  16. _importExpression = Build(expression);
  17. }
  18. /// <summary>
  19. /// https://tc39.es/ecma262/#sec-import-calls
  20. /// </summary>
  21. protected override object EvaluateInternal(EvaluationContext context)
  22. {
  23. var referencingScriptOrModule = context.Engine.GetActiveScriptOrModule();
  24. var argRef = _importExpression.Evaluate(context);
  25. var specifier = context.Engine.GetValue(argRef); //.UnwrapIfPromise();
  26. var promiseCapability = PromiseConstructor.NewPromiseCapability(context.Engine, context.Engine.Realm.Intrinsics.Promise);
  27. string specifierString;
  28. try
  29. {
  30. specifierString = TypeConverter.ToString(specifier);
  31. }
  32. catch (JavaScriptException e)
  33. {
  34. promiseCapability.Reject.Call(JsValue.Undefined, new[] { e.Error });
  35. return JsValue.Undefined;
  36. }
  37. context.Engine._host.ImportModuleDynamically(referencingScriptOrModule, specifierString, promiseCapability);
  38. context.Engine.RunAvailableContinuations();
  39. return promiseCapability.PromiseInstance;
  40. }
  41. }