Skip to content

Commit 6f19794

Browse files
fix: context tests, tick interval
1 parent 46c8ab5 commit 6f19794

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

crates/js-component-bindgen/src/intrinsics/component.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ impl ComponentIntrinsic {
176176
this.#componentIdx = args.componentIdx;
177177
const self = this;
178178
179-
this.#taskResumerInterval = setTimeout(() => {{
179+
this.#taskResumerInterval = setInterval(() => {{
180180
try {{
181181
if (self.errored()) {{
182182
self.stopTaskResumer();
183183
console.error(`(component ${{this.#errored.componentIdx}}) ASYNC ERROR:`, this.#errored);
184184
return;
185185
}}
186-
if (this.tick()) {{ setTimeout(() => {{ this.tick(); }}, 0); }}
186+
this.tick();
187187
}} catch (err) {{
188188
{debug_log_fn}('[{class_name}#taskResumer()] tick failed', {{ err }});
189189
}}
@@ -312,7 +312,7 @@ impl ComponentIntrinsic {
312312
313313
#removeSuspendedTaskMeta(taskID) {{
314314
{debug_log_fn}('[{class_name}#removeSuspendedTaskMeta()] removing suspended task', {{ taskID }});
315-
const idx = this.#suspendedTaskIDs.findIndex(t => t && t.taskID === taskID);
315+
const idx = this.#suspendedTaskIDs.findIndex(t => t === taskID);
316316
const meta = this.#suspendedTasksByTaskID.get(taskID);
317317
this.#suspendedTaskIDs[idx] = null;
318318
this.#suspendedTasksByTaskID.delete(taskID);

packages/jco/test/helpers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export async function setupAsyncTest(args) {
133133
'Both component.path and component.build should not be specified at the same time'
134134
);
135135
}
136+
if (componentPath && componentPath.endsWith(".wasm") && !componentName) {
137+
componentName = basename(componentPath).replace(/.wasm$/, "");
138+
}
136139

137140
// If this component should be built "just in time" -- i.e. created when this test is run
138141
let componentBuildCleanup;

packages/jco/test/p3/context.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@ const P3_COMPONENT_FIXTURES_DIR = join(COMPONENT_FIXTURES_DIR, 'p3');
1515

1616
suite('Context (WASI P3)', () => {
1717
test('context.get/set (sync export, sync call)', async () => {
18-
const componentName = 'context-sync';
19-
const componentPath = join(
20-
P3_COMPONENT_FIXTURES_DIR,
21-
componentName,
22-
'component.wasm'
23-
);
18+
const name = 'context-sync';
2419

2520
// NOTE: Despite not specifying the export as async (via jco transpile options in setupAsyncTest),
2621
// the export is async -- since the component lifted the function in an async manner.
2722
//
2823
// This test performs a sync call of an async lifted export.
2924
const { instance, cleanup } = await setupAsyncTest({
3025
component: {
31-
name: componentName,
32-
path: componentPath,
26+
name,
27+
path: join(
28+
P3_COMPONENT_FIXTURES_DIR,
29+
name,
30+
'component.wasm'
31+
),
3332
},
3433
});
3534

@@ -42,19 +41,17 @@ suite('Context (WASI P3)', () => {
4241
await cleanup();
4342
});
4443

45-
// TODO: re-enable after support of async task results
4644
test('context.get/set (async export, async porcelain)', async () => {
47-
const componentName = 'context-async';
48-
const componentPath = join(
49-
P3_COMPONENT_FIXTURES_DIR,
50-
componentName,
51-
'component.wasm'
52-
);
45+
const name = 'context-async';
5346
const { instance, cleanup } = await setupAsyncTest({
5447
asyncMode: 'jspi',
5548
component: {
56-
name: componentName,
57-
path: componentPath,
49+
name,
50+
path: join(
51+
P3_COMPONENT_FIXTURES_DIR,
52+
name,
53+
'component.wasm'
54+
),
5855
},
5956
jco: {
6057
transpile: {
@@ -73,41 +70,49 @@ suite('Context (WASI P3)', () => {
7370
assert.strictEqual(instance.pullContext instanceof AsyncFunction, true);
7471

7572
await instance.pushContext(42);
76-
// NOTE: context is wiped from task to task, and sync call tasks end as soon as they return
73+
// NOTE: context is wiped from task to task
7774
expect(await instance.pullContext()).toEqual(0);
7875

7976
await cleanup();
8077
});
8178

82-
// TODO: re-enable after support of async task results
83-
// TODO: support *Sync() variants of task fns like yield()
84-
test('context.get/set (async export, sync porcelain)', async () => {
85-
const componentName = 'context-async';
86-
const componentPath = join(
87-
P3_COMPONENT_FIXTURES_DIR,
88-
componentName,
89-
'component.wasm'
90-
);
79+
/*
80+
* TODO: enable support of sync lowering an async export that does NOT
81+
* perform any calls to async imports (YIELD = no-op, WAIT / POLL = trap).
82+
*
83+
* In those cases, we must perform the waiting and checking synchronously,
84+
* and not force the function to be a promise.
85+
*
86+
* The check of whether functions require porcelain is the ONLY indicator that should
87+
* be used (rather than whether the function itself is async) as that's how we currently
88+
* express the choice of how we lower an import.
89+
*/
90+
test.skip('context.get/set (async export, sync porcelain)', async () => {
91+
const name = 'context-async';
9192
const { instance, cleanup } = await setupAsyncTest({
9293
component: {
93-
name: componentName,
94-
path: componentPath,
94+
name,
95+
path: join(
96+
P3_COMPONENT_FIXTURES_DIR,
97+
name,
98+
'component.wasm'
99+
),
95100
},
96101
});
97102

98-
// TODO: we need to support sync porcelain on async exports...
99-
// This means doing stuff like yieldSync etc
100-
101103
expect(instance.pushContext).toBeTruthy();
102104
assert.strictEqual(
103105
instance.pushContext instanceof AsyncFunction,
104-
false
106+
false, // see TODO for test
107+
"despite sync lower, async lift forces this function to be async",
105108
);
106109

107110
expect(instance.pullContext).toBeTruthy();
108111
assert.strictEqual(
109112
instance.pullContext instanceof AsyncFunction,
110-
false
113+
false, // see TODO for test
114+
'pullContext should not be an async function',
115+
111116
);
112117

113118
instance.pushContext(42);

0 commit comments

Comments
 (0)