Skip to content

Commit

Permalink
optimize getter/setter creation for StructArray elements (#5108)
Browse files Browse the repository at this point in the history
  • Loading branch information
pirxpilot authored and jfirebaugh committed Aug 7, 2017
1 parent b93bc45 commit 0b7e9c3
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/util/struct_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,18 @@ function createStructArrayType(options: StructArrayTypeParameters): Class<Struct

for (const member of members) {
for (let c = 0; c < member.components; c++) {
const name = member.name + (member.components === 1 ? '' : c);
let name = member.name;
if (member.components > 1) {
name += c;
}
if (name in StructType.prototype) {
throw new Error(`${name} is a reserved name and cannot be used as a member name.`);
}
Object.defineProperty(StructType.prototype, name, {
get: createGetter(member, c),
set: createSetter(member, c)
});
Object.defineProperty(
StructType.prototype,
name,
createAccessors(member, c)
);
}
}

Expand Down Expand Up @@ -400,10 +404,10 @@ function createMemberComponentString(member, component) {
return `this._structArray.${getArrayViewName(member.type)}[${index}]`;
}

function createGetter(member, c) {
return new Function(`return ${createMemberComponentString(member, c)};`);
}

function createSetter(member, c) {
return new Function('x', `${createMemberComponentString(member, c)} = x;`);
function createAccessors(member, c) {
const code = createMemberComponentString(member, c);
return {
get: new Function(`return ${code};`),
set: new Function('x', `${code} = x;`)
};
}

0 comments on commit 0b7e9c3

Please sign in to comment.