Skip to content

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
Copy link
Contributor

@zshipko zshipko left a comment

Choose a reason for hiding this comment

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

This looks great! It's nice to get rid of all those __invokeHost functions

Copy link
Contributor

@bhelx bhelx left a comment

Choose a reason for hiding this comment

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

A bit tricky to mentally parse but looks correct and the tests look good

@mhmd-azeez mhmd-azeez merged commit 71edbd2 into main Feb 27, 2025
6 checks passed
@mhmd-azeez mhmd-azeez deleted the host-function-params branch February 27, 2025 06:01
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.

3 participants