Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
separate hostcalls from context
Browse files Browse the repository at this point in the history
  • Loading branch information
mathetake committed Sep 7, 2020
1 parent 73d62ef commit d57f18a
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 516 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gotest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
go-version: 1.14

- name: build examples
run: find ./ -type f -name "main.go" | xargs -Ip tinygo build -o p.wasm -target=wasm -wasm-abi=generic p
run: find ./examples -type f -name "main.go" | xargs -Ip tinygo build -o p.wasm -target=wasm -wasm-abi=generic p

test:
name: test
Expand Down
4 changes: 2 additions & 2 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func newHelloWorld(contextID uint32) runtime.RootContext {
// override
func (ctx *helloWorld) OnVMStart(_ int) bool {
runtime.LogInfo("proxy_on_vm_start from Go!")
if err := ctx.SetTickPeriod(1000); err != nil {
if err := runtime.HostCallSetTickPeriodMilliSeconds(1000); err != nil {
runtime.LogCritical("failed to set tick period: " + err.Error())
}
return true
}

// override
func (ctx *helloWorld) OnTick() {
t := ctx.GetCurrentTime()
t := runtime.HostCallGetCurrentTime()
msg := "OnTick on " + strconv.FormatUint(uint64(ctx.contextID), 10)
msg += ", it's " + strconv.FormatInt(t, 10)
runtime.LogInfo(msg)
Expand Down
14 changes: 7 additions & 7 deletions examples/http_auth_random/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func newContext(contextID uint32) runtime.HttpContext {

// override default
func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) types.Action {
hs, err := ctx.GetHttpRequestHeaders()
hs, err := runtime.HostCallGetHttpRequestHeaders()
if err != nil {
runtime.LogCritical("failed to get request headers: " + err.Error())
return types.ActionContinue
Expand All @@ -33,7 +33,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) types.Action {
runtime.LogInfo("request header: " + h[0] + ": " + h[1])
}

if _, err := ctx.DispatchHttpCall(
if _, err := runtime.HostCallDispatchHttpCall(
"httpbin", hs, "", [][2]string{}, 50000); err != nil {
runtime.LogCritical("dipatch httpcall failed: " + err.Error())
}
Expand All @@ -42,29 +42,29 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) types.Action {

// override default
func (ctx *httpHeaders) OnHttpCallResponse(_ uint32, _ int, bodySize int, _ int) {
b, err := ctx.GetHttpCallResponseBody(0, bodySize)
b, err := runtime.HostCallGetHttpCallResponseBody(0, bodySize)
if err != nil {
runtime.LogCritical("failed to get response body: " + err.Error())
ctx.ResumeHttpRequest()
runtime.HostCallResumeHttpRequest()
return
}

s := fnv.New32a()
if _, err := s.Write(b); err != nil {
runtime.LogCritical("failed to calculate hash: " + err.Error())
ctx.ResumeHttpRequest()
runtime.HostCallResumeHttpRequest()
return
}

if s.Sum32()%2 == 0 {
runtime.LogInfo("access granted")
ctx.ResumeHttpRequest()
runtime.HostCallResumeHttpRequest()
return
}

msg := "access forbidden"
runtime.LogInfo(msg)
ctx.SendHttpResponse(403, [][2]string{
runtime.HostCallSendHttpResponse(403, [][2]string{
{"powered-by", "proxy-wasm-go!!"},
}, msg)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/http_headers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newContext(contextID uint32) runtime.HttpContext {

// override
func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) types.Action {
hs, err := ctx.GetHttpRequestHeaders()
hs, err := runtime.HostCallGetHttpRequestHeaders()
if err != nil {
runtime.LogCritical("failed to get request headers: " + err.Error())
}
Expand All @@ -36,7 +36,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) types.Action {

// override
func (ctx *httpHeaders) OnHttpResponseHeaders(_ int, _ bool) types.Action {
hs, err := ctx.GetHttpResponseHeaders()
hs, err := runtime.HostCallGetHttpResponseHeaders()
if err != nil {
runtime.LogCritical("failed to get request headers: " + err.Error())
}
Expand Down
File renamed without changes.
Loading

0 comments on commit d57f18a

Please sign in to comment.