@@ -4,7 +4,7 @@ import { type ExtensionInvocation, hooked } from "../tool-runtime.js"
44import type { Interpreter } from "./interpreter.js"
55import { createErrorValue , isErrorType } from "./intrinsics.js"
66import { MAX_VALUE_DEPTH } from "./limits.js"
7- import { PendingThrow , Throw , typeError } from "./model.js"
7+ import { type AstNode , PendingThrow , Throw , typeError } from "./model.js"
88import type { Host } from "./runner.js"
99import { fn } from "./native.js"
1010import {
@@ -32,7 +32,7 @@ import { describeValue, isOpaque } from "./references.js"
3232/**
3333 * The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and
3434 * built-in wrappers are copied, a host function becomes a program function whose calls cross the same way, and a
35- * host Promise becomes a program promise.
35+ * host Promise or Effect becomes a program promise.
3636 */
3737export const extensionGlobals = < R > (
3838 ctx : Interpreter < R > ,
@@ -140,40 +140,57 @@ export const extensionGlobals = <R>(
140140 const wrap = ( value : Function , label : string , describe ?: ( args : ReadonlyArray < unknown > ) => ExtensionInvocation ) =>
141141 fn ( builtins , value . name , value . length , ( _ , values , site ) => {
142142 const args = values . map ( ( item , index ) => toHost ( item , `Argument ${ index + 1 } to ${ label } ` ) )
143+ const call = callHost < R > ( value , args )
143144 const hooks = ctx . tools . hooks
144- const settle = ( run : Effect . Effect < unknown , unknown , R > ) =>
145- ( describe === undefined
146- ? run
147- : hooked ( describe ( args ) , hooks [ "extension.before" ] , hooks [ "extension.after" ] , run )
148- ) . pipe (
149- Effect . mapError ( ( reason ) => new Throw ( fromHost ( reason , label ) ) ) ,
150- Effect . map ( ( settled ) => fromHost ( settled , label ) ) ,
151- )
152- let result : unknown
153- try {
154- result = value . apply ( undefined , args )
155- } catch ( reason ) {
156- return blocking ( ctx . runner . blocking ( settle ( Effect . fail ( reason ) ) ) )
157- }
158- if ( ! ( result instanceof Promise ) ) return blocking ( ctx . runner . blocking ( settle ( Effect . succeed ( result ) ) ) )
159- const runner = ctx . runner
160- const promise = runner . newPromise ( )
161- const resolving = runner . resolving ( promise , site )
162- runner . host (
163- settle ( Effect . tryPromise ( { try : ( ) => result , catch : ( reason ) => reason } ) ) ,
164- ( settled ) => ( settled . ok ? resolving . resolve ( settled . value as Value ) : resolving . reject ( settled . error ) ) ,
165- site ,
145+ const work = (
146+ describe === undefined
147+ ? call . effect
148+ : hooked ( describe ( args ) , hooks [ "extension.before" ] , hooks [ "extension.after" ] , call . effect )
149+ ) . pipe (
150+ Effect . mapBoth ( {
151+ onFailure : ( reason ) => new Throw ( fromHost ( reason , label ) ) ,
152+ onSuccess : ( result ) => fromHost ( result , label ) ,
153+ } ) ,
166154 )
167- return promise
155+ return call . async ? pending ( work , site ) : blocking ( ctx . runner . blocking ( work ) )
168156 } )
169157
158+ // An asynchronous host call: the program holds a promise that settles when the host's work does.
159+ const pending = ( work : Effect . Effect < Value , Throw , R > , site : AstNode | undefined ) => {
160+ const promise = ctx . runner . newPromise ( )
161+ const resolving = ctx . runner . resolving ( promise , site )
162+ ctx . runner . host (
163+ work ,
164+ ( settled ) => ( settled . ok ? resolving . resolve ( settled . value as Value ) : resolving . reject ( settled . error ) ) ,
165+ site ,
166+ )
167+ return promise
168+ }
169+
170170 return extensions . flatMap ( ( extension ) =>
171171 Object . entries ( extension . globals ) . map (
172172 ( [ name , value ] ) => [ name , wrap ( value , name , ( args ) => ( { extension : extension . name , name, args } ) ) ] as const ,
173173 ) ,
174174 )
175175}
176176
177+ /**
178+ * Calls a host function and describes what the program waits on. A returned Effect or Promise settles later; a
179+ * returned value or a throw settles now. An Effect has not started yet, so it runs only once the hooks admit it.
180+ */
181+ const callHost = < R > ( value : Function , args : ReadonlyArray < unknown > ) => {
182+ try {
183+ const result : unknown = value . apply ( undefined , args )
184+ // Extension.Services adds an Effect global's requirements to R.
185+ if ( Effect . isEffect ( result ) ) return { async : true , effect : result as Effect . Effect < unknown , unknown , R > }
186+ if ( result instanceof Promise )
187+ return { async : true , effect : Effect . tryPromise ( { try : ( ) => result , catch : ( reason ) => reason } ) }
188+ return { async : false , effect : Effect . succeed ( result ) }
189+ } catch ( reason ) {
190+ return { async : false , effect : Effect . fail ( reason ) }
191+ }
192+ }
193+
177194// A synchronous host call: its hooks run before any other job, as the call itself did.
178195function * blocking ( host : Host ) {
179196 return ( yield host ) as Value
0 commit comments