From f59c18dfed0d012981d6329a429cf146119a49a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BA=A2=E5=B2=A9?= Date: Wed, 1 Nov 2023 16:54:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BA=95=E5=B1=82=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E9=93=BE=E6=8E=A5=E5=BA=93=E5=BC=82=E5=B8=B8=E6=8D=95?= =?UTF-8?q?=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cef/exception/exception.go | 21 ++++++++++ cef/init.go | 19 --------- cef/internal/def/proc-def-index.go | 2 + cef/internal/def/proc-def.go | 2 + cef/internal/exception/exception.go | 45 +++++++++++++++++++++ cef/internal/exception/exception_posix.go | 38 +++++++++++++++++ cef/internal/exception/exception_windows.go | 17 ++++++++ example/ipc-on-emit/go-to-js/go-to-js.go | 4 ++ 8 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 cef/exception/exception.go create mode 100644 cef/internal/exception/exception.go create mode 100644 cef/internal/exception/exception_posix.go create mode 100644 cef/internal/exception/exception_windows.go diff --git a/cef/exception/exception.go b/cef/exception/exception.go new file mode 100644 index 00000000..80b346a5 --- /dev/null +++ b/cef/exception/exception.go @@ -0,0 +1,21 @@ +//---------------------------------------- +// +// Copyright © yanghy. All Rights Reserved. +// +// Licensed under Apache License Version 2.0, January 2004 +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +//---------------------------------------- + +// Package exception +// Underlying dynamic link library exception capture +// Supports: Windows, MacOS. +package exception + +import "github.com/energye/energy/v2/cef/internal/exception" + +// SetOnException 设置 liblcl -> CEF 低层异常捕获回调函数 +func SetOnException(fn exception.Callback) { + exception.HandlerInit(fn) +} diff --git a/cef/init.go b/cef/init.go index 0087c1bf..705dec30 100644 --- a/cef/init.go +++ b/cef/init.go @@ -17,7 +17,6 @@ import ( . "github.com/energye/energy/v2/cef/process" . "github.com/energye/energy/v2/common" "github.com/energye/energy/v2/common/imports/tempdll" - "github.com/energye/energy/v2/logger" "github.com/energye/golcl/energy/inits" "github.com/energye/golcl/lcl" "github.com/energye/golcl/lcl/api" @@ -27,16 +26,6 @@ import ( "runtime" ) -// ExceptionCallback 异常回调函数 -type ExceptionCallback func(sender lcl.IObject, e *lcl.Exception) - -var exceptionCallback ExceptionCallback - -// SetOnException 设置 lib-lcl -> CEF 低层异常捕获回调函数 -func SetOnException(exception ExceptionCallback) { - exceptionCallback = exception -} - // GlobalInit 全局初始化 // 需要手动调用的函数,在main函数中调用 // 参数: @@ -90,14 +79,6 @@ func GlobalInit(libs *embed.FS, resources *embed.FS) { } // main thread run call applicationQueueAsyncCallInit() - //应用低层出错异常捕获 - lcl.Application.SetOnException(func(sender lcl.IObject, e *lcl.Exception) { - if exceptionCallback != nil { - exceptionCallback(sender, e) - } else { - logger.Error("ResultString:", e.Message()) - } - }) } // v8init v8初始化 diff --git a/cef/internal/def/proc-def-index.go b/cef/internal/def/proc-def-index.go index 481b0a5e..576a6671 100644 --- a/cef/internal/def/proc-def-index.go +++ b/cef/internal/def/proc-def-index.go @@ -23,6 +23,8 @@ package def const ( //null nil null_nil = iota + // Exception + SetExceptionHandlerCallback // TForm Form_SetOnMessagesEvent // CEF diff --git a/cef/internal/def/proc-def.go b/cef/internal/def/proc-def.go index 366a66e2..46ec6255 100644 --- a/cef/internal/def/proc-def.go +++ b/cef/internal/def/proc-def.go @@ -23,6 +23,8 @@ func init() { var energyImportDefs = []*dllimports.ImportTable{ //null nil dllimports.NewEnergyImport("", 0), + // Exception + dllimports.NewEnergyImport("SetExceptionHandlerCallback", 0), // TForm dllimports.NewEnergyImport("Form_SetOnMessagesEvent", 0), //CEF diff --git a/cef/internal/exception/exception.go b/cef/internal/exception/exception.go new file mode 100644 index 00000000..55ada163 --- /dev/null +++ b/cef/internal/exception/exception.go @@ -0,0 +1,45 @@ +//---------------------------------------- +// +// Copyright © yanghy. All Rights Reserved. +// +// Licensed under Apache License Version 2.0, January 2004 +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +//---------------------------------------- + +// Package exception +// Underlying dynamic link library exception capture +// Supports: Windows, MacOS. +package exception + +import ( + "github.com/energye/energy/v2/cef/internal/def" + "github.com/energye/energy/v2/common/imports" + "github.com/energye/golcl/lcl/api" + "github.com/energye/golcl/lcl/api/dllimports" +) + +type Callback func(message string) + +var ( + exceptionHandler dllimports.ProcAddr + exceptionHandlerCallback Callback +) + +// HandlerInit +// 底层库异常处理器初始化 +func HandlerInit(fn Callback) { + if exceptionHandlerCallback == nil { + exceptionHandler = imports.Proc(def.SetExceptionHandlerCallback) + exceptionHandler.Call(exceptionHandlerProcEventAddr) + exceptionHandlerCallback = fn + } +} + +func exceptionHandlerProc(message uintptr) uintptr { + if exceptionHandlerCallback != nil { + exceptionHandlerCallback(api.GoStr(message)) + } + return 0 +} diff --git a/cef/internal/exception/exception_posix.go b/cef/internal/exception/exception_posix.go new file mode 100644 index 00000000..693dd44d --- /dev/null +++ b/cef/internal/exception/exception_posix.go @@ -0,0 +1,38 @@ +//---------------------------------------- +// +// Copyright © yanghy. All Rights Reserved. +// +// Licensed under Apache License Version 2.0, January 2004 +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +//---------------------------------------- + +//go:build !windows && cgo +// +build !windows,cgo + +package exception + +//// #cgo darwin CFLAGS: -mmacosx-version-min=10.8 -DMACOSX_DEPLOYMENT_TARGET=10.8 +// #cgo darwin CFLAGS: -mmacosx-version-min=10.10 +// #cgo darwin LDFLAGS: -mmacosx-version-min=10.10 +// +// extern void* doExceptionHandlerProc(void* f); +// static void* doExceptionHandlerProcEventAddr() { +// return &doExceptionHandlerProc; +// } +import "C" + +import ( + "unsafe" +) + +//export doExceptionHandlerProc +func doExceptionHandlerProc(f unsafe.Pointer) unsafe.Pointer { + exceptionHandlerProc(uintptr(f)) + return nil +} + +var ( + exceptionHandlerProcEventAddr = uintptr(C.doExceptionHandlerProcEventAddr()) +) diff --git a/cef/internal/exception/exception_windows.go b/cef/internal/exception/exception_windows.go new file mode 100644 index 00000000..f8116e33 --- /dev/null +++ b/cef/internal/exception/exception_windows.go @@ -0,0 +1,17 @@ +//---------------------------------------- +// +// Copyright © yanghy. All Rights Reserved. +// +// Licensed under Apache License Version 2.0, January 2004 +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +//---------------------------------------- + +package exception + +import "syscall" + +var ( + exceptionHandlerProcEventAddr = syscall.NewCallback(exceptionHandlerProc) +) diff --git a/example/ipc-on-emit/go-to-js/go-to-js.go b/example/ipc-on-emit/go-to-js/go-to-js.go index 5fb568de..17bb9e17 100644 --- a/example/ipc-on-emit/go-to-js/go-to-js.go +++ b/example/ipc-on-emit/go-to-js/go-to-js.go @@ -5,6 +5,7 @@ import ( "embed" "fmt" "github.com/energye/energy/v2/cef" + "github.com/energye/energy/v2/cef/exception" "github.com/energye/energy/v2/cef/ipc" "github.com/energye/energy/v2/cef/ipc/context" "github.com/energye/energy/v2/pkgs/assetserve" @@ -17,6 +18,9 @@ var resources embed.FS func main() { //全局初始化 每个应用都必须调用的 cef.GlobalInit(nil, &resources) + exception.SetOnException(func(message string) { + fmt.Println("message", message) + }) //创建应用 cefApp := cef.NewApplication() //指定一个URL地址,或本地html文件目录