Skip to content

Commit 694d966

Browse files
committed
make APIs global just like in JavaScript, convert example to portable mode. Wasm build still works. JS build is TODO, next.
1 parent 7dd8bfb commit 694d966

10 files changed

+116
-84
lines changed

asconfig.json

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
{
2-
"targets": {
3-
"debug": {
4-
"binaryFile": "build/untouched.wasm",
5-
"textFile": "build/untouched.wat",
6-
"sourceMap": true,
7-
"debug": true
8-
},
9-
"release": {
10-
"binaryFile": "build/optimized.wasm",
11-
"textFile": "build/optimized.wat",
12-
"sourceMap": true,
13-
"optimizeLevel": 3,
14-
"shrinkLevel": 1,
15-
"converge": false,
16-
"noAssert": false
17-
}
18-
},
19-
"options": {}
20-
}
2+
"entries": ["assembly/index.ts"],
3+
"targets": {
4+
"debug": {
5+
"binaryFile": "build/untouched.wasm",
6+
"textFile": "build/untouched.wat",
7+
"sourceMap": true,
8+
"debug": true
9+
},
10+
"release": {
11+
"binaryFile": "build/optimized.wasm",
12+
"textFile": "build/optimized.wat",
13+
"sourceMap": true,
14+
"optimizeLevel": 3,
15+
"shrinkLevel": 1,
16+
"converge": false,
17+
"noAssert": false
18+
}
19+
},
20+
"options": {}
21+
}

assembly/Promise.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import {ptr} from './utils'
33

44
// TODO convert to callback form once closures are out.
55
// type Executor<T> = (resolve: (result: T) => void, reject: (error: Error | null) => void) => void
6-
type Executor<T, E extends Error> = (resRej: PromiseActions<T, E>) => void
6+
type Executor<T, E extends Error> = (resRej: PromiseActions<T, E>, rejectJSOnly: () => void) => void
77

88
// We shouldn't have to export this (this class should not even exist) once AS supports closures.
9+
@global
910
export class PromiseActions<T, E extends Error> {
1011
/*friend*/ constructor(private promise: Promise<T, E>) {}
1112

@@ -22,14 +23,14 @@ export class PromiseActions<T, E extends Error> {
2223
this.promise.__runThen()
2324
}
2425

25-
reject(error: E): void {
26+
reject(reason: E): void {
2627
// @ts-ignore, internal access
2728
if (this.promise.__isSettled) return
2829
// @ts-ignore, internal access
2930
this.promise.__isSettled = true
3031

3132
// @ts-ignore, internal access
32-
this.promise.__error.push(error)
33+
this.promise.__error.push(reason)
3334

3435
// @ts-ignore, internal access
3536
this.promise.__runCatch()
@@ -45,7 +46,8 @@ export class PromiseActions<T, E extends Error> {
4546
* closures in AS lands, we'll change this to be two callbacks instead of an
4647
* object, as per the Promise spec.
4748
*/
48-
export class Promise<T, E extends Error> {
49+
@global
50+
export class Promise<T, E extends Error = Error> {
4951
private __ptr: usize = ptr(this)
5052
private __isSettled: boolean = false
5153

@@ -63,7 +65,7 @@ export class Promise<T, E extends Error> {
6365
private __finallyCallback: Array<() => void> = []
6466

6567
constructor(private executor: Executor<T, E>) {
66-
this.executor(this.__actions)
68+
this.executor(this.__actions, () => {})
6769
}
6870

6971
/**

assembly/requestAnimationFrame.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ export {_requestAnimationFrame}
44

55
export type AnimationFrameCallback = (time: f64) => void
66

7+
// @ts-expect-error func decos
8+
@global
79
export function requestAnimationFrame(fn: AnimationFrameCallback): i32 {
810
return _requestAnimationFrame(fn.index)
911
}
1012

13+
// @ts-expect-error func decos
14+
@global
1115
export declare function cancelAnimationFrame(id: i32): void

assembly/setInterval.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ declare function _setInterval(fn: usize, milliseconds: f32): i32
22

33
export {_setInterval}
44

5+
// @ts-expect-error func decos
6+
@global
57
export function setInterval(fn: () => void, milliseconds: f32 = 0.0): i32 {
68
return _setInterval(fn.index, milliseconds)
79
}
810

11+
// @ts-expect-error func decos
12+
@global
913
export declare function clearInterval(id: i32): void

assembly/setTimeout.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ declare function _setTimeout(fn: usize, milliseconds: f32): i32
22

33
export {_setTimeout}
44

5+
// @ts-expect-error function decorators
6+
@global
57
export function setTimeout(fn: () => void, milliseconds: f32 = 0.0): i32 {
68
return _setTimeout(fn.index, milliseconds)
79
}
810

11+
// @ts-expect-error function decorators
12+
@global
913
export declare function clearTimeout(id: i32): void

example/asconfig.json

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
{
2-
"targets": {
3-
"debug": {
4-
"binaryFile": "build/untouched.wasm",
5-
"textFile": "build/untouched.wat",
6-
"sourceMap": true,
7-
"debug": true
8-
},
9-
"release": {
10-
"binaryFile": "build/optimized.wasm",
11-
"textFile": "build/optimized.wat",
12-
"sourceMap": true,
13-
"optimizeLevel": 3,
14-
"shrinkLevel": 1,
15-
"converge": false,
16-
"noAssert": false
17-
}
18-
},
19-
"options": {}
20-
}
2+
"entries": ["assembly/index.ts"],
3+
"targets": {
4+
"debug": {
5+
"binaryFile": "build/untouched.wasm",
6+
"textFile": "build/untouched.wat",
7+
"sourceMap": true,
8+
"debug": true
9+
},
10+
"release": {
11+
"binaryFile": "build/optimized.wasm",
12+
"textFile": "build/optimized.wat",
13+
"sourceMap": true,
14+
"optimizeLevel": 3,
15+
"shrinkLevel": 1,
16+
"converge": false,
17+
"noAssert": false
18+
}
19+
},
20+
"options": {}
21+
}

example/assembly/index.ts

+43-30
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
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'
112
import {logf32, logf64, logstr} from './log'
123

134
// User should not new PromiseActions directly, but we have to export it so
@@ -59,19 +50,29 @@ export function testSetInterval(): void {
5950
export function testPromiseThen(): void {
6051
logf32(2.0)
6152

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
6456

6557
logf32(3.0)
6658

6759
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+
}
7576
}, 2000)
7677
})
7778

@@ -92,15 +93,18 @@ let actions2: PromiseActions<i32, Error> | null = null
9293
export function testPromiseCatch(): void {
9394
logf32(5.0)
9495

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
9799

98100
logf32(6.0)
99101

100102
setTimeout(() => {
101103
logf32(7.0)
104+
const e = new Error('rejected1')
102105

103-
actions2!.reject(new Error('rejected1'))
106+
if (ASC_TARGET == 0) reject(e)
107+
else actions2!.reject(e)
104108
}, 2000)
105109
})
106110

@@ -121,15 +125,17 @@ let actions3: PromiseActions<i32, Error> | null = null
121125
export function testPromiseThenFinally(): void {
122126
logf32(8.1)
123127

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
126131

127132
logf32(8.2)
128133

129134
setTimeout(() => {
130135
logf32(8.3)
131136

132-
actions3!.resolve(3200)
137+
if (ASC_TARGET == 0) actionsOrResolve(3200)
138+
else actions3!.resolve(3200)
133139
}, 2000)
134140
})
135141

@@ -154,15 +160,18 @@ let actions4: PromiseActions<i32, Error> | null = null
154160
export function testPromiseCatchFinally(): void {
155161
logf32(8.5)
156162

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
159166

160167
logf32(8.6)
161168

162169
setTimeout(() => {
163170
logf32(8.7)
171+
const e = new Error('rejected2')
164172

165-
actions4!.reject(new Error('rejected2'))
173+
if (ASC_TARGET == 0) reject(e)
174+
else actions4!.reject(e)
166175
}, 2000)
167176
})
168177

@@ -189,13 +198,17 @@ let loop: AnimationFrameCallback = (t: f64) => {}
189198
export function testRAF(): void {
190199
logf32(9.0)
191200

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
194204

195205
logf32(10.0)
196206

197207
requestAnimationFrame(time => {
198208
actions5!.resolve(time)
209+
210+
if (ASC_TARGET == 0) actionsOrResolve(time)
211+
else actions5!.resolve(time)
199212
})
200213
})
201214

example/assembly/tsconfig.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"extends": "../../../../.npm-packages/lib/node_modules/assemblyscript/std/assembly.json",
3-
"include": [
4-
"./**/*.ts"
5-
]
6-
}
2+
"extends": "assemblyscript/std/portable.json",
3+
"compilerOptions": {
4+
"lib": ["dom", "esnext"]
5+
},
6+
"include": ["./**/*.ts"]
7+
}

example/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"start": "npm i && npm run dev",
55
"build": "npm run asbuild",
66
"dev": "npm run asbuild:untouched && npm test && echo TODO: watch mode",
7-
"asbuild:untouched": "asc assembly/index.ts --target debug --exportTable --exportRuntime",
8-
"asbuild:optimized": "asc assembly/index.ts --target release --exportTable --exportRuntime",
7+
"asbuild:untouched": "asc --target debug --exportTable --exportRuntime",
8+
"asbuild:optimized": "asc --target release --exportTable --exportRuntime",
99
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
1010
"test": "node tests"
1111
},

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
"start": "npm run dev",
1515
"build": "npm run asbuild",
1616
"dev": "npm run asbuild:untouched && npm test && echo TODO: watch mode",
17-
"asbuild:untouched": "asc assembly/index.ts --target debug --exportTable --exportRuntime",
18-
"asbuild:optimized": "asc assembly/index.ts --target release --exportTable --exportRuntime",
17+
"asbuild:untouched": "asc --target debug --exportTable --exportRuntime",
18+
"asbuild:optimized": "asc --target release --exportTable --exportRuntime",
1919
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
20-
"test": "cd example && npm start",
20+
"clean": "rimraf build && rimraf example/build",
21+
"test": "npm run clean && npm run asbuild && cd example && npm start",
2122
"release:patch": "npm version patch -m 'v%s' && npm publish && git push --follow-tags",
2223
"release:minor": "npm version minor -m 'v%s' && npm publish && git push --follow-tags",
2324
"release:major": "npm version major -m 'v%s' && npm publish && git push --follow-tags"
2425
},
2526
"devDependencies": {
2627
"assemblyscript": "^0.20.0",
27-
"prettier": "^2.2.1"
28+
"prettier": "^2.2.1",
29+
"rimraf": "^3.0.2"
2830
},
2931
"keywords": [
3032
"assemblyscript",

0 commit comments

Comments
 (0)