Skip to content
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

Storage: fix clear and removeItem #1136

Merged
merged 7 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main-thread/commands/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommandExecutorInterface } from './interface';
import { TransferrableMutationType, StorageMutationIndex } from '../../transfer/TransferrableMutation';
import { StorageLocation } from '../../transfer/TransferrableStorage';
import { TransferrableKeys } from '../../transfer/TransferrableKeys';
import { MessageType, StorageValueToWorker, GetOrSet } from '../../transfer/Messages';
import { MessageType, StorageValueToWorker, GetOrSet, DELETION_INDEX } from '../../transfer/Messages';

export const StorageProcessor: CommandExecutorInterface = (strings, nodeContext, workerContext, objectContext, config) => {
const allowedExecution = config.executorsAllowed.includes(TransferrableMutationType.STORAGE);
Expand Down Expand Up @@ -64,8 +64,8 @@ export const StorageProcessor: CommandExecutorInterface = (strings, nodeContext,

// TODO(choumx): Clean up key/value strings (or don't store them in the first place)
// to avoid leaking memory.
const key = keyIndex >= 0 ? strings.get(keyIndex) : '';
const value = valueIndex >= 0 ? strings.get(valueIndex) : null;
const key = keyIndex != DELETION_INDEX && keyIndex >= 0 ? strings.get(keyIndex) : '';
const value = valueIndex != DELETION_INDEX && valueIndex >= 0 ? strings.get(valueIndex) : null;

if (operation === GetOrSet.GET) {
get(location, key);
Expand Down
23 changes: 23 additions & 0 deletions src/test/main-thread/commands/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import { TransferrableKeys } from '../../../transfer/TransferrableKeys';

const test = anyTest as TestInterface<{}>;

type SetStorageMeta = { location: StorageLocation; key: string | null; value: string | null };

function getStorageProcessor(strings: string[]): {
processor: CommandExecutor;
messages: Array<MessageToWorker>;
getLastSetStorage: () => any;
} {
const stringCtx = new StringContext();
stringCtx.storeValues(strings);
const messages: Array<MessageToWorker> = [];
let lastSetStorage: null | SetStorageMeta = null;

const processor = StorageProcessor(
stringCtx,
Expand All @@ -34,12 +38,16 @@ function getStorageProcessor(strings: string[]): {
getStorage() {
return Promise.resolve({ hello: 'world' });
},
setStorage(location: StorageLocation, key: string | null, value: string | null) {
lastSetStorage = { location, key, value };
},
} as unknown as Sanitizer,
} as WorkerDOMConfiguration,
);
return {
processor,
messages,
getLastSetStorage: () => lastSetStorage,
};
}

Expand All @@ -63,3 +71,18 @@ test('StorageProcessor sends storage value event to worker', async (t) => {
t.is(messages.length, 1);
t.deepEqual(messages, [expectedMessage]);
});

test('StorageProcessor handles deletion event from worker', async (t) => {
const { processor, getLastSetStorage } = getStorageProcessor(['t', 'hello']);
const mutation: number[] = [];
mutation[StorageMutationIndex.Operation] = GetOrSet.SET;
mutation[StorageMutationIndex.Location] = StorageLocation.Local;
mutation[StorageMutationIndex.Key] = 1;
mutation[StorageMutationIndex.Value] = -1;
const mutations = new Uint16Array(mutation);

processor.execute(mutations, 0, true);
await Promise.resolve(setTimeout);

t.deepEqual(getLastSetStorage(), { location: StorageLocation.Local, key: 'hello', value: null });
});
6 changes: 3 additions & 3 deletions src/test/mutation-transfer/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import anyTest, { TestInterface } from 'ava';
import { Document } from '../../worker-thread/dom/Document';
import { GetOrSet } from '../../transfer/Messages';
import { DELETION_INDEX, GetOrSet } from '../../transfer/Messages';
import { Storage, createStorage } from '../../worker-thread/Storage';
import { StorageLocation } from '../../transfer/TransferrableStorage';
import { TransferrableMutationType } from '../../transfer/TransferrableMutation';
Expand Down Expand Up @@ -54,7 +54,7 @@ test.serial.cb('Storage.removeItem', (t) => {
const { document, storage } = t.context;

expectMutations(document, (mutations) => {
t.deepEqual(mutations, [TransferrableMutationType.STORAGE, GetOrSet.SET, StorageLocation.Local, getForTesting('foo'), 0]);
t.deepEqual(mutations, [TransferrableMutationType.STORAGE, GetOrSet.SET, StorageLocation.Local, getForTesting('foo'), DELETION_INDEX]);
t.end();
});

Expand All @@ -65,7 +65,7 @@ test.serial.cb('Storage.clear', (t) => {
const { document, storage } = t.context;

expectMutations(document, (mutations) => {
t.deepEqual(mutations, [TransferrableMutationType.STORAGE, GetOrSet.SET, StorageLocation.Local, 0, 0]);
t.deepEqual(mutations, [TransferrableMutationType.STORAGE, GetOrSet.SET, StorageLocation.Local, DELETION_INDEX, DELETION_INDEX]);
t.end();
});

Expand Down
4 changes: 4 additions & 0 deletions src/transfer/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ export const enum ResolveOrReject {
RESOLVE = 1,
REJECT = 2,
}

// Mutations are are modeled as Uint16Array.
// Deletion is specified as a -1, which when placed into the array becomes 2^16-1.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we setting them to -1 in the first place? Wouldn't 0 work?

Copy link
Member Author

@samouri samouri Feb 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b/c then 0 would be overloaded and impossible to distinguish between "remove" or 0th index into Strings array. by picking the last index we make the issue much less likely.

An alternative could be to always have null as the first element of the strings array (reserved to mean deletions)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just define it as index as the + 1 of the strings index? Then 0 is impossible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always sending over +1 from the worker seems messier on both ends than reserving the 0th index. WDYT

export const DELETION_INDEX = 2 ** 16 - 1;
18 changes: 3 additions & 15 deletions src/worker-thread/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Document } from './dom/Document';
import { GetOrSet } from '../transfer/Messages';
import { DELETION_INDEX, GetOrSet } from '../transfer/Messages';
import { StorageLocation } from '../transfer/TransferrableStorage';
import { TransferrableMutationType } from '../transfer/TransferrableMutation';
import { store } from './strings';
Expand Down Expand Up @@ -58,13 +58,7 @@ export function createStorage(document: Document | DocumentStub, location: Stora
value(key: string): void {
delete this[key];

transfer(document, [
TransferrableMutationType.STORAGE,
GetOrSet.SET,
location,
store(key),
0, // value == 0 represents deletion.
]);
transfer(document, [TransferrableMutationType.STORAGE, GetOrSet.SET, location, store(key), DELETION_INDEX]);
},
});
define(storage, 'clear', {
Expand All @@ -73,13 +67,7 @@ export function createStorage(document: Document | DocumentStub, location: Stora
delete this[key];
});

transfer(document, [
TransferrableMutationType.STORAGE,
GetOrSet.SET,
location,
0, // key == 0 represents all keys.
0, // value == 0 represents deletion.
]);
transfer(document, [TransferrableMutationType.STORAGE, GetOrSet.SET, location, DELETION_INDEX, DELETION_INDEX]);
},
});
return storage as Storage;
Expand Down