Skip to content

Commit d36911a

Browse files
committed
Implement Date.now() static method
- Implement Date.now() static method returning milliseconds since Unix epoch - Extract Date static method handling into handle_date_static_method in js_date.rs - Update evaluate_call and evaluate_optional_call to dispatch Date static methods
1 parent 4c3f374 commit d36911a

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/plan.md
2+
/brief.md
3+
/brief_zh.md
14
/*.js
25
/quickjs
36
.vscode/

src/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,6 +3422,7 @@ fn evaluate_call(env: &JSObjectDataPtr, func_expr: &Expr, args: &[Expr]) -> Resu
34223422
"Object" => crate::js_object::handle_object_method(method, args, env),
34233423
"Array" => crate::js_array::handle_array_static_method(method, args, env),
34243424
"Promise" => crate::js_promise::handle_promise_static_method(method, args, env),
3425+
"Date" => crate::js_date::handle_date_static_method(method, args, env),
34253426
_ => Err(JSError::EvaluationError {
34263427
message: format!("{} has no static method '{}'", func_name, method),
34273428
}),
@@ -3576,6 +3577,7 @@ fn evaluate_optional_call(env: &JSObjectDataPtr, func_expr: &Expr, args: &[Expr]
35763577
match func_name.as_str() {
35773578
"Object" => crate::js_object::handle_object_method(method_name, args, env),
35783579
"Array" => crate::js_array::handle_array_static_method(method_name, args, env),
3580+
"Date" => crate::js_date::handle_date_static_method(method_name, args, env),
35793581
_ => Err(JSError::EvaluationError {
35803582
message: format!("{} has no static method '{}'", func_name, method_name),
35813583
}),

src/js_date.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,22 @@ pub(crate) fn handle_date_method(obj_map: &JSObjectDataPtr, method: &str, args:
399399
}),
400400
}
401401
}
402+
403+
/// Handle Date static method calls
404+
pub(crate) fn handle_date_static_method(method: &str, args: &[Expr], _env: &JSObjectDataPtr) -> Result<Value, JSError> {
405+
match method {
406+
"now" => {
407+
if !args.is_empty() {
408+
return Err(JSError::TypeError {
409+
message: "Date.now() takes no arguments".to_string(),
410+
});
411+
}
412+
use std::time::{SystemTime, UNIX_EPOCH};
413+
let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
414+
Ok(Value::Number(duration.as_millis() as f64))
415+
}
416+
_ => Err(JSError::EvaluationError {
417+
message: format!("Date has no static method '{}'", method),
418+
}),
419+
}
420+
}

0 commit comments

Comments
 (0)