123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using Jint.Native.Errors;
- using Jint.Native.Object;
- using Jint.Parser.Ast;
- using Jint.Runtime.Environments;
- namespace Jint.Native.Function
- {
- public abstract class FunctionInstance : ObjectInstance, ICallable
- {
- private readonly Engine _engine;
- protected FunctionInstance(Engine engine, ObjectInstance prototype, Identifier[] parameters, LexicalEnvironment scope, bool strict) : base(prototype)
- {
- _engine = engine;
- FormalParameters = parameters;
- Scope = scope;
- Strict = strict;
- }
- /// <summary>
- /// Executed when a function object is used as a function
- /// </summary>
- /// <param name="thisObject"></param>
- /// <param name="arguments"></param>
- /// <returns></returns>
- public abstract object Call(object thisObject, object[] arguments);
- public LexicalEnvironment Scope { get; private set; }
-
- public Identifier[] FormalParameters { get; private set; }
- public bool Strict { get; private set; }
- // todo: implement
- public object TargetFunction { get; set; }
- // todo: implement
- public object BoundThis { get; set; }
- // todo: implement
- public object BoundArgs { get; set; }
- public bool HasInstance(object instance)
- {
- var v = instance as ObjectInstance;
- if (v == null)
- {
- return false;
- }
- while (true)
- {
- v = v.Prototype;
- if (v == null)
- {
- return false;
- }
- if (v == this.Prototype)
- {
- return true;
- }
- }
- return false;
- }
- public override string Class
- {
- get
- {
- return "Function";
- }
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.4
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public override object Get(string propertyName)
- {
- var v = base.Get(propertyName);
- var f = v as FunctionInstance;
- if (propertyName == "caller" && f != null && f.Strict)
- {
- throw new TypeError();
- }
- return v;
- }
- }
- }
|