Skip to content

Instantly share code, notes, and snippets.

@tpmccallum
Last active November 13, 2019 06:10
Show Gist options
  • Save tpmccallum/7512fb89cf64fc07a70260da33c71a26 to your computer and use it in GitHub Desktop.
Save tpmccallum/7512fb89cf64fc07a70260da33c71a26 to your computer and use it in GitHub Desktop.
<html>
	<head>
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity_no="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" />
		<script>
    if (!('WebAssembly' in window)) {
      alert('you need a browser with wasm support enabled :(');
    }
(async () => {
  const response = await fetch('triple.wasm');
  const buffer = await response.arrayBuffer();
  const module = await WebAssembly.compile(buffer);
  const instance = await WebAssembly.instantiate(module);
  const exports = instance.exports;
  const triple = exports.triple;
  var buttonOne = document.getElementById('buttonOne');
        buttonOne.value = 'Triple the number';
        buttonOne.addEventListener('click', function() {
          var input = $("#numberInput").val();
          alert(input + ' tripled equals ' + triple(input));
        }, false);
})();    

  </script>
	</head>
	<body>
		<div class="row">
			<div class="col-sm-4"></div>
			<div class="col-sm-4">
				<b>Rust to Wasm in under 5 minutes - Triple the number</b>
			</div>
			<div class="col-sm-4"></div>
		</div>
		<hr />
		<div class="row">
			<div class="col-sm-2"></div>
			<div class="col-sm-4">Place a number in the box</div>
			<div class="col-sm-4"> Click the button</div>
			<div class="col-sm-2"></div>
		</div>
		<div class="row">
			<div class="col-sm-2"></div>
			<div class="col-sm-4">
				<input type="text" id="numberInput" placeholder="1", value="1">
				</div>
				<div class="col-sm-4">
					<button class="bg-light" id="buttonOne">Triple the number</button>
				</div>
				<div class="col-sm-2"></div>
			</div>
		</body>
		<script
  src="https://code.jquery.com/jquery-3.4.1.js"
  integrity_no="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
  crossorigin="anonymous"></script>
	</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment