Test262Test.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using Esprima;
  9. using Jint.Runtime;
  10. using Jint.Runtime.Descriptors;
  11. using Jint.Runtime.Interop;
  12. using Newtonsoft.Json.Linq;
  13. using Xunit.Abstractions;
  14. using Xunit.Sdk;
  15. namespace Jint.Tests.Test262
  16. {
  17. public abstract class Test262Test
  18. {
  19. private static readonly Dictionary<string, string> Sources;
  20. private static readonly string BasePath;
  21. private static readonly TimeZoneInfo _pacificTimeZone;
  22. private static readonly Dictionary<string, string> _skipReasons =
  23. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  24. private static readonly HashSet<string> _strictSkips =
  25. new HashSet<string>(StringComparer.OrdinalIgnoreCase);
  26. static Test262Test()
  27. {
  28. //NOTE: The Date tests in test262 assume the local timezone is Pacific Standard Time
  29. _pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
  30. var assemblyPath = new Uri(typeof(Test262Test).GetTypeInfo().Assembly.CodeBase).LocalPath;
  31. var assemblyDirectory = new FileInfo(assemblyPath).Directory;
  32. BasePath = assemblyDirectory.Parent.Parent.Parent.FullName;
  33. string[] files =
  34. {
  35. "sta.js",
  36. "assert.js",
  37. "arrayContains.js",
  38. "propertyHelper.js",
  39. "compareArray.js",
  40. "decimalToHexString.js",
  41. "proxyTrapsHelper.js",
  42. "dateConstants.js",
  43. "assertRelativeDateMs.js",
  44. "regExpUtils.js",
  45. "nans.js",
  46. "compareIterator.js",
  47. "nativeFunctionMatcher.js",
  48. "wellKnownIntrinsicObjects.js",
  49. "fnGlobalObject.js"
  50. };
  51. Sources = new Dictionary<string, string>(files.Length);
  52. for (var i = 0; i < files.Length; i++)
  53. {
  54. Sources[files[i]] = File.ReadAllText(Path.Combine(BasePath, "harness", files[i]));
  55. }
  56. var content = File.ReadAllText(Path.Combine(BasePath, "test/skipped.json"));
  57. var doc = JArray.Parse(content);
  58. foreach (var entry in doc.Values<JObject>())
  59. {
  60. var source = entry["source"].Value<string>();
  61. _skipReasons[source] = entry["reason"].Value<string>();
  62. if (entry.TryGetValue("mode", out var mode) && mode.Value<string>() == "strict")
  63. {
  64. _strictSkips.Add(source);
  65. }
  66. }
  67. }
  68. protected void RunTestCode(string code, bool strict)
  69. {
  70. var engine = new Engine(cfg => cfg
  71. .LocalTimeZone(_pacificTimeZone)
  72. .Strict(strict)
  73. );
  74. engine.Execute(Sources["sta.js"], CreateParserOptions("sta.js"));
  75. engine.Execute(Sources["assert.js"], CreateParserOptions("assert.js"));
  76. engine.SetValue("print", new ClrFunctionInstance(engine, "print", (thisObj, args) => TypeConverter.ToString(args.At(0))));
  77. var o = engine.Object.Construct(Arguments.Empty);
  78. o.FastSetProperty("evalScript", new PropertyDescriptor(new ClrFunctionInstance(engine, "evalScript", (thisObj, args) =>
  79. {
  80. if (args.Length > 1)
  81. {
  82. throw new Exception("only script parsing supported");
  83. }
  84. var options = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  85. var parser = new JavaScriptParser(args.At(0).AsString(), options);
  86. var script = parser.ParseScript(strict);
  87. var value = engine.Execute(script, false).GetCompletionValue();
  88. return value;
  89. }), true, true, true));
  90. engine.SetValue("$262", o);
  91. var includes = Regex.Match(code, @"includes: \[(.+?)\]");
  92. if (includes.Success)
  93. {
  94. var files = includes.Groups[1].Captures[0].Value.Split(',');
  95. foreach (var file in files)
  96. {
  97. engine.Execute(Sources[file.Trim()], CreateParserOptions(file.Trim()));
  98. }
  99. }
  100. if (code.IndexOf("propertyHelper.js", StringComparison.OrdinalIgnoreCase) != -1)
  101. {
  102. engine.Execute(Sources["propertyHelper.js"], CreateParserOptions("propertyHelper.js"));
  103. }
  104. string lastError = null;
  105. bool negative = code.IndexOf("negative:", StringComparison.Ordinal) > -1;
  106. try
  107. {
  108. engine.Execute(code);
  109. }
  110. catch (JavaScriptException j)
  111. {
  112. lastError = j.ToString();
  113. }
  114. catch (Exception e)
  115. {
  116. lastError = e.ToString();
  117. }
  118. if (!negative && !string.IsNullOrWhiteSpace(lastError))
  119. {
  120. throw new XunitException(lastError);
  121. }
  122. }
  123. protected void RunTestInternal(SourceFile sourceFile)
  124. {
  125. if (sourceFile.Skip)
  126. {
  127. return;
  128. }
  129. if (sourceFile.Code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
  130. {
  131. RunTestCode(sourceFile.Code, strict: false);
  132. }
  133. if (!_strictSkips.Contains(sourceFile.Source)
  134. && sourceFile.Code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
  135. {
  136. RunTestCode(sourceFile.Code, strict: true);
  137. }
  138. }
  139. public static IEnumerable<object[]> SourceFiles(string pathPrefix, bool skipped)
  140. {
  141. var results = new ConcurrentBag<object[]>();
  142. var fixturesPath = Path.Combine(BasePath, "test");
  143. var searchPath = Path.Combine(fixturesPath, pathPrefix);
  144. var files = Directory.GetFiles(searchPath, "*", SearchOption.AllDirectories);
  145. foreach (var file in files)
  146. {
  147. var name = file.Substring(fixturesPath.Length + 1).Replace("\\", "/");
  148. bool skip = _skipReasons.TryGetValue(name, out var reason);
  149. var code = skip ? "" : File.ReadAllText(file);
  150. var flags = Regex.Match(code, "flags: \\[(.+?)\\]");
  151. if (flags.Success)
  152. {
  153. var items = flags.Groups[1].Captures[0].Value.Split(',');
  154. foreach (var item in items.Select(x => x.Trim()))
  155. {
  156. switch (item)
  157. {
  158. // TODO implement
  159. case "async":
  160. skip = true;
  161. reason = "async not implemented";
  162. break;
  163. }
  164. }
  165. }
  166. var features = Regex.Match(code, "features: \\[(.+?)\\]");
  167. if (features.Success)
  168. {
  169. var items = features.Groups[1].Captures[0].Value.Split(',');
  170. foreach (var item in items.Select(x => x.Trim()))
  171. {
  172. switch (item)
  173. {
  174. // TODO implement
  175. case "cross-realm":
  176. skip = true;
  177. reason = "realms not implemented";
  178. break;
  179. case "tail-call-optimization":
  180. skip = true;
  181. reason = "tail-calls not implemented";
  182. break;
  183. case "BigInt":
  184. skip = true;
  185. reason = "BigInt not implemented";
  186. break;
  187. case "generators":
  188. skip = true;
  189. reason = "generators not implemented";
  190. break;
  191. case "async-functions":
  192. skip = true;
  193. reason = "async-functions not implemented";
  194. break;
  195. case "async-iteration":
  196. skip = true;
  197. reason = "async not implemented";
  198. break;
  199. case "class-fields-private":
  200. case "class-fields-public":
  201. skip = true;
  202. reason = "private/public class fields not implemented in esprima";
  203. break;
  204. case "new.target":
  205. skip = true;
  206. reason = "MetaProperty not implemented";
  207. break;
  208. case "super":
  209. skip = true;
  210. reason = "super not implemented";
  211. break;
  212. case "String.prototype.replaceAll":
  213. skip = true;
  214. reason = "not in spec yet";
  215. break;
  216. case "u180e":
  217. skip = true;
  218. reason = "unicode/regexp not implemented";
  219. break;
  220. case "regexp-match-indices":
  221. skip = true;
  222. reason = "regexp-match-indices not implemented";
  223. break;
  224. case "regexp-named-groups":
  225. skip = true;
  226. reason = "regexp-named-groups not implemented";
  227. break;
  228. case "regexp-lookbehind":
  229. skip = true;
  230. reason = "regexp-lookbehind not implemented";
  231. break;
  232. case "TypedArray":
  233. skip = true;
  234. reason = "TypedArray not implemented";
  235. break;
  236. }
  237. }
  238. }
  239. if (code.IndexOf("SpecialCasing.txt") > -1)
  240. {
  241. skip = true;
  242. reason = "SpecialCasing.txt not implemented";
  243. }
  244. if (name.StartsWith("language/expressions/object/dstr-async-gen-meth-"))
  245. {
  246. skip = true;
  247. reason = "Esprima problem, Unexpected token *";
  248. }
  249. if (name.StartsWith("built-ins/RegExp/property-escapes/generated/"))
  250. {
  251. skip = true;
  252. reason = "Esprima problem, Invalid regular expression";
  253. }
  254. if (name.StartsWith("built-ins/RegExp/unicode_"))
  255. {
  256. skip = true;
  257. reason = "Unicode support and its special cases need more work";
  258. }
  259. if (name.StartsWith("language/statements/class/subclass/builtin-objects/Promise"))
  260. {
  261. skip = true;
  262. reason = "Promise not implemented";
  263. }
  264. if (name.StartsWith("language/statements/class/subclass/builtin-objects/TypedArray"))
  265. {
  266. skip = true;
  267. reason = "TypedArray not implemented";
  268. }
  269. if (name.StartsWith("language/statements/class/subclass/builtin-objects/WeakMap"))
  270. {
  271. skip = true;
  272. reason = "WeakMap not implemented";
  273. }
  274. if (name.StartsWith("language/statements/class/subclass/builtin-objects/WeakSet"))
  275. {
  276. skip = true;
  277. reason = "WeakSet not implemented";
  278. }
  279. if (name.StartsWith("language/statements/class/subclass/builtin-objects/ArrayBuffer/"))
  280. {
  281. skip = true;
  282. reason = "ArrayBuffer not implemented";
  283. }
  284. if (name.StartsWith("language/statements/class/subclass/builtin-objects/DataView"))
  285. {
  286. skip = true;
  287. reason = "DataView not implemented";
  288. }
  289. if (name.StartsWith("language/statements/class/subclass/builtins.js"))
  290. {
  291. skip = true;
  292. reason = "Uint8Array not implemented";
  293. }
  294. if (name.StartsWith("built-ins/RegExp/CharacterClassEscapes/"))
  295. {
  296. skip = true;
  297. reason = "for-of not implemented";
  298. }
  299. if (file.EndsWith("tv-line-continuation.js")
  300. || file.EndsWith("tv-line-terminator-sequence.js")
  301. || file.EndsWith("special-characters.js"))
  302. {
  303. // LF endings required
  304. code = code.Replace("\r\n", "\n");
  305. }
  306. var sourceFile = new SourceFile(
  307. name,
  308. file,
  309. skip,
  310. reason,
  311. code);
  312. if (skipped == sourceFile.Skip)
  313. {
  314. results.Add(new object[]
  315. {
  316. sourceFile
  317. });
  318. }
  319. }
  320. return results;
  321. }
  322. private static ParserOptions CreateParserOptions(string fileName) =>
  323. new ParserOptions(fileName)
  324. {
  325. AdaptRegexp = true,
  326. Tolerant = true,
  327. Loc = true
  328. };
  329. }
  330. public class SourceFile : IXunitSerializable
  331. {
  332. public SourceFile()
  333. {
  334. }
  335. public SourceFile(
  336. string source,
  337. string fullPath,
  338. bool skip,
  339. string reason,
  340. string code)
  341. {
  342. Skip = skip;
  343. Source = source;
  344. Reason = reason;
  345. FullPath = fullPath;
  346. Code = code;
  347. }
  348. public string Source { get; set; }
  349. public bool Skip { get; set; }
  350. public string Reason { get; set; }
  351. public string FullPath { get; set; }
  352. public string Code { get; set; }
  353. public void Deserialize(IXunitSerializationInfo info)
  354. {
  355. Skip = info.GetValue<bool>(nameof(Skip));
  356. Source = info.GetValue<string>(nameof(Source));
  357. Reason = info.GetValue<string>(nameof(Reason));
  358. FullPath = info.GetValue<string>(nameof(FullPath));
  359. Code = info.GetValue<string>(nameof(Code));
  360. }
  361. public void Serialize(IXunitSerializationInfo info)
  362. {
  363. info.AddValue(nameof(Skip), Skip);
  364. info.AddValue(nameof(Source), Source);
  365. info.AddValue(nameof(Reason), Reason);
  366. info.AddValue(nameof(FullPath), FullPath);
  367. info.AddValue(nameof(Code), Code);
  368. }
  369. public override string ToString()
  370. {
  371. return Source;
  372. }
  373. }
  374. }