1
- import {
2
- Promise ,
3
- PromiseActions ,
4
- setTimeout ,
5
- requestAnimationFrame ,
6
- clearTimeout ,
7
- setInterval ,
8
- clearInterval ,
9
- AnimationFrameCallback ,
10
- } from '../node_modules/ecmassembly/assembly/index'
1
+ import { PromiseActions , AnimationFrameCallback } from '../node_modules/ecmassembly/assembly/index'
11
2
import { logf32 , logf64 , logstr } from './log'
12
3
13
4
// User should not new PromiseActions directly, but we have to export it so
@@ -59,19 +50,29 @@ export function testSetInterval(): void {
59
50
export function testPromiseThen ( ) : void {
60
51
logf32 ( 2.0 )
61
52
62
- const p = new Promise < i32 , Error > ( _actions => {
63
- actions = _actions
53
+ const p = new Promise < i32 > ( actionsOrResolve => {
54
+ // @ts -expect-error for Wasm only
55
+ actions = actionsOrResolve
64
56
65
57
logf32 ( 3.0 )
66
58
67
59
setTimeout ( ( ) => {
68
- // We will not need any null assertion when closures allows us to
69
- // remove PromiseActions and therefore the user relies on the
70
- // resolve/reject functions that will be passed into here, but for
71
- // now they rely on actions object being passed in, and there's no
72
- // way to reference it inside the setTimeout callback except for
73
- // storing it on a global variable due to lacking closures.
74
- actions ! . resolve ( 1000 )
60
+ if ( ASC_TARGET == 0 ) {
61
+ // JS
62
+ actionsOrResolve ( 1000 )
63
+ } else {
64
+ // Wasm
65
+ // TODO once we have closures, remove this checking, as we'll
66
+ // have plain resolve/reject functions.
67
+
68
+ // We will not need any null assertion when closures allows us to
69
+ // remove PromiseActions and therefore the user relies on the
70
+ // resolve/reject functions that will be passed into here, but for
71
+ // now they rely on actions object being passed in, and there's no
72
+ // way to reference it inside the setTimeout callback except for
73
+ // storing it on a global variable due to lacking closures.
74
+ actions ! . resolve ( 1000 )
75
+ }
75
76
} , 2000 )
76
77
} )
77
78
@@ -92,15 +93,18 @@ let actions2: PromiseActions<i32, Error> | null = null
92
93
export function testPromiseCatch ( ) : void {
93
94
logf32 ( 5.0 )
94
95
95
- const p2 = new Promise < i32 , Error > ( _actions => {
96
- actions2 = _actions
96
+ const p2 = new Promise < i32 > ( ( actionsOrResolve , reject ) => {
97
+ // @ts -expect-error for Wasm only
98
+ actions2 = actionsOrResolve
97
99
98
100
logf32 ( 6.0 )
99
101
100
102
setTimeout ( ( ) => {
101
103
logf32 ( 7.0 )
104
+ const e = new Error ( 'rejected1' )
102
105
103
- actions2 ! . reject ( new Error ( 'rejected1' ) )
106
+ if ( ASC_TARGET == 0 ) reject ( e )
107
+ else actions2 ! . reject ( e )
104
108
} , 2000 )
105
109
} )
106
110
@@ -121,15 +125,17 @@ let actions3: PromiseActions<i32, Error> | null = null
121
125
export function testPromiseThenFinally ( ) : void {
122
126
logf32 ( 8.1 )
123
127
124
- const p2 = new Promise < i32 , Error > ( _actions => {
125
- actions3 = _actions
128
+ const p2 = new Promise < i32 > ( ( actionsOrResolve , reject ) => {
129
+ // @ts -expect-error for Wasm only
130
+ actions3 = actionsOrResolve
126
131
127
132
logf32 ( 8.2 )
128
133
129
134
setTimeout ( ( ) => {
130
135
logf32 ( 8.3 )
131
136
132
- actions3 ! . resolve ( 3200 )
137
+ if ( ASC_TARGET == 0 ) actionsOrResolve ( 3200 )
138
+ else actions3 ! . resolve ( 3200 )
133
139
} , 2000 )
134
140
} )
135
141
@@ -154,15 +160,18 @@ let actions4: PromiseActions<i32, Error> | null = null
154
160
export function testPromiseCatchFinally ( ) : void {
155
161
logf32 ( 8.5 )
156
162
157
- const p2 = new Promise < i32 , Error > ( _actions => {
158
- actions4 = _actions
163
+ const p2 = new Promise < i32 > ( ( actionsOrResolve , reject ) => {
164
+ // @ts -expect-error for Wasm only
165
+ actions4 = actionsOrResolve
159
166
160
167
logf32 ( 8.6 )
161
168
162
169
setTimeout ( ( ) => {
163
170
logf32 ( 8.7 )
171
+ const e = new Error ( 'rejected2' )
164
172
165
- actions4 ! . reject ( new Error ( 'rejected2' ) )
173
+ if ( ASC_TARGET == 0 ) reject ( e )
174
+ else actions4 ! . reject ( e )
166
175
} , 2000 )
167
176
} )
168
177
@@ -189,13 +198,17 @@ let loop: AnimationFrameCallback = (t: f64) => {}
189
198
export function testRAF ( ) : void {
190
199
logf32 ( 9.0 )
191
200
192
- const p = new Promise < f64 , Error > ( _actions => {
193
- actions5 = _actions
201
+ const p = new Promise < f64 > ( ( actionsOrResolve , reject ) => {
202
+ // @ts -expect-error for Wasm only
203
+ actions5 = actionsOrResolve
194
204
195
205
logf32 ( 10.0 )
196
206
197
207
requestAnimationFrame ( time => {
198
208
actions5 ! . resolve ( time )
209
+
210
+ if ( ASC_TARGET == 0 ) actionsOrResolve ( time )
211
+ else actions5 ! . resolve ( time )
199
212
} )
200
213
} )
201
214
0 commit comments