Skip to content

Commit

Permalink
fix(marshal): remove Data
Browse files Browse the repository at this point in the history
refs #2018
  • Loading branch information
warner committed Mar 16, 2021
1 parent d8cee03 commit 543d5ca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 98 deletions.
1 change: 0 additions & 1 deletion packages/marshal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export {
makeMarshal,
Remotable,
Far,
Data,
} from './src/marshal';

export { stringify, parse } from './src/marshal-stringify';
84 changes: 9 additions & 75 deletions packages/marshal/src/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,42 +248,13 @@ function isPassByCopyArray(val) {
return true;
}

/**
* Everything having to do with a `dataProto` is a temporary kludge until
* we're on the other side of #2018. TODO remove.
*/
const dataProto = harden(
create(objectPrototype, {
[PASS_STYLE]: { value: 'copyRecord' },
}),
);

const isDataProto = proto => {
if (!isFrozen(proto)) {
return false;
}
if (getPrototypeOf(proto) !== objectPrototype) {
return false;
}
const {
// @ts-ignore
[PASS_STYLE]: passStyleDesc,
...rest
} = getOwnPropertyDescriptors(proto);
return (
passStyleDesc &&
passStyleDesc.value === 'copyRecord' &&
ownKeys(rest).length === 0
);
};

/**
* @param {Passable} val
* @returns {boolean}
*/
function isPassByCopyRecord(val) {
const proto = getPrototypeOf(val);
if (proto !== objectPrototype && !isDataProto(proto)) {
if (proto !== objectPrototype) {
return false;
}
const descs = getOwnPropertyDescriptors(val);
Expand Down Expand Up @@ -946,20 +917,14 @@ export function makeMarshal(
}
return ibidTable.finish(result);
} else {
let result = ibidTable.start({});
const names = ownKeys(rawTree);
if (names.length === 0) {
// eslint-disable-next-line no-use-before-define
result = Data(result);
} else {
for (const name of names) {
assert.typeof(
name,
'string',
X`Property ${name} of ${rawTree} must be a string`,
);
result[name] = fullRevive(rawTree[name]);
}
const result = ibidTable.start({});
for (const name of ownKeys(rawTree)) {
assert.typeof(
name,
'string',
X`Property ${name} of ${rawTree} must be a string`,
);
result[name] = fullRevive(rawTree[name]);
}
return ibidTable.finish(result);
}
Expand Down Expand Up @@ -1086,34 +1051,3 @@ const Far = (farName, remotable = {}) =>

harden(Far);
export { Far };

/**
* Everything having to do with `Data` is a temporary kludge until
* we're on the other side of #2018. TODO remove.
*
* @param {Object} record
*/
const Data = record => {
// Ensure that the record isn't already marked.
assert(
!(PASS_STYLE in record),
X`Record ${record} is already marked as a ${q(record[PASS_STYLE])}`,
);
// Ensure that the record isn't already frozen.
assert(!isFrozen(record), X`Record ${record} is already frozen`);
assert(
getPrototypeOf(record) === objectPrototype,
X`A record ${record} must initially inherit from Object.prototype`,
);

setPrototypeOf(record, dataProto);
harden(record);
assert(
isPassByCopyRecord(record),
X`Data() can only be applied to otherwise pass-by-copy records`,
);
return record;
};

harden(Data);
export { Data };
22 changes: 0 additions & 22 deletions packages/marshal/test/test-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import test from 'ava';
import {
Remotable,
Far,
Data,
getInterfaceOf,
makeMarshal,
passStyleOf,
Expand Down Expand Up @@ -467,18 +466,13 @@ test('records', t => {
props[symNonenumFunc] = { enumerable: false, value: () => 0 };
} else if (opt === 'nonenumSymbolGetFunc') {
props[symNonenumGetFunc] = { enumerable: false, get: () => () => 0 };
} else if (opt === 'data') {
mark = 'data';
} else if (opt === 'far') {
mark = 'far';
} else {
throw Error(`unknown option ${opt}`);
}
}
const o = create(objectPrototype, props);
if (mark === 'data') {
return Data(o);
}
if (mark === 'far') {
return Far('iface', o);
}
Expand All @@ -492,7 +486,6 @@ test('records', t => {
const NOACC = /Records must not contain accessors/;
const RECENUM = /Record fields must be enumerable/;
const NOMETH = /cannot serialize objects with non-methods/;
const NODATA = /Data\(\) can only be applied to otherwise pass-by-copy records/;

// empty objects

Expand All @@ -506,12 +499,7 @@ test('records', t => {
// harden({})
t.deepEqual(ser(build()), emptyData);

// Data({key1: 'data'})
// old: not applicable, Data() not yet added
// interim1: pass-by-copy without warning, but Data() is not necessary
// final: not applicable, Data() removed
const key1Data = { body: JSON.stringify({ key1: 'data' }), slots: [] };
t.deepEqual(ser(build('enumStringData', 'data')), key1Data);

// Serialized data should roundtrip properly
t.deepEqual(unser(ser(harden({}))), {});
Expand All @@ -521,13 +509,6 @@ test('records', t => {
t.deepEqual(ser(unser(emptyData)), emptyData);
t.deepEqual(ser(unser(key1Data)), key1Data);

// Data({})
// old: not applicable, Data() not yet added
// interim1: pass-by-copy without warning
// interim2: pass-by-copy without warning
// final: not applicable, Data() removed
t.deepEqual(ser(build('data')), emptyData); // interim 1+2

// Far('iface', {})
// all cases: pass-by-ref
t.deepEqual(ser(build('far')), yesIface);
Expand Down Expand Up @@ -556,9 +537,6 @@ test('records', t => {
t.deepEqual(ser(build('nonenumStringFunc')), noIface);
t.deepEqual(ser(build('nonenumSymbolFunc')), noIface);

// Data({ key: data, key: func }) : rejected
shouldThrow(['data', 'enumStringData', 'enumStringFunc'], NODATA);

// Far('iface', { key: data, key: func }) : rejected
// (some day this might add auxilliary data, but not now
shouldThrow(['far', 'enumStringData', 'enumStringFunc'], CSO);
Expand Down

0 comments on commit 543d5ca

Please sign in to comment.