-
-
Notifications
You must be signed in to change notification settings - Fork 496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Agree and document the meaning of ReferenceFlags::Read
and Write
#5165
Comments
I have a simple idea. const x = {
get value() {
console.log('read');
},
set value(v) {
console.log('write');
}
} For example, with |
I like that - it's easy to understand. So by that definition of "read" and "write", let x = {
get value() { console.log('read'); },
set value(v) { console.log('write'); },
};
++x.value; But... I am still wondering what the purpose of the read and write flags is? What are they meant to be used for? That might affect what is the right mental model to have. |
https://tc39.es/ecma262/#sec-prefix-increment-operator-runtime-semantics-evaluation This is the semantics of read and write. |
So do I understand right that the flags we currently have on Either way, am reopening this issue as the "action" I was requesting is that we document it. |
You may need to update the issue title and description. |
ReferenceFlags::Read
mean?ReferenceFlags::Read
and Write
Have changed the issue title. To (hopefully) conclude this, have I understood correctly from spec above that |
yes |
Thank you. I'll add some docs, and open an issue to fix flags on |
swc-project/swc#9558 (comment) This is an example where you should use ReferenceFlags::Read and Write to replace rvalue enum values without affecting others. |
For those of us who find the spec hard to follow, a runtime test function for determining read/write status of vars. Its output aligns with the spec. function test(code) {
console.log(`\n> ${code}`);
const proxy = new Proxy(Object.create(null), {
has(target, propName) {
if (typeof propName === 'symbol') return Reflect.has(target, propName);
return true;
},
get(target, propName) {
const value = Reflect.get(target, propName);
if (typeof propName !== 'symbol') console.log(`read ${propName}: ${value}`);
return value;
},
set(target, propName, value) {
const success = Reflect.set(target, propName, value);
if (typeof propName !== 'symbol') console.log(`write ${propName}: ${value}`);
return success;
},
});
(0, eval)(`__proxy => { with (__proxy) { ${code} } }`)(proxy);
} test('x = y = 1;');
// write y: 1
// write x: 1
test('x = 1; x++;');
// write x: 1
// read x: 1
// write x: 2 |
Add more docs to `ReferenceFlags`, mainly to include the runtime test in #5165 (comment).
Add more docs to `ReferenceFlags`, mainly to include the runtime test in #5165 (comment).
This question came up previously in #3326, and has reared its head again in #5158.
++x
currently gets flagged asWrite
only.This doesn't make sense to me as it is equivalent to
x += 1
which isRead | Write
.The tests indicate this is intentional, but the reason why is not documented anywhere.
@Dunqing's guess was "I believe this variable can be safely removed by the Minifier/Compressor."
But as I pointed out,
++x
can have side effects ifx
is an object with avalueOf
method:So that suggests that
++x
cannot be removed by minifier in all cases.Whatever the rationale is, can we please hash it out and document it?
The text was updated successfully, but these errors were encountered: