Skip to content

Commit

Permalink
Serialize arguments objects
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jun 10, 2020
1 parent fdbcff3 commit 32f93a8
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion lib/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ module.exports = {
const proto = Object.getPrototypeOf(val);
if (isFunction(val)) return this.serializeFunction(val, proto, record);
if (proto === null) return this.serializeNullPrototypeObject(val);
if (proto === Object.prototype) return this.serializeProperties(val);
if (proto === Object.prototype) {
// `arguments` objects have proto of `Object.prototype` but are not plain objects
if (Object.prototype.toString.call(val) === '[object Arguments]') {
return this.serializeArgumentsObject(val);
}
return this.serializeProperties(val);
}
if (proto === Array.prototype) return this.serializeArray(val);
if (proto === RegExp.prototype) return this.serializeRegex(val);
if (proto === Date.prototype) return this.serializeDate(val);
Expand Down Expand Up @@ -147,6 +153,47 @@ module.exports = {
: serializePropertyKey(key);
},

serializeArgumentsObject(args) {
// Get/create record for `createArgumentsObject` function
let createArgsRecord = this.getRecord(createArgumentsObject);
if (!createArgsRecord) {
createArgsRecord = Object.create(null);
createArgsRecord.path = '';
createArgsRecord.parent = null;
createArgsRecord.packageDepth = 0;
createArgsRecord.pathDepth = 0;
createArgsRecord.keyDepth = 0;
createArgsRecord.js = 'function() { return arguments; }';
this.records.set(createArgumentsObject, createArgsRecord);
}

// Serialize args
const propJss = [],
dependencies = [];
let len = 0;
for (const key of Object.getOwnPropertyNames(args)) {
if (key !== '0' && !key.match(/^[1-9]\d*$/)) continue;

const val = args[key];
if (!isPrimitive(val)) dependencies.push(val);
propJss.push(this.serializeValue(val));
len++;
}

// Serialize as call to `createArgumentsObject(arg1, arg2, ...)`
const js = `${valuePlaceholder(createArgsRecord.id)}(${propJss.join(', ')})`;
dependencies.push(createArgumentsObject);

return this.wrapWithProps(
args, js, dependencies,
key => (
(key === 'length' && args.length === len)
|| key === 'callee'
|| key === '0' || key.match(/^[1-9]\d*$/)
)
);
},

serializeArray(arr) {
const dependencies = [];
let previousEmpty = true;
Expand Down Expand Up @@ -249,3 +296,5 @@ module.exports = {
return {js: `${assignJs}(${targetJs}, ${propsJs})`, dependencies};
}
};

function createArgumentsObject() {}

0 comments on commit 32f93a8

Please sign in to comment.