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

optimize getter/setter creation for StructArray elements #5108

Merged
merged 1 commit into from
Aug 7, 2017
Merged
Changes from all commits
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
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;`)
};
}