mirror of https://github.com/tinygo-org/tinygo.git
Ayke van Laethem
6 years ago
15 changed files with 199 additions and 17 deletions
@ -0,0 +1,9 @@ |
|||
package main |
|||
|
|||
func main() { |
|||
} |
|||
|
|||
//go:export add
|
|||
func add(a, b int) int { |
|||
return a + b |
|||
} |
@ -0,0 +1,15 @@ |
|||
<!DOCTYPE html> |
|||
|
|||
<html> |
|||
<head> |
|||
<meta charset="utf-8"/> |
|||
<title>Go WebAssembly</title> |
|||
<meta name="viewport" content="width=device-width, initial-scale=1"/> |
|||
<script src="wasm.js" async></script> |
|||
</head> |
|||
<body> |
|||
<h1>WebAssembly</h1> |
|||
<p>Add two numbers, using WebAssembly:</p> |
|||
<input type="number" id="a" value="2"/> + <input type="number" id="b" value="2"/> = <input type="number" id="result" readonly/> |
|||
</body> |
|||
</html> |
@ -0,0 +1,55 @@ |
|||
'use strict'; |
|||
|
|||
const WASM_URL = '../../../wasm.wasm'; |
|||
|
|||
var wasm; |
|||
var logLine = []; |
|||
var memory8; |
|||
|
|||
var importObject = { |
|||
env: { |
|||
io_get_stdout: function() { |
|||
return 1; |
|||
}, |
|||
resource_write: function(fd, ptr, len) { |
|||
if (fd == 1) { |
|||
for (let i=0; i<len; i++) { |
|||
let c = memory8[ptr+i]; |
|||
if (c == 13) { // CR
|
|||
// ignore
|
|||
} else if (c == 10) { // LF
|
|||
// write line
|
|||
let line = new TextDecoder("utf-8").decode(new Uint8Array(logLine)); |
|||
logLine = []; |
|||
console.log(line); |
|||
} else { |
|||
logLine.push(c); |
|||
} |
|||
} |
|||
} else { |
|||
console.error('invalid file descriptor:', fd); |
|||
} |
|||
}, |
|||
}, |
|||
}; |
|||
|
|||
function updateResult() { |
|||
let a = parseInt(document.querySelector('#a').value); |
|||
let b = parseInt(document.querySelector('#b').value); |
|||
let result = wasm.exports.add(a, b); |
|||
document.querySelector('#result').value = result; |
|||
} |
|||
|
|||
function init() { |
|||
document.querySelector('#a').oninput = updateResult; |
|||
document.querySelector('#b').oninput = updateResult; |
|||
|
|||
WebAssembly.instantiateStreaming(fetch(WASM_URL), importObject).then(function(obj) { |
|||
wasm = obj.instance; |
|||
memory8 = new Uint8Array(wasm.exports.memory.buffer); |
|||
wasm.exports.cwa_main(); |
|||
updateResult(); |
|||
}) |
|||
} |
|||
|
|||
init(); |
@ -0,0 +1,54 @@ |
|||
// +build wasm,!arm,!avr
|
|||
|
|||
package runtime |
|||
|
|||
type timeUnit int64 |
|||
|
|||
const tickMicros = 1 |
|||
|
|||
var timestamp timeUnit |
|||
|
|||
// CommonWA: io_get_stdout
|
|||
func _Cfunc_io_get_stdout() int32 |
|||
|
|||
// CommonWA: resource_write
|
|||
func _Cfunc_resource_write(id int32, ptr *uint8, len int32) int32 |
|||
|
|||
var stdout int32 |
|||
|
|||
func init() { |
|||
stdout = _Cfunc_io_get_stdout() |
|||
} |
|||
|
|||
//go:export _start
|
|||
func _start() { |
|||
initAll() |
|||
} |
|||
|
|||
//go:export cwa_main
|
|||
func cwa_main() { |
|||
initAll() // _start is not called by olin/cwa so has to be called here
|
|||
mainWrapper() |
|||
} |
|||
|
|||
func putchar(c byte) { |
|||
_Cfunc_resource_write(stdout, &c, 1) |
|||
} |
|||
|
|||
func sleepTicks(d timeUnit) { |
|||
// TODO: actually sleep here for the given time.
|
|||
timestamp += d |
|||
} |
|||
|
|||
func ticks() timeUnit { |
|||
return timestamp |
|||
} |
|||
|
|||
// Align on word boundary.
|
|||
func align(ptr uintptr) uintptr { |
|||
return (ptr + 3) &^ 3 |
|||
} |
|||
|
|||
func abort() { |
|||
// TODO
|
|||
} |
@ -0,0 +1,10 @@ |
|||
{ |
|||
"llvm-target": "wasm32-unknown-unknown-wasm", |
|||
"build-tags": ["js", "wasm"], |
|||
"linker": "ld.lld-7", |
|||
"pre-link-args": [ |
|||
"-flavor", "wasm", |
|||
"-allow-undefined-file", "targets/wasm.syms" |
|||
], |
|||
"emulator": ["cwa"] |
|||
} |
@ -0,0 +1,2 @@ |
|||
io_get_stdout |
|||
resource_write |
Loading…
Reference in new issue