You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Question: How to handle Date object in native C? #4058
Hi, I'm having hard time to figure out how to get the Date.year, month, date in the native C code. Also having hard time to figure out how to return a Date object from native to javascript. Thanks
staticjerry_value_tfunction_native_driver_gui_calendar_setTodayDate(constjerry_value_tfunc_value, /**< function object */constjerry_value_tthis_value, /**< this arg */constjerry_value_targs[], /**< function arguments */constjerry_length_targs_cnt) /**< number of function arguments */
{
//How to get the year, month, date of a Date object?//The following code returns all zeroprintf("Year:%d Month:%d Day:%d\n", ecma_date_year_from_time(args[0]), ecma_date_month_from_time(args[0]), ecma_date_date_from_time(args[0]));
//How to return "new Date(2020, 0,0)" value to the javascript?returnjerry_create_date();
}
Unfortunately, currently we do not have any API functions to handle date objects.
Also the reason is why the the called ecma level functions are not working, it that these methods expect an ecma_number_t instead of a jerry_value_t. If you unpack it into a double variable with jerry_get_number_from_value(args[0]) you can use these internal methods, with the warning of the usage of non-api functions is not the best practice.
Moreover I can suggest you two better solutions: A:
ecma_date_year_from_time(args[0]) is almost equivalent to perform Date.prototype.getYear.call(args[0])
.call() can be performed with jerry_invoke_function with args[0] as this argument
getYear is the property of Date.prototype
Date.prototype is the property of Date
Date is the property of the global object.
So putting the pieces together, use jerry_get_property (global, "Date"), then jerry_get_property(Date, "prototype"), then jerry_get_property(Date.prototype, "getYear") and finally jerry_invoke_function (Date.prototype.getYear, args[0], NULL, 0)
B:
If you have free-time/capacity it'd be great to publish these internal method to the API. I think we should have one single universal function for retrieving information form a date object. So my proposal would look like this:
typedefenum
{
JERRY_DATE_OPERATION_GET_YEAR,
JERRY_DATE_OPERATION_GET_MONTH,
// ... and so on
} jerry_date_operation_t;
jerry_value_tjerry_date_get (jerry_date_operation_toperation, jerry_value_tvalue)
{
// step 1: validate date object// step 2: get the internal dateecma_number_tdate_num=get_the_stored_internal_date_value() // see ecma_builtin_date_prototype_dispatch_routineecma_number_tresult;
// step 3: choose the operationswitch (operation) {
caseJERRY_DATE_OPERATION_GET_YEAR: {
result=ecma_date_year_from_time (date_num);
break;
}
caseJERRY_DATE_OPERATION_GET_MONTH: {
result=ecma_date_month_from_time (date_num);
break;
}
// ... and so on
}
returnecma_make_number_value (result);
}
Both options are suitable for resolve your problem, but IMHO the option B would be more future-proof also much efficient as well.
Activity
rerobika commentedon Jul 27, 2020
Hello @weixiongmei !
Unfortunately, currently we do not have any API functions to handle date objects.
Also the reason is why the the called
ecmalevel functions are not working, it that these methods expect anecma_number_tinstead of ajerry_value_t. If you unpack it into a double variable withjerry_get_number_from_value(args[0])you can use these internal methods, with the warning of the usage of non-api functions is not the best practice.Moreover I can suggest you two better solutions:
A:
Date.prototype.getYear.call(args[0]).call()can be performed withjerry_invoke_functionwithargs[0]asthisargumentgetYearis the property ofDate.prototypeDate.prototypeis the property ofDateDateis the property of theglobal object.jerry_get_property (global, "Date"), thenjerry_get_property(Date, "prototype"), thenjerry_get_property(Date.prototype, "getYear")and finallyjerry_invoke_function (Date.prototype.getYear, args[0], NULL, 0)B:
Both options are suitable for resolve your problem, but IMHO the option B would be more future-proof also much efficient as well.
weixiongmei commentedon Jul 29, 2020
@rerobika Thank you so much~~