view Jint.Runtime/VM/Box.cs @ 6:a6329b092499

Added scopes, function builder
author cin
date Wed, 30 Oct 2013 17:38:35 +0400
parents cb13da6e3349
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jint.Runtime.VM.OpCodes;
using System.ComponentModel;

namespace Jint.Runtime.VM {
    class Box<T>: AbstractBox, IGettter<T>, ISetter<T> {

        public T holdingValue;
        object[] m_impl;
        RuntimeContext m_runtime;
        TypeConverter m_converter;

        public Box(T value, RuntimeContext runtime): base(typeof(T)) {
            if (runtime == null)
                throw new ArgumentNullException("runtime");

            holdingValue = value;
            m_runtime = runtime;
        }

        object[] Impl {
            get {
                if (m_impl == null)
                    m_impl = m_runtime.GetImpl(typeof(T));
                return m_impl;
            }
        }

        TypeConverter TypeConverter {
            get {
                if (m_converter == null)
                    m_converter = TypeDescriptor.GetConverter(typeof(T));
                return m_converter;
            }
        }

        public T Get() {
            return holdingValue;
        }

        public void Set(T value) {
            holdingValue = value;
        }

        public override void InvokeBinaryOperation(Codes code, int arg2, int dest, Frame frame) {
            var op = (BinaryOperation<T>)Impl[(int)code];
            frame.SetValue(dest, op(holdingValue, frame.GetValue<T>(arg2)));
        }

        public override void InvokeUnaryOperation(Codes code, int dest, Frame frame) {
            var op = (UnaryOperation<T>)Impl[(int)code];
            frame.SetValue(dest, op(holdingValue));
        }

        public override int InvokeCompareOperation(int arg2, Frame frame) {
            var op = (CompareOperation<T>)Impl[(int)Codes.Cmp];
            return op(holdingValue, frame.GetValue<T>(arg2));
        }

        public override bool InvokeEqualityOperation(int arg2, Frame frame) {
            var op = (EqualityOperation<T>)Impl[(int)Codes.Cmp];
            return op(holdingValue, frame.GetValue<T>(arg2));
        }

        public override T2 Convert<T2>() {
            return (T2)TypeConverter.ConvertTo(holdingValue, typeof(T2));
        }

		public override void CopyTo (Frame frame, int dest)
		{
			frame.SetValue (dest, holdingValue);
		}
    }
}