Пример #1
0
        public void VBScriptEngine_ExecuteCommand_HostConvert()
        {
            var dateHostItem = HostItem.Wrap(engine, new DateTime(2007, 5, 22, 6, 15, 43));

            engine.AddHostObject("date", dateHostItem);
            Assert.AreEqual(dateHostItem.ToString(), engine.ExecuteCommand("eval date"));
        }
Пример #2
0
        public void BugFix_IDispatchExArgLeak_GetProperty_VBScript()
        {
            engine.Dispose();
            engine = new VBScriptEngine();

            WeakReference wr1 = null;
            WeakReference wr2 = null;

            new Action(() =>
            {
                object x1 = Guid.NewGuid();
                wr1       = new WeakReference(x1);
                object x2 = Guid.NewGuid();
                wr2       = new WeakReference(x2);

                engine.Execute(@"
                    class MyClass
                        public property get foo(x1, x2)
                            foo = x1.ToString() & x2.ToString()
                        end property
                    end class
                    set bar = new MyClass
                ");

                var bar  = (DynamicObject)engine.Script.bar;
                var args = new[] { "foo", HostItem.Wrap(engine, x1), HostItem.Wrap(engine, x2) };
                Assert.IsTrue(bar.GetMetaObject(Expression.Constant(bar)).TryGetIndex(args, out var result));
                Assert.AreEqual(x1.ToString() + x2, result);
            })();

            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();
            Assert.IsFalse(wr1.IsAlive);
            Assert.IsFalse(wr2.IsAlive);
        }
Пример #3
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("testObject", testInterface = testObject = new TestObject());
     engine.AddHostObject("testInterface", HostItem.Wrap(engine, testObject, typeof(ITestInterface)));
     engine.AddHostType("Guid", typeof(Guid));
 }
Пример #4
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.DisableCaseInsensitivePropertyLookups     = true;
     engine.AddHostObject("testObject", testInterface = testObject = new TestObject());
     engine.AddHostObject("testInterface", HostItem.Wrap(engine, testObject, typeof(ITestInterface)));
     engine.AddHostType("Guid", typeof(Guid));
 }
Пример #5
0
        internal override object MarshalToScript(object obj, HostItemFlags flags)
        {
            if (obj == null)
            {
                return(DBNull.Value);
            }

            if (obj is Undefined)
            {
                return(null);
            }

            if (obj is Nonexistent)
            {
                return(obj);
            }

            if (obj is Nothing)
            {
                return(null);
            }

            if (engineFlags.HasFlag(V8ScriptEngineFlags.EnableDateTimeConversion) && (obj is DateTime))
            {
                return(obj);
            }

            var hostItem = obj as HostItem;

            if (hostItem != null)
            {
                if ((hostItem.Engine == this) && (hostItem.Flags == flags))
                {
                    return(obj);
                }

                obj = hostItem.Target;
            }

            var hostTarget = obj as HostTarget;

            if ((hostTarget != null) && !(hostTarget is IHostVariable))
            {
                obj = hostTarget.Target;
            }

            var scriptItem = obj as ScriptItem;

            if (scriptItem != null)
            {
                if (scriptItem.Engine == this)
                {
                    return(scriptItem.Unwrap());
                }
            }

            return(HostItem.Wrap(this, hostTarget ?? obj, flags));
        }
Пример #6
0
        internal override object MarshalToScript(object obj, HostItemFlags flags)
        {
            if (obj == null)
            {
                return(DBNull.Value);
            }

            if (obj is Undefined)
            {
                return(null);
            }

            if (obj is Nonexistent)
            {
                return(null);
            }

            var hostItem = obj as HostItem;

            if (hostItem != null)
            {
                if ((hostItem.Engine == this) && (hostItem.Flags == flags))
                {
                    return(obj);
                }

                obj = hostItem.Target;
            }

            var hostTarget = obj as HostTarget;

            if (hostTarget != null)
            {
                obj = hostTarget.Target;
            }

            var scriptItem = obj as ScriptItem;

            if (scriptItem != null)
            {
                if (scriptItem.Engine == this)
                {
                    return(scriptItem.Unwrap());
                }
            }

            return(HostItem.Wrap(this, hostTarget ?? obj, flags));
        }
Пример #7
0
        public static object ToDynamicResult(this object result, ScriptEngine engine)
        {
            if (result is Nonexistent)
            {
                return(Undefined.Value);
            }

            if ((result is HostTarget) || (result is IPropertyBag))
            {
                // Returning an instance of HostTarget (an internal type) isn't likely to be
                // useful. Wrapping it in a dynamic object makes sense in this context. Wrapping
                // a property bag allows it to participate in dynamic invocation chaining, which
                // may be useful when dealing with things like host type collections. HostItem
                // supports dynamic conversion, so the client can unwrap the object if necessary.

                return(HostItem.Wrap(engine, result));
            }

            return(result);
        }
Пример #8
0
        private object MarshalToScriptInternal(object obj, HostItemFlags flags, HashSet <Array> marshaledArraySet)
        {
            if (obj == null)
            {
                if (engineFlags.HasFlag(WindowsScriptEngineFlags.MarshalNullAsDispatch))
                {
                    return(nullDispatch);
                }

                return(DBNull.Value);
            }

            if (obj is Undefined)
            {
                return(null);
            }

            if (obj is Nonexistent)
            {
                return(null);
            }

            if (engineFlags.HasFlag(WindowsScriptEngineFlags.MarshalDecimalAsCurrency) && (obj is decimal))
            {
                return(new CurrencyWrapper(obj));
            }

            var hostItem = obj as HostItem;

            if (hostItem != null)
            {
                if ((hostItem.Engine == this) && (hostItem.Flags == flags))
                {
                    return(obj);
                }

                obj = hostItem.Target;
            }

            var hostTarget = obj as HostTarget;

            if ((hostTarget != null) && !(hostTarget is IHostVariable))
            {
                obj = hostTarget.Target;
            }

            var scriptItem = obj as ScriptItem;

            if (scriptItem != null)
            {
                if (scriptItem.Engine == this)
                {
                    return(scriptItem.Unwrap());
                }
            }

            if (engineFlags.HasFlag(WindowsScriptEngineFlags.MarshalArraysByValue))
            {
                var array = obj as Array;
                if ((array != null) && ((hostTarget == null) || (typeof(Array).IsAssignableFrom(hostTarget.Type))))
                {
                    bool alreadyMarshaled;
                    if (marshaledArraySet != null)
                    {
                        alreadyMarshaled = marshaledArraySet.Contains(array);
                    }
                    else
                    {
                        marshaledArraySet = new HashSet <Array>();
                        alreadyMarshaled  = false;
                    }

                    if (!alreadyMarshaled)
                    {
                        marshaledArraySet.Add(array);
                        var dimensions     = Enumerable.Range(0, array.Rank).ToArray();
                        var marshaledArray = Array.CreateInstance(typeof(object), dimensions.Select(array.GetLength).ToArray(), dimensions.Select(array.GetLowerBound).ToArray());
                        array.Iterate(indices => marshaledArray.SetValue(MarshalToScriptInternal(array.GetValue(indices), flags, marshaledArraySet), indices));
                        return(marshaledArray);
                    }

                    // COM interop can't handle circularly referenced arrays
                    return(MarshalToScriptInternal(null, flags, marshaledArraySet));
                }
            }

            return(HostItem.Wrap(this, hostTarget ?? obj, flags));
        }