Skip to content
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

docs: JS-PDK interface description #16

Merged
merged 8 commits into from
Mar 5, 2024
Merged

docs: JS-PDK interface description #16

merged 8 commits into from
Mar 5, 2024

Conversation

bhelx
Copy link
Contributor

@bhelx bhelx commented Dec 2, 2023

Copy link
Contributor

@chrisdickinson chrisdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgive the all-over-the-place review!

This looks really good; introspecting TypeScript definitions feels like a natural control surface for JS devs to use. Awesome work getting this going!

EIP-009-js-pdk-interface-definition.md Outdated Show resolved Hide resolved
Comment on lines 41 to 53
```typescript
// all extism exports have type () -> i32
interface Exports {
myExport(): I32;
}

// this interface is optional and types are defined by you
// to match your host functions
interface Imports {
myHostFunc1(p: I64, q: I32): I64;
myHostFunc2(p: I64): I64;
}
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```typescript
// all extism exports have type () -> i32
interface Exports {
myExport(): I32;
}
// this interface is optional and types are defined by you
// to match your host functions
interface Imports {
myHostFunc1(p: I64, q: I32): I64;
myHostFunc2(p: I64): I64;
}
```
```typescript
// modules named like WIT-style packages ("extism:host") declare host function imports
declare module 'extism:host' { // namespace
interface user {
export myHostFunction1(p: I32, q: I32): I32;
export myHostFunction2(p: I32): I64;
} // which tees us up to implement "import { user } from 'extism:host'"
}
// The "main" module declares exports
declare module 'main' {
// all extism exports have type () -> i32
export function greet(): I32;
}

local.get 0
(call_indirect (type $int2int) (i32.const 0))
)
(export "__invokeHostFunc" (func $callHostFunc))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(We talked offline a bit about this, but noting it here for posterity) dispatching based on the different host import type signatures might be a little tricky (but not insurmountable!)


It exposes this indirect call with __invokeHostFunc which can be used by the
core quickjs engine module and invoked from javascript.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Another note from offline conversation, saved here for posterity!) There'll be an interesting middle step as we have to partially evaluate the module using Wizer while disabling the host function calls.

(This is probably retreading ground you've already walked, but it'd be interesting to consider whether or not users would want to "stub in" host fn calls made during this partial evaluation – as you noted, Wizer is able to implement stub functions.)

EIP-009-js-pdk-interface-definition.md Outdated Show resolved Hide resolved

The programmer will define their interface in a typescript file `interface.d.ts`:

```typescript
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: the imports and exports: it might be worth gesturing at future work here – noting that we might do basic mapping of higher-level types like string / Record and that we're leaving it at F32, I32, & friends for now.

@bhelx bhelx marked this pull request as ready for review February 2, 2024 19:55
@bhelx
Copy link
Contributor Author

bhelx commented Mar 5, 2024

With Zach's change to support different arities i think this is done extism/js-pdk#48

bhelx and others added 2 commits March 5, 2024 15:13
Co-authored-by: Chris Dickinson <chris@neversaw.us>
Co-authored-by: Chris Dickinson <chris@neversaw.us>
@bhelx bhelx merged commit f585f25 into main Mar 5, 2024
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

Successfully merging this pull request may close these issues.

None yet

2 participants