[bun:ffi] Support double and float

This commit is contained in:
Jarred Sumner
2022-05-02 04:37:24 -07:00
parent 15466865e0
commit d70e92e79f
7 changed files with 65 additions and 65 deletions

View File

@@ -99,6 +99,7 @@ typedef union EncodedJSValue {
#endif
void* asPtr;
double asDouble;
} EncodedJSValue;
EncodedJSValue ValueUndefined = { TagValueUndefined };
@@ -115,6 +116,7 @@ JSContext cachedJSContext;
void* cachedCallbackFunction;
#endif
static EncodedJSValue INT32_TO_JSVALUE(int32_t val) __attribute__((__always_inline__));
static EncodedJSValue DOUBLE_TO_JSVALUE(double val) __attribute__((__always_inline__));
static EncodedJSValue FLOAT_TO_JSVALUE(float val) __attribute__((__always_inline__));
@@ -148,19 +150,16 @@ static EncodedJSValue INT32_TO_JSVALUE(int32_t val) {
return res;
}
static EncodedJSValue DOUBLE_TO_JSVALUE(double val) {
EncodedJSValue res;
#ifdef USES_FLOAT
res.asInt64 = trunc(val) == val ? val : val - DoubleEncodeOffset;
#else
// should never get here
res.asInt64 = 0xa;
#endif
EncodedJSValue res;
res.asDouble = val;
res.asInt64 += DoubleEncodeOffset;
return res;
}
static EncodedJSValue FLOAT_TO_JSVALUE(float val) {
return DOUBLE_TO_JSVALUE(val);
return DOUBLE_TO_JSVALUE((double)val);
}
static EncodedJSValue BOOLEAN_TO_JSVALUE(bool val) {
@@ -171,7 +170,8 @@ static EncodedJSValue BOOLEAN_TO_JSVALUE(bool val) {
static double JSVALUE_TO_DOUBLE(EncodedJSValue val) {
return val.asInt64 + DoubleEncodeOffset;
val.asInt64 -= DoubleEncodeOffset;
return val.asDouble;
}
static float JSVALUE_TO_FLOAT(EncodedJSValue val) {

View File

@@ -1477,7 +1477,7 @@ pub fn nanoseconds(
_: JSC.C.ExceptionRef,
) JSC.C.JSValueRef {
const ns = JSC.VirtualMachine.vm.origin_timer.read();
JSC.JSValue.jsNumberFromUint64(ns).asObjectRef();
return JSC.JSValue.jsNumberFromUint64(ns).asObjectRef();
}
pub fn serve(

View File

@@ -740,7 +740,6 @@ pub const FFI = struct {
}
_ = TCC.tcc_set_output_type(state, TCC.TCC_OUTPUT_MEMORY);
CompilerRT.inject(state);
const compilation_result = TCC.tcc_compile_string(
state,
@@ -763,6 +762,7 @@ pub const FFI = struct {
_ = TCC.tcc_add_symbol(state, "bun_call", JSC.C.JSObjectCallAsFunction);
_ = TCC.tcc_add_symbol(state, "cachedJSContext", js_context);
_ = TCC.tcc_add_symbol(state, "cachedCallbackFunction", js_function);
CompilerRT.inject(state);
var relocation_size = TCC.tcc_relocate(state, null);
if (relocation_size == 0) return;

View File

@@ -34,7 +34,10 @@ using FFIFunction = JSC::EncodedJSValue (*)(JSC::JSGlobalObject* globalObject, J
*
* It was about 20% faster than using the JavaScriptCore C API for functions with 1 argument
*
* Note: there is no wrapper function here
* There is no wrapper function. It does zero bounds checking on the arguments.
* It does not check for exceptions. It does not check for return value.
* It is the caller's responsibility to not buffer overflow the arguments
* For all those reasons, this shouldn't be used directly.
*/
class JSFFIFunction final : public JSC::JSFunction {
public: