-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChange.go
More file actions
115 lines (97 loc) · 3.32 KB
/
Copy pathChange.go
File metadata and controls
115 lines (97 loc) · 3.32 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
package client
import (
. "github.com/tinywasm/fmt"
)
func (w *WasmClient) Shortcuts() []map[string]string {
return []map[string]string{
{w.buildLargeSizeShortcut: Translate("mode", "Large", "stLib").String()},
{w.buildMediumSizeShortcut: Translate("mode", "Medium", "tinygo").String()},
{w.buildSmallSizeShortcut: Translate("mode", "Small", "tinygo").String()},
}
}
// Change updates the compiler mode for WasmClient.
// Implements the HandlerEdit interface: Change(newValue string)
func (w *WasmClient) Change(newValue string) {
// Normalize input: trim spaces and convert to uppercase
newValue = Convert(newValue).ToUpper().String()
// Validate mode
if err := w.ValidateMode(newValue); err != nil {
w.Logger(err.Error())
return
}
// Lazily verify TinyGo installation status ONLY when a TinyGo mode is requested
if w.RequiresTinyGo(newValue) {
w.verifyTinyGoInstallationStatus()
if !w.TinyGoInstalled {
if err := w.handleTinyGoMissing(); err != nil {
w.Logger(err.Error())
return
}
// TinyGo installed successfully — update status so builders use it
w.TinyGoInstalled = true
}
}
// Update active builder
w.UpdateCurrentBuilder(newValue)
// Save mode to store if available
if w.Database != nil {
w.Database.Set(StoreKeySizeMode, newValue)
}
// Auto-recompile
compilationSuccess := true
if err := w.RecompileMainWasm(); err != nil {
errorMsg := Translate("Error:", "auto", "compilation", "failed:", err).String()
//errorMsg = "Error: auto compilation failed: " + err.Error()
w.Logger(errorMsg)
compilationSuccess = false
// Don't return early - still need to update assets and notify listeners
}
// Only notify listener when compilation succeeded.
// If compilation failed, the new mode's runtime would mismatch with the
// old mode's .wasm binary, causing the browser to freeze on reload.
if compilationSuccess && w.OnWasmExecChange != nil {
w.OnWasmExecChange()
}
if compilationSuccess {
w.LogSuccessState("Changed", "To", "Mode", newValue)
}
}
// RecompileMainWasm recompiles the main WASM file using the current Storage mode.
func (w *WasmClient) RecompileMainWasm() error {
if w.Storage == nil {
return Err("Storage not initialized")
}
// Use Storage.Compile() to respect In-Memory vs Disk mode
return w.Storage.Compile()
}
// ValidateMode validates if the provided mode is supported
func (w *WasmClient) ValidateMode(mode string) error {
// Ensure mode is uppercase to match configured shortcuts which are
// expected to be single uppercase letters by default.
mode = Convert(mode).ToUpper().String()
validModes := []string{
Convert(w.buildLargeSizeShortcut).ToUpper().String(),
Convert(w.buildMediumSizeShortcut).ToUpper().String(),
Convert(w.buildSmallSizeShortcut).ToUpper().String(),
}
for _, valid := range validModes {
if mode == valid {
return nil
}
}
return Err("mode", ":", mode, "invalid", "valid", ":", validModes)
}
func (w *WasmClient) storageMode() string {
switch w.Storage.(type) {
case *MemoryStorage:
return "mem"
default:
return "disk"
}
}
// LogSuccessState logs the standard success message with WASM details (Safe: Acquires Lock)
func (w *WasmClient) LogSuccessState(messages ...any) {
event := Translate(messages...).String()
suffix := Sprintf("[%s|%s]", w.storageMode(), w.activeSizeBuilder.BinarySize())
w.Logger(event, " ", suffix)
}