Skip to content

Commit

Permalink
fix internal cutpoint name preservation (#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored May 22, 2024
1 parent dfc87e2 commit 45578e8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-carrots-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix internal cutpoint name preservation
20 changes: 9 additions & 11 deletions packages/effect/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,21 +786,19 @@ export const structuralRegion = <A>(body: () => A, tester?: (a: unknown, b: unkn
}

const tracingFunction = (name: string) => {
const internalCall = <A>(body: () => A): A => body()
Object.defineProperty(internalCall, "name", { value: name })
return internalCall
const wrap = {
[name]<A>(body: () => A) {
return body()
}
}
return function<A>(fn: () => A): A {
return wrap[name](fn)
}
}

/**
* @since 3.2.2
* @status experimental
* @category tracing
*/
export const effect_internal_function = tracingFunction("effect_internal_function")

/**
* @since 3.2.2
* @status experimental
* @category tracing
*/
export const effect_internal_generator = tracingFunction("effect_internal_generator")
export const internalCall = tracingFunction("effect_internal_function")
6 changes: 2 additions & 4 deletions packages/effect/src/internal/cause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,12 +1061,10 @@ const prettyErrorStack = (message: string, stack: string, span?: Span | undefine
const lines = stack.split("\n")

for (let i = 1; i < lines.length; i++) {
if (lines[i].includes("effect_internal_function")) {
out.pop()
if (lines[i].includes("Generator.next")) {
break
}
if (lines[i].includes("effect_internal_generator")) {
out.pop()
if (lines[i].includes("effect_internal_function")) {
out.pop()
break
}
Expand Down
8 changes: 4 additions & 4 deletions packages/effect/src/internal/core-effect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { effect_internal_function, effect_internal_generator } from "effect/Utils"
import { internalCall } from "effect/Utils"
import * as Arr from "../Array.js"
import type * as Cause from "../Cause.js"
import * as Chunk from "../Chunk.js"
Expand Down Expand Up @@ -772,15 +772,15 @@ export const gen: typeof Effect.gen = function() {
}
return core.suspend(() => {
const iterator = f(pipe)
const state = effect_internal_generator(() => iterator.next())
const state = internalCall(() => iterator.next())
const run = (
state: IteratorYieldResult<any> | IteratorReturnResult<any>
): Effect.Effect<any, any, any> => {
return (state.done
? core.succeed(state.value)
: core.flatMap(
yieldWrapGet(state.value) as any,
(val: any) => run(effect_internal_generator(() => iterator.next(val)))
(val: any) => run(internalCall(() => iterator.next(val)))
))
}
return run(state)
Expand Down Expand Up @@ -2185,7 +2185,7 @@ export const functionWithSpan = <Args extends Array<any>, Ret extends Effect.Eff
? options.options.apply(null, arguments as any)
: options.options
return withSpan(
core.suspend(() => effect_internal_function(() => options.body.apply(this, arguments as any))),
core.suspend(() => internalCall(() => options.body.apply(this, arguments as any))),
opts.name,
{
...opts,
Expand Down
14 changes: 7 additions & 7 deletions packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { effect_internal_function } from "effect/Utils"
import { internalCall } from "effect/Utils"
import * as Arr from "../Array.js"
import type * as Cause from "../Cause.js"
import * as Chunk from "../Chunk.js"
Expand Down Expand Up @@ -534,9 +534,9 @@ export const async = <A, E = never, R = never>(
let controllerRef: AbortController | void = undefined
if (this.effect_instruction_i0.length !== 1) {
controllerRef = new AbortController()
cancelerRef = effect_internal_function(() => this.effect_instruction_i0(proxyResume, controllerRef!.signal))
cancelerRef = internalCall(() => this.effect_instruction_i0(proxyResume, controllerRef!.signal))
} else {
cancelerRef = effect_internal_function(() => (this.effect_instruction_i0 as any)(proxyResume))
cancelerRef = internalCall(() => (this.effect_instruction_i0 as any)(proxyResume))
}
return (cancelerRef || controllerRef) ?
onInterrupt(effect, (_) => {
Expand Down Expand Up @@ -1007,8 +1007,8 @@ export const interruptibleMask = <A, E, R>(
effect.effect_instruction_i0 = RuntimeFlagsPatch.enable(_runtimeFlags.Interruption)
effect.effect_instruction_i1 = (oldFlags: RuntimeFlags.RuntimeFlags) =>
_runtimeFlags.interruption(oldFlags)
? effect_internal_function(() => this.effect_instruction_i0(interruptible))
: effect_internal_function(() => this.effect_instruction_i0(uninterruptible))
? internalCall(() => this.effect_instruction_i0(interruptible))
: internalCall(() => this.effect_instruction_i0(uninterruptible))
return effect
})

Expand Down Expand Up @@ -1323,8 +1323,8 @@ export const uninterruptibleMask = <A, E, R>(
effect.effect_instruction_i0 = RuntimeFlagsPatch.disable(_runtimeFlags.Interruption)
effect.effect_instruction_i1 = (oldFlags: RuntimeFlags.RuntimeFlags) =>
_runtimeFlags.interruption(oldFlags)
? effect_internal_function(() => this.effect_instruction_i0(interruptible))
: effect_internal_function(() => this.effect_instruction_i0(uninterruptible))
? internalCall(() => this.effect_instruction_i0(interruptible))
: internalCall(() => this.effect_instruction_i0(uninterruptible))
return effect
})

Expand Down
22 changes: 11 additions & 11 deletions packages/effect/src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { effect_internal_function } from "effect/Utils"
import { internalCall } from "effect/Utils"
import * as RA from "../Array.js"
import * as Boolean from "../Boolean.js"
import type * as Cause from "../Cause.js"
Expand Down Expand Up @@ -147,7 +147,7 @@ const contOpSuccess = {
cont: core.OnSuccess,
value: unknown
) => {
return effect_internal_function(() => cont.effect_instruction_i1(value))
return internalCall(() => cont.effect_instruction_i1(value))
},
["OnStep"]: (
_: FiberRuntime<any, any>,
Expand All @@ -161,7 +161,7 @@ const contOpSuccess = {
cont: core.OnSuccessAndFailure,
value: unknown
) => {
return effect_internal_function(() => cont.effect_instruction_i2(value))
return internalCall(() => cont.effect_instruction_i2(value))
},
[OpCodes.OP_REVERT_FLAGS]: (
self: FiberRuntime<any, any>,
Expand All @@ -180,10 +180,10 @@ const contOpSuccess = {
cont: core.While,
value: unknown
) => {
effect_internal_function(() => cont.effect_instruction_i2(value))
if (effect_internal_function(() => cont.effect_instruction_i0())) {
internalCall(() => cont.effect_instruction_i2(value))
if (internalCall(() => cont.effect_instruction_i0())) {
self.pushStack(cont)
return effect_internal_function(() => cont.effect_instruction_i1())
return internalCall(() => cont.effect_instruction_i1())
} else {
return core.void
}
Expand Down Expand Up @@ -1074,7 +1074,7 @@ export class FiberRuntime<in out A, in out E = never> implements Fiber.RuntimeFi
}

[OpCodes.OP_SYNC](op: core.Primitive & { _op: OpCodes.OP_SYNC }) {
const value = effect_internal_function(() => op.effect_instruction_i0())
const value = internalCall(() => op.effect_instruction_i0())
const cont = this.getNextSuccessCont()
if (cont !== undefined) {
if (!(cont._op in contOpSuccess)) {
Expand Down Expand Up @@ -1113,7 +1113,7 @@ export class FiberRuntime<in out A, in out E = never> implements Fiber.RuntimeFi
case OpCodes.OP_ON_FAILURE:
case OpCodes.OP_ON_SUCCESS_AND_FAILURE: {
if (!(_runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted())) {
return effect_internal_function(() => cont.effect_instruction_i1(cause))
return internalCall(() => cont.effect_instruction_i1(cause))
} else {
return core.exitFailCause(internalCause.stripFailures(cause))
}
Expand Down Expand Up @@ -1144,7 +1144,7 @@ export class FiberRuntime<in out A, in out E = never> implements Fiber.RuntimeFi
}

[OpCodes.OP_WITH_RUNTIME](op: core.Primitive & { _op: OpCodes.OP_WITH_RUNTIME }) {
return effect_internal_function(() =>
return internalCall(() =>
op.effect_instruction_i0(
this as FiberRuntime<unknown, unknown>,
FiberStatus.running(this._runtimeFlags) as FiberStatus.Running
Expand Down Expand Up @@ -1210,7 +1210,7 @@ export class FiberRuntime<in out A, in out E = never> implements Fiber.RuntimeFi
// Since we updated the flags, we need to revert them
const revertFlags = _runtimeFlags.diff(newRuntimeFlags, oldRuntimeFlags)
this.pushStack(new core.RevertFlags(revertFlags, op))
return effect_internal_function(() => op.effect_instruction_i1!(oldRuntimeFlags))
return internalCall(() => op.effect_instruction_i1!(oldRuntimeFlags))
} else {
return core.exitVoid
}
Expand Down Expand Up @@ -1262,7 +1262,7 @@ export class FiberRuntime<in out A, in out E = never> implements Fiber.RuntimeFi
}

[OpCodes.OP_COMMIT](op: core.Primitive & { _op: OpCodes.OP_COMMIT }) {
return effect_internal_function(() => op.commit())
return internalCall(() => op.commit())
}

/**
Expand Down
22 changes: 11 additions & 11 deletions packages/effect/src/internal/stm/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { effect_internal_function } from "effect/Utils"
import { internalCall } from "effect/Utils"
import * as Cause from "../../Cause.js"
import * as Context from "../../Context.js"
import * as Effect from "../../Effect.js"
Expand Down Expand Up @@ -505,17 +505,17 @@ export class STMDriver<in out R, out E, out A> {
case "Commit": {
switch (current.effect_instruction_i0) {
case OpCodes.OP_DIE: {
exit = TExit.die(effect_internal_function(() => current.effect_instruction_i1()))
exit = TExit.die(internalCall(() => current.effect_instruction_i1()))
break
}
case OpCodes.OP_FAIL: {
const cont = this.nextFailure()
if (cont === undefined) {
exit = TExit.fail(effect_internal_function(() => current.effect_instruction_i1()))
exit = TExit.fail(internalCall(() => current.effect_instruction_i1()))
} else {
curr = effect_internal_function(() =>
curr = internalCall(() =>
cont.effect_instruction_i2(
effect_internal_function(() => current.effect_instruction_i1())
internalCall(() => current.effect_instruction_i1())
) as Primitive
)
}
Expand All @@ -526,7 +526,7 @@ export class STMDriver<in out R, out E, out A> {
if (cont === undefined) {
exit = TExit.retry
} else {
curr = effect_internal_function(() => cont.effect_instruction_i2() as Primitive)
curr = internalCall(() => cont.effect_instruction_i2() as Primitive)
}
break
}
Expand All @@ -535,7 +535,7 @@ export class STMDriver<in out R, out E, out A> {
break
}
case OpCodes.OP_WITH_STM_RUNTIME: {
curr = effect_internal_function(() =>
curr = internalCall(() =>
current.effect_instruction_i1(this as STMDriver<unknown, unknown, unknown>) as Primitive
)
break
Expand All @@ -549,7 +549,7 @@ export class STMDriver<in out R, out E, out A> {
}
case OpCodes.OP_PROVIDE: {
const env = this.env
this.env = effect_internal_function(() => current.effect_instruction_i2(env))
this.env = internalCall(() => current.effect_instruction_i2(env))
curr = pipe(
current.effect_instruction_i1,
ensuring(sync(() => (this.env = env)))
Expand All @@ -562,17 +562,17 @@ export class STMDriver<in out R, out E, out A> {
if (cont === undefined) {
exit = TExit.succeed(value)
} else {
curr = effect_internal_function(() => cont.effect_instruction_i2(value) as Primitive)
curr = internalCall(() => cont.effect_instruction_i2(value) as Primitive)
}
break
}
case OpCodes.OP_SYNC: {
const value = effect_internal_function(() => current.effect_instruction_i1())
const value = internalCall(() => current.effect_instruction_i1())
const cont = this.nextSuccess()
if (cont === undefined) {
exit = TExit.succeed(value)
} else {
curr = effect_internal_function(() => cont.effect_instruction_i2(value) as Primitive)
curr = internalCall(() => cont.effect_instruction_i2(value) as Primitive)
}
break
}
Expand Down

0 comments on commit 45578e8

Please sign in to comment.