internal ScriptObject SpeciesConstructor([NotNull] ScriptObject obj, [NotNull] ScriptFunctionObject defaultConstructor)
        {
            //https://tc39.github.io/ecma262/#sec-speciesconstructor
            var constructor = obj.Get("constructor");

            if (constructor == ScriptValue.Undefined)
            {
                return(defaultConstructor);
            }

            if (!constructor.IsObject)
            {
                throw CreateTypeError();
            }

            var species = ((ScriptObject)constructor).Get(Symbol.Species);

            if (species == ScriptValue.Undefined || species == ScriptValue.Null)
            {
                return(defaultConstructor);
            }

            if (IsConstructor(species))
            {
                return((ScriptObject)species);
            }

            throw CreateTypeError();
        }
Exemple #2
0
        internal ScriptFunctionObject FunctionInitialise([NotNull] ScriptFunctionObject function, FunctionKind kind, [NotNull] IReadOnlyList <ExpressionNode> parameters, [NotNull] BaseNode body, [NotNull] LexicalEnvironment scope)
        {
            //https://tc39.github.io/ecma262/#sec-functioninitialize
            Debug.Assert(function.IsExtensible && function.GetOwnProperty("length") == null);
            var length = ExpectedArgumentCount(parameters);

            DefinePropertyOrThrow(function, "length", new PropertyDescriptor(length, false, false, true));

            var strict = function.Strict;

            function.Environment      = scope;
            function.FormalParameters = parameters;
            function.ECMAScriptCode   = new FunctionNode(body);
            function.ScriptOrModule   = GetActiveScriptOrModule();
            if (kind == FunctionKind.Arrow)
            {
                function.ThisMode = ThisMode.Lexical;
            }
            else if (strict)
            {
                function.ThisMode = ThisMode.Strict;
            }
            else
            {
                function.ThisMode = ThisMode.Global;
            }
            return(function);
        }
Exemple #3
0
 public FunctionEnvironment([NotNull] ScriptFunctionObject function, ThisBindingStatus thisBindingStatus, ScriptObject homeObject, ScriptObject newTarget) :
     base(function.Agent)
 {
     FunctionObject         = function;
     this.thisBindingStatus = thisBindingStatus;
     this.homeObject        = homeObject;
     NewTarget = newTarget;
 }
Exemple #4
0
 internal ScriptArguments([NotNull] ScriptFunctionObject function, ScriptValue thisValue, ScriptObject newTarget, IReadOnlyList <ScriptValue> arguments)
 {
     Function       = function;
     Agent          = function.Agent;
     ThisValue      = thisValue;
     NewTarget      = newTarget;
     this.arguments = arguments;
 }
Exemple #5
0
        public static ScriptObject AllocateArrayBuffer([NotNull] Agent agent, [NotNull] ScriptFunctionObject constructor, long byteLength)
        {
            var obj   = agent.OrdinaryCreateFromConstructor(constructor, constructor.Realm.ArrayBufferPrototype, SpecialObjectType.ArrayBuffer);
            var block = new byte[byteLength];

            obj.ArrayBuffer.Data = block;
            return(obj);
        }
Exemple #6
0
        public static ScriptFunctionObject CreateBuiltinFunction([NotNull] Realm realm, [NotNull] Func <ScriptArguments, ScriptValue> callback, ScriptObject prototype, int length, string name, ConstructorKind constructorKind = ConstructorKind.None)
        {
            var function = new ScriptFunctionObject(realm, prototype, true, callback, constructorKind);

            function.DefineOwnProperty("length", new PropertyDescriptor(length, false, false, true));
            function.DefineOwnProperty("name", new PropertyDescriptor(name, false, false, true));
            return(function);
        }
        public static LexicalEnvironment NewFunctionEnvironment([NotNull] ScriptFunctionObject function, ScriptObject newTarget)
        {
            //https://tc39.github.io/ecma262/#sec-newfunctionenvironment
            var environmentRecord = new FunctionEnvironment(function,
                                                            function.ThisMode == ThisMode.Lexical ? ThisBindingStatus.Lexical : ThisBindingStatus.Uninitialised,
                                                            function.HomeObject,
                                                            newTarget);

            return(new LexicalEnvironment(environmentRecord, function.Environment));
        }
        private static ScriptValue Revocable([NotNull] ScriptArguments arg)
        {
            //https://tc39.github.io/ecma262/#sec-proxy.revocable
            var proxy = ProxyCreate(arg.Agent, arg[0], arg[1]);

            var revoker = new ScriptFunctionObject(arg.Function.Realm, arg.Function.Realm.FunctionPrototype, true, ProxyRevocation, SpecialObjectType.RevocableProxy);

            revoker.DefineOwnProperty("length", new PropertyDescriptor(0, false, false, true));
            revoker.RevocableProxy = proxy;

            var result = arg.Agent.ObjectCreate(arg.Function.Realm.ObjectPrototype);

            result.CreateDataProperty("proxy", proxy);
            result.CreateDataProperty("revoke", revoker);
            return(result);
        }
Exemple #9
0
        internal void MakeConstructor([NotNull] ScriptFunctionObject function, bool writablePrototype = true, [CanBeNull] ScriptObject prototype = null)
        {
            //https://tc39.github.io/ecma262/#sec-makeconstructor

            Debug.Assert(function.ConstructorKind != ConstructorKind.None);
            Debug.Assert(function.IsExtensible);
            Debug.Assert(!function.HasOwnProperty("prototype"));

            if (prototype == null)
            {
                prototype = ObjectCreate(RunningExecutionContext.Realm.ObjectPrototype);
                DefinePropertyOrThrow(prototype, "constructor", new PropertyDescriptor(function, writablePrototype, false, true));
            }

            DefinePropertyOrThrow(function, "prototype", new PropertyDescriptor(prototype, writablePrototype, false, false));
        }
        private static double MakeDay(double year, double month, double date)
        {
            //https://tc39.github.io/ecma262/#sec-makeday
            if (double.IsInfinity(year) || double.IsInfinity(month) || double.IsInfinity(date))
            {
                return(double.NaN);
            }

            year  = Agent.ToInteger(year);
            month = Agent.ToInteger(month);
            date  = Agent.ToInteger(date);

            year  += Math.Floor(month / 12);
            month %= 12;

            var yearDay  = Math.Floor(TimeFromYear(year) / MILLISECONDS_PER_DAY);
            var monthDay = DayFromMonth((int)month, (int)year);

            return(yearDay + monthDay + date - 1);
        }
Exemple #11
0
        private static (ScriptFunctionObject Resolve, ScriptFunctionObject Reject) CreateResolvingFunctions([NotNull] Realm realm, ScriptObject promise)
        {
            //https://tc39.github.io/ecma262/#sec-createresolvingfunctions
            var resolve = new ScriptFunctionObject(realm, realm.FunctionPrototype, true, arguments =>
            {
                //https://tc39.github.io/ecma262/#sec-promise-resolve-functions
                throw new NotImplementedException();
            }, SpecialObjectType.Promise);

            resolve.Promise.Promise = promise;
            resolve.Promise.Value   = false;
            var reject = new ScriptFunctionObject(realm, realm.FunctionPrototype, true, arguments =>
            {
                //https://tc39.github.io/ecma262/#sec-promise-reject-functions
                throw new NotImplementedException();
            }, SpecialObjectType.Promise);

            reject.Promise.Promise = promise;
            reject.Promise.Value   = false;
            return(Resolve : resolve, Reject : reject);
        }
Exemple #12
0
        //https://tc39.github.io/ecma262/#sec-operations-on-iterator-objects

        internal (ScriptObject Iterator, ScriptFunctionObject NextMethod, bool Done) GetIterator(ScriptValue obj, [CanBeNull] ScriptFunctionObject method = null)
        {
            //https://tc39.github.io/ecma262/#sec-getiterator

            if (method == null)
            {
                method = GetMethod(obj, Symbol.Iterator);
            }

            var iterator = Call(method, obj);

            if (!iterator.IsObject)
            {
                throw CreateTypeError();
            }

            var nextMethod = GetValue(iterator, "next");

            return((ScriptObject)iterator, (ScriptFunctionObject)(ScriptObject)nextMethod, false);
        }
Exemple #13
0
 internal static void MakeMethod([NotNull] ScriptFunctionObject function, ScriptObject homeObject)
 {
     //https://tc39.github.io/ecma262/#sec-makemethod
     function.HomeObject = homeObject;
 }
Exemple #14
0
 internal static void MakeClassConstructor([NotNull] ScriptFunctionObject function)
 {
     //https://tc39.github.io/ecma262/#sec-makeclassconstructor
     Debug.Assert(function.FunctionKind == FunctionKind.Normal);
     function.FunctionKind = FunctionKind.ClassConstructor;
 }
Exemple #15
0
 public static void DefineAccessorProperty([NotNull] ScriptObject scriptObject, ScriptValue property, [CanBeNull] ScriptFunctionObject get, [CanBeNull] ScriptFunctionObject set, bool enumerable = false, bool configurable = true)
 {
     scriptObject.DefineOwnProperty(property, new PropertyDescriptor(get == null ? null : (ScriptValue?)(ScriptValue)get,
                                                                     set == null ? null : (ScriptValue?)(ScriptValue)set,
                                                                     enumerable, configurable));
 }