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

Make sure buffer is a DataView #3127

Merged
merged 2 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 1 addition & 8 deletions jupyterlab_widgets/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,7 @@ export abstract class LabWidgetManager extends ManagerBase
) {
const data = msg.content.data as any;
const buffer_paths = data.buffer_paths || [];
// Make sure the buffers are DataViews
const buffers = (msg.buffers || []).map(b => {
if (b instanceof DataView) {
return b;
} else {
return new DataView(b instanceof ArrayBuffer ? b : b.buffer);
}
});
const buffers = msg.buffers || [];
put_buffers(data.state, buffer_paths, buffers);
info.resolve({ comm, msg });
}
Expand Down
15 changes: 11 additions & 4 deletions packages/base/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,25 @@ export function reject(message: string, log: boolean) {
export function put_buffers(
state: Dict<BufferJSON>,
buffer_paths: (string | number)[][],
buffers: DataView[]
buffers: any[]
Copy link
Member

Choose a reason for hiding this comment

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

any seems overly permissive. AFAICT, this should be a union of:

  • DataView
  • ArrayBuffer
  • a generic type that includes a buffer attribute of the correct type (ArrayBuffer). This would catch TypedArrays.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, we also need to add ArrayBufferView in the union.

): void {
for (let i = 0; i < buffer_paths.length; i++) {
const buffer_path = buffer_paths[i];
// say we want to set state[x][y][z] = buffers[i]
// make sure the buffers are DataViews
let buffer = buffers[i];
if (!(buffer instanceof DataView)) {
buffer = new DataView(
buffer instanceof ArrayBuffer ? buffer : buffer.buffer
);
}
// say we want to set state[x][y][z] = buffer
let obj = state as any;
// we first get obj = state[x][y]
for (let j = 0; j < buffer_path.length - 1; j++) {
obj = obj[buffer_path[j]];
}
// and then set: obj[z] = buffers[i]
obj[buffer_path[buffer_path.length - 1]] = buffers[i];
// and then set: obj[z] = buffer
obj[buffer_path[buffer_path.length - 1]] = buffer;
}
}

Expand Down