From 00bcb845cd36f8593e1fa17eda5d8faababf33ac Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Wed, 18 Oct 2023 19:14:06 +0200 Subject: [PATCH] Expose internal C API and enum values --- build-emscripten.sh | 2 ++ post.js | 26 ++++++++++++++++++++++++++ scripts/test-javascript.js | 37 +++++++++++++++++++------------------ 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/build-emscripten.sh b/build-emscripten.sh index dd8a039229..f9449c2888 100755 --- a/build-emscripten.sh +++ b/build-emscripten.sh @@ -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..." @@ -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 \ diff --git a/post.js b/post.js index 8228564925..d7aaf3824c 100644 --- a/post.js +++ b/post.js @@ -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]; +} diff --git a/scripts/test-javascript.js b/scripts/test-javascript.js index 0f70d87d97..a75621ef40 100644 --- a/scripts/test-javascript.js +++ b/scripts/test-javascript.js @@ -18,26 +18,27 @@ * You should have received a copy of the GNU Lesser General Public License * along with libheif. If not, see . */ -(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")