EvalFunctionInstance.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Jint.Native.Object;
  2. using Jint.Parser;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Environments;
  5. namespace Jint.Native.Function
  6. {
  7. public class EvalFunctionInstance: FunctionInstance
  8. {
  9. private readonly Engine _engine;
  10. public EvalFunctionInstance(Engine engine, ObjectInstance prototype, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine, parameters, scope, strict)
  11. {
  12. _engine = engine;
  13. }
  14. public override object Call(object thisObject, object[] arguments)
  15. {
  16. if (StrictModeScope.IsStrictModeCode)
  17. {
  18. throw new JavaScriptException(Engine.SyntaxError, "eval() is not allowed in strict mode.");
  19. }
  20. var code = TypeConverter.ToString(arguments.At(0));
  21. var parser = new JavaScriptParser();
  22. try
  23. {
  24. var program = parser.Parse(code);
  25. using (new StrictModeScope(program.Strict))
  26. {
  27. return _engine.ExecuteStatement(program).Value ?? Undefined.Instance;
  28. }
  29. }
  30. catch (ParserError e)
  31. {
  32. throw new JavaScriptException(Engine.SyntaxError);
  33. }
  34. }
  35. }
  36. }