This repository was archived by the owner on Dec 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionRegistry.go
More file actions
402 lines (317 loc) · 10.3 KB
/
functionRegistry.go
File metadata and controls
402 lines (317 loc) · 10.3 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* (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 (
"embed"
"log"
"path"
"reflect"
"slices"
"sort"
"strconv"
"strings"
)
var gFunctionRegistry *FunctionRegistry
//region ExposedFunction
type RegisteredFunction struct {
IsAsync bool
Group string
JsFunctionName string
GoFunctionName string
GoFunctionFullName string
GoFunctionRef any
GoFunctionInfos ParsedGoFunction
}
//endregion
//region FunctionRegistry
type FunctionRegistry struct {
modules map[string]bool
functionsArray []*RegisteredFunction
functionsMap map[string]*RegisteredFunction
extraGoNamespaces []string
jsModulesTSX map[string]EmbeddedFile
useDynamicMode bool
}
func GetFunctionRegistry() *FunctionRegistry {
if gFunctionRegistry == nil {
gFunctionRegistry = &FunctionRegistry{
modules: make(map[string]bool),
jsModulesTSX: make(map[string]EmbeddedFile),
functionsMap: make(map[string]*RegisteredFunction),
}
}
return gFunctionRegistry
}
func (m *FunctionRegistry) GetNamespaces() map[string]bool {
nsMap := make(map[string]bool)
for _, e := range m.extraGoNamespaces {
nsMap[e] = true
}
for _, fct := range m.functionsMap {
fctNS := fct.GoFunctionInfos.CallParamNamespaces
if fctNS != nil {
for _, e := range fctNS {
nsMap[e] = true
}
}
}
return nsMap
}
func (m *FunctionRegistry) UseGoNamespace(goNamespace string) *FunctionModule {
nsParts := strings.Split(goNamespace, "/")
modName := nsParts[len(nsParts)-1]
m.extraGoNamespaces = append(m.extraGoNamespaces, goNamespace)
return &FunctionModule{
functionRegistry: m,
namespacePath: goNamespace,
moduleName: modName,
}
}
func (m *FunctionRegistry) declareModuleAsNotEmpty(modName string) {
m.modules[modName] = true
}
func (m *FunctionRegistry) addFunction(isAsync bool, group string, jsFunctionName string, goFunctionName string, goFunctionRef any) {
fct := &RegisteredFunction{
IsAsync: isAsync,
Group: group,
JsFunctionName: jsFunctionName,
GoFunctionName: goFunctionName,
GoFunctionFullName: group + "/" + goFunctionName,
GoFunctionRef: goFunctionRef,
}
parsed, err := ParseGoFunction(fct)
if err != nil {
log.Fatal("Error when parsing function " + goFunctionName + ".\nMessage: " + err.Error())
}
fct.GoFunctionInfos = parsed
m.functionsArray = append(m.functionsArray, fct)
m.functionsMap[fct.JsFunctionName] = fct
}
func (m *FunctionRegistry) GetAllFunctions(sortList bool) []*RegisteredFunction {
if sortList {
sort.Slice(m.functionsArray, func(i, j int) bool {
return m.functionsArray[i].GoFunctionFullName < m.functionsArray[j].GoFunctionFullName
})
}
return m.functionsArray
}
/*func (m *JsFunctionRegistry) installNodeModules(targetDir string) {
for key, entry := range m.jsModulesTSX {
asBytes, err := entry.Read()
if err == nil {
// Convert for filesystem where the path-separator is \
parts := strings.Split(key, "/")
key := path.Join(parts...)
progpScripts.SafeWriteFile(path.Join(targetDir, key)+".tsx", asBytes)
}
}
}*/
func (m *FunctionRegistry) declareNodeModule(embedded embed.FS, innerPath string, modName string) {
innerPath = path.Join("embedded", innerPath, modName) + ".tsx"
modName = "@progp/" + modName
m.jsModulesTSX[modName] = EmbeddedFile{FS: embedded, InnerPath: innerPath}
}
func (m *FunctionRegistry) GetEmbeddedModulesTSX() map[string]EmbeddedFile {
return m.jsModulesTSX
}
func (m *FunctionRegistry) GetRefToFunction(jsFunctionName string) *RegisteredFunction {
v, ok := m.functionsMap[jsFunctionName]
if ok {
return v
}
return nil
}
func (m *FunctionRegistry) EnableDynamicMode(enabled bool) {
m.useDynamicMode = enabled
}
func (m *FunctionRegistry) IsUsingDynamicMode() bool {
return m.useDynamicMode
}
//endregion
//region FunctionGroup
type FunctionGroup struct {
jsGroupName string
goModule *FunctionModule
}
func (m *FunctionGroup) AddFunction(javascriptName string, goFunctionName string, goFunctionRef any) {
m.goModule.addFunction(false, m.jsGroupName, javascriptName, goFunctionName, goFunctionRef)
}
func (m *FunctionGroup) AddAsyncFunction(jsName string, goFunctionName string, jsFunction any) {
m.goModule.addFunction(true, m.jsGroupName, jsName, goFunctionName, jsFunction)
}
//endregion
//region FunctionModule
type FunctionModule struct {
namespacePath string
moduleName string
isModuleInjected bool
functionRegistry *FunctionRegistry
modGroup FunctionGroup
}
func (m *FunctionModule) ModName() string {
return m.moduleName
}
// AddFunction add a function to a javascript group
// which name is the name of the go namespace last part.
func (m *FunctionModule) AddFunction(javascriptName string, goFunctionName string, goFunctionRef any) {
m.addFunction(false, m.moduleName, javascriptName, goFunctionName, goFunctionRef)
}
// AddAsyncFunction add an async function to a javascript group
// which name is the name of the go namespace last part.
func (m *FunctionModule) AddAsyncFunction(jsName string, goFunctionName string, jsFunction any) {
m.addFunction(true, m.moduleName, jsName, goFunctionName, jsFunction)
}
func (m *FunctionModule) GetFunctionRegistry() *FunctionRegistry {
return m.functionRegistry
}
// UseCustomGroup allows using another javascript group than
// the default group for this go namespace.
func (m *FunctionModule) UseCustomGroup(jsGroupName string) *FunctionGroup {
return &FunctionGroup{jsGroupName: jsGroupName, goModule: m}
}
// UseGroupGlobal allows using the global group
// where functionsArray directly accessible to javascript scripts without importing them.
func (m *FunctionModule) UseGroupGlobal() *FunctionGroup { return m.UseCustomGroup("global") }
func (m *FunctionModule) addFunction(isAsync bool, groupName string, javascriptName string, goFunctionName string, goFunctionRef any) {
if groupName == "" {
groupName = "global"
}
goFunctionName = m.moduleName + "." + goFunctionName
endsWithAsync := strings.HasSuffix(goFunctionName, "Async")
if isAsync {
if !endsWithAsync {
log.Fatalf("Go function `%s` is asynchrone and MUST ends with 'Async'", goFunctionName)
}
} else {
if endsWithAsync {
log.Fatalf("Go function `%s` is NOT asynchrone and MUST NOT ends with 'Async'", goFunctionName)
}
}
if !m.isModuleInjected {
m.isModuleInjected = true
m.functionRegistry.declareModuleAsNotEmpty(m.moduleName)
}
m.functionRegistry.addFunction(isAsync, groupName, javascriptName, goFunctionName, goFunctionRef)
}
func (m *FunctionModule) DeclareNodeModule(embedded embed.FS, embeddedDirPath string, modName string) {
m.functionRegistry.declareNodeModule(embedded, embeddedDirPath, modName)
}
//endregion
//region EmbeddedFile
type EmbeddedFile struct {
FS embed.FS
InnerPath string
}
func (m *EmbeddedFile) Read() ([]byte, error) {
return m.FS.ReadFile(m.InnerPath)
}
//endregion
//region GoFunctionInfos
type ParsedGoFunction struct {
GeneratorUniqName string
GoFunctionName string
ParamTypes []string
ParamTypeRefs []reflect.Type
CallParamNamespaces []string
ReturnType string
ReturnErrorOffset int
JsFunctionName string
JsGroupName string
}
func (m *ParsedGoFunction) GetJsFunctionName() string {
return m.JsFunctionName
}
func (m *ParsedGoFunction) GetGoFunctionName() string {
return m.GoFunctionName
}
//endregion
var gNextGoFunctionId = 0
func ParseGoFunction(fct *RegisteredFunction) (ParsedGoFunction, error) {
reflectFct := reflect.TypeOf(fct.GoFunctionRef)
return ParseGoFunctionReflect(reflectFct, fct.GoFunctionFullName)
}
func ParseGoFunctionReflect(reflectFct reflect.Type, goFunctionFullName string) (ParsedGoFunction, error) {
res := ParsedGoFunction{ReturnErrorOffset: -1}
// > Parse parameters
inCount := reflectFct.NumIn()
res.ParamTypeRefs = make([]reflect.Type, inCount)
for i := 0; i < inCount; i++ {
param := reflectFct.In(i)
paramTypeName := param.String()
res.ParamTypes = append(res.ParamTypes, paramTypeName)
res.ParamTypeRefs[i] = param
// > Extract namespace
// If pointer then take the target type.
for {
kind := param.Kind()
if (kind == reflect.Pointer) || (kind == reflect.Array) || (kind == reflect.Slice) || (kind == reflect.Map) {
param = param.Elem()
} else {
break
}
}
// Add the namespace if enclosed into a namespace.
//
pkgPath := param.PkgPath()
//
if pkgPath != "" {
if !slices.Contains(res.CallParamNamespaces, pkgPath) {
res.CallParamNamespaces = append(res.CallParamNamespaces, pkgPath)
}
}
}
// > Parse return infos
outCount := reflectFct.NumOut()
var returnTypes []string
for i := 0; i < outCount; i++ {
outParam := reflectFct.Out(i)
outType := outParam.String()
returnTypes = append(returnTypes, outType)
}
if outCount >= 1 {
if outCount == 1 {
if returnTypes[0] == "error" {
res.ReturnErrorOffset = 0
returnTypes = nil
} else {
res.ReturnType = returnTypes[0]
}
} else if outCount == 2 {
if returnTypes[0] == "error" {
res.ReturnErrorOffset = 0
returnTypes = returnTypes[1:]
if returnTypes[0] == "error" {
log.Fatalf("Function %s can return (error, error)", goFunctionFullName)
}
} else if returnTypes[1] == "error" {
res.ReturnErrorOffset = 1
returnTypes = returnTypes[0:1]
} else {
log.Fatalf("Function %s has more than 1 return type", goFunctionFullName)
}
res.ReturnType = returnTypes[0]
} else {
log.Fatalf("Function %s has more than 1 return type", goFunctionFullName)
}
}
gNextGoFunctionId++
res.GeneratorUniqName = strconv.Itoa(gNextGoFunctionId)
return res, nil
}
// StringBuffer allows the code generator to known that we want this bytes
// to be send as if it was a string. Allows to avoid the cost of converting
// []byte to string before calling javascript.
type StringBuffer []byte