11import { Effect } from "effect"
2- import type { Extension } from "../extension.js"
2+ import { type Extension , isAsync } from "../extension.js"
33import { 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"
77import { type AstNode , PendingThrow , Throw , typeError } from "./model.js"
8- import type { Host } from "./runner.js"
98import { fn } from "./native.js"
109import {
1110 define ,
@@ -134,25 +133,32 @@ export const extensionGlobals = <R>(
134133 throw typeError ( `${ label } produced ${ describeHost ( value ) } , which the program cannot hold.` )
135134 }
136135
137- // A host function as a program function. Arguments cross in; a global's call runs inside the host's extension
138- // hooks, which see the host's own error on failure; then whatever came back, or was thrown, crosses out so the
139- // program catches a copy. Functions inside results are part of a value's API and skip the hooks.
136+ // A host function as a program function. Arguments cross in; for a global, the `extension.before` hook runs before
137+ // the host function is called, so a call it denies never reaches the host. The host's result, or what it threw,
138+ // crosses out so the program catches a copy, and `extension.after` sees how the call ended. Functions inside
139+ // results are part of a value's API and skip the hooks.
140140 const wrap = ( value : Function , label : string , describe ?: ( args : ReadonlyArray < unknown > ) => ExtensionInvocation ) =>
141- fn ( builtins , value . name , value . length , ( _ , values , site ) => {
141+ fn ( builtins , value . name , value . length , function * ( _ , values , site ) {
142142 const args = values . map ( ( item , index ) => toHost ( item , `Argument ${ index + 1 } to ${ label } ` ) )
143- const call = callHost < R > ( value , args )
143+ const cross = ( reason : unknown ) => new Throw ( fromHost ( reason , label ) )
144+ const call = describe ?.( args )
144145 const hooks = ctx . tools . hooks
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- } ) ,
154- )
155- return call . async ? pending ( work , site ) : blocking ( ctx . runner . blocking ( work ) )
146+ const before = call && hooks [ "extension.before" ] ?.( call ) . pipe ( Effect . mapError ( cross ) )
147+ const after = call && hooks [ "extension.after" ]
148+ const settle = ( effect : Effect . Effect < unknown , unknown , R > ) =>
149+ ( after ? hooked ( call , undefined , after , effect ) : effect ) . pipe (
150+ Effect . mapBoth ( { onFailure : cross , onSuccess : ( result ) => fromHost ( result , label ) } ) ,
151+ )
152+ // A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
153+ if ( isAsync ( value ) )
154+ return pending (
155+ Effect . andThen ( before ?? Effect . void , settle ( Effect . suspend ( ( ) => callHost < R > ( value , args ) . effect ) ) ) ,
156+ site ,
157+ )
158+ if ( before ) yield ctx . runner . blocking ( before )
159+ const host = callHost < R > ( value , args )
160+ if ( host . async ) return pending ( settle ( host . effect ) , site )
161+ return ( yield ctx . runner . blocking ( settle ( host . effect ) ) ) as Value
156162 } )
157163
158164 // An asynchronous host call: the program holds a promise that settles when the host's work does.
@@ -176,7 +182,7 @@ export const extensionGlobals = <R>(
176182
177183/**
178184 * 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.
185+ * returned value or a throw settles now.
180186 */
181187const callHost = < R > ( value : Function , args : ReadonlyArray < unknown > ) => {
182188 try {
@@ -191,11 +197,6 @@ const callHost = <R>(value: Function, args: ReadonlyArray<unknown>) => {
191197 }
192198}
193199
194- // A synchronous host call: its hooks run before any other job, as the call itself did.
195- function * blocking ( host : Host ) {
196- return ( yield host ) as Value
197- }
198-
199200/**
200201 * An error crosses as its name, message, `cause`, and own enumerable fields, such as Node's `code`, `errno`,
201202 * `syscall`, and `path`. `stack` stays on its own side, and no field may shadow an Error method. A field that cannot
0 commit comments