Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions crates/js-component-bindgen/src/intrinsics/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,17 @@ impl LowerIntrinsic {

ctx.resultPtr = originalPtr + payloadOffset32;

const payloadBytesWritten = lowerFn({{
memory,
realloc,
vals: [val],
storagePtr,
storageLen,
componentIdx,
}});
let payloadBytesWritten = 0;
if (lowerFn) {{
lowerFn({{
memory,
realloc,
vals: [val],
storagePtr,
storageLen,
componentIdx,
}});
}}
let bytesWritten = payloadOffset + payloadBytesWritten;

const rem = ctx.storagePtr % align32;
Expand Down
23 changes: 23 additions & 0 deletions crates/js-component-bindgen/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,9 @@ pub fn render_intrinsics(args: RenderIntrinsicsArgs) -> Source {
if args
.intrinsics
.contains(&Intrinsic::Waitable(WaitableIntrinsic::WaitableSetPoll))
|| args
.intrinsics
.contains(&Intrinsic::Waitable(WaitableIntrinsic::WaitableSetWait))
{
args.intrinsics
.extend([&Intrinsic::Host(HostIntrinsic::StoreEventInComponentMemory)]);
Expand Down Expand Up @@ -1268,6 +1271,22 @@ pub fn render_intrinsics(args: RenderIntrinsicsArgs) -> Source {
]);
}

if args
.intrinsics
.contains(&Intrinsic::Lower(LowerIntrinsic::LowerFlatResult))
{
args.intrinsics
.insert(Intrinsic::Lower(LowerIntrinsic::LowerFlatVariant));
}

if args
.intrinsics
.contains(&Intrinsic::Lower(LowerIntrinsic::LowerFlatOption))
{
args.intrinsics
.insert(Intrinsic::Lower(LowerIntrinsic::LowerFlatVariant));
}

if args
.intrinsics
.contains(&Intrinsic::Lower(LowerIntrinsic::LowerFlatVariant))
Expand Down Expand Up @@ -1364,10 +1383,14 @@ pub fn render_intrinsics(args: RenderIntrinsicsArgs) -> Source {
if args
.intrinsics
.contains(&Intrinsic::AsyncStream(AsyncStreamIntrinsic::StreamWrite))
|| args
.intrinsics
.contains(&Intrinsic::AsyncStream(AsyncStreamIntrinsic::StreamRead))
{
args.intrinsics.extend([
&Intrinsic::GlobalBufferManager,
&Intrinsic::AsyncTask(AsyncTaskIntrinsic::AsyncBlockedConstant),
&Intrinsic::AsyncEventCodeEnum,
]);
}

Expand Down
16 changes: 9 additions & 7 deletions crates/js-component-bindgen/src/intrinsics/p3/async_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ impl AsyncStreamIntrinsic {
r#"
class {host_stream_class_name} {{
#componentIdx;
#streamEndIdx;
#streamEndWaitableIdx;
#streamTableIdx;

#payloadLiftFn;
Expand All @@ -1162,24 +1162,26 @@ impl AsyncStreamIntrinsic {
if (!args.payloadLowerFn) {{ throw new TypeError("missing payload lower fn"); }}
this.#payloadLowerFn = args.payloadLowerFn;

if (args.streamEndIdx === undefined) {{ throw new Error("missing stream idx"); }}
if (args.streamEndWaitableIdx === undefined) {{ throw new Error("missing stream idx"); }}
if (args.streamTableIdx === undefined) {{ throw new Error("missing stream table idx"); }}
this.#streamEndIdx = args.streamEndIdx;
this.#streamEndWaitableIdx = args.streamEndWaitableIdx;
this.#streamTableIdx = args.streamTableIdx;

this.#isUnitStream = args.isUnitStream;
}}

setRep(r) {{ this.#rep = r; }}

createUserStream(args) {{
if (this.#userStream) {{ return this.#userStream; }}
if (this.#rep === null) {{ throw new Error("unexpectedly missing rep for host stream"); }}

const cstate = {get_or_create_async_state_fn}(this.#componentIdx);
if (!cstate) {{ throw new Error(`missing async state for component [${{this.#componentIdx}}]`); }}

const streamEnd = cstate.getStreamEnd({{ tableIdx: this.#streamTableIdx, streamEndIdx: this.#streamEndIdx }});
const streamEnd = cstate.getStreamEnd({{ tableIdx: this.#streamTableIdx, streamEndWaitableIdx: this.#streamEndWaitableIdx }});
if (!streamEnd) {{
throw new Error(`missing stream [${{this.#streamEndIdx}}] (table [${{this.#streamTableIdx}}], component [${{this.#componentIdx}}]`);
throw new Error(`missing stream [${{this.#streamEndWaitableIdx}}] (table [${{this.#streamTableIdx}}], component [${{this.#componentIdx}}]`);
}}

return new {external_stream_class}({{
Expand Down Expand Up @@ -1366,7 +1368,7 @@ impl AsyncStreamIntrinsic {
{debug_log_fn}('[{stream_new_from_lift_fn}()] args', {{ ctx }});
const {{
componentIdx,
streamEndIdx,
streamEndWaitableIdx,
streamTableIdx,
payloadLiftFn,
payloadTypeSize32,
Expand All @@ -1376,7 +1378,7 @@ impl AsyncStreamIntrinsic {

const stream = new {host_stream_class}({{
componentIdx,
streamEndIdx,
streamEndWaitableIdx,
streamTableIdx,
payloadLiftFn: payloadLiftFn,
payloadLowerFn: payloadLowerFn,
Expand Down
5 changes: 3 additions & 2 deletions crates/js-component-bindgen/src/intrinsics/p3/waitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ impl WaitableIntrinsic {
throw Error(`task component [${{task.componentIdx}}] !== executing component [${{componentIdx}}]`);
}}

const event = await task.waitForEvent({{ waitableSetRep, isAsync }});
return {store_event_in_component_memory_fn}(memory, task, event, resultPtr);
const memory = getMemoryFn();
const event = await task.waitUntil({{ waitableSetRep, readyFn: () => true, cancellable: false }});
return {store_event_in_component_memory_fn}({{ memory, ptr: resultPtr, event }});
}}
"#));
}
Expand Down
4 changes: 2 additions & 2 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,12 +1202,12 @@ impl<'a> Instantiator<'a, '_> {
uwriteln!(
self.src.js,
r#"
const trampoline{i} = {waitable_set_wait_fn}.bind(null, {{
const trampoline{i} = new WebAssembly.Suspending({waitable_set_wait_fn}.bind(null, {{
componentIdx: {instance_idx},
isAsync: {async_},
memoryIdx: {memory_idx},
getMemoryFn: () => memory{memory_idx},
}});
}}));
"#,
);
}
Expand Down
Loading