Skip to content

[browser][bindings] Fix error with SharedArrayBuffer when used as a backing view.#46625

Merged
kjpou1 merged 12 commits intodotnet:masterfrom
kjpou1:wasm-fix-sharedarraybuffer
Jan 8, 2021
Merged

[browser][bindings] Fix error with SharedArrayBuffer when used as a backing view.#46625
kjpou1 merged 12 commits intodotnet:masterfrom
kjpou1:wasm-fix-sharedarraybuffer

Conversation

@kjpou1
Copy link
Contributor

@kjpou1 kjpou1 commented Jan 6, 2021

Fails with "Object '...' is not a typed array" when used as a backing view for TypedArrays.

public static int TestSum(SharedArrayBuffer sharedArrayBuffer)
{
    Int32Array array = new Int32Array(sharedArrayBuffer);
    Span<int> nativeArray = array; // error                        
    
    int sum = 0;
    for (int i = 0; i < nativeArray.Length; i++)
    {
        sum += nativeArray[i];
    }

    return sum;
}

Fixes #46624

Thanks @benaadams for the firefox support !!!

@ghost
Copy link

ghost commented Jan 6, 2021

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

Fails with "Object '...' is not a typed array" when used as a backing view for TypedArrays.

public static int TestSum(SharedArrayBuffer sharedArrayBuffer)
{
    Int32Array array = new Int32Array(sharedArrayBuffer);
    Span<int> nativeArray = array; // error                        
    
    int sum = 0;
    for (int i = 0; i < nativeArray.Length; i++)
    {
        sum += nativeArray[i];
    }

    return sum;
}

Fixes #46624

Author: kjpou1
Assignees: -
Labels:

arch-wasm, area-System.Runtime.InteropServices.JavaScript

Milestone: -

@kjpou1 kjpou1 self-assigned this Jan 6, 2021
@radical
Copy link
Member

radical commented Jan 6, 2021

These might be useful:

        private static TheoryData<SharedArrayBuffer> GetTestData(int length)
        {
            // create a SharedArrayBuffer with a size in bytes
            SharedArrayBuffer buffer = new SharedArrayBuffer(length);
            Int32Array int32View = new Int32Array(buffer);  // create view
            for (int i = 0; i < int32View.Length; i ++)
                int32View[i] = i + 10;

            int32View[1] = 42;
            return new TheoryData<SharedArrayBuffer> { buffer };
        }

        [Theory]
        [MemberData(nameof(GetTestData), 16)]
        public static void SharedArrayBufferSliceAndDice3_Subset(SharedArrayBuffer buffer)
        {
            Int32Array sliced = new Int32Array(buffer.Slice(4,12));

            Assert.Equal(2, sliced.Length);
            Assert.Equal(42, sliced[0]);
            Assert.Equal(12, sliced[1]);
        }

        [Theory]
        [MemberData(nameof(GetTestData), 16)]
        public static void SharedArrayBufferSliceAndDice3_SubsetFromTheBack(SharedArrayBuffer buffer)
        {
            Int32Array sliced = new Int32Array(buffer.Slice(-4));

            Assert.Equal(1, sliced.Length);
            Assert.Equal(13, sliced[0]);
        }

        [Theory]
        [MemberData(nameof(GetTestData), 16)]
        public static void SharedArrayBufferSliceAndDice3_SubsetFromTheBackWithEnd(SharedArrayBuffer buffer)
        {
            Int32Array sliced = new Int32Array(buffer.Slice(-12, -4));

            Assert.Equal(2, sliced.Length);
            Assert.Equal(42, sliced[0]);
            Assert.Equal(12, sliced[1]);
        }

@kjpou1
Copy link
Contributor Author

kjpou1 commented Jan 7, 2021

Thanks Ankit. Will incorporate

@kjpou1 kjpou1 requested review from benaadams and radical January 7, 2021 10:02
Copy link
Member

@radical radical left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

@kjpou1 kjpou1 merged commit f3c6cb0 into dotnet:master Jan 8, 2021
@kjpou1 kjpou1 deleted the wasm-fix-sharedarraybuffer branch January 8, 2021 04:39
@ghost ghost locked as resolved and limited conversation to collaborators Feb 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[browser][bindings] "Object '...' is not a typed array" in js_typed_array_to_array.

4 participants