Skip to content

Commit

Permalink
Expose internal C API and enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Oct 18, 2023
1 parent d08c45c commit 00bcb84
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
2 changes: 2 additions & 0 deletions build-emscripten.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ emcmake cmake ${SRCDIR} $CONFIGURE_ARGS \
VERBOSE=1 emmake make -j${CORES}

LIBHEIFA="libheif/libheif.a"
EXPORTED_FUNCTIONS=$($EMSDK/upstream/bin/llvm-nm $LIBHEIFA --format=just-symbols | grep "^heif_\|^de265_\|^aom_" | grep "[^:]$" | sed 's/^/_/' | paste -sd "," -)

echo "Running Emscripten..."

Expand All @@ -122,6 +123,7 @@ if [ "$DEBUG" = "1" ]; then
fi

emcc -Wl,--whole-archive "$LIBHEIFA" -Wl,--no-whole-archive \
-sEXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS,_free,_malloc,_memcpy" \
-sMODULARIZE \
-sEXPORT_NAME="libheif" \
-sWASM_ASYNC_COMPILATION=0 \
Expand Down
26 changes: 26 additions & 0 deletions post.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,29 @@ var fourcc = function(s) {
Module.HeifImage = HeifImage;
Module.HeifDecoder = HeifDecoder;
Module.fourcc = fourcc;

// Expose enum values.
const enums = [
'heif_error_code',
'heif_suberror_code',
'heif_compression_format',
'heif_chroma',
'heif_colorspace',
'heif_channel'
];
for (const e of enums) {
for (const key in Module[e]) {
if (!Module[e].hasOwnProperty(key) || key === 'values') {
continue;
}
Module[key] = Module[e][key];
}
}

// Expose internal C API.
for (const key in Module) {
if (key.indexOf('_heif_') !== 0 || Module[key.slice(1)] !== undefined) {
continue;
}
Module[key.slice(1)] = Module[key];
}
37 changes: 19 additions & 18 deletions scripts/test-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/
(function() {

console.log("Running libheif JavaScript tests ...");
const assert = require('assert');
const fs = require('fs');

var libheif = require('../libheif.js')();
console.log("Loaded libheif.js", libheif.heif_get_version());
console.log('Running libheif JavaScript tests ...');

// Decode the example file and make sure at least one image is returned.
var fs = require('fs');
fs.readFile('examples/example.heic', function(err, data) {
if (err) {
throw err;
}
const libheif = require('../libheif.js')();

var decoder = new libheif.HeifDecoder();
var image_data = decoder.decode(data);
console.log("Loaded images:", image_data.length);
if (!image_data.length) {
throw new Error("Should have loaded images");
}
});
// Test Embind API.
console.log('Loaded libheif.js', libheif.heif_get_version());

})();
// Test internal C API.
assert(libheif.heif_get_version_number_major() === 1, 'libheif major version should be 1')

// Test enum values.
assert(libheif.heif_error_Ok.value === 0, 'heif_error_Ok should be 0')

// Decode the example file and make sure at least one image is returned.
const data = fs.readFileSync('examples/example.heic');
const decoder = new libheif.HeifDecoder();
const image_data = decoder.decode(data);

console.log('Loaded images:', image_data.length);
assert(image_data.length > 0, "Should have loaded images")

0 comments on commit 00bcb84

Please sign in to comment.