Example #1
0
        internal IMemoryOwner <char> JsCopyStringUtf16Pooled()
        {
            const int start  = 0;
            int       length = int.MaxValue;

            var errorCode = NativeMethods.JsCopyStringUtf16(this, start, length, null, out var written);

            JsErrorHelpers.ThrowIfError(errorCode);

            length = (int)written;
            var buffer = ArrayPool <char> .Shared.Rent(length);


            unsafe
            {
                fixed(char *bufferPtr = buffer)
                {
                    JsErrorHelpers.ThrowIfError(NativeMethods.JsCopyStringUtf16(this, start, length, (IntPtr)bufferPtr, out written));
                }
            }

            return(new PooledCharBuffer(buffer, length));
        }
Example #2
0
        /// <summary>
        /// Retrieves a string pointer of a <c>String</c> value
        /// </summary>
        /// <remarks>
        /// <para>
        /// This function retrieves the string pointer of a <c>String</c> value. It will fail with
        /// <c>InvalidArgument</c> if the type of the value is not <c>String</c>.
        /// </para>
        /// <para>
        /// Requires an active script context.
        /// </para>
        /// </remarks>
        /// <returns>The string</returns>
        public new string ToString()
        {
            string      result;
            JsErrorCode errorCode;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                int     start  = 0;
                int     length = int.MaxValue;
                char[]  buffer = null;
                UIntPtr written;

                errorCode = NativeMethods.JsCopyStringUtf16(this, start, length, buffer, out written);
                JsErrorHelpers.ThrowIfError(errorCode);

                var charArrayPool = ArrayPool <char> .Shared;
                length         = (int)written;
                buffer         = charArrayPool.Rent(length + 1);
                buffer[length] = '\0';

                try
                {
                    errorCode = NativeMethods.JsCopyStringUtf16(this, start, length, buffer, out written);
                    JsErrorHelpers.ThrowIfError(errorCode);

                    result = new string(buffer, start, length);
                }
                finally
                {
                    charArrayPool.Return(buffer);
                }
            }
            else
            {
                byte[]  buffer     = null;
                UIntPtr bufferSize = UIntPtr.Zero;
                UIntPtr length;

                errorCode = NativeMethods.JsCopyString(this, buffer, bufferSize, out length);
                JsErrorHelpers.ThrowIfError(errorCode);

                var byteArrayPool = ArrayPool <byte> .Shared;
                bufferSize = length;
                int bufferLength = (int)bufferSize;
                buffer = byteArrayPool.Rent(bufferLength + 1);
                buffer[bufferLength] = 0;

                try
                {
                    errorCode = NativeMethods.JsCopyString(this, buffer, bufferSize, out length);
                    JsErrorHelpers.ThrowIfError(errorCode);

                    result = Encoding.UTF8.GetString(buffer, 0, bufferLength);
                }
                finally
                {
                    byteArrayPool.Return(buffer);
                }
            }

            return(result);
        }