2
0

ModuleTestHost.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.IO;
  5. using Jint.Native;
  6. using Jint.Native.Object;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Tests.Test262.Language
  10. {
  11. // Hacky way to get objects from assert.js and sta.js into the module context
  12. internal sealed class ModuleTestHost : Host
  13. {
  14. private readonly static Dictionary<string, JsValue> _staticValues = new();
  15. static ModuleTestHost()
  16. {
  17. var assemblyPath = new Uri(typeof(ModuleTestHost).GetTypeInfo().Assembly.Location).LocalPath;
  18. var assemblyDirectory = new FileInfo(assemblyPath).Directory;
  19. var basePath = assemblyDirectory.Parent.Parent.Parent.FullName;
  20. var engine = new Engine();
  21. var assertSource = File.ReadAllText(Path.Combine(basePath, "harness", "assert.js"));
  22. var staSource = File.ReadAllText(Path.Combine(basePath, "harness", "sta.js"));
  23. engine.Execute(assertSource);
  24. engine.Execute(staSource);
  25. _staticValues["assert"] = engine.GetValue("assert");
  26. _staticValues["Test262Error"] = engine.GetValue("Test262Error");
  27. _staticValues["$ERROR"] = engine.GetValue("$ERROR");
  28. _staticValues["$DONOTEVALUATE"] = engine.GetValue("$DONOTEVALUATE");
  29. _staticValues["print"] = new ClrFunctionInstance(engine, "print", (thisObj, args) => TypeConverter.ToString(args.At(0)));
  30. }
  31. protected override ObjectInstance CreateGlobalObject(Realm realm)
  32. {
  33. var globalObj = base.CreateGlobalObject(realm);
  34. foreach (var key in _staticValues.Keys)
  35. {
  36. globalObj.FastAddProperty(key, _staticValues[key], true, true, true);
  37. }
  38. return globalObj;
  39. }
  40. }
  41. }