examples icon indicating copy to clipboard operation
examples copied to clipboard

"Hello, World!"

Open monoclex opened this issue 3 years ago • 1 comments

Perhaps one of the more simpler requests of an example, which has a complicated solution. If WASI is implemented, this would be a call to fd_write. If they aren't, then this would be one hell of a "Hello, World!".

monoclex avatar Sep 04 '20 10:09 monoclex

It's not WASI, but here hello world example in browser. Also I recommend to check reference types example #15 if you want to write console.log() directly in AssemblyScript

//main.ts

export function hello(s: string): string {
  return "Hello " + s;
}
//main.js

import * as AsBind from '../node_modules/as-bind/dist/as-bind.esm.js';

async function init() {
  const instance = await AsBind.instantiate(fetch('../build/b.wasm'), imports);
  const wasm = instance.exports;
  console.log(wasm.hello('world!'));
};
init();
//package.json

"scripts": {
  "build": "asc main.ts --exportRuntime --transform as-bind -b b.wasm",
  "start": "servez"
},
<!-- index.html -->

<!DOCTYPE html>
<html>
<head>
  <link rel="icon" href="data:;base64,iVBORw0KGgo=">
  <title>Test</title>
</head>
<body>
  <script type="module" src="main.js"></script>
</body>
</html>

munrocket avatar Jan 28 '22 08:01 munrocket