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

feat: support i32, i64, f32, f64 param/return types for Host functions #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mhmd-azeez
Copy link
Contributor

@mhmd-azeez mhmd-azeez commented Feb 5, 2025

This PR improves implementation of host function calls by removing the limitation of only having I64 params/return types. Now Host Functions can have params/return types of any of the supported types: I32, I64, F32, F64

Example index.d.ts

declare module 'extism:host' {
  interface user {
    floatInputs(p1: F64, p2: F32): I64;
    floatOutput(p1: I32): F64;
  }
}

2. Call Flow

When JavaScript calls a host function:

  1. User calls floatInputs (from host.ts)
  2. floatInputs calls __invokeHostFunc(index, ...bit_patterns) (from globals.rs)
  3. __invokeHostFunc(index, ...bit_patterns) uses __get_function_arg_type (from shim layer) to know the expected type of each parameter, converts them to i64 numbers and then calls __invokeHostFunc (from the shim layer)
  4. __invokeHostFunc calls __conv_floatInputs (from shim layer)
  5. __conv_floatInputs re-interprets the arguments to the expected types, and does an indirect call to floatInputs (imported from the host)
  6. __conv_floatInputs (from shim layer) takes the return value, re-interprets it as i64
  7. __invokeHostFunc (from shim layer) returns the return value back to __invokeHostFunc (from globals.rs)
  8. __invokeHostFunc (from globals.rs) then uses __get_function_return_type to convert the returned i64 value back to the correct rquickjs type and returns it to floatInputs (from host.ts)

3. Type System

Generated functions include:

// Converters (match import signatures)
__conv_floatInputs(i32, f64, f32) -> i64
__conv_floatOutput(i32, i32) -> i64

// Router (standard 5-param interface)
__invokeHostFunc(i32, i64, i64, i64, i64, i64) -> i64

// Type info
__get_function_return_type(i32) -> i32
__get_function_arg_type(i32, i32) -> i32

4. Generated Host Imports

"extism:host/user" {
  "floatInputs": (f64, f32) -> i64
  "floatOutput": (i32) -> f64
}

@mhmd-azeez mhmd-azeez force-pushed the host-function-params branch from 86e47f8 to db850c0 Compare February 6, 2025 09:21
@mhmd-azeez mhmd-azeez marked this pull request as ready for review February 6, 2025 09:21
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.

1 participant