示例#1
0
        public static string Typeof(TsObject obj)
        {
            switch (obj.Type)
            {
            case VariableType.Array1:
                return("array1");

            case VariableType.Array2:
                return("array2");

            case VariableType.Null:
                return("null");

            case VariableType.Real:
                return("real");

            case VariableType.String:
                return("string");

            case VariableType.Delegate:
                return("script");

            case VariableType.Instance:
                return(obj.GetInstanceUnchecked().ObjectType);

            default:
                return("unknown");
            }
        }
示例#2
0
        /// <summary>
        /// Sets the value at the given index in the 1D array held by this object.
        /// </summary>
        /// <param name="index">The array index.</param>
        /// <param name="right">The value of the index.</param>
        public void ArraySet(int index, TsObject right)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            if (Type != VariableType.Array1)
            {
                Type = VariableType.Array1;
                var temp = new TsObject[index + 1];
                temp[index] = right;
                Value       = new TsMutableValue <TsObject[]>(temp);
                return;
            }
            var self = (TsMutableValue <TsObject[]>)Value;
            var arr  = self.StrongValue;

            if (index >= arr.Length)
            {
                var temp = new TsObject[index + 1];
                Array.Copy(arr, 0, temp, 0, arr.Length);
                arr = temp;
                self.StrongValue = temp;
            }
            arr[index] = right;
        }
示例#3
0
        /// <summary>
        /// Gets the names of all of the variables defined by an instance.
        /// </summary>
        /// <param name="inst">The instance to get the variable names from</param>
        /// <returns></returns>
        public static TsObject[] VariableInstanceGetNames(ITsInstance inst)
        {
            TsObject[] arr;
            int        i;

            switch (inst)
            {
            case TsInstance ts:
                arr = new TsObject[ts._members.Count];
                i   = 0;
                foreach (var key in ts._members.Keys)
                {
                    arr[i++] = key;
                }

                return(arr);

            case DynamicInstance di:
                arr = new TsObject[di._members.Count];
                i   = 0;
                foreach (var key in di._members.Keys)
                {
                    arr[i++] = key;
                }

                return(arr);

            default:
                throw new InvalidOperationException($"Could not get variable names from instance of type {inst.ObjectType}");
            }
        }
示例#4
0
 /// <summary>
 /// Gets the value of a global variable.
 /// </summary>
 /// <param name="name">The name of the gloable variable</param>
 /// <returns></returns>
 public static TsObject VariableGlobalGet(string name)
 {
     if (TsInstance.Global._members.TryGetValue(name, out var result))
     {
         return(result);
     }
     return(TsObject.Empty());
 }
示例#5
0
 /// <summary>
 /// Converts any TaffyScript object to a TaffyScript string.
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static TsObject ToString(TsObject obj)
 {
     if (obj.Type != VariableType.String)
     {
         return(new TsObject(obj.ToString()));
     }
     else
     {
         return(obj);
     }
 }
示例#6
0
        /// <summary>
        /// Gets the names of all the global variables.
        /// </summary>
        /// <returns></returns>
        public static TsObject[] VariableGlobalGetNames()
        {
            var arr = new TsObject[TsInstance.Global._members.Count];
            int i   = 0;

            foreach (var key in TsInstance.Global._members.Keys)
            {
                arr[i++] = key;
            }

            return(arr);
        }
示例#7
0
        public static TsObject InstanceCreate(ITsInstance inst, TsObject[] args)
        {
            var type = (string)args[0];

            if (!Constructors.TryGetValue(type, out var ctor))
            {
                throw new ArgumentException($"Cannot create instance of '{type}': Type doesn't exist", "type");
            }

            TsObject[] ctorArgs = null;
            if (args.Length > 1)
            {
                ctorArgs = new TsObject[args.Length - 1];
                Array.Copy(args, 1, ctorArgs, 0, args.Length - 1);
            }
            return(new TsObject(ctor(ctorArgs)));
        }
示例#8
0
        /// <summary>
        /// Copies one TS array into another, resizing the destination if needed.
        /// </summary>
        /// <param name="dest"></param>
        /// <param name="destIndex"></param>
        /// <param name="src"></param>
        /// <param name="srcIndex"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static TsObject ArrayCopy(TsObject src, int srcIndex, TsObject dest, int destIndex, int length)
        {
            //We need to get the value wrapper in case we need to resize the internal array.
            var destWrapper = dest.Value as TsMutableValue <TsObject[]> ?? throw new ArgumentException("Can only copy 1D arrays", "dest");
            var srcWrapper  = src.Value as TsMutableValue <TsObject[]> ?? throw new ArgumentException("Can only copy 1D arrays", "src");
            var destValue   = destWrapper.StrongValue;
            var srcValue    = srcWrapper.StrongValue;

            if (destIndex + length >= destValue.Length)
            {
                var temp = new TsObject[destIndex + length + 1];
                Array.Copy(destValue, 0, temp, 0, destValue.Length);
                destValue = temp;
                destWrapper.StrongValue = destValue;
            }
            Array.Copy(srcValue, srcIndex, destValue, destIndex, length);
            return(TsObject.Empty());
        }
示例#9
0
        public static TsObject ArrayCreate(ITsInstance target, TsObject[] args)
        {
            var size  = args[0].GetInt();
            var value = TsObject.Empty();

            if (args.Length > 1)
            {
                value = args[1];
            }
            var result = new TsObject[size];

            for (var i = 0; i < size; ++i)
            {
                result[i] = value;
            }

            return(new TsObject(result));
        }
示例#10
0
        public static TsObject CallInstanceScript(ITsInstance inst, TsObject[] args)
        {
            if (TsReflection.TryGetScript((string)args[1], (string)args[2], out var ev))
            {
                TsObject[] copy;
                if (args.Length > 3)
                {
                    copy = new TsObject[args.Length - 3];
                    Array.Copy(args, 3, copy, 0, copy.Length);
                }
                else
                {
                    copy = null;
                }

                return(ev.Invoke(args[0].GetInstance(), copy));
            }
            return(TsObject.Empty());
        }
示例#11
0
        public static TsObject EventPerform(ITsInstance inst, TsObject[] args)
        {
            if (inst.TryGetDelegate((string)args[1], out var ev))
            {
                TsObject[] copy;
                if (args.Length > 2)
                {
                    copy = new TsObject[args.Length - 2];
                    Array.Copy(args, 2, copy, 0, copy.Length);
                }
                else
                {
                    copy = null;
                }

                return(ev.Invoke(args[0].GetInstance(), copy));
            }
            return(TsObject.Empty());
        }
示例#12
0
        public static TsObject CallGlobalScript(ITsInstance inst, TsObject[] args)
        {
            if (args.Length < 1)
            {
                throw new ArgumentException("You must pass at least a script name to script_execute.");
            }
            var name = args[0].GetString();

            if (!TsReflection.GlobalScripts.TryGetValue(name, out var function))
            {
                throw new ArgumentException($"Tried to execute a non-existant function: {name}");
            }
            var parameters = new TsObject[args.Length - 1];

            if (parameters.Length != 0)
            {
                Array.Copy(args, 1, parameters, 0, parameters.Length);
            }
            return(function.Invoke(inst, parameters));
        }
示例#13
0
        /// <summary>
        /// Sets the value at the given indeces in the 2D array held by this object.
        /// </summary>
        /// <param name="index1">The index of the first dimension.</param>
        /// <param name="index2">The index of the second dimension.</param>
        /// <param name="right">The value of the index.</param>
        public void ArraySet(int index1, int index2, TsObject right)
        {
            if (index1 < 0 || index2 < 0)
            {
                throw new ArgumentOutOfRangeException($"{(index1 < 0 ? nameof(index1) : nameof(index2))}");
            }

            if (Type != VariableType.Array2)
            {
                Type = VariableType.Array2;
                var temp  = new TsObject[index1 + 1][];
                var inner = new TsObject[index2 + 1];
                inner[index2] = right;
                temp[index1]  = inner;
                Value         = new TsMutableValue <TsObject[][]>(temp);
                return;
            }

            var self = (TsMutableValue <TsObject[][]>)Value;

            if (index1 >= self.StrongValue.Length)
            {
                var temp = new TsObject[index1 + 1][];
                Array.Copy(self.StrongValue, 0, temp, 0, self.StrongValue.Length);
                self.StrongValue = temp;
            }
            if (self.StrongValue[index1] == null)
            {
                self.StrongValue[index1] = new TsObject[index2 + 1];
            }
            else if (index2 >= self.StrongValue[index1].Length)
            {
                var temp = new TsObject[index2 + 1];
                Array.Copy(self.StrongValue[index1], 0, temp, 0, self.StrongValue[index1].Length);
                self.StrongValue[index1] = temp;
            }
            self.StrongValue[index1][index2] = right;
        }
示例#14
0
 /// <summary>
 /// Gets the value at the given index in the 1D array held by this object.
 /// </summary>
 /// <param name="index">The index of the value.</param>
 /// <returns></returns>
 public TsObject ArrayGet(TsObject index)
 => ArrayGet((int)index);
示例#15
0
 /// <summary>
 /// Sets the value at the given indeces in the 2D array held by this object.
 /// </summary>
 /// <param name="index1">The index of the first dimension.</param>
 /// <param name="index2">The index of the second dimension.</param>
 /// <param name="right">The value of the index.</param>
 public void ArraySet(TsObject index1, int index2, TsObject right)
 => ArraySet((int)index1, index2, right);
示例#16
0
 /// <summary>
 /// Sets a variable of the given name on the instance with an id that matches the value of this object.
 /// </summary>
 /// <param name="name">The name of the variable.</param>
 /// <param name="value">The value of the variable.</param>
 public void MemberSet(string name, TsObject value)
 {
     GetInstance()[name] = value;
 }
示例#17
0
 public static bool IsNull(TsObject obj)
 {
     return(obj.Type == VariableType.Null);
 }
示例#18
0
 public static bool IsDelegate(TsObject obj)
 {
     return(obj.Type == VariableType.Delegate);
 }
示例#19
0
 public static bool IsString(TsObject obj)
 {
     return(obj.Type == VariableType.String);
 }
示例#20
0
 public static bool IsReal(TsObject obj)
 {
     return(obj.Type == VariableType.Real);
 }
示例#21
0
 /// <summary>
 /// Gets the value at the given indeces in the 2D array held by this object.
 /// </summary>
 /// <param name="index1">The index of the first dimension.</param>
 /// <param name="index2">The index of the second dimension.</param>
 /// <returns></returns>
 public TsObject ArrayGet(TsObject index1, int index2)
 => ArrayGet((int)index1, index2);
示例#22
0
 /// <summary>
 /// Sets a variable on an instance
 /// </summary>
 /// <param name="inst">The instance to set the variable on</param>
 /// <param name="name">The name of the variable</param>
 /// <param name="value">The value to set</param>
 public static void VariableInstanceSet(ITsInstance inst, string name, TsObject value)
 {
     inst.SetMember(name, value);
 }
示例#23
0
 /// <summary>
 /// Sets the value of a global variable.
 /// </summary>
 /// <param name="name">The name of the global variable</param>
 /// <param name="value">The value to set</param>
 public static void VariableGlobalSet(string name, TsObject value)
 {
     TsInstance.Global._members[name] = value;
 }
示例#24
0
 /// <summary>
 /// Gets the value at the given indeces in the 2D array held by this object.
 /// </summary>
 /// <param name="index1">The index of the first dimension.</param>
 /// <param name="index2">The index of the second dimension.</param>
 /// <returns></returns>
 public TsObject ArrayGet(int index1, TsObject index2)
 => ArrayGet(index1, (int)index2);
示例#25
0
        #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

        #endregion

        #region Base Class Library

        public static bool IsArray(TsObject obj)
        {
            return(obj.Type == VariableType.Array1 || obj.Type == VariableType.Array2);
        }