Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions runtime/ejs-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ _ejs_date_unix_now ()
return OBJECT_TO_EJSVAL(rv);
}

ejsval
ejs_date_new (long tstamp) {
EJSDate* rv = _ejs_gc_new (EJSDate);

_ejs_init_object ((EJSObject*)rv, _ejs_Date_prototype, &_ejs_Date_specops);

/* recover timezone first */
gettimeofday (&rv->tv, &rv->tz);

/* convert and assign milliseconds afterwards */
rv->tv.tv_sec = tstamp / 1000;
rv->tv.tv_usec = (tstamp % 1000) * 1000;
rv->valid = EJS_TRUE;

return OBJECT_TO_EJSVAL(rv);
}

double
_ejs_date_get_time (EJSDate *date)
{
Expand Down
3 changes: 2 additions & 1 deletion runtime/ejs-date.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ typedef struct {
struct timezone tz;
} EJSDate;


EJS_BEGIN_DECLS

ejsval ejs_date_new (long tstamp);

extern ejsval _ejs_Date;
extern ejsval _ejs_Date_prototype;
extern EJSSpecOps _ejs_Date_specops;
Expand Down
4 changes: 4 additions & 0 deletions runtime/ejs-jsobjc.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
+(CKValue*)nsStringValue:(NSString*)str;
+(CKValue*)utf8StringValue:(const char*)str;

+(CKValue*)nsDateValue:(NSDate*)date;

+(CKValue*)valueWithJSValue:(ejsval)val;

-(ejsval)jsValue;
Expand Down Expand Up @@ -148,9 +150,11 @@
-(BOOL)isFunction;
-(BOOL)isConstructor;
-(BOOL)isArray;
-(BOOL)isDate;

-(jsuint)arrayLength;
-(uint16_t)functionArity;;
-(jslong)dateTimestamp;

-(CKObject*)prototype;

Expand Down
19 changes: 19 additions & 0 deletions runtime/ejs-jsobjc.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ejs-objc.h"
#include "ejs-object.h"
#include "ejs-array.h"
#include "ejs-date.h"
#include "ejs-string.h"

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

+(id)nsDateValue:(NSDate*)date
{
ejsval jsDate = ejs_date_new (floor(date.timeIntervalSince1970 * 1000.0));
return [[[CKValue alloc] initWithJSValue:jsDate] autorelease];
}


+(id)valueWithJSValue:(ejsval)val
{
return [[[CKValue alloc] initWithJSValue:val] autorelease];
Expand Down Expand Up @@ -486,6 +494,11 @@ -(BOOL)isArray
return _obj->ops == &_ejs_Array_specops || _obj->ops == &_ejs_sparsearray_specops;
}

-(BOOL)isDate
{
return _obj->ops == &_ejs_Date_specops;
}

-(jsuint)arrayLength
{
return ((EJSArray*)_obj)->array_length;
Expand All @@ -499,6 +512,12 @@ -(uint16_t)functionArity
#endif
}

-(jslong)dateTimestamp
{
EJSDate *date = (EJSDate*)_obj;
return (date->tv.tv_sec * 1000) + (date->tv.tv_usec / 1000);
}

-(CKObject*)prototype
{
ejsval p = _ejs_object_getprop (OBJECT_TO_EJSVAL(_obj), _ejs_atom_prototype);
Expand Down
14 changes: 14 additions & 0 deletions runtime/ejs-objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ -(NSString*) description
return [CKValue nsStringValue:(NSString*)objc_id];
}

if ([objc_id isKindOfClass:[NSDate class]]) {
return [CKValue nsDateValue:(NSDate*)objc_id];
}

CKObject* ctor_obj = NULL;
Class return_class = object_getClass (objc_id);

Expand Down Expand Up @@ -523,6 +527,13 @@ -(NSString*) description
return [NSArray arrayWithArray:nsarray];
}

static NSDate*
marshal_jsdate_as_nsdate (CKObject *o)
{
NSTimeInterval tstamp = [o dateTimestamp] / 1000.0;
return [NSDate dateWithTimeIntervalSince1970:tstamp];
}

static ejsval
invokeSelectorFromJS (ejsval env, ejsval _this, uint32_t argc, ejsval* args)
{
Expand Down Expand Up @@ -619,7 +630,10 @@ -(NSString*) description
// let's assume we marshal this as an NSArray for now... XXX
arg_ptr = marshal_jsarray_as_nsarray (o);
}
if ([o isDate])
arg_ptr = marshal_jsdate_as_nsdate (o);
}

SPEW(NSLog (@"arg %d: object marshalling of %@", i, arg_ptr);)
}
else {
Expand Down
1 change: 1 addition & 0 deletions runtime/ejs-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef unsigned long long uint64_t;

typedef int32_t jsint;
typedef uint32_t jsuint;
typedef long jslong;
typedef double jsdouble;

typedef uint16_t jschar;
Expand Down