Test262Test.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Jint.Native;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Interop;
  5. using Test262Harness;
  6. namespace Jint.Tests.Test262;
  7. public abstract partial class Test262Test
  8. {
  9. private static Engine BuildTestExecutor(Test262File file)
  10. {
  11. var engine = new Engine(cfg =>
  12. {
  13. var relativePath = Path.GetDirectoryName(file.FileName);
  14. cfg.EnableModules(new Test262ModuleLoader(State.Test262Stream.Options.FileSystem, relativePath));
  15. cfg.ExperimentalFeatures = ExperimentalFeature.All;
  16. });
  17. if (file.Flags.Contains("raw"))
  18. {
  19. // nothing should be loaded
  20. return engine;
  21. }
  22. engine.Execute(State.Sources["assert.js"]);
  23. engine.Execute(State.Sources["sta.js"]);
  24. engine.SetValue("print", new ClrFunction(engine, "print", (_, args) => TypeConverter.ToString(args.At(0))));
  25. var o = engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  26. o.FastSetProperty("evalScript", new PropertyDescriptor(new ClrFunction(engine, "evalScript",
  27. (_, args) =>
  28. {
  29. if (args.Length > 1)
  30. {
  31. throw new Exception("only script parsing supported");
  32. }
  33. var script = Engine.PrepareScript(args.At(0).AsString(), options: new ScriptPreparationOptions
  34. {
  35. ParsingOptions = ScriptParsingOptions.Default with { Tolerant = false },
  36. });
  37. return engine.Evaluate(script);
  38. }), true, true, true));
  39. o.FastSetProperty("createRealm", new PropertyDescriptor(new ClrFunction(engine, "createRealm",
  40. (_, args) =>
  41. {
  42. var realm = engine._host.CreateRealm();
  43. realm.GlobalObject.Set("global", realm.GlobalObject);
  44. return realm.GlobalObject;
  45. }), true, true, true));
  46. o.FastSetProperty("detachArrayBuffer", new PropertyDescriptor(new ClrFunction(engine, "detachArrayBuffer",
  47. (_, args) =>
  48. {
  49. var buffer = (JsArrayBuffer) args.At(0);
  50. buffer.DetachArrayBuffer();
  51. return JsValue.Undefined;
  52. }), true, true, true));
  53. o.FastSetProperty("gc", new PropertyDescriptor(new ClrFunction(engine, "gc",
  54. (_, _) =>
  55. {
  56. GC.Collect();
  57. GC.WaitForPendingFinalizers();
  58. return JsValue.Undefined;
  59. }), true, true, true));
  60. engine.SetValue("$262", o);
  61. foreach (var include in file.Includes)
  62. {
  63. engine.Execute(State.Sources[include]);
  64. }
  65. if (file.Flags.Contains("async"))
  66. {
  67. engine.Execute(State.Sources["doneprintHandle.js"]);
  68. }
  69. return engine;
  70. }
  71. private static void ExecuteTest(Engine engine, Test262File file)
  72. {
  73. if (file.Type == ProgramType.Module)
  74. {
  75. var specifier = "./" + Path.GetFileName(file.FileName);
  76. engine.Modules.Add(specifier, builder => builder.AddSource(file.Program));
  77. engine.Modules.Import(specifier);
  78. }
  79. else
  80. {
  81. var script = Engine.PrepareScript(file.Program, source: file.FileName, options: new ScriptPreparationOptions
  82. {
  83. ParsingOptions = ScriptParsingOptions.Default with { Tolerant = false },
  84. });
  85. engine.Execute(script);
  86. }
  87. }
  88. private partial bool ShouldThrow(Test262File testCase, bool strict)
  89. {
  90. return testCase.Negative;
  91. }
  92. }