EvalFunctionInstance.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. var code = TypeConverter.ToString(arguments.At(0));
  17. try
  18. {
  19. var parser = new JavaScriptParser(StrictModeScope.IsStrictModeCode);
  20. var program = parser.Parse(code);
  21. using (new StrictModeScope(program.Strict))
  22. {
  23. var result = _engine.ExecuteStatement(program);
  24. if (result.Type == Completion.Throw)
  25. {
  26. throw new JavaScriptException(result.Value);
  27. }
  28. else
  29. {
  30. return result.Value ?? Undefined.Instance;
  31. }
  32. }
  33. }
  34. catch (ParserError)
  35. {
  36. throw new JavaScriptException(Engine.SyntaxError);
  37. }
  38. }
  39. }
  40. }