Skip to content

Commit 1d92b78

Browse files
committed
Implement marshaling support between NSDate/Date.
1 parent 94111b9 commit 1d92b78

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

Diff for: runtime/ejs-date.c

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ _ejs_date_unix_now ()
2525
return OBJECT_TO_EJSVAL(rv);
2626
}
2727

28+
ejsval
29+
ejs_date_new (long tstamp) {
30+
EJSDate* rv = _ejs_gc_new (EJSDate);
31+
32+
_ejs_init_object ((EJSObject*)rv, _ejs_Date_prototype, &_ejs_Date_specops);
33+
34+
/* recover timezone first */
35+
gettimeofday (&rv->tv, &rv->tz);
36+
37+
/* convert and assign milliseconds afterwards */
38+
rv->tv.tv_sec = tstamp / 1000;
39+
rv->tv.tv_usec = (tstamp % 1000) * 1000;
40+
rv->valid = EJS_TRUE;
41+
42+
return OBJECT_TO_EJSVAL(rv);
43+
}
44+
2845
double
2946
_ejs_date_get_time (EJSDate *date)
3047
{

Diff for: runtime/ejs-date.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ typedef struct {
1919
struct timezone tz;
2020
} EJSDate;
2121

22-
2322
EJS_BEGIN_DECLS
2423

24+
ejsval ejs_date_new (long tstamp);
25+
2526
extern ejsval _ejs_Date;
2627
extern ejsval _ejs_Date_prototype;
2728
extern EJSSpecOps _ejs_Date_specops;

Diff for: runtime/ejs-jsobjc.h

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
+(CKValue*)nsStringValue:(NSString*)str;
7070
+(CKValue*)utf8StringValue:(const char*)str;
7171

72+
+(CKValue*)nsDateValue:(NSDate*)date;
73+
7274
+(CKValue*)valueWithJSValue:(ejsval)val;
7375

7476
-(ejsval)jsValue;
@@ -148,9 +150,11 @@
148150
-(BOOL)isFunction;
149151
-(BOOL)isConstructor;
150152
-(BOOL)isArray;
153+
-(BOOL)isDate;
151154

152155
-(jsuint)arrayLength;
153156
-(uint16_t)functionArity;;
157+
-(jslong)dateTimestamp;
154158

155159
-(CKObject*)prototype;
156160

Diff for: runtime/ejs-jsobjc.m

+19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "ejs-objc.h"
88
#include "ejs-object.h"
99
#include "ejs-array.h"
10+
#include "ejs-date.h"
1011
#include "ejs-string.h"
1112

1213
#define SPEW(x)
@@ -243,6 +244,13 @@ +(id)utf8StringValue:(const char*)str
243244
return [[[CKValue alloc] initWithJSValue:STRING_TO_EJSVAL([[CKString stringWithUTF8CString:str] jsString])] autorelease];
244245
}
245246

247+
+(id)nsDateValue:(NSDate*)date
248+
{
249+
ejsval jsDate = ejs_date_new (floor(date.timeIntervalSince1970 * 1000.0));
250+
return [[[CKValue alloc] initWithJSValue:jsDate] autorelease];
251+
}
252+
253+
246254
+(id)valueWithJSValue:(ejsval)val
247255
{
248256
return [[[CKValue alloc] initWithJSValue:val] autorelease];
@@ -486,6 +494,11 @@ -(BOOL)isArray
486494
return _obj->ops == &_ejs_Array_specops || _obj->ops == &_ejs_sparsearray_specops;
487495
}
488496

497+
-(BOOL)isDate
498+
{
499+
return _obj->ops == &_ejs_Date_specops;
500+
}
501+
489502
-(jsuint)arrayLength
490503
{
491504
return ((EJSArray*)_obj)->array_length;
@@ -499,6 +512,12 @@ -(uint16_t)functionArity
499512
#endif
500513
}
501514

515+
-(jslong)dateTimestamp
516+
{
517+
EJSDate *date = (EJSDate*)_obj;
518+
return (date->tv.tv_sec * 1000) + (date->tv.tv_usec / 1000);
519+
}
520+
502521
-(CKObject*)prototype
503522
{
504523
ejsval p = _ejs_object_getprop (OBJECT_TO_EJSVAL(_obj), _ejs_atom_prototype);

Diff for: runtime/ejs-objc.m

+14
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ -(NSString*) description
440440
return [CKValue nsStringValue:(NSString*)objc_id];
441441
}
442442

443+
if ([objc_id isKindOfClass:[NSDate class]]) {
444+
return [CKValue nsDateValue:(NSDate*)objc_id];
445+
}
446+
443447
CKObject* ctor_obj = NULL;
444448
Class return_class = object_getClass (objc_id);
445449

@@ -523,6 +527,13 @@ -(NSString*) description
523527
return [NSArray arrayWithArray:nsarray];
524528
}
525529

530+
static NSDate*
531+
marshal_jsdate_as_nsdate (CKObject *o)
532+
{
533+
NSTimeInterval tstamp = [o dateTimestamp] / 1000.0;
534+
return [NSDate dateWithTimeIntervalSince1970:tstamp];
535+
}
536+
526537
static ejsval
527538
invokeSelectorFromJS (ejsval env, ejsval _this, uint32_t argc, ejsval* args)
528539
{
@@ -619,7 +630,10 @@ -(NSString*) description
619630
// let's assume we marshal this as an NSArray for now... XXX
620631
arg_ptr = marshal_jsarray_as_nsarray (o);
621632
}
633+
if ([o isDate])
634+
arg_ptr = marshal_jsdate_as_nsdate (o);
622635
}
636+
623637
SPEW(NSLog (@"arg %d: object marshalling of %@", i, arg_ptr);)
624638
}
625639
else {

Diff for: runtime/ejs-types.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef unsigned long long uint64_t;
2323

2424
typedef int32_t jsint;
2525
typedef uint32_t jsuint;
26+
typedef long jslong;
2627
typedef double jsdouble;
2728

2829
typedef uint16_t jschar;

0 commit comments

Comments
 (0)