DelSkayn / rquickjs

High level bindings to the quickjs javascript engine
MIT License
435 stars 59 forks source link

type_of() returns Constructor for all non-arrow functions. #192

Closed richarddd closed 11 months ago

richarddd commented 11 months ago

When passing a named or anon function they are treated like Constructors and behave different from JS.

const reference = function(){
}

function namedFunction(){
}

print(typeof reference)
print(typeof namedFunction)
printTypeofRust(reference)
printTypeofRust(namedFunction)
printTypeofRust(() => {})
globals.set("print", Func::from(|value:String| {
    println!("{}", value);
}))?;
globals.set("printTypeofRust", Func::from(|value: Value| {
    let type_of = value.type_of();
    println!("{:?}", type_of);
}))?;

Output:

function
function
Constructor
Constructor
Function
DelSkayn commented 11 months ago

This is the intended behavior, all those functions are constructors as they can be used as constructors (i.e. you can call them with new). Constructor is a sub type of Function.

I didn't really intend to have the type_of function to be the same as the JavaScript typeof operator. The type has a bunch more variants then what the typeof operator can return, like Float, Int, Array, Exception and Constructor. Especially Float and Int are a integral part of rquickjs since QuickJS values store JavaScript numbers in either a Float or an Int.