Oolite
Loading...
Searching...
No Matches
OOJSClock.m
Go to the documentation of this file.
1/*
2
3OOJSClock.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSClock.h"
27#import "OODebugStandards.h"
28#import "OOJSPlayer.h"
30#import "OOStringParsing.h"
31#import "PlayerEntity.h"
33#import "Universe.h"
34
36
37// Methods
38static JSValue JSClockToString(OOJS_NATIVE_ARGS);
40static JSValue ClockAddSeconds(OOJS_NATIVE_ARGS);
41
42static JSClassID sClockClassID;
43static JSClassDef sClockClass = {
44 .class_name = "Clock",
45};
46
47enum {
48 // Property IDs
49 kClock_absoluteSeconds, // game real time clock, double, read-only
50 kClock_seconds, // game clock time, double, read-only
51 kClock_minutes, // game clock time minutes (rounded down), integer double, read-only
52 kClock_hours, // game clock time hours (rounded down), integer double, read-only
53 kClock_days, // game clock time days (rounded down), int, read-only
54 kClock_secondsComponent, // second component of game clock time, double, read-only
55 kClock_minutesComponent, // minute component of game clock time (rounded down), int, read-only
56 kClock_hoursComponent, // hour component of game clock time (rounded down), int, read-only
57 kClock_daysComponent, // day component of game clock time (rounded down), int, read-only
58 kClock_clockString, // game clock time as display string, string, read-only
59 kClock_isAdjusting, // clock is adjusting, boolean, read-only
60 kClock_adjustedSeconds, // game clock time, including adjustments, double, read-only
61 kClock_legacy_scriptTimer // legacy scriptTimer_number, double, read-only
62};
63
80
81static const JSCFunctionListEntry sClockMethods[] = {
82 // JS name Function min args
83 JS_CFUNC_DEF("toString", 0, JSClockToString),
84 JS_CFUNC_DEF("clockStringForTime", 1, ClockClockStringForTime),
85 JS_CFUNC_DEF("addSeconds", 1, ClockAddSeconds),
86};
87
88void InitOOJSClock(JSContext* context, JSValueConst global)
89{
91 JSValueConst clockPrototype = OOJSInitClass(context, global, sClockClassID, JS_UNDEFINED, OOJSUnconstructableConstruct, 0,
93 JS_DefinePropertyValueStr(context, global, "clock", JS_NewObjectProtoClass(context, clockPrototype, sClockClassID), OOJS_PROP_READONLY);
94}
95
97{
98 OOJS_NATIVE_ENTER(context)
99
101 double clockTime;
102
103 clockTime = [player clockTime];
104
105 /* INT_TO_JSVAL() truncated its (double) argument to an integer; JS_NewInt32()
106 does the same through the implicit conversion.
107 */
108 switch (magic) {
110 return JS_NewNumber(context, [UNIVERSE getTime]);
111
112 case kClock_seconds:
113 return JS_NewNumber(context, clockTime);
114
115 case kClock_minutes:
116 return JS_NewNumber(context, floor(clockTime / 60.0));
117
118 case kClock_hours:
119 return JS_NewNumber(context, floor(clockTime / 3600.0));
120
122 return JS_NewInt32(context, fmod(clockTime, 60.0));
123
125 return JS_NewInt32(context, fmod(floor(clockTime / 60.0), 60.0));
126
128 return JS_NewInt32(context, fmod(floor(clockTime / 3600.0), 24.0));
129
130 case kClock_days:
132 return JS_NewInt32(context, floor(clockTime / 86400.0));
133
135 return OOJSValueFromNativeObject(context, [player dial_clock]);
136
138 return OOJSValueFromBOOL([player clockAdjusting]);
139
141 return JS_NewNumber(context, [player clockTimeAdjusted]);
142
144 OOStandardsDeprecated(@"The legacy_scriptTimer property is deprecated");
145 return JS_NewNumber(context, [player scriptTimer]);
146
147 default:
149 return JS_EXCEPTION;
150 }
151
153}
154
155// *** Methods ***
156
157// toString() : String
159{
160 OOJS_NATIVE_ENTER(context)
161
163
165}
166
167// clockStringForTime(time : Number) : String
169{
170 OOJS_NATIVE_ENTER(context)
171
172 double time;
173
174 if (EXPECT_NOT(argc < 1 || JS_ToFloat64(context, &time, OOJS_ARGV[0]) < 0)) {
175 if (JS_HasException(context))
176 JS_FreeValue(context, JS_GetException(context));
177 JSValueConst arg = JS_UNDEFINED;
178 if (argc > 0)
179 arg = OOJS_ARGV[0];
180 OOJSReportBadArguments(context, @"Clock", @"clockStringForTime", 1, &arg, nil, @"number");
181 return JS_EXCEPTION;
182 }
183
185
187}
188
189// addSeconds(seconds : Number) : String
191{
192 OOJS_NATIVE_ENTER(context)
193
194 double time;
195 const double kMaxTime = 30.0 * 24.0 * 3600.0; // 30 days
196
197 if (EXPECT_NOT(argc < 1 || JS_ToFloat64(context, &time, OOJS_ARGV[0]) < 0)) {
198 if (JS_HasException(context))
199 JS_FreeValue(context, JS_GetException(context));
200 JSValueConst arg = JS_UNDEFINED;
201 if (argc > 0)
202 arg = OOJS_ARGV[0];
203 OOJSReportBadArguments(context, @"Clock", @"addSeconds", 1, &arg, nil, @"number");
204 return JS_EXCEPTION;
205 }
206
207 if (time > kMaxTime || time < 1.0 || !isfinite(time)) {
209 }
210
212
213 OOJS_RETURN_BOOL(YES);
214
216}
void OOStandardsDeprecated(NSString *message)
#define EXPECT_NOT(x)
@ kClock_absoluteSeconds
Definition OOJSClock.m:59
@ kClock_isAdjusting
Definition OOJSClock.m:69
@ kClock_daysComponent
Definition OOJSClock.m:67
@ kClock_adjustedSeconds
Definition OOJSClock.m:70
@ kClock_days
Definition OOJSClock.m:63
@ kClock_seconds
Definition OOJSClock.m:60
@ kClock_clockString
Definition OOJSClock.m:68
@ kClock_hours
Definition OOJSClock.m:62
@ kClock_secondsComponent
Definition OOJSClock.m:64
@ kClock_hoursComponent
Definition OOJSClock.m:66
@ kClock_minutes
Definition OOJSClock.m:61
@ kClock_minutesComponent
Definition OOJSClock.m:65
@ kClock_legacy_scriptTimer
Definition OOJSClock.m:71
static JSBool ClockClockStringForTime(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:185
static JSBool ClockAddSeconds(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:205
static JSClass sClockClass
Definition OOJSClock.m:42
static JSPropertySpec sClockProperties[]
Definition OOJSClock.m:74
void InitOOJSClock(JSContext *context, JSObject *global)
Definition OOJSClock.m:100
static JSBool ClockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSClock.m:106
static JSFunctionSpec sClockMethods[]
Definition OOJSClock.m:92
static JSBool JSClockToString(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:175
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
#define OOJS_ARGV
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
return nil
NSString * ClockToString(double clock, BOOL adjusting)
static JSClassID sClockClassID
Definition OOJSClock.m:42
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
#define OOJS_PROPERTY_RO(name, getter, magic)
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_GETTER_ARGS
#define OOJS_NATIVE_ARGS
#define OOJS_COUNT(array)
#define UNIVERSE
Definition Universe.h:827
void addToAdjustTime:(double seconds)