@xyo-network/wasm 2.74.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.
@@ -0,0 +1,168 @@
1
+ import {
2
+ bigInt,
3
+ bulkMemory,
4
+ exceptions,
5
+ extendedConst,
6
+ gc,
7
+ memory64,
8
+ multiValue,
9
+ mutableGlobals,
10
+ referenceTypes,
11
+ relaxedSimd,
12
+ saturatedFloatToInt,
13
+ signExtensions,
14
+ simd,
15
+ streamingCompilation,
16
+ tailCall,
17
+ threads,
18
+ } from 'wasm-feature-detect'
19
+
20
+ export const WasmFeatureDetectors = {
21
+ bigInt: bigInt,
22
+ bulkMemory: bulkMemory,
23
+ exceptions: exceptions,
24
+ extendedConst: extendedConst,
25
+ gc: gc,
26
+ memory64: memory64,
27
+ multiValue: multiValue,
28
+ mutableGlobals: mutableGlobals,
29
+ referenceTypes: referenceTypes,
30
+ relaxedSimd: relaxedSimd,
31
+ saturatedFloatToInt: saturatedFloatToInt,
32
+ signExtensions: signExtensions,
33
+ simd: simd,
34
+ streamingCompilation: streamingCompilation,
35
+ tailCall: tailCall,
36
+ threads: threads,
37
+ } as const
38
+
39
+ export type WasmFeature = keyof typeof WasmFeatureDetectors
40
+
41
+ export class WasmSupport {
42
+ private _allowWasm = true
43
+ private _featureSupport: Partial<Record<WasmFeature, boolean>> = {}
44
+ private _forceWasm = false
45
+ private _isInitialized = false
46
+ private _isWasmFeatureSetSupported = false
47
+
48
+ /**
49
+ * Instance constructor for use where async instantiation
50
+ * is not possible. Where possible, prefer the static
51
+ * create method over use of this constructor directly
52
+ * as no initialization (feature detection) is able to
53
+ * be done here
54
+ * @param desiredFeatures The desired feature set
55
+ */
56
+ constructor(protected desiredFeatures: WasmFeature[]) {}
57
+
58
+ /**
59
+ * Is Wasm allowed
60
+ */
61
+ get allowWasm(): boolean {
62
+ return this._allowWasm
63
+ }
64
+ /**
65
+ * Whether or not to allow WASM usage
66
+ */
67
+ set allowWasm(v: boolean) {
68
+ this._allowWasm = v
69
+ }
70
+
71
+ /**
72
+ * Whether or not Wasm should be used based on the desired
73
+ * feature set, initialization state, or force-use settings
74
+ */
75
+ get canUseWasm(): boolean {
76
+ return (
77
+ // Just force WASM
78
+ this._forceWasm ||
79
+ // Or if we haven't checked be optimistic
80
+ (this._allowWasm && !this._isInitialized) ||
81
+ // Or if we have checked and WASM is not supported, be realistic
82
+ (this._allowWasm && this._isInitialized && this._isWasmFeatureSetSupported)
83
+ )
84
+ }
85
+
86
+ /**
87
+ * Returns a object containing a property for each desired wasm feature
88
+ * with a boolean value indicating whether or not the feature is supported
89
+ */
90
+ get featureSupport(): Readonly<Partial<Record<WasmFeature, boolean>>> {
91
+ return { ...this._featureSupport }
92
+ }
93
+
94
+ /**
95
+ * Force use of Wasm
96
+ */
97
+ get forceWasm(): boolean {
98
+ return this._forceWasm
99
+ }
100
+ /**
101
+ * Whether or not to force Wasm usage
102
+ */
103
+ set forceWasm(v: boolean) {
104
+ this._forceWasm = v
105
+ }
106
+
107
+ /**
108
+ * Whether or not Wasm is supported based
109
+ * on the desired feature set
110
+ */
111
+ get isDesiredFeatureSetSupported(): boolean {
112
+ return this._isWasmFeatureSetSupported
113
+ }
114
+
115
+ /**
116
+ * Whether or not Wasm detection has been run
117
+ * for the desired feature set
118
+ */
119
+ get isInitialized(): boolean {
120
+ return this._isInitialized
121
+ }
122
+
123
+ /**
124
+ * Static creation & async initialization for use where
125
+ * async instantiation is possible
126
+ * @param desiredFeatures The desired feature set
127
+ * @returns An initialized instance of the class with detection
128
+ * for the desired feature set
129
+ */
130
+ static async create(desiredFeatures: WasmFeature[]): Promise<WasmSupport> {
131
+ const instance = new WasmSupport(desiredFeatures)
132
+ await instance.initialize()
133
+ return Promise.resolve(instance)
134
+ }
135
+
136
+ /**
137
+ * Checks for specific wasm features
138
+ * @param features The list of features to check for
139
+ * @returns True if all the features are supported, false otherwise
140
+ */
141
+ async featureCheck(features: WasmFeature[]): Promise<boolean> {
142
+ const results = await Promise.all(features.map((feature) => WasmFeatureDetectors[feature]).map(async (detector) => await detector()))
143
+ return results.every((result) => result)
144
+ }
145
+
146
+ /**
147
+ * Does feature detection for the desired feature set
148
+ */
149
+ async initialize(): Promise<void> {
150
+ if (this._isInitialized) return
151
+ await this.detectDesiredFeatures()
152
+ this._isInitialized = true
153
+ return
154
+ }
155
+
156
+ protected async detectDesiredFeatures(): Promise<void> {
157
+ for (let feature = 0; feature < this.desiredFeatures.length; feature++) {
158
+ const desiredFeature = this.desiredFeatures[feature]
159
+ const detector = WasmFeatureDetectors[desiredFeature]
160
+ if (!(await detector())) {
161
+ this._featureSupport[desiredFeature] = false
162
+ } else {
163
+ this._featureSupport[desiredFeature] = true
164
+ }
165
+ }
166
+ this._isWasmFeatureSetSupported = Object.values(this._featureSupport).every((v) => v)
167
+ }
168
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './WasmSupport'
package/typedoc.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "entryPoints": ["./src/index.ts"],
4
+ "tsconfig": "./tsconfig.typedoc.json"
5
+ }