Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Feb 6, 2025
1 parent 579caa0 commit 86e47f8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion crates/cli/src/shims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn generate_wasm_shims(
arg_type_getter.export("__get_function_arg_type");
arg_type_getter.body = arg_type_getter_builder;

// Create converters for each function type
// Create converters for each host function to reinterpret the I64 bit pattern as the expected type
let mut converter_indices = Vec::new();
for (_, (name, _index, params, results)) in import_funcs.iter().enumerate() {
let import_type = module
Expand Down
80 changes: 41 additions & 39 deletions crates/core/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@ fn build_module_object(this: Ctx) -> anyhow::Result<Object> {
Ok(module)
}


#[derive(PartialEq)]
enum TypeCode {
Void = 0,
I32 = 1,
I64 = 2,
F32 = 3,
F64 = 4,
}

fn build_host_object<'js>(this: Ctx<'js>) -> anyhow::Result<Object<'js>> {
let host_input_bytes = Function::new(
this.clone(),
Expand Down Expand Up @@ -226,29 +216,34 @@ fn add_host_functions<'a>(this: Ctx<'a>) -> anyhow::Result<()> {
let globals = this.globals();
let host_object = globals.get::<_, Object>("Host")?;

let host_invoke = Function::new(this.clone(), move |cx: Ctx<'a>, args: Rest<Value<'a>>| -> Result<Value<'a>, rquickjs::Error> {
let func_id = args.first().unwrap().as_int().unwrap() as u32;
let mut params = [0u64; 5];

for i in 1..args.len() {
let arg = args.get(i).unwrap();
params[i-1] = convert_to_u64_bits(arg, func_id, (i-1) as u32);
}

let result = unsafe {
__invokeHostFunc(func_id, params[0], params[1], params[2], params[3], params[4])
};

let return_type = unsafe { __get_function_return_type(func_id) };
Ok(match return_type {
TYPE_VOID => Undefined.into_value(cx.clone()),
TYPE_I32 => Value::new_float(cx, (result & 0xFFFFFFFF) as i32 as f64),
TYPE_I64 => Value::new_float(cx, result as f64),
TYPE_F32 => Value::new_float(cx, f32::from_bits(result as u32) as f64),
TYPE_F64 => Value::new_float(cx, f64::from_bits(result)),
_ => panic!("Unsupported return type: {:?}", return_type)
})
})?;
let host_invoke = Function::new(
this.clone(),
move |cx: Ctx<'a>, args: Rest<Value<'a>>| -> Result<Value<'a>, rquickjs::Error> {
let func_id = args.first().unwrap().as_int().unwrap() as u32;
let mut params = [0u64; 5];

for i in 1..args.len() {
let arg = args.get(i).unwrap();
params[i - 1] = convert_to_u64_bits(arg, func_id, (i - 1) as u32);
}

let result = unsafe {
__invokeHostFunc(
func_id, params[0], params[1], params[2], params[3], params[4],
)
};

let return_type = unsafe { __get_function_return_type(func_id) };
Ok(match return_type {
TYPE_VOID => Undefined.into_value(cx.clone()),
TYPE_I32 => Value::new_float(cx, (result & 0xFFFFFFFF) as i32 as f64),
TYPE_I64 => Value::new_float(cx, result as f64),
TYPE_F32 => Value::new_float(cx, f32::from_bits(result as u32) as f64),
TYPE_F64 => Value::new_float(cx, f64::from_bits(result)),
_ => panic!("Unsupported return type: {:?}", return_type),
})
},
)?;

host_object.set("invokeFunc", host_invoke)?;
Ok(())
Expand Down Expand Up @@ -764,18 +759,25 @@ const TYPE_F64: u32 = 4;
fn convert_to_u64_bits(value: &Value, func_id: u32, arg_idx: u32) -> u64 {
match unsafe { __get_function_arg_type(func_id, arg_idx) } {
TYPE_I32 => value.as_number().unwrap_or_default() as i32 as u64,
TYPE_I64 => value.as_big_int()
TYPE_I64 => value
.as_big_int()
.and_then(|b| b.clone().to_i64().ok())
.or_else(|| value.as_number().map(|n| n as i64))
.unwrap_or_default() as u64,
TYPE_F32 => {
let f = value.as_number().unwrap_or_default() as f32;
f.to_bits() as u64
},
TYPE_F64 => value.as_float()
f.to_bits() as u64
}
TYPE_F64 => value
.as_float()
.or_else(|| value.as_number().map(|n| n as f64))
.unwrap_or_default()
.to_bits(),
_ => panic!("{}, {} unsupported type: {:?}", func_id, arg_idx, value.type_of())
_ => panic!(
"{}, {} unsupported type: {:?}",
func_id,
arg_idx,
value.type_of()
),
}
}
}

0 comments on commit 86e47f8

Please sign in to comment.