JavaScriptEngineSwitcher.ChakraCore.JsRt.JsValue.FromString C# (CSharp) Method

FromString() public static method

Creates a String value from a string pointer
Requires an active script context.
public static FromString ( string value ) : JsValue
value string The string to convert to a String value
return JsValue
		public static JsValue FromString(string value)
		{
			JsValue reference;
			JsErrorCode errorCode;

			if (Utils.IsWindows())
			{
				var stringLength = new UIntPtr((uint)value.Length);
				errorCode = NativeMethods.JsPointerToString(value, stringLength, out reference);
			}
			else
			{
				var byteCount = new UIntPtr((uint)Encoding.GetEncoding(0).GetByteCount(value));
				errorCode = NativeMethods.JsCreateStringUtf8(value, byteCount, out reference);
			}

			JsErrorHelpers.ThrowIfError(errorCode);

			return reference;
		}

Usage Example

        /// <summary>
        /// Serializes a parsed script to a buffer than can be reused
        /// </summary>
        /// <remarks>
        /// <para>
        /// <c>SerializeScript</c> parses a script and then stores the parsed form of the script in a
        /// runtime-independent format. The serialized script then can be deserialized in any
        /// runtime without requiring the script to be re-parsed.
        /// </para>
        /// <para>
        /// Requires an active script context.
        /// </para>
        /// </remarks>
        /// <param name="script">The script to serialize</param>
        /// <param name="parseAttributes">Attribute mask for parsing the script</param>
        /// <returns>The buffer to put the serialized script into</returns>
        public static byte[] SerializeScript(string script, ref JsParseScriptAttributes parseAttributes)
        {
            JsValue scriptValue = JsValue.FromString(script);

            scriptValue.AddRef();

            JsValue bufferValue;

            try
            {
                JsErrorCode errorCode = NativeMethods.JsSerialize(scriptValue, out bufferValue, parseAttributes);
                JsErrorHelpers.ThrowIfError(errorCode);
            }
            finally
            {
                scriptValue.Release();
            }

            byte[] buffer = bufferValue.ArrayBufferBytes;

            return(buffer);
        }
All Usage Examples Of JavaScriptEngineSwitcher.ChakraCore.JsRt.JsValue::FromString