Optimize code usage

This commit is contained in:
杨红岩 2022-12-13 23:02:40 +08:00
parent af6c471824
commit 129602cf09
4 changed files with 27 additions and 32 deletions

View File

@ -22,8 +22,7 @@ import (
func init() {
var resourceEventGet = func(fn interface{}, getVal func(idx int) uintptr, resp bool) (sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, request *ICefRequest, response *ICefResponse) {
var (
instance uintptr
ptr unsafe.Pointer
instance unsafe.Pointer
)
// 指针
getPtr := func(i int) unsafe.Pointer {
@ -39,10 +38,9 @@ func init() {
Id: StrToInt64(api.GoStr(tempFrame.Identifier)),
}
cefRequest := (*rICefRequest)(getPtr(3))
instance, ptr = GetInstancePtr(cefRequest.Instance)
instance = GetInstancePtr(cefRequest.Instance)
request = &ICefRequest{
instance: instance,
ptr: ptr,
Url: api.GoStr(cefRequest.Url),
Method: api.GoStr(cefRequest.Method),
ReferrerUrl: api.GoStr(cefRequest.ReferrerUrl),
@ -55,10 +53,9 @@ func init() {
}
if resp {
cefResponse := (*iCefResponse)(getPtr(4))
instance, ptr = GetInstancePtr(cefResponse.Instance)
instance = GetInstancePtr(cefResponse.Instance)
response = &ICefResponse{
instance: instance,
ptr: ptr,
Status: int32(cefResponse.Status),
StatusText: api.GoStr(cefResponse.StatusText),
MimeType: api.GoStr(cefResponse.MimeType),

View File

@ -16,8 +16,7 @@ import (
)
type ICefRequest struct {
instance uintptr
ptr unsafe.Pointer
instance unsafe.Pointer
Url string
Method string
ReferrerUrl string
@ -44,46 +43,46 @@ type rICefRequest struct {
//request
func (m *ICefRequest) IsReadOnly() bool {
return api.GoBool(cefRequest_IsReadOnly(m.instance))
return api.GoBool(cefRequest_IsReadOnly(uintptr(m.instance)))
}
func (m *ICefRequest) SetUrl(url string) {
cefRequest_SetUrl(m.instance, url)
cefRequest_SetUrl(uintptr(m.instance), url)
}
func (m *ICefRequest) SetMethod(method string) {
cefRequest_SetMethod(m.instance, method)
cefRequest_SetMethod(uintptr(m.instance), method)
}
func (m *ICefRequest) SetReferrer(referrerUrl string, policy TCefReferrerPolicy) {
cefRequest_SetReferrer(m.instance, referrerUrl, policy)
cefRequest_SetReferrer(uintptr(m.instance), referrerUrl, policy)
}
func (m *ICefRequest) SetFlags(flags TCefUrlRequestFlags) {
cefRequest_SetFlags(m.instance, flags)
cefRequest_SetFlags(uintptr(m.instance), flags)
}
func (m *ICefRequest) SetFirstPartyForCookies(url string) {
cefRequest_SetFirstPartyForCookies(m.instance, url)
cefRequest_SetFirstPartyForCookies(uintptr(m.instance), url)
}
func (m *ICefRequest) GetHeaderByName(name string) string {
return api.GoStr(cefRequest_GetHeaderByName(m.instance, name))
return api.GoStr(cefRequest_GetHeaderByName(uintptr(m.instance), name))
}
func (m *ICefRequest) SetHeaderByName(name, value string, overwrite bool) {
cefRequest_SetHeaderByName(m.instance, name, value, overwrite)
cefRequest_SetHeaderByName(uintptr(m.instance), name, value, overwrite)
}
func (m *ICefRequest) GetHeaderMap() *ICefStringMultiMap {
headerMap := &ICefStringMultiMap{}
headerMap.instance = cefRequest_GetHeaderMap(m.instance)
headerMap.instance = cefRequest_GetHeaderMap(uintptr(m.instance))
headerMap.ptr = unsafe.Pointer(headerMap.instance)
return headerMap
}
func (m *ICefRequest) SetHeaderMap(headerMap *ICefStringMultiMap) {
cefRequest_SetHeaderMap(m.instance, headerMap.instance)
cefRequest_SetHeaderMap(uintptr(m.instance), headerMap.instance)
}
//request

View File

@ -16,8 +16,7 @@ import (
)
type ICefResponse struct {
instance uintptr
ptr unsafe.Pointer
instance unsafe.Pointer
Status int32
StatusText string
MimeType string
@ -37,40 +36,40 @@ type iCefResponse struct {
}
func (m *ICefResponse) IsReadOnly() bool {
return api.GoBool(cefResponse_IsReadOnly(m.instance))
return api.GoBool(cefResponse_IsReadOnly(uintptr(m.instance)))
}
func (m *ICefResponse) SetError(error TCefErrorCode) {
cefResponse_SetError(m.instance, error)
cefResponse_SetError(uintptr(m.instance), error)
}
func (m *ICefResponse) SetStatus(status int32) {
cefResponse_SetStatus(m.instance, status)
cefResponse_SetStatus(uintptr(m.instance), status)
}
func (m *ICefResponse) SetStatusText(statusText string) {
cefResponse_SetStatusText(m.instance, statusText)
cefResponse_SetStatusText(uintptr(m.instance), statusText)
}
func (m *ICefResponse) SetMimeType(mimetype string) {
cefResponse_SetMimeType(m.instance, mimetype)
cefResponse_SetMimeType(uintptr(m.instance), mimetype)
}
func (m *ICefResponse) SetCharset(charset string) {
cefResponse_SetCharset(m.instance, charset)
cefResponse_SetCharset(uintptr(m.instance), charset)
}
func (m *ICefResponse) GetHeaderByName(name string) string {
return api.GoStr(cefResponse_GetHeaderByName(m.instance, name))
return api.GoStr(cefResponse_GetHeaderByName(uintptr(m.instance), name))
}
func (m *ICefResponse) SetHeaderByName(name, value string, overwrite bool) {
cefResponse_SetHeaderByName(m.instance, name, value, overwrite)
cefResponse_SetHeaderByName(uintptr(m.instance), name, value, overwrite)
}
func (m *ICefResponse) SetURL(url string) {
cefResponse_SetURL(m.instance, url)
cefResponse_SetURL(uintptr(m.instance), url)
}
func (m *ICefResponse) GetHeaderMap() *ICefStringMultiMap {
headerMap := &ICefStringMultiMap{}
headerMap.instance = cefResponse_GetHeaderMap(m.instance)
headerMap.instance = cefResponse_GetHeaderMap(uintptr(m.instance))
headerMap.ptr = unsafe.Pointer(headerMap.instance)
return headerMap
}

View File

@ -753,7 +753,7 @@ func ArrayIndexOf[T any](array []T, a interface{}) int {
}
//获取指针的指针的地址
func GetInstancePtr(ptr uintptr) (uintptr, unsafe.Pointer) {
func GetInstancePtr(ptr uintptr) unsafe.Pointer {
ptr = *(*uintptr)(unsafe.Pointer(ptr))
return ptr, unsafe.Pointer(ptr)
return unsafe.Pointer(ptr)
}