1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.

use crate::Context;
use crate::Date;
use crate::HandleScope;
use crate::Local;

extern "C" {
  fn v8__Date__New(context: *const Context, value: f64) -> *const Date;
  fn v8__Date__ValueOf(this: *const Date) -> f64;
}

/// An instance of the built-in Date constructor (ECMA-262, 15.9).
impl Date {
  pub fn new<'s>(
    scope: &mut HandleScope<'s>,
    value: f64,
  ) -> Option<Local<'s, Date>> {
    unsafe {
      scope.cast_local(|sd| v8__Date__New(sd.get_current_context(), value))
    }
  }

  /// A specialization of Value::NumberValue that is more efficient
  /// because we know the structure of this object.
  pub fn value_of(&self) -> f64 {
    unsafe { v8__Date__ValueOf(self) }
  }
}