1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5#include "go_asm.h"
6#include "textflag.h"
7
8// _rt0_wasm_js is not used itself. It only exists to mark the exported functions as alive.
9TEXT _rt0_wasm_js(SB),NOSPLIT,$0
10	I32Const $wasm_export_run(SB)
11	Drop
12	I32Const $wasm_export_resume(SB)
13	Drop
14	I32Const $wasm_export_getsp(SB)
15	Drop
16
17// wasm_export_run gets called from JavaScript. It initializes the Go runtime and executes Go code until it needs
18// to wait for an event. It does NOT follow the Go ABI. It has two WebAssembly parameters:
19// R0: argc (i32)
20// R1: argv (i32)
21TEXT wasm_export_run(SB),NOSPLIT,$0
22	MOVD $runtime·wasmStack+(m0Stack__size-16)(SB), SP
23
24	Get SP
25	Get R0 // argc
26	I64ExtendI32U
27	I64Store $0
28
29	Get SP
30	Get R1 // argv
31	I64ExtendI32U
32	I64Store $8
33
34	I32Const $0 // entry PC_B
35	Call runtime·rt0_go(SB)
36	Drop
37	Call wasm_pc_f_loop(SB)
38
39	Return
40
41// wasm_export_resume gets called from JavaScript. It resumes the execution of Go code until it needs to wait for
42// an event.
43TEXT wasm_export_resume(SB),NOSPLIT,$0
44	I32Const $0
45	Call runtime·handleEvent(SB)
46	Drop
47	Call wasm_pc_f_loop(SB)
48
49	Return
50
51// wasm_export_getsp gets called from JavaScript to retrieve the SP.
52TEXT wasm_export_getsp(SB),NOSPLIT,$0
53	Get SP
54	Return
55
56TEXT runtime·pause(SB), NOSPLIT, $0-8
57	MOVD newsp+0(FP), SP
58	I32Const $1
59	Set PAUSE
60	RETUNWIND
61
62TEXT runtime·exit(SB), NOSPLIT, $0-4
63	I32Const $0
64	Call runtime·wasmExit(SB)
65	I32Const $1
66	Set PAUSE
67	RETUNWIND
68