Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Refactor and clean up realm.reportSideEffectCallbacks #2254

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/completions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ export class ThrowCompletion extends AbruptCompletion {
super(value, precedingEffects, location);
this.nativeStack = nativeStack || new Error().stack;
let realm = value.$Realm;
if (realm.isInPureScope() && realm.reportSideEffectCallback !== undefined) {
realm.reportSideEffectCallback("EXCEPTION_THROWN", undefined, location);
if (realm.isInPureScope()) {
for (let callback of realm.reportSideEffectCallbacks) {
callback("EXCEPTION_THROWN", undefined, location);
}
}
}

Expand Down
42 changes: 21 additions & 21 deletions src/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ export class Realm {
verbose: opts.reactVerbose || false,
};

this.reportSideEffectCallbacks = new Set();

this.alreadyDescribedLocations = new WeakMap();
this.stripFlow = opts.stripFlow || false;

Expand Down Expand Up @@ -346,9 +348,9 @@ export class Realm {
createdObjects: void | CreatedObjects;
createdObjectsTrackedForLeaks: void | CreatedObjects;
reportObjectGetOwnProperties: void | (ObjectValue => void);
reportSideEffectCallback:
| void
| ((sideEffectType: SideEffectType, binding: void | Binding | PropertyBinding, expressionLocation: any) => void);
reportSideEffectCallbacks: Set<
(sideEffectType: SideEffectType, binding: void | Binding | PropertyBinding, expressionLocation: any) => void
>;
reportPropertyAccess: void | (PropertyBinding => void);
savedCompletion: void | PossiblyNormalCompletion;

Expand Down Expand Up @@ -716,26 +718,21 @@ export class Realm {
| ((sideEffectType: SideEffectType, binding: void | Binding | PropertyBinding, value: void | Value) => void)
): T {
let saved_createdObjectsTrackedForLeaks = this.createdObjectsTrackedForLeaks;
let saved_reportSideEffectCallback = this.reportSideEffectCallback;
// Track all objects (including function closures) created during
// this call. This will be used to make the assumption that every
// *other* object is unchanged (pure). These objects are marked
// as leaked if they're passed to abstract functions.
this.createdObjectsTrackedForLeaks = new Set();
this.reportSideEffectCallback = (...args) => {
if (reportSideEffectFunc != null) {
reportSideEffectFunc(...args);
}
// Ensure we call any previously nested side-effect callbacks
if (saved_reportSideEffectCallback != null) {
saved_reportSideEffectCallback(...args);
}
};
if (reportSideEffectFunc !== null) {
this.reportSideEffectCallbacks.add(reportSideEffectFunc);
}
try {
return f();
} finally {
this.createdObjectsTrackedForLeaks = saved_createdObjectsTrackedForLeaks;
this.reportSideEffectCallback = saved_reportSideEffectCallback;
if (reportSideEffectFunc !== null) {
this.reportSideEffectCallbacks.delete(reportSideEffectFunc);
}
}
}

Expand Down Expand Up @@ -1539,16 +1536,17 @@ export class Realm {
this.modifiedBindings !== undefined &&
!this.modifiedBindings.has(binding) &&
value !== undefined &&
this.isInPureScope() &&
this.reportSideEffectCallback !== undefined
this.isInPureScope()
) {
let env = binding.environment;

if (
!(env instanceof DeclarativeEnvironmentRecord) ||
(env instanceof DeclarativeEnvironmentRecord && !isDefinedInsidePureFn(env))
) {
this.reportSideEffectCallback("MODIFIED_BINDING", binding, value.expressionLocation);
for (let callback of this.reportSideEffectCallbacks) {
callback("MODIFIED_BINDING", binding, value.expressionLocation);
}
}
}

Expand Down Expand Up @@ -1591,11 +1589,13 @@ export class Realm {

if (createdObjectsTrackedForLeaks !== undefined && !createdObjectsTrackedForLeaks.has(object)) {
if (binding.object === this.$GlobalObject) {
this.reportSideEffectCallback &&
this.reportSideEffectCallback("MODIFIED_GLOBAL", binding, object.expressionLocation);
for (let callback of this.reportSideEffectCallbacks) {
callback("MODIFIED_GLOBAL", binding, object.expressionLocation);
}
} else {
this.reportSideEffectCallback &&
this.reportSideEffectCallback("MODIFIED_PROPERTY", binding, object.expressionLocation);
for (let callback of this.reportSideEffectCallbacks) {
callback("MODIFIED_PROPERTY", binding, object.expressionLocation);
}
}
}
}
Expand Down