Skip to content

Binding ideas #1

@zth

Description

@zth

Exploring/throwing ideas around for bindings/FFI. Here are some quick examples building on rescript-lang/syntax#217 (comment):

Path

Comes from the module path which you'd import.

https://github.com/oven-sh/bun/blob/main/packages/bun-types/path.d.ts

// From "path" package
declare module "path" {
  export function normalize(p: string): string;
  export function join(...paths: string[]): string;
  export function resolve(...pathSegments: string[]): string;
  export function isAbsolute(p: string): boolean;
  export var delimiter: string;
}
external.import "path" {
   normalize: string => string,
   join: @variadic string => string,
   resolve: @variadic string => string,
   isAbsolute: string => bool,
   delimiter: string
} 

FileSink

A globally available type/class.

https://github.com/oven-sh/bun/blob/main/packages/bun-types/bun.d.ts#L592C3-L600C15

export interface FileSink {
    write(
      chunk: string | ArrayBuffer,
    ): number;
}
external.global "FileSink" {
    write as writeString: string => float,
    write as writeArrayBuffer: ArrayBuffer.t => float
}

FormData

Global, which you can instantiate, and where we're doing "t-first" bindings.

https://github.com/oven-sh/bun/blob/main/packages/bun-types/globals.d.ts#L884-L917C1

interface FormData {
  append(name: string, value: string | Blob, fileName?: string): void;
}

declare var FormData: {
  prototype: FormData;
  new (): FormData;
};
external.global "FormData" {
    append: (@self, string, ~fileName: string=?) => unit,
    append as appendBlob: (@self, Blob.t, ~fileName: string=?) => unit,
    @new make: unit => @self
}

Questions and thoughts

  • What about defining types etc? Should that be allowed inside of the external block. If not, it'll be hard to co-locate type defs and their bindings, and/or we'd potentially end up with multiple "external" blocks for each thing, which might/might not work. If yes, it'll probably complicate parsing etc a bit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions