Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 88 lines (77 sloc) 2.8 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IronJS.Runtime
{
    public static class TypeTags
    {
        public const uint Box = 0x00000000u;
        public const uint Bool = 0xFFFFFF01u;
        public const uint Number = 0xFFFFFF02u;
        public const uint Clr = 0xFFFFFF03u;
        public const uint String = 0xFFFFFF04u;
        public const uint SuffixString = 0xFFFFFF05u;
        public const uint Undefined = 0xFFFFFF06u;
        public const uint Object = 0xFFFFFF07u;
        public const uint Function = 0xFFFFFF08u;

        private static readonly Dictionary<uint, string> names = new Dictionary<uint, string>
        {
            { Box, "internal" },
            { Bool, "boolean" },
            { Number, "number" },
            { Clr, "clr" },
            { String, "string" },
            { SuffixString, "string" },
            { Undefined, "undefined" },
            { Object, "object" },
            { Function, "function" }
        };

        public static string GetName(uint tag)
        {
            return names[tag];
        }
    }

    public static class TypeTag
    {
        static Dictionary<Type, uint> map
            = new Dictionary<Type, uint>();

        static TypeTag()
        {
            map.Add(typeof(bool), TypeTags.Bool);
            map.Add(typeof(double), TypeTags.Number);
            map.Add(typeof(string), TypeTags.String);
            map.Add(typeof(SuffixString), TypeTags.SuffixString);
            map.Add(typeof(Undefined), TypeTags.Undefined);
            map.Add(typeof(FunctionObject), TypeTags.Function);
            map.Add(typeof(ArrayObject), TypeTags.Object);
            map.Add(typeof(CommonObject), TypeTags.Object);
            map.Add(typeof(ValueObject), TypeTags.Object);
            map.Add(typeof(StringObject), TypeTags.Object);
            map.Add(typeof(NumberObject), TypeTags.Object);
            map.Add(typeof(ErrorObject), TypeTags.Object);
            map.Add(typeof(MathObject), TypeTags.Object);
            map.Add(typeof(BooleanObject), TypeTags.Object);
            map.Add(typeof(RegExpObject), TypeTags.Object);
            map.Add(typeof(DateObject), TypeTags.Object);
            map.Add(typeof(BoxedValue), TypeTags.Box);
        }

        public static uint OfType(Type type)
        {
            uint tag;

            if (map.TryGetValue(type, out tag))
            {
                return tag;
            }

            return type.IsSubclassOf(typeof(CommonObject))
                    ? TypeTags.Object
                    : TypeTags.Clr;
        }

        public static uint OfObject(object o)
        {
            if (o == null)
                return TypeTags.Clr;

            return OfType(o.GetType());
        }
    }
}
Something went wrong with that request. Please try again.