Example #1
0
        /// <summary>
        /// Recursively retrieves named properties. Returns true if all property names were found.
        /// If a value along the chain is not an object, false is returned and that value is the result.
        /// </summary>
        public bool TryGetNested(out JS.Value result, params string[] propertyNames)
        {
            var searchScope = Pointer;
            var temp        = new Rooted <JS.Value>(Context, JS.Value.Undefined);

            for (int i = 0, l = propertyNames.Length; i < l; i++)
            {
                var name = propertyNames[i];
                temp = searchScope.GetProperty(Context, name);

                if (i == (l - 1))
                {
                    result = temp.Value;
                    return(true);
                }

                if (temp.Value.ValueType != JSValueType.OBJECT)
                {
                    result = temp.Value;
                    return(false);
                }

                searchScope = temp.Value.AsObject;
            }

            throw new Exception("Unexpected");
        }
Example #2
0
        public void ObjectBuilderTest()
        {
            using (var tc = new TestContext())
                using (var obj = new JSObjectBuilder(tc)) {
                    tc.Global["obj"] = obj;
                    obj["a"]         = new JS.Value(5);

                    var evalResult = tc.Context.Evaluate(tc.Global, "obj.a");
                    Assert.AreEqual(5, evalResult.Value.ToManaged(tc));
                }
        }
Example #3
0
        public void ValueFromObject()
        {
            using (var tc = new TestContext()) {
                // Implicit conversion from Rooted<JSObjectPtr> to JS.Value
                JS.Value val = tc.Global.Root;
                tc.Global["g"] = val;

                var evalResult = tc.Context.Evaluate(tc.Global, "g");
                Assert.AreEqual(val, evalResult.Value);
                Assert.AreEqual(tc.Global.Pointer, evalResult.Value.AsObject);
            }
        }
Example #4
0
        public void NewInstanceTest()
        {
            using (var tc = new TestContext()) {
                tc.Context.Evaluate(tc.Global, "function cls (x) { this.x = x; };");

                var cls      = tc.Global["cls"];
                var arg1     = new JS.Value(1.5);
                var instance = new JSObjectReference(
                    tc, cls.InvokeConstructor(tc, arg1)
                    );

                Assert.AreEqual(arg1, instance["x"]);
            }
        }
Example #5
0
 /// <summary>
 /// If value does not contain an object, this will throw.
 /// </summary>
 public JSObjectReference(JSContextPtr context, JS.Value value)
     : this(context, value.AsObject)
 {
 }
Example #6
0
 public override string ToString()
 {
     JS.Value self = this;
     return(self.ToManagedString(Context));
 }