gowm 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +500 -0
- package/composables/useWasm.js +286 -0
- package/hooks/useWasm.js +89 -0
- package/package.json +52 -0
- package/runtime/wasm_exec.js +575 -0
- package/src/bridge.js +171 -0
- package/src/index.js +97 -0
- package/src/loader.js +150 -0
- package/types/index.d.ts +121 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
Toutes les modifications notables de ce projet seront documentées dans ce fichier.
|
|
4
|
+
|
|
5
|
+
## [1.0.0] - 2025-05-28
|
|
6
|
+
|
|
7
|
+
### Ajouté
|
|
8
|
+
- Support complet pour l'intégration de modules WebAssembly Go
|
|
9
|
+
- Loader automatique pour les fichiers WASM
|
|
10
|
+
- Bridge JavaScript pour l'interaction avec les fonctions Go
|
|
11
|
+
- Hooks React pour une intégration facile
|
|
12
|
+
- Composables Vue.js pour les applications Vue
|
|
13
|
+
- Exemples d'utilisation basique et avancée
|
|
14
|
+
- Support du navigateur et de Node.js
|
|
15
|
+
- Calculatrice de démonstration en React et Vue
|
|
16
|
+
- Documentation complète avec README
|
|
17
|
+
- Types TypeScript pour un développement type-safe
|
|
18
|
+
|
|
19
|
+
### Fonctionnalités
|
|
20
|
+
- Chargement automatique des modules WASM
|
|
21
|
+
- Gestion d'erreurs robuste
|
|
22
|
+
- Support asynchrone complet
|
|
23
|
+
- API simple et intuitive
|
|
24
|
+
- Compatible avec les principaux frameworks
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 devbyben
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
# GoWM - Go WebAssembly Manager
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<img src="./logo.png" alt="GoWM Logo" width="200" />
|
|
5
|
+
|
|
6
|
+
<p><strong>GoWM</strong> simplifie l'intégration de modules WebAssembly écrits en Go dans vos projets JavaScript.</p>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
## ✨ Fonctionnalités
|
|
10
|
+
|
|
11
|
+
- 🚀 **Interface unifiée** pour charger des modules WASM Go
|
|
12
|
+
- 🔧 **Support Node.js et navigateur** avec détection automatique
|
|
13
|
+
- ⚛️ **Hook React** intégré (`useWasm`, `useWasmFromNPM`)
|
|
14
|
+
- 🖖 **Composables Vue.js** pour Vue 3 (`useWasm`, `useWasmFromNPM`, `useMultipleWasm`)
|
|
15
|
+
- 📦 **Chargement depuis NPM** des packages WASM avec résolution automatique
|
|
16
|
+
- 🛡️ **Gestion d'erreurs robuste** et détection de fonctions
|
|
17
|
+
- 🧹 **Gestion automatique de la mémoire** et nettoyage des ressources
|
|
18
|
+
- 📊 **Statistiques et monitoring** des modules chargés
|
|
19
|
+
- 🔄 **Appels synchrones et asynchrones** avec `call()` et `callAsync()`
|
|
20
|
+
- 🎯 **API singleton** et classe GoWM pour différents usages
|
|
21
|
+
|
|
22
|
+
## 📥 Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install gowm
|
|
26
|
+
# ou
|
|
27
|
+
yarn add gowm
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 🚀 Utilisation Rapide
|
|
31
|
+
|
|
32
|
+
### Node.js
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
const { load } = require('gowm');
|
|
36
|
+
|
|
37
|
+
async function example() {
|
|
38
|
+
try {
|
|
39
|
+
// Charger un module WASM
|
|
40
|
+
const math = await load('./math.wasm', { name: 'math' });
|
|
41
|
+
|
|
42
|
+
// Appeler des fonctions
|
|
43
|
+
const result = math.call('add', 5, 3);
|
|
44
|
+
console.log('5 + 3 =', result);
|
|
45
|
+
|
|
46
|
+
// Appel asynchrone
|
|
47
|
+
const asyncResult = await math.callAsync('multiply', 4, 7);
|
|
48
|
+
console.log('4 * 7 =', asyncResult);
|
|
49
|
+
|
|
50
|
+
// Vérifier si une fonction existe
|
|
51
|
+
if (math.hasFunction('subtract')) {
|
|
52
|
+
console.log('Function subtract is available');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Obtenir les statistiques
|
|
56
|
+
const stats = math.getStats();
|
|
57
|
+
console.log('Functions:', stats.functions);
|
|
58
|
+
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error('Erreur:', error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
example();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### React
|
|
68
|
+
|
|
69
|
+
```jsx
|
|
70
|
+
import React, { useState } from 'react';
|
|
71
|
+
import { useWasm } from 'gowm/hooks/useWasm';
|
|
72
|
+
|
|
73
|
+
function CalculatorComponent() {
|
|
74
|
+
const { wasm, loading, error } = useWasm('/math.wasm', { name: 'math' });
|
|
75
|
+
const [result, setResult] = useState(null);
|
|
76
|
+
|
|
77
|
+
const calculate = async () => {
|
|
78
|
+
if (wasm) {
|
|
79
|
+
try {
|
|
80
|
+
const sum = await wasm.callAsync('add', 10, 20);
|
|
81
|
+
setResult(sum);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
console.error('Calculation error:', err);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
if (loading) return <div>Chargement du module WASM...</div>;
|
|
89
|
+
if (error) return <div>Erreur: {error.message}</div>;
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<div>
|
|
93
|
+
<button onClick={calculate}>Calculer 10 + 20</button>
|
|
94
|
+
{result !== null && <p>Résultat: {result}</p>}
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Vue.js
|
|
101
|
+
|
|
102
|
+
```vue
|
|
103
|
+
<template>
|
|
104
|
+
<div class="calculator">
|
|
105
|
+
<div v-if="loading">Chargement du module WASM...</div>
|
|
106
|
+
<div v-else-if="error">Erreur: {{ error.message }}</div>
|
|
107
|
+
<div v-else>
|
|
108
|
+
<input v-model.number="num1" type="number" placeholder="Premier nombre" />
|
|
109
|
+
<input v-model.number="num2" type="number" placeholder="Deuxième nombre" />
|
|
110
|
+
<button @click="calculate">Calculer {{ num1 }} + {{ num2 }}</button>
|
|
111
|
+
<div v-if="result !== null">Résultat: {{ result }}</div>
|
|
112
|
+
</div>
|
|
113
|
+
</div>
|
|
114
|
+
</template>
|
|
115
|
+
|
|
116
|
+
<script>
|
|
117
|
+
import { ref } from 'vue';
|
|
118
|
+
import { useWasm } from 'gowm/composables/useWasm';
|
|
119
|
+
|
|
120
|
+
export default {
|
|
121
|
+
name: 'CalculatorComponent',
|
|
122
|
+
setup() {
|
|
123
|
+
const { wasm, loading, error } = useWasm('/math.wasm', { name: 'math' });
|
|
124
|
+
const num1 = ref(10);
|
|
125
|
+
const num2 = ref(20);
|
|
126
|
+
const result = ref(null);
|
|
127
|
+
|
|
128
|
+
const calculate = async () => {
|
|
129
|
+
if (wasm.value) {
|
|
130
|
+
try {
|
|
131
|
+
result.value = await wasm.value.callAsync('add', num1.value, num2.value);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error('Erreur de calcul:', err);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return { wasm, loading, error, num1, num2, result, calculate };
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
</script>
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Chargement depuis NPM
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
const { loadFromNPM } = require('gowm');
|
|
148
|
+
|
|
149
|
+
async function useLibrary() {
|
|
150
|
+
try {
|
|
151
|
+
// GoWM essaie automatiquement main.wasm, index.wasm, puis [package-name].wasm
|
|
152
|
+
const math = await loadFromNPM('my-wasm-math');
|
|
153
|
+
const result = math.call('add', 10, 20);
|
|
154
|
+
console.log('Résultat:', result);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.error('Erreur de chargement NPM:', error);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## 📚 API Référence
|
|
162
|
+
|
|
163
|
+
### load(wasmPath, options)
|
|
164
|
+
|
|
165
|
+
Charge un module WASM depuis un fichier local.
|
|
166
|
+
|
|
167
|
+
**Paramètres:**
|
|
168
|
+
- `wasmPath` (string): Chemin vers le fichier .wasm
|
|
169
|
+
- `options` (object, optionnel):
|
|
170
|
+
- `name` (string): Nom du module (par défaut: 'default')
|
|
171
|
+
- `goRuntimePath` (string): Chemin personnalisé vers wasm_exec.js
|
|
172
|
+
- `preInit` (boolean): Pré-initialiser le module (par défaut: true)
|
|
173
|
+
|
|
174
|
+
**Retour:** Promise<WasmBridge>
|
|
175
|
+
|
|
176
|
+
### loadFromNPM(packageName, options)
|
|
177
|
+
|
|
178
|
+
Charge un module WASM depuis un package NPM avec résolution automatique des fichiers.
|
|
179
|
+
|
|
180
|
+
**Paramètres:**
|
|
181
|
+
- `packageName` (string): Nom du package NPM
|
|
182
|
+
- `options` (object, optionnel): Mêmes options que `load()`
|
|
183
|
+
|
|
184
|
+
**Résolution automatique:** Essaie `main.wasm`, `index.wasm`, puis `[packageName].wasm`
|
|
185
|
+
|
|
186
|
+
**Retour:** Promise<WasmBridge>
|
|
187
|
+
|
|
188
|
+
### get(name)
|
|
189
|
+
|
|
190
|
+
Récupère un module déjà chargé par son nom.
|
|
191
|
+
|
|
192
|
+
**Paramètres:**
|
|
193
|
+
- `name` (string, optionnel): Nom du module (par défaut: 'default')
|
|
194
|
+
|
|
195
|
+
**Retour:** WasmBridge | null
|
|
196
|
+
|
|
197
|
+
### Fonctions utilitaires
|
|
198
|
+
|
|
199
|
+
- `listModules()`: Liste tous les modules chargés
|
|
200
|
+
- `getStats()`: Obtient les statistiques de tous les modules
|
|
201
|
+
- `unload(name)`: Décharge un module spécifique
|
|
202
|
+
- `unloadAll()`: Décharge tous les modules
|
|
203
|
+
|
|
204
|
+
### WasmBridge
|
|
205
|
+
|
|
206
|
+
Interface principale pour interagir avec un module WASM chargé.
|
|
207
|
+
|
|
208
|
+
#### Méthodes
|
|
209
|
+
|
|
210
|
+
- `call(funcName, ...args)`: Appel synchrone d'une fonction Go WASM
|
|
211
|
+
- `callAsync(funcName, ...args)`: Appel asynchrone d'une fonction (retourne une Promise)
|
|
212
|
+
- `hasFunction(funcName)`: Vérifier si une fonction existe dans le module
|
|
213
|
+
- `getAvailableFunctions()`: Obtenir la liste des fonctions disponibles
|
|
214
|
+
- `registerCallback(name, callback)`: Enregistrer un callback JavaScript accessible depuis Go
|
|
215
|
+
- `unregisterCallback(name)`: Supprimer un callback JavaScript
|
|
216
|
+
- `createBuffer(data)`: Créer un buffer pour transfert de données (Float64Array, Uint8Array, string)
|
|
217
|
+
- `getStats()`: Obtenir les statistiques du module (fonctions, callbacks, mémoire)
|
|
218
|
+
|
|
219
|
+
### Composables Vue.js
|
|
220
|
+
|
|
221
|
+
#### useWasm(wasmPath, options)
|
|
222
|
+
|
|
223
|
+
Composable Vue 3 pour charger et utiliser un module WASM avec réactivité.
|
|
224
|
+
|
|
225
|
+
**Paramètres:**
|
|
226
|
+
- `wasmPath` (string|Ref<string>): Chemin vers le fichier .wasm
|
|
227
|
+
- `options` (object|Ref<object>, optionnel): Options de chargement
|
|
228
|
+
|
|
229
|
+
**Retour:**
|
|
230
|
+
```javascript
|
|
231
|
+
{
|
|
232
|
+
wasm: Ref<WasmBridge | null>,
|
|
233
|
+
loading: Ref<boolean>,
|
|
234
|
+
error: Ref<Error | null>,
|
|
235
|
+
reload: () => void
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### useWasmFromNPM(packageName, options)
|
|
240
|
+
|
|
241
|
+
Composable Vue 3 pour charger un module WASM depuis NPM.
|
|
242
|
+
|
|
243
|
+
**Paramètres:**
|
|
244
|
+
- `packageName` (string|Ref<string>): Nom du package NPM
|
|
245
|
+
- `options` (object|Ref<object>, optionnel): Options de chargement
|
|
246
|
+
|
|
247
|
+
**Retour:** Même interface que `useWasm`
|
|
248
|
+
|
|
249
|
+
#### useMultipleWasm(modules)
|
|
250
|
+
|
|
251
|
+
Composable pour charger et gérer plusieurs modules WASM simultanément.
|
|
252
|
+
|
|
253
|
+
**Paramètres:**
|
|
254
|
+
- `modules` (Array|Ref<Array>): Tableau de configuration des modules
|
|
255
|
+
|
|
256
|
+
**Configuration d'un module:**
|
|
257
|
+
```javascript
|
|
258
|
+
{
|
|
259
|
+
name?: string, // Nom du module
|
|
260
|
+
path?: string, // Chemin local vers le fichier WASM
|
|
261
|
+
package?: string, // Nom du package NPM
|
|
262
|
+
options?: LoadOptions // Options de chargement
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
**Retour:**
|
|
267
|
+
```javascript
|
|
268
|
+
{
|
|
269
|
+
modules: Ref<Record<string, WasmBridge>>, // Modules chargés par nom
|
|
270
|
+
loading: Ref<boolean>, // État de chargement global
|
|
271
|
+
errors: Ref<Record<string, Error>>, // Erreurs par module
|
|
272
|
+
reload: () => void // Recharger tous les modules
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
**Exemple:**
|
|
277
|
+
```javascript
|
|
278
|
+
const { modules, loading, errors } = useMultipleWasm([
|
|
279
|
+
{ name: 'math', path: './math.wasm' },
|
|
280
|
+
{ name: 'utils', package: 'my-utils-wasm' }
|
|
281
|
+
]);
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## 🔧 Création de Modules WASM Go Compatibles
|
|
285
|
+
|
|
286
|
+
### Structure Recommandée
|
|
287
|
+
|
|
288
|
+
```
|
|
289
|
+
my-wasm-lib/
|
|
290
|
+
├── main.go # Code source Go
|
|
291
|
+
├── go.mod # Module Go
|
|
292
|
+
├── build.sh # Script de compilation
|
|
293
|
+
├── package.json # Configuration NPM
|
|
294
|
+
└── README.md # Documentation
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Code Go Optimisé
|
|
298
|
+
|
|
299
|
+
```go
|
|
300
|
+
//go:build js && wasm
|
|
301
|
+
|
|
302
|
+
package main
|
|
303
|
+
|
|
304
|
+
import (
|
|
305
|
+
"syscall/js"
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
func add(this js.Value, args []js.Value) interface{} {
|
|
309
|
+
if len(args) != 2 {
|
|
310
|
+
return js.ValueOf("Erreur: deux arguments requis")
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
a := args[0].Float()
|
|
314
|
+
b := args[1].Float()
|
|
315
|
+
return js.ValueOf(a + b)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
func multiply(this js.Value, args []js.Value) interface{} {
|
|
319
|
+
if len(args) != 2 {
|
|
320
|
+
return js.ValueOf("Erreur: deux arguments requis")
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
a := args[0].Float()
|
|
324
|
+
b := args[1].Float()
|
|
325
|
+
return js.ValueOf(a * b)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
func main() {
|
|
329
|
+
// Enregistrer les fonctions globalement
|
|
330
|
+
js.Global().Set("add", js.FuncOf(add))
|
|
331
|
+
js.Global().Set("multiply", js.FuncOf(multiply))
|
|
332
|
+
|
|
333
|
+
// Signal de prêt pour GoWM (important pour la détection)
|
|
334
|
+
js.Global().Set("__gowm_ready", js.ValueOf(true))
|
|
335
|
+
|
|
336
|
+
// Maintenir le programme en vie
|
|
337
|
+
select {}
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### Script de Build
|
|
342
|
+
|
|
343
|
+
```bash
|
|
344
|
+
#!/bin/bash
|
|
345
|
+
# build.sh
|
|
346
|
+
|
|
347
|
+
set -e
|
|
348
|
+
|
|
349
|
+
echo "🔨 Compilation du module WASM..."
|
|
350
|
+
|
|
351
|
+
# Compilation optimisée pour la taille et les performances
|
|
352
|
+
GOOS=js GOARCH=wasm go build \
|
|
353
|
+
-ldflags="-s -w" \
|
|
354
|
+
-o main.wasm \
|
|
355
|
+
main.go
|
|
356
|
+
|
|
357
|
+
echo "📦 Copie du runtime Go..."
|
|
358
|
+
cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" .
|
|
359
|
+
|
|
360
|
+
echo "✅ Build terminé!"
|
|
361
|
+
echo "📊 Taille du fichier WASM: $(du -h main.wasm | cut -f1)"
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### Package.json pour Distribution
|
|
365
|
+
|
|
366
|
+
```json
|
|
367
|
+
{
|
|
368
|
+
"name": "my-wasm-math",
|
|
369
|
+
"version": "1.0.0",
|
|
370
|
+
"description": "Bibliothèque mathématique en WebAssembly Go",
|
|
371
|
+
"main": "main.wasm",
|
|
372
|
+
"files": [
|
|
373
|
+
"main.wasm",
|
|
374
|
+
"wasm_exec.js",
|
|
375
|
+
"README.md"
|
|
376
|
+
],
|
|
377
|
+
"scripts": {
|
|
378
|
+
"build": "./build.sh"
|
|
379
|
+
},
|
|
380
|
+
"keywords": ["wasm", "webassembly", "go", "math"],
|
|
381
|
+
"peerDependencies": {
|
|
382
|
+
"gowm": "^1.0.0"
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
## 🧪 Tests et Exemples
|
|
388
|
+
|
|
389
|
+
Le dossier `examples/` contient des exemples complets d'utilisation.
|
|
390
|
+
|
|
391
|
+
### Tests disponibles
|
|
392
|
+
|
|
393
|
+
```bash
|
|
394
|
+
# Test basique Node.js
|
|
395
|
+
npm run test:basic
|
|
396
|
+
|
|
397
|
+
# Test avancé avec gestion d'erreurs
|
|
398
|
+
npm run test:advanced
|
|
399
|
+
|
|
400
|
+
# Tests complets des fonctionnalités WASM
|
|
401
|
+
npm run test:wasm
|
|
402
|
+
|
|
403
|
+
# Tests complets (tous les tests)
|
|
404
|
+
npm test
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### Démo navigateur
|
|
408
|
+
|
|
409
|
+
```bash
|
|
410
|
+
# Démarrer un serveur local
|
|
411
|
+
npm run demo:serve
|
|
412
|
+
|
|
413
|
+
# Puis ouvrir http://localhost:3000/browser-demo.html
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Compiler l'exemple math-wasm
|
|
417
|
+
|
|
418
|
+
```bash
|
|
419
|
+
# Compiler le module d'exemple
|
|
420
|
+
npm run build:example
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
## 🤝 Support TypeScript
|
|
424
|
+
|
|
425
|
+
GoWM inclut des types TypeScript complets. Importez simplement:
|
|
426
|
+
|
|
427
|
+
```typescript
|
|
428
|
+
import { load, WasmBridge, LoadOptions, GoWM } from 'gowm';
|
|
429
|
+
|
|
430
|
+
// Exemple d'utilisation typée
|
|
431
|
+
async function example(): Promise<void> {
|
|
432
|
+
const bridge: WasmBridge = await load('./math.wasm');
|
|
433
|
+
const result: number = bridge.call('add', 10, 20);
|
|
434
|
+
|
|
435
|
+
const stats = bridge.getStats();
|
|
436
|
+
console.log('Functions:', stats.functions);
|
|
437
|
+
}
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### Types disponibles
|
|
441
|
+
|
|
442
|
+
- `WasmBridge`: Interface principale pour les modules WASM
|
|
443
|
+
- `LoadOptions`: Options de chargement des modules
|
|
444
|
+
- `ModuleStats`: Statistiques des modules
|
|
445
|
+
- `BufferInfo`: Information sur les buffers mémoire
|
|
446
|
+
- `GoWM`: Classe principale pour la gestion avancée
|
|
447
|
+
- `UseWasmResult`: Résultat du hook React
|
|
448
|
+
- `VueWasmResult`: Résultat des composables Vue
|
|
449
|
+
|
|
450
|
+
## 📋 Prérequis
|
|
451
|
+
|
|
452
|
+
- **Node.js** >= 14.0.0
|
|
453
|
+
- **Navigateurs** modernes supportant WebAssembly
|
|
454
|
+
- **React** >= 16.8.0 (pour les hooks React, optionnel)
|
|
455
|
+
- **Vue.js** >= 3.0.0 (pour les composables Vue, optionnel)
|
|
456
|
+
- **Go** >= 1.21 (pour compiler vos propres modules WASM)
|
|
457
|
+
|
|
458
|
+
## 🔍 Débogage et Monitoring
|
|
459
|
+
|
|
460
|
+
### Obtenir des statistiques
|
|
461
|
+
|
|
462
|
+
```javascript
|
|
463
|
+
const bridge = await load('./module.wasm');
|
|
464
|
+
const stats = bridge.getStats();
|
|
465
|
+
|
|
466
|
+
console.log('Module prêt:', stats.ready);
|
|
467
|
+
console.log('Fonctions disponibles:', stats.functions);
|
|
468
|
+
console.log('Callbacks enregistrés:', stats.callbacks);
|
|
469
|
+
console.log('Utilisation mémoire:', stats.memoryUsage, 'bytes');
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### Gestion d'erreurs
|
|
473
|
+
|
|
474
|
+
```javascript
|
|
475
|
+
try {
|
|
476
|
+
const result = bridge.call('nonExistentFunction');
|
|
477
|
+
} catch (error) {
|
|
478
|
+
console.error('Erreur d\'appel WASM:', error.message);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Vérifier avant d'appeler
|
|
482
|
+
if (bridge.hasFunction('myFunction')) {
|
|
483
|
+
const result = bridge.call('myFunction', args);
|
|
484
|
+
}
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### Callbacks JavaScript
|
|
488
|
+
|
|
489
|
+
```javascript
|
|
490
|
+
// Enregistrer un callback accessible depuis Go
|
|
491
|
+
bridge.registerCallback('logMessage', (message) => {
|
|
492
|
+
console.log('Message depuis Go:', message);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
// Dans votre code Go :
|
|
496
|
+
// js.Global().Call("__gowm_callback_logMessage", "Hello from Go!")
|
|
497
|
+
```
|
|
498
|
+
---
|
|
499
|
+
|
|
500
|
+
Créé avec ❤️ pour simplifier l'utilisation de WebAssembly Go dans JavaScript.
|