ShadowRealmPrototype.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Jint.Native.Object;
  2. using Jint.Native.Symbol;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.ShadowRealm;
  7. /// <summary>
  8. /// https://tc39.es/proposal-shadowrealm/#sec-properties-of-the-shadowrealm-prototype-object
  9. /// </summary>
  10. internal sealed class ShadowRealmPrototype : Prototype
  11. {
  12. private readonly ShadowRealmConstructor _constructor;
  13. internal ShadowRealmPrototype(
  14. Engine engine,
  15. Realm realm,
  16. ShadowRealmConstructor constructor,
  17. ObjectPrototype prototype) : base(engine, realm)
  18. {
  19. _prototype = prototype;
  20. _constructor = constructor;
  21. }
  22. protected override void Initialize()
  23. {
  24. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  25. var properties = new PropertyDictionary(5, checkExistingKeys: false)
  26. {
  27. ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable),
  28. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  29. ["evaluate"] = new PropertyDescriptor(new ClrFunction(Engine, "evaluate", Evaluate, 1, PropertyFlag.Configurable), propertyFlags),
  30. ["importValue"] = new PropertyDescriptor(new ClrFunction(Engine, "importValue", ImportValue, 2, PropertyFlag.Configurable), propertyFlags),
  31. };
  32. SetProperties(properties);
  33. var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ShadowRealm", false, false, true) };
  34. SetSymbols(symbols);
  35. }
  36. /// <summary>
  37. /// https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.evaluate
  38. /// </summary>
  39. private JsValue Evaluate(JsValue thisObject, JsCallArguments arguments)
  40. {
  41. var shadowRealm = ValidateShadowRealmObject(thisObject);
  42. var sourceText = arguments.At(0);
  43. if (!sourceText.IsString())
  44. {
  45. ExceptionHelper.ThrowTypeError(_realm, "Invalid source text " + sourceText);
  46. }
  47. var parserOptions = _engine.GetActiveParserOptions();
  48. // Just like in the case of eval, we don't allow top level returns.
  49. var adjustedParserOptions = parserOptions.AllowReturnOutsideFunction
  50. ? parserOptions with { AllowReturnOutsideFunction = false }
  51. : parserOptions;
  52. var parser = _engine.GetParserFor(adjustedParserOptions);
  53. return shadowRealm.PerformShadowRealmEval(sourceText.AsString(), parserOptions, parser, _realm);
  54. }
  55. /// <summary>
  56. /// https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.importvalue
  57. /// </summary>
  58. private JsValue ImportValue(JsValue thisObject, JsCallArguments arguments)
  59. {
  60. var specifier = arguments.At(0);
  61. var exportName = arguments.At(1);
  62. var O = ValidateShadowRealmObject(thisObject);
  63. var specifierString = TypeConverter.ToJsString(specifier);
  64. if (!specifier.IsString())
  65. {
  66. ExceptionHelper.ThrowTypeError(_realm, "Invalid specifier");
  67. }
  68. if (!exportName.IsString())
  69. {
  70. ExceptionHelper.ThrowTypeError(_realm, "Invalid exportName");
  71. }
  72. var callerRealm = _realm;
  73. return O.ShadowRealmImportValue(specifierString.ToString(), exportName.ToString(), callerRealm);
  74. }
  75. private ShadowRealm ValidateShadowRealmObject(JsValue thisObject)
  76. {
  77. if (thisObject is ShadowRealm shadowRealm)
  78. {
  79. return shadowRealm;
  80. }
  81. ExceptionHelper.ThrowTypeError(_realm, "object must be a ShadowRealm");
  82. return default;
  83. }
  84. }