ScriptFunctionInstance.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Jint.Native.Object;
  5. using Jint.Parser.Ast;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Environments;
  9. namespace Jint.Native.Function
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  15. {
  16. private readonly Engine _engine;
  17. private readonly Statement _body;
  18. private readonly IEnumerable<Identifier> _parameters;
  19. public ScriptFunctionInstance(Engine engine, Statement body, string name, Identifier[] parameters, ObjectInstance instancePrototype, ObjectInstance functionPrototype, LexicalEnvironment scope, bool strict)
  20. : base(engine, instancePrototype, parameters, scope, strict)
  21. {
  22. // http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  23. _engine = engine;
  24. _body = body;
  25. _parameters = parameters;
  26. Extensible = true;
  27. var len = parameters.Count();
  28. DefineOwnProperty("length", new DataDescriptor(len) { Writable = false, Enumerable = false, Configurable = false }, false);
  29. DefineOwnProperty("name", new DataDescriptor(name), false);
  30. instancePrototype.DefineOwnProperty("constructor", new DataDescriptor(this) { Writable = true, Enumerable = true, Configurable = true }, false);
  31. DefineOwnProperty("prototype", new DataDescriptor(functionPrototype) { Writable = true, Enumerable = true, Configurable = true }, false);
  32. }
  33. /// <summary>
  34. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  35. /// </summary>
  36. /// <param name="thisObject"></param>
  37. /// <param name="arguments"></param>
  38. /// <returns></returns>
  39. public override object Call(object thisObject, object[] arguments)
  40. {
  41. object thisBinding;
  42. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  43. if (_engine.Options.IsStrict())
  44. {
  45. thisBinding = thisObject;
  46. }
  47. else if (thisObject == Undefined.Instance || thisObject == Null.Instance)
  48. {
  49. thisBinding = _engine.Global;
  50. }
  51. else if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  52. {
  53. thisBinding = TypeConverter.ToObject(_engine, thisObject);
  54. }
  55. else
  56. {
  57. thisBinding = thisObject;
  58. }
  59. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(Scope);
  60. _engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  61. var env = localEnv.Record;
  62. int i = 0;
  63. foreach (var parameter in _parameters)
  64. {
  65. env.SetMutableBinding(parameter.Name, i < arguments.Length ? arguments[i++] : Undefined.Instance, false);
  66. }
  67. var result = _engine.ExecuteStatement(_body);
  68. _engine.LeaveExecutionContext();
  69. return result;
  70. }
  71. public ObjectInstance Construct(object[] arguments)
  72. {
  73. // todo: http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  74. var instance = new FunctionShim(_engine, Prototype, null, null);
  75. return instance;
  76. }
  77. }
  78. }