ShadowRealmPrototype.cs 3.6 KB

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