示例#1
0
        /// <summary>
        /// Gets an object's property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="source">The JavaScript value</param>
        /// <param name="name">The name of the property</param>
        /// <returns>The value of the property</returns>
        public static JsValue GetProperty(this JsValue source, string name)
        {
            JsPropertyId id          = JsPropertyId.FromString(name);
            JsValue      resultValue = source.GetProperty(id);

            return(resultValue);
        }
示例#2
0
        /// <summary>
        /// Deletes an object's property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="source">The JavaScript value</param>
        /// <param name="propertyName">The name of the property</param>
        /// <param name="useStrictRules">The property set should follow strict mode rules</param>
        /// <returns>Whether the property was deleted</returns>
        public static JsValue DeleteProperty(this JsValue source, string propertyName, bool useStrictRules)
        {
            JsPropertyId propertyId  = JsPropertyId.FromString(propertyName);
            JsValue      resultValue = source.DeleteProperty(propertyId, useStrictRules);

            return(resultValue);
        }
示例#3
0
        /// <summary>
        /// Defines a new object's own property from a property descriptor
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="source">The JavaScript value</param>
        /// <param name="propertyName">The name of the property</param>
        /// <param name="propertyDescriptor">The property descriptor</param>
        /// <returns>Whether the property was defined</returns>
        public static bool DefineProperty(this JsValue source, string propertyName, JsValue propertyDescriptor)
        {
            JsPropertyId propertyId = JsPropertyId.FromString(propertyName);
            bool         result     = source.DefineProperty(propertyId, propertyDescriptor);

            return(result);
        }
示例#4
0
        /// <summary>
        /// Gets a property descriptor for an object's own property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="source">The JavaScript value</param>
        /// <param name="propertyName">The name of the property</param>
        /// <returns>The property descriptor</returns>
        public static JsValue GetOwnPropertyDescriptor(this JsValue source, string propertyName)
        {
            JsPropertyId propertyId  = JsPropertyId.FromString(propertyName);
            JsValue      resultValue = source.GetOwnPropertyDescriptor(propertyId);

            return(resultValue);
        }
示例#5
0
        protected override bool InnerHasVariable(string variableName)
        {
            bool result = _dispatcher.Invoke(() =>
            {
                using (CreateJsScope())
                {
                    try
                    {
                        JsValue globalObj       = JsValue.GlobalObject;
                        JsPropertyId variableId = JsPropertyId.FromString(variableName);
                        bool variableExist      = globalObj.HasProperty(variableId);

                        if (variableExist)
                        {
                            JsValue variableValue = globalObj.GetProperty(variableId);
                            variableExist         = variableValue.ValueType != JsValueType.Undefined;
                        }

                        return(variableExist);
                    }
                    catch (OriginalException e)
                    {
                        throw WrapJsException(e);
                    }
                }
            });

            return(result);
        }
示例#6
0
        /// <summary>
        /// Determines whether an object has a non-inherited property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="source">The JavaScript value</param>
        /// <param name="propertyName">The name of the property</param>
        /// <returns>Whether the object has the non-inherited property</returns>
        public static bool HasOwnProperty(this JsValue source, string propertyName)
        {
            JsPropertyId propertyId = JsPropertyId.FromString(propertyName);
            bool         result     = source.HasOwnProperty(propertyId);

            return(result);
        }
示例#7
0
        protected override object InnerCallFunction(string functionName, params object[] args)
        {
            object result = _dispatcher.Invoke(() =>
            {
                using (CreateJsScope())
                {
                    try
                    {
                        JsValue globalObj       = JsValue.GlobalObject;
                        JsPropertyId functionId = JsPropertyId.FromString(functionName);

                        bool functionExist = globalObj.HasProperty(functionId);
                        if (!functionExist)
                        {
                            throw new JsRuntimeException(
                                string.Format(CoreStrings.Runtime_FunctionNotExist, functionName));
                        }

                        JsValue resultValue;
                        JsValue functionValue = globalObj.GetProperty(functionId);

                        if (args.Length > 0)
                        {
                            JsValue[] processedArgs = MapToScriptType(args);

                            foreach (JsValue processedArg in processedArgs)
                            {
                                AddReferenceToValue(processedArg);
                            }

                            JsValue[] allProcessedArgs = new[] { globalObj }
                            .Concat(processedArgs)
                            .ToArray()
                            ;
                            resultValue = functionValue.CallFunction(allProcessedArgs);

                            foreach (JsValue processedArg in processedArgs)
                            {
                                RemoveReferenceToValue(processedArg);
                            }
                        }
                        else
                        {
                            resultValue = functionValue.CallFunction(globalObj);
                        }

                        return(MapToHostType(resultValue));
                    }
                    catch (OriginalJsException e)
                    {
                        throw ConvertJsExceptionToJsRuntimeException(e);
                    }
                }
            });

            return(result);
        }
示例#8
0
        /// <summary>
        /// Makes a mapping of value from the script type to a host type
        /// </summary>
        /// <param name="value">The source value</param>
        /// <returns>The mapped value</returns>
        public object MapToHostType(JsValue value)
        {
            JsValueType valueType = value.ValueType;
            object      result    = null;

            switch (valueType)
            {
            case JsValueType.Null:
                result = null;
                break;

            case JsValueType.Undefined:                     // Undefined is not mapped
                result = value;
                break;

            case JsValueType.Boolean:
                result = value.ToBoolean();
                break;

            case JsValueType.Number:
                result = NumericHelpers.CastDoubleValueToCorrectType(value.ToDouble());
                break;

            case JsValueType.String:
                result = value.ToString();
                break;

            case JsValueType.Function:
                JsPropertyId externalObjectPropertyId = JsPropertyId.FromString(ExternalObjectPropertyName);
                if (value.HasProperty(externalObjectPropertyId))
                {
                    JsValue externalObjectValue = value.GetProperty(externalObjectPropertyId);
                    result = externalObjectValue.HasExternalData ?
                             GCHandle.FromIntPtr(externalObjectValue.ExternalData).Target : null;
                }

                result ??= value.ConvertToObject();
                break;

            case JsValueType.Object:
            case JsValueType.Error:
            case JsValueType.Array:
            case JsValueType.Symbol:
            case JsValueType.ArrayBuffer:
            case JsValueType.TypedArray:
            case JsValueType.DataView:
                result = value.HasExternalData ?
                         GCHandle.FromIntPtr(value.ExternalData).Target : value.ConvertToObject();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(result);
        }
示例#9
0
        public static IEnumerable <string> EnumeratePropertyNames(this JsValue val)
        {
            var lenId = JsPropertyId.FromString("length");
            var names = val.GetOwnPropertyNames();
            var len   = names.GetProperty(lenId).ToInt32();

            for (var i = 0; i < len; i++)
            {
                yield return(names.GetIndexedProperty(JsValue.FromInt(i)).ToString());
            }
        }
示例#10
0
        protected override void InnerRemoveVariable(string variableName)
        {
            InvokeScript(() =>
            {
                JsValue globalObj       = JsValue.GlobalObject;
                JsPropertyId variableId = JsPropertyId.FromString(variableName);

                if (globalObj.HasProperty(variableId))
                {
                    globalObj.SetProperty(variableId, JsValue.Undefined, true);
                }
            });
        }
        private JToken VisitObject(JsValue value)
        {
            var jsonObject = new JObject();
            var properties = Visit(value.GetOwnPropertyNames()).ToObject <string[]>();

            foreach (var property in properties)
            {
                var propertyId    = JsPropertyId.FromString(property);
                var propertyValue = value.GetProperty(propertyId);
                jsonObject.Add(property, Visit(propertyValue));
            }

            return(jsonObject);
        }
        private JsValue VisitObject(JObject token)
        {
            var jsonObject = AddRef(JsValue.CreateObject());

            foreach (var entry in token)
            {
                var value      = Visit(entry.Value);
                var propertyId = JsPropertyId.FromString(entry.Key);
                jsonObject.SetProperty(propertyId, value);
                value.Release();
            }

            return(jsonObject);
        }
示例#13
0
        public static IEnumerable <JsValue> EnumerateArrayValues(this JsValue val)
        {
            if (val.ValueType != JsValueType.Array)
            {
                throw new InvalidOperationException("Can't enumerate non array value");
            }
            var lenId = JsPropertyId.FromString("length");
            var len   = val.GetProperty(lenId).ToInt32();

            for (var i = 0; i < len; i++)
            {
                yield return(val.GetIndexedProperty(JsValue.FromInt(i)));
            }
        }
        private JToken VisitArray(JsValue value)
        {
            var array      = new JArray();
            var propertyId = JsPropertyId.FromString("length");
            var length     = value.GetProperty(propertyId).ToInt32();

            for (var i = 0; i < length; ++i)
            {
                var index   = JsValue.FromInt(i);
                var element = value.GetIndexedProperty(index);
                array.Add(Visit(element));
            }

            return(array);
        }
 public void JsPropertyIdCanBeCreated()
 {
     using (var rt = BaristaRuntimeFactory.CreateRuntime())
     {
         using (var ctx = rt.CreateContext())
         {
             using (ctx.Scope())
             {
                 //TODO: This might not be the final signature -- creating a propertyid requires a current context.
                 var propertyId = JsPropertyId.FromString(rt.Engine, "foo");
                 Assert.NotNull(propertyId);
                 propertyId.Dispose();
             }
         }
     }
 }
示例#16
0
        protected override object InnerCallFunction(string functionName, params object[] args)
        {
            object result = InvokeScript(() =>
            {
                JsValue globalObj       = JsValue.GlobalObject;
                JsPropertyId functionId = JsPropertyId.FromString(functionName);

                bool functionExist = globalObj.HasProperty(functionId);
                if (!functionExist)
                {
                    throw new JsRuntimeException(
                        string.Format(CoreStrings.Runtime_FunctionNotExist, functionName));
                }

                JsValue resultValue;
                JsValue functionValue = globalObj.GetProperty(functionId);

                if (args.Length > 0)
                {
                    JsValue[] processedArgs = MapToScriptType(args);

                    foreach (JsValue processedArg in processedArgs)
                    {
                        AddReferenceToValue(processedArg);
                    }

                    JsValue[] allProcessedArgs = new[] { globalObj }.Concat(processedArgs).ToArray();
                    resultValue = functionValue.CallFunction(allProcessedArgs);

                    foreach (JsValue processedArg in processedArgs)
                    {
                        RemoveReferenceToValue(processedArg);
                    }
                }
                else
                {
                    resultValue = functionValue.CallFunction(globalObj);
                }
                //modified by chuan.yin in 2017/4/29
                //return MapToHostType(resultValue);
                return(ConvertJsObjectToNetObject(resultValue));
            });

            return(result);
        }
示例#17
0
        protected override bool InnerHasVariable(string variableName)
        {
            bool result = InvokeScript(() =>
            {
                JsValue globalObj       = JsValue.GlobalObject;
                JsPropertyId variableId = JsPropertyId.FromString(variableName);
                bool variableExist      = globalObj.HasProperty(variableId);

                if (variableExist)
                {
                    JsValue variableValue = globalObj.GetProperty(variableId);
                    variableExist         = variableValue.ValueType != JsValueType.Undefined;
                }

                return(variableExist);
            });

            return(result);
        }
示例#18
0
        protected override void InnerRemoveVariable(string variableName)
        {
            _dispatcher.Invoke(() =>
            {
                using (CreateJsScope())
                {
                    try
                    {
                        JsValue globalObj       = JsValue.GlobalObject;
                        JsPropertyId variableId = JsPropertyId.FromString(variableName);

                        if (globalObj.HasProperty(variableId))
                        {
                            globalObj.SetProperty(variableId, JsValue.Undefined, true);
                        }
                    }
                    catch (OriginalException e)
                    {
                        throw WrapJsException(e);
                    }
                }
            });
        }
		internal static extern JsErrorCode JsCopyPropertyIdUtf8(JsPropertyId propertyId, byte[] buffer,
			UIntPtr bufferSize, out UIntPtr length);
		internal static extern JsErrorCode JsCreatePropertyIdUtf8(string name, UIntPtr length,
			out JsPropertyId propertyId);
		internal static extern JsErrorCode JsDefineProperty(JsValue obj, JsPropertyId propertyId,
			JsValue propertyDescriptor, out bool result);
示例#22
0
 internal static extern JsErrorCode JsDeleteProperty(JsValue obj, JsPropertyId propertyId, bool useStrictRules,
                                                     out JsValue result);
示例#23
0
 public static IEnumerable <KeyValuePair <string, JsValue> > EnumerateProperties(this JsValue _this)
 => from nameVal in EnumerateArrayValues(_this.GetOwnPropertyNames())
 let name = nameVal.ToString()
            let propId                     = JsPropertyId.FromString(name)
                                   let val = _this.GetProperty(propId)
                                             select new KeyValuePair <string, JsValue>(name, val);
		internal static extern JsErrorCode JsGetPropertyIdFromSymbol(JsValue symbol, out JsPropertyId propertyId);
		internal static extern JsErrorCode JsGetSymbolFromPropertyId(JsPropertyId propertyId, out JsValue symbol);
		/// <summary>
		/// Defines a new object's own property from a property descriptor
		/// </summary>
		/// <remarks>
		/// Requires an active script context.
		/// </remarks>
		/// <param name="propertyId">The ID of the property</param>
		/// <param name="propertyDescriptor">The property descriptor</param>
		/// <returns>Whether the property was defined</returns>
		public bool DefineProperty(JsPropertyId propertyId, JsValue propertyDescriptor)
		{
			bool result;
			JsErrorHelpers.ThrowIfError(NativeMethods.JsDefineProperty(this, propertyId, propertyDescriptor, out result));

			return result;
		}
示例#27
0
 internal static extern JsErrorCode JsSetProperty(JsValue obj, JsPropertyId propertyId, JsValue value,
                                                  bool useStrictRules);
		/// <summary>
		/// Gets a property descriptor for an object's own property
		/// </summary>
		/// <remarks>
		/// Requires an active script context.
		/// </remarks>
		/// <param name="propertyId">The ID of the property</param>
		/// <returns>The property descriptor</returns>
		public JsValue GetOwnPropertyDescriptor(JsPropertyId propertyId)
		{
			JsValue descriptorReference;
			JsErrorHelpers.ThrowIfError(NativeMethods.JsGetOwnPropertyDescriptor(this, propertyId, out descriptorReference));

			return descriptorReference;
		}
示例#29
0
 internal static extern JsErrorCode JsGetPropertyIdFromSymbol(JsValue symbol, out JsPropertyId propertyId);
示例#30
0
 internal static extern JsErrorCode JsGetSymbolFromPropertyId(JsPropertyId propertyId, out JsValue symbol);
示例#31
0
 internal static extern JsErrorCode JsGetPropertyIdType(JsPropertyId propertyId,
                                                        out JsPropertyIdType propertyIdType);
示例#32
0
 internal static extern JsErrorCode JsDefineProperty(JsValue obj, JsPropertyId propertyId,
                                                     JsValue propertyDescriptor, out bool result);
		internal static extern JsErrorCode JsGetPropertyIdFromName(string name, out JsPropertyId propertyId);
		internal static extern JsErrorCode JsGetPropertyNameFromId(JsPropertyId propertyId, out string name);
示例#35
0
 internal static extern JsErrorCode JsGetOwnPropertyDescriptor(JsValue obj, JsPropertyId propertyId,
                                                               out JsValue propertyDescriptor);
		internal static extern JsErrorCode JsGetPropertyIdType(JsPropertyId propertyId,
			out JsPropertyIdType propertyIdType);
		internal static extern JsErrorCode JsHasProperty(JsValue obj, JsPropertyId propertyId, out bool hasProperty);
		/// <summary>
		/// Determines whether an object has a property
		/// </summary>
		/// <remarks>
		/// Requires an active script context.
		/// </remarks>
		/// <param name="propertyId">The ID of the property</param>
		/// <returns>Whether the object (or a prototype) has the property</returns>
		public bool HasProperty(JsPropertyId propertyId)
		{
			bool hasProperty;
			JsErrorHelpers.ThrowIfError(NativeMethods.JsHasProperty(this, propertyId, out hasProperty));

			return hasProperty;
		}
		internal static extern JsErrorCode JsDeleteProperty(JsValue obj, JsPropertyId propertyId, bool useStrictRules,
			out JsValue result);
		/// <summary>
		/// Deletes an object's property
		/// </summary>
		/// <remarks>
		/// Requires an active script context.
		/// </remarks>
		/// <param name="propertyId">The ID of the property</param>
		/// <param name="useStrictRules">The property set should follow strict mode rules</param>
		/// <returns>Whether the property was deleted</returns>
		public JsValue DeleteProperty(JsPropertyId propertyId, bool useStrictRules)
		{
			JsValue returnReference;
			JsErrorHelpers.ThrowIfError(NativeMethods.JsDeleteProperty(this, propertyId, useStrictRules, out returnReference));

			return returnReference;
		}
示例#41
0
 internal static extern JsErrorCode JsHasProperty(JsValue obj, JsPropertyId propertyId, out bool hasProperty);
		internal static extern JsErrorCode JsGetProperty(JsValue obj, JsPropertyId propertyId, out JsValue value);
		/// <summary>
		/// Sets an object's property
		/// </summary>
		/// <remarks>
		/// Requires an active script context.
		/// </remarks>
		/// <param name="id">The ID of the property</param>
		/// <param name="value">The new value of the property</param>
		/// <param name="useStrictRules">The property set should follow strict mode rules</param>
		public void SetProperty(JsPropertyId id, JsValue value, bool useStrictRules)
		{
			JsErrorHelpers.ThrowIfError(NativeMethods.JsSetProperty(this, id, value, useStrictRules));
		}
		internal static extern JsErrorCode JsGetOwnPropertyDescriptor(JsValue obj, JsPropertyId propertyId,
			out JsValue propertyDescriptor);
		internal static extern JsErrorCode JsSetProperty(JsValue obj, JsPropertyId propertyId, JsValue value,
			bool useStrictRules);
示例#46
0
 internal static extern JsErrorCode JsGetPropertyIdFromName(string name, out JsPropertyId propertyId);
		/// <summary>
		/// Gets an object's property
		/// </summary>
		/// <remarks>
		/// Requires an active script context.
		/// </remarks>
		/// <param name="id">The ID of the property</param>
		/// <returns>The value of the property</returns>
		public JsValue GetProperty(JsPropertyId id)
		{
			JsValue propertyReference;
			JsErrorHelpers.ThrowIfError(NativeMethods.JsGetProperty(this, id, out propertyReference));

			return propertyReference;
		}
示例#48
0
        /// <summary>
        /// Sets an object's property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="source">The JavaScript value</param>
        /// <param name="name">The name of the property</param>
        /// <param name="value">The new value of the property</param>
        /// <param name="useStrictRules">The property set should follow strict mode rules</param>
        public static void SetProperty(this JsValue source, string name, JsValue value, bool useStrictRules)
        {
            JsPropertyId id = JsPropertyId.FromString(name);

            source.SetProperty(id, value, useStrictRules);
        }
示例#49
0
 internal static extern JsErrorCode JsGetPropertyNameFromId(JsPropertyId propertyId, out string name);