wa-lang.org/wazero@v1.0.2/imports/proxywasm/internal/vmstate.go (about) 1 // Copyright 2020-2021 Tetrate 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package internal 16 17 import ( 18 "wa-lang.org/wazero/imports/proxywasm/types" 19 ) 20 21 type ( 22 pluginContextState struct { 23 context types.PluginContext 24 httpCallbacks map[uint32]*httpCallbackAttribute 25 } 26 27 httpCallbackAttribute struct { 28 callback func(numHeaders, bodySize, numTrailers int) 29 callerContextID uint32 30 } 31 ) 32 33 type state struct { 34 vmContext types.VMContext 35 pluginContexts map[uint32]*pluginContextState 36 httpContexts map[uint32]types.HttpContext 37 tcpContexts map[uint32]types.TcpContext 38 39 contextIDToRootID map[uint32]uint32 40 activeContextID uint32 41 } 42 43 var currentState = &state{ 44 pluginContexts: make(map[uint32]*pluginContextState), 45 httpContexts: make(map[uint32]types.HttpContext), 46 tcpContexts: make(map[uint32]types.TcpContext), 47 contextIDToRootID: make(map[uint32]uint32), 48 } 49 50 func SetVMContext(vmContext types.VMContext) { 51 currentState.vmContext = vmContext 52 } 53 54 func RegisterHttpCallout(calloutID uint32, callback func(numHeaders, bodySize, numTrailers int)) { 55 currentState.registerHttpCallOut(calloutID, callback) 56 } 57 58 func (s *state) createPluginContext(contextID uint32) { 59 ctx := s.vmContext.NewPluginContext(contextID) 60 s.pluginContexts[contextID] = &pluginContextState{ 61 context: ctx, 62 httpCallbacks: map[uint32]*httpCallbackAttribute{}, 63 } 64 65 // NOTE: this is a temporary work around for avoiding nil pointer panic 66 // when users make http dispatch(es) on PluginContext. 67 // See https://github.com/tetratelabs/proxy-wasm-go-sdk/issues/110 68 // TODO: refactor 69 s.contextIDToRootID[contextID] = contextID 70 } 71 72 func (s *state) createTcpContext(contextID uint32, pluginContextID uint32) bool { 73 root, ok := s.pluginContexts[pluginContextID] 74 if !ok { 75 panic("invalid plugin context id") 76 } 77 78 if _, ok := s.tcpContexts[contextID]; ok { 79 panic("context id duplicated") 80 } 81 82 ctx := root.context.NewTcpContext(contextID) 83 if ctx == nil { 84 // NewTcpContext is not defined by the user 85 return false 86 } 87 s.contextIDToRootID[contextID] = pluginContextID 88 s.tcpContexts[contextID] = ctx 89 return true 90 } 91 92 func (s *state) createHttpContext(contextID uint32, pluginContextID uint32) bool { 93 root, ok := s.pluginContexts[pluginContextID] 94 if !ok { 95 panic("invalid plugin context id") 96 } 97 98 if _, ok := s.httpContexts[contextID]; ok { 99 panic("context id duplicated") 100 } 101 102 ctx := root.context.NewHttpContext(contextID) 103 if ctx == nil { 104 // NewHttpContext is not defined by the user 105 return false 106 } 107 s.contextIDToRootID[contextID] = pluginContextID 108 s.httpContexts[contextID] = ctx 109 return true 110 } 111 112 func (s *state) registerHttpCallOut(calloutID uint32, callback func(numHeaders, bodySize, numTrailers int)) { 113 r := s.pluginContexts[s.contextIDToRootID[s.activeContextID]] 114 r.httpCallbacks[calloutID] = &httpCallbackAttribute{callback: callback, callerContextID: s.activeContextID} 115 } 116 117 func (s *state) setActiveContextID(contextID uint32) { 118 s.activeContextID = contextID 119 }