asm.js

Business logic written in C, compiled with Emscripten into asm.js.

Source code: luhn.c (Compiles to luhn.js)

To recompile luhn.js, install emscripten, and run:

        emcc luhn.c -o luhn.js -s EXPORTED_FUNCTIONS='["_is_valid_cc"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall"]' -s WASM=0
            

There is also a small piece of Javascript bootstrap code in the <head> to expose the Python function as a click handler:

    <script type="text/javascript">
        function validate(node) {
            card_number = document.getElementById(node).value
            if (card_number) {
                if (Module.ccall('is_valid_cc', 'int', ['string'], [card_number])) {
                    alert("Card is valid!!")
                } else {
                    alert("Card is NOT valid!!")
                }
            }
        }
    </script>