ShadowRealmTests.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  1. using Jint.Native.Object;
  2. namespace Jint.Tests.Runtime;
  3. public class ShadowRealmTests
  4. {
  5. [Fact]
  6. public void CanUseViaEngineMethods()
  7. {
  8. var engine = new Engine(options => options.EnableModules(ModuleTests.GetBasePath()));
  9. var shadowRealm1 = engine.Realm.Intrinsics.ShadowRealm.Construct();
  10. // lexically scoped (let/const) are visible during single call
  11. Assert.Equal(123, shadowRealm1.Evaluate("const s = 123; const f = () => s; f();"));
  12. Assert.Equal(true, shadowRealm1.Evaluate("typeof f === 'undefined'"));
  13. // vars hold longer
  14. Assert.Equal(456, shadowRealm1.Evaluate("function foo() { return 456; }; foo();"));
  15. Assert.Equal(456, shadowRealm1.Evaluate("foo();"));
  16. // not visible in global engine though
  17. Assert.Equal(true, engine.Evaluate("typeof foo === 'undefined'"));
  18. // modules
  19. var importValue = shadowRealm1.ImportValue("./modules/format-name.js", "formatName");
  20. var formatName = (ObjectInstance) importValue.UnwrapIfPromise();
  21. var result = engine.Invoke(formatName, "John", "Doe").AsString();
  22. Assert.Equal("John Doe", result);
  23. }
  24. }