Skip to content

Commit

Permalink
Fix napi_get_date_value (#10575)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Apr 27, 2024
1 parent 4cbd215 commit cc8cdf6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
30 changes: 30 additions & 0 deletions src/bun.js/bindings/napi.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@


#include "node_api.h"
#include "root.h"

#include "JavaScriptCore/DateInstance.h"
#include "JavaScriptCore/JSCast.h"
#include "ZigGlobalObject.h"
#include "JavaScriptCore/JSGlobalObject.h"
#include "JavaScriptCore/SourceCode.h"
Expand Down Expand Up @@ -534,6 +538,32 @@ extern "C" napi_status napi_has_property(napi_env env, napi_value object,
scope.clearException();
return napi_ok;
}

extern "C" napi_status napi_get_date_value(napi_env env, napi_value value, double* result)
{
NAPI_PREMABLE

if (UNLIKELY(!env)) {
return napi_invalid_arg;
}

JSValue jsValue = toJS(value);
if (UNLIKELY(!jsValue)) {
return napi_date_expected;
}

auto* date = jsDynamicCast<JSC::DateInstance*>(jsValue);
if (UNLIKELY(!date)) {
return napi_date_expected;
}

if (result) {
*result = date->internalNumber();
}

return napi_ok;
}

extern "C" napi_status napi_get_property(napi_env env, napi_value object,
napi_value key, napi_value* result)
{
Expand Down
12 changes: 1 addition & 11 deletions src/napi/napi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -900,17 +900,7 @@ pub export fn napi_is_date(_: napi_env, value: napi_value, is_date: *bool) napi_
is_date.* = value.jsTypeLoose() == .JSDate;
return .ok;
}
pub export fn napi_get_date_value(env: napi_env, value: napi_value, result: *f64) napi_status {
log("napi_get_date_value", .{});
const getTimeFunction = value.get(env, "getTime") orelse {
return .date_expected;
};

result.* = JSValue.c(
JSC.C.JSObjectCallAsFunction(env.ref(), getTimeFunction.asObjectRef(), value.asObjectRef(), 0, null, TODO_EXCEPTION),
).asNumber();
return .ok;
}
pub extern fn napi_get_date_value(env: napi_env, value: napi_value, result: *f64) napi_status;
pub extern fn napi_add_finalizer(env: napi_env, js_object: napi_value, native_object: ?*anyopaque, finalize_cb: napi_finalize, finalize_hint: ?*anyopaque, result: *Ref) napi_status;
pub export fn napi_create_bigint_int64(env: napi_env, value: i64, result: *napi_value) napi_status {
log("napi_create_bigint_int64", .{});
Expand Down

0 comments on commit cc8cdf6

Please sign in to comment.