Skip to content

Commit

Permalink
js: Add preconditions as assertions
Browse files Browse the repository at this point in the history
Also add bytes() to test code for clarity
  • Loading branch information
zeux committed Aug 3, 2021
1 parent 76f8de9 commit 8fe4260
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
14 changes: 14 additions & 0 deletions js/meshopt_encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ var MeshoptEncoder = (function() {
return result.buffer.slice(0, write);
}

function assert(cond) {
if (!cond) {
throw new Error("Assertion failed");
}
}

function reorder(indices, vertices, optf) {
var sbrk = instance.exports.sbrk;
var ip = sbrk(indices.length * 4);
Expand Down Expand Up @@ -119,6 +125,8 @@ var MeshoptEncoder = (function() {
return reorder(indices, maxindex(indices) + 1, optf);
},
encodeVertexBuffer: function(source, count, size) {
assert(size > 0 && size <= 256);
assert(size % 4 == 0);
var bound = instance.exports.meshopt_encodeVertexBufferBound(count, size);
return encode(instance.exports.meshopt_encodeVertexBuffer, bound, source, count, size);
},
Expand All @@ -131,12 +139,18 @@ var MeshoptEncoder = (function() {
return encode(instance.exports.meshopt_encodeIndexSequence, bound, index32(source), count, 4);
},
encodeFilterOct: function(source, count, stride, bits) {
assert(stride == 4 || stride == 8);
assert(bits >= 1 && bits <= 16);
return filter(instance.exports.meshopt_encodeFilterOct, source, count, stride, bits, 4);
},
encodeFilterQuat: function(source, count, stride, bits) {
assert(stride == 8);
assert(bits >= 4 && bits <= 16);
return filter(instance.exports.meshopt_encodeFilterQuat, source, count, stride, bits, 4);
},
encodeFilterExp: function(source, count, stride, bits) {
assert(stride > 0 && stride % 4 == 0);
assert(bits >= 1 && bits <= 24);
return filter(instance.exports.meshopt_encodeFilterExp, source, count, stride, bits, stride/4);
},
};
Expand Down
14 changes: 14 additions & 0 deletions js/meshopt_encoder.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ var MeshoptEncoder = (function() {
return result.buffer.slice(0, write);
}

function assert(cond) {
if (!cond) {
throw new Error("Assertion failed");
}
}

function reorder(indices, vertices, optf) {
var sbrk = instance.exports.sbrk;
var ip = sbrk(indices.length * 4);
Expand Down Expand Up @@ -119,6 +125,8 @@ var MeshoptEncoder = (function() {
return reorder(indices, maxindex(indices) + 1, optf);
},
encodeVertexBuffer: function(source, count, size) {
assert(size > 0 && size <= 256);
assert(size % 4 == 0);
var bound = instance.exports.meshopt_encodeVertexBufferBound(count, size);
return encode(instance.exports.meshopt_encodeVertexBuffer, bound, source, count, size);
},
Expand All @@ -131,12 +139,18 @@ var MeshoptEncoder = (function() {
return encode(instance.exports.meshopt_encodeIndexSequence, bound, index32(source), count, 4);
},
encodeFilterOct: function(source, count, stride, bits) {
assert(stride == 4 || stride == 8);
assert(bits >= 1 && bits <= 16);
return filter(instance.exports.meshopt_encodeFilterOct, source, count, stride, bits, 4);
},
encodeFilterQuat: function(source, count, stride, bits) {
assert(stride == 8);
assert(bits >= 4 && bits <= 16);
return filter(instance.exports.meshopt_encodeFilterQuat, source, count, stride, bits, 4);
},
encodeFilterExp: function(source, count, stride, bits) {
assert(stride > 0 && stride % 4 == 0);
assert(bits >= 1 && bits <= 24);
return filter(instance.exports.meshopt_encodeFilterExp, source, count, stride, bits, stride/4);
},
};
Expand Down
10 changes: 7 additions & 3 deletions js/meshopt_encoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ process.on('unhandledRejection', error => {
process.exit(1);
});

function bytes(view) {
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
}

var tests = {
reorderMesh: function() {
var indices = new Uint32Array([
Expand Down Expand Up @@ -129,7 +133,7 @@ var tests = {

// 4 vectors, encode each vector into 8 bytes with 12 bits of precision/component
var encoded = encoder.encodeFilterOct(data, 4, 8, 12);
assert.deepEqual(encoded, new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength));
assert.deepEqual(encoded, bytes(expected));
},

encodeFilterQuat12: function() {
Expand All @@ -149,7 +153,7 @@ var tests = {

// 4 quaternions, encode each quaternion into 8 bytes with 12 bits of precision/component
var encoded = encoder.encodeFilterQuat(data, 4, 8, 12);
assert.deepEqual(encoded, new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength));
assert.deepEqual(encoded, bytes(expected));
},

encodeFilterExp: function() {
Expand All @@ -167,7 +171,7 @@ var tests = {

// 1 vector with 3 components (12 bytes), encode each vector into 12 bytes with 15 bits of precision/component
var encoded = encoder.encodeFilterExp(data, 1, 12, 15);
assert.deepEqual(encoded, new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength));
assert.deepEqual(encoded, bytes(expected));
},
};

Expand Down

0 comments on commit 8fe4260

Please sign in to comment.