Skip to content

Swapping two arrays seems to behave asynchronous outside function #11047

@ghost

Description

Version: v7.1.0
Platform: Windows 8.1; 64-bit

Issue details: Swapping two variables from a function sometimes behaves like it is asynchronous as seen from outside that function, but synchronous from inside. It only happens when variables are swapped using the following short syntax:

[a, b] = [b, a];

However, it doesn't hapen every time. This is an example

'use strict';

var a = [];
var b = [];
var c = () => a;
var d = () => [a, b] = [b, a];

d();

console.log(a === c()); // Sometimes it prints `false`, which should be impossible

However, it doesn't work always. But, after a few hours of testing, I came up with the following code, which always prints wrong output:

'use strict';

// Here if I change `const` to `var` it prints 111
const w = 100;
const h = 100;

var first = new Array(100).fill(0).map(() => new Uint8Array(100));
var second = new Array(100).fill(0).map(() => new Uint8Array(100));
var getFirst = () => first;

var x, y;
var beginning = true;
var i = 0;

var func = () => {
	if(beginning){
		beginning = false;

		// If I remove bitwise `or` operator from here, it prints 111
		for(y = 0; (y | 0) < (h | 0); y++) for(x = 0; (x | 0) < (w | 0); x++){}
	}else{
		for(y = 0; y < h; y++) for(x = 0; x < w; x++){

			// If I change the number 1 to 0 from this line, it prints 111
			if((first[x | 0][y | 0] | 0) + 1);
		}

		[first, second] = [second, first];
	}

	console.log(first === getFirst() ? 1 : 0); // It should always print 1, but third time it prints 0
};

func(); // Prints 1, that is ok
func(); // Prints 1 too, its  ok
func(); // This one always prints 0

Here is how output looks like. I cannot find any possible explanations why is zero here:

1

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.v8 engineIssues and PRs related to the V8 dependency.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions