Skip to content

Binding ideas #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zth opened this issue Nov 20, 2023 · 0 comments
Open

Binding ideas #1

zth opened this issue Nov 20, 2023 · 0 comments

Comments

@zth
Copy link
Collaborator

zth commented Nov 20, 2023

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant