aboutsummaryrefslogtreecommitdiff
path: root/examples/ffi.ts
blob: a09aeefd2b68cb192f6dafc8421194a9c6518c2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Bind, Effect, Kind1 } from '../stdlib/effect'
import { DefineEffect, JsExpr } from '../stdlib/sys'
import { Debug } from '../stdlib/stdio'

interface PrintK<Label extends string = ''> extends Kind1 {
  return: Debug<Label, this['input']>
}

// Declare custom effect
interface CustomThingy<_A, _B extends number> extends Effect {}

// Alias for squaring a number
type Square<N extends number> = JsExpr<`${N} ** 2`>

export type main = [
  // FFI via js expression
  Bind<JsExpr<'{ boobaa: [5 * 3, 5 * 2] }'>, PrintK<'|'>>,
  Bind<Square<69>, PrintK<'69^2 ='>>,

  // FFI via custom effect
  DefineEffect<
    'CustomThingy',
    `([a, b], ctx) => {
      console.log(ctx.typeToString(a))
      return { result: 5 * ctx.getTypeValue(b) }
    }`
  >,
  Bind<CustomThingy<'a', 69>, PrintK<'69 * 5 ='>>
]