// Test that a script can call a go function to reverse an ArrayBuffer.
// The function reverses the ArrayBuffer in-place, i.e. this is a call-by-reference.
Test that a script can call a go function to reverse an ArrayBuffer.
The function reverses the ArrayBuffer in-place, i.e. this is a call-by-reference.
// Test that a script can call a go function to reverse an ArrayBuffer . // The function reverses the ArrayBuffer in - place i . e . this is a call - by - reference .
func TestModifyArrayBuffer(t *testing.T) {
	t.Parallel()
	iso := v8.NewIsolate()
	ctx := v8.NewContext(iso)
	if err := injectArrayBufferTester(ctx, "reverseArrayBuffer", reverseArrayBufferFunctionCallback); err != nil {
		t.Error(err)
	}
	js := `
		let ab = new ArrayBuffer(10);
		let view = new Uint8Array(ab);
		for (let i = 0; i < 10; i++) view[i] = i;
		native.reverseArrayBuffer(ab);
		ab;
	`
	if val, err := ctx.RunScript(js, ""); err != nil {
		t.Error(err)
	} else {
		if !val.IsArrayBuffer() {
			t.Errorf("Expected ArrayBuffer return value")
		}
		ab := val.ArrayBuffer()
		if ab.ByteLength() != 10 {
			t.Errorf("Got wrong ArrayBuffer length %d, expected 10", ab.ByteLength())
		}
		bytes := ab.GetBytes()
		fmt.Printf("Got reversed ArrayBuffer from script: %v\n", bytes)
		for i := int64(0); i < ab.ByteLength(); i++ {
			if bytes[i] != uint8(10-i-1) {
				t.Errorf("Incorrect byte at index %d (whole array: %v)", i, ab)
			}
		}
	}
}
func TestModifyArrayBuffer ( t * testing . T ) { t . Parallel ( ) iso := v8 . NewIsolate ( ) ctx := v8 . NewContext ( iso ) if err := injectArrayBufferTester ( ctx , " " , reverseArrayBufferFunctionCallback ) ; err != nil { t . Error ( err ) } js := `
		let ab = new ArrayBuffer(10);
		let view = new Uint8Array(ab);
		for (let i = 0; i < 10; i++) view[i] = i;
		native.reverseArrayBuffer(ab);
		ab;
	` if val , err := ctx . RunScript ( js , " " ) ; err != nil { t . Error ( err ) } else { if ! val . IsArrayBuffer ( ) { t . Errorf ( " " ) } ab := val . ArrayBuffer ( ) if ab . ByteLength ( ) != 10 { t . Errorf ( " " , ab . ByteLength ( ) ) } bytes := ab . GetBytes ( ) fmt . Printf ( " \n " , bytes ) for i := int64 ( 0 ) ; i < ab . ByteLength ( ) ; i ++ { if bytes [ i ] != uint8 ( 10 - i - 1 ) { t . Errorf ( " " , i , ab ) } } } }
Test that a script can call a go function to reverse an ArrayBuffer.
Test that a script can call a go function to reverse an ArrayBuffer .