-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifeCycle.go
212 lines (164 loc) · 4.35 KB
/
lifeCycle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* (C) Copyright 2024 Johan Michel PIQUET, France (https://johanpiquet.fr/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package progpAPI
import (
"fmt"
"sync"
)
//region Error management
//region ScriptErrorMessage
type JsErrorMessage struct {
scriptContext JsContext
isTranslated bool
isLogged bool
isPrinted bool
Error string
ErrorLevel int
StartColumn int
EndColumn int
StartPosition int
EndPosition int
SourceMapUrl string
SourceMap string
ScriptPath string
StackTraceFrameCount int
StackTraceFrames []StackTraceFrame
}
func NewScriptErrorMessage(ctx JsContext) *JsErrorMessage {
return &JsErrorMessage{scriptContext: ctx}
}
func (m *JsErrorMessage) GetScriptContext() JsContext {
return m.scriptContext
}
type StackTraceFrame struct {
Line int
Column int
Function string
Source string
}
func (m *JsErrorMessage) Translate() {
if m.isTranslated {
return
}
m.isTranslated = true
if gErrorTranslator != nil {
gErrorTranslator(m)
}
}
func (m *JsErrorMessage) StackTrace() string {
m.Translate()
res := ""
for _, frame := range m.StackTraceFrames {
if frame.Function == "" {
res += fmt.Sprintf("at %s:%d:%d\n", frame.Source, frame.Line, frame.Column)
} else {
res += fmt.Sprintf("at %s:%d:%d: %s\n", frame.Source, frame.Line, frame.Column, frame.Function)
}
}
return res
}
func (m *JsErrorMessage) Print(forcePrinting bool) {
if m.isPrinted && !forcePrinting {
return
}
m.isPrinted = true
m.Translate()
fmt.Printf("Javascript Error - %s\n", m.Error)
print(m.StackTrace())
}
func (m *JsErrorMessage) LogError() {
if (m == nil) || m.isLogged {
return
}
m.isLogged = true
m.Print(false)
}
// DisarmError allows to continue after an un-catch error.
func (m *JsErrorMessage) DisarmError(ctx JsContext) {
ctx.DisarmError(m)
}
//endregion
func OnUnCatchScriptError(error *JsErrorMessage) {
error.LogError()
error.scriptContext.GetScriptEngine().Shutdown()
}
func SetErrorTranslator(handler ErrorTranslatorF) {
gErrorTranslator = handler
}
type ErrorTranslatorF func(error *JsErrorMessage)
var gErrorTranslator ErrorTranslatorF
//endregion
//region ScriptExecResult
type ScriptExecResult struct {
ScriptError *JsErrorMessage
GoError error
}
type V8ScriptCallback func(result ScriptExecResult)
func (m *ScriptExecResult) HasError() bool {
return m.ScriptError != nil || m.GoError != nil
}
func (m *ScriptExecResult) PrintError() bool {
if m.ScriptError != nil {
m.ScriptError.Print(false)
return true
} else if m.GoError != nil {
println("GO ERROR - " + m.GoError.Error())
return true
}
return false
}
//endregion
//region Background tasks
var gBackgroundTasksCount = 0
var gBackgroundTasksCountMutex sync.Mutex
var gBackgroundTasksWaitChannel = make(chan bool)
func DeclareBackgroundTaskStarted() {
gBackgroundTasksCountMutex.Lock()
defer gBackgroundTasksCountMutex.Unlock()
gBackgroundTasksCount++
}
func DeclareBackgroundTaskEnded() {
gBackgroundTasksCountMutex.Lock()
defer gBackgroundTasksCountMutex.Unlock()
if gBackgroundTasksCount != 0 {
gBackgroundTasksCount--
}
if gBackgroundTasksCount == 0 {
close(gBackgroundTasksWaitChannel)
gBackgroundTasksWaitChannel = nil
}
}
// ForceExitingVM allows stopping the process without doing an os.exit.
// It's a requirement if profiling the memory, since without that, the log file isn't correctly flushed.
func ForceExitingVM() {
gBackgroundTasksCountMutex.Lock()
defer gBackgroundTasksCountMutex.Unlock()
gBackgroundTasksCount = 0
if gBackgroundTasksWaitChannel != nil {
close(gBackgroundTasksWaitChannel)
}
ForEachScriptEngine(func(e ScriptEngine) {
e.Shutdown()
})
}
// WaitTasksEnd wait until all background tasks are finished.
// It's used in order to know if the application can exit.
func WaitTasksEnd() {
if gBackgroundTasksWaitChannel != nil {
<-gBackgroundTasksWaitChannel
}
}
//endregion