Skip to content

Invalid output when using binary operations using SIMPLE_OPTIMIZATIONS #3017

Closed
@brainexe

Description

@brainexe

I found an issue with "simple" compression and binary "or" operations. In this case, the variable is just overwritten with "48" instead of doing clean "or" operations on given input.
With advanced compression (or just whitespace removal) I get valid code.

Input code: (generated by https://github.com/openfl/lime convertor)

var addFlags = function(data) {
    data.flags = (data.flags |= 8) | 4
    data.flags = (data.flags |= 16) | 32
}

var data = {flags:1};
addFlags(data)
console.log(data.flags); // expected: 61

Output in SIMPLE_OPTIMIZATIONS

var addFlags = function(a) {
  a.flags = 12;
  a.flags = 48;
}, data = {flags:1};
addFlags(data);
console.log(data.flags);

-> output 48 👎

Output in ADVANCED_OPTIMIZATIONS

var addFlags = function(a) {
  a.flags |= 12;
  a.flags |= 48;
};
var b = {flags:1};
addFlags(b);
console.log(b.flags);

-> output: 61 👍

Playground: https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520SIMPLE_OPTIMIZATIONS%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250Avar%2520addFlags%2520%253D%2520function(data)%2520%257B%250A%2520%2520%2520%2520data.flags%2520%253D%2520(data.flags%2520%257C%253D%25208)%2520%257C%25204%250A%2520%2520%2520%2520data.flags%2520%253D%2520(data.flags%2520%257C%253D%252016)%2520%257C%252032%250A%257D%250A%250Avar%2520data%2520%253D%2520%257Bflags%253A1%257D%253B%250AaddFlags(data)%250Aconsole.log(data.flags)%253B

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions