Oolite
Loading...
Searching...
No Matches
OOJavaScriptEnginePoc.m File Reference
import "OOJavaScriptEngine.h"
import "OOJSQuaternion.h"
import "OOJSScript.h"
import "OOJSVector.h"
#include <time.h>
Include dependency graph for OOJavaScriptEnginePoc.m:

Go to the source code of this file.

Macros

#define OOJS_RUNTIME_SIZE_MiB   256
#define OOJS_TIME_LIMIT   (1)

Functions

static int InterruptHandler (OOJSInterruptHandlerArg *arg, void *opaque)
static JSValue GlobalLog (OOJS_NATIVE_ARGS)
JSAtom OOJSIDFromString (NSString *string)
JSValue OOJSNativeFailure (JSContext *context)
NSString * OOJSWrapScriptSource (NSString *source)
static double MonotonicTime (void)
void OOJSStartTimeLimiterWithTimeLimit (OOTimeDelta limit)
void OOJSStopTimeLimiter (void)
void OOJSPauseTimeLimiter (void)
void OOJSResumeTimeLimiter (void)
static BOOL GetLocationFromStack (NSString *stack, NSString **outFile, unsigned *outLine)
static NSString * StackOfValue (JSContext *context, JSValueConst value)
static void LogProblem (NSString *severity, NSString *errorName, NSString *message, NSString *stack)
void OOJSReportPendingException (JSContext *context)
static NSString * CallerPrefix (NSString *scriptClass, NSString *function)
void OOJSReportError (JSContext *context, NSString *format,...)
void OOJSReportErrorForCaller (JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
void OOJSReportErrorWithArguments (JSContext *context, NSString *format, va_list args)
void OOJSReportWrappedException (JSContext *context, id exception)
void OOJSReportWarning (JSContext *context, NSString *format,...)
void OOJSReportWarningForCaller (JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
void OOJSReportWarningWithArguments (JSContext *context, NSString *format, va_list args)
static NSString * PropertyNameForMagic (int magic, const JSCFunctionListEntry *list, size_t count)
void OOJSReportBadPropertySelector (JSContext *context, JSValueConst thisObj, int magic, const JSCFunctionListEntry *list, size_t count)
void OOJSReportBadPropertyValue (JSContext *context, JSValueConst thisObj, int magic, const JSCFunctionListEntry *list, size_t count, JSValueConst value)
void OOJSReportBadArguments (JSContext *context, NSString *scriptClass, NSString *function, int argc, JSValueConst *argv, NSString *message, NSString *expectedArgsDescription)
BOOL OOJSArgumentListGetNumber (JSContext *context, NSString *scriptClass, NSString *function, int argc, JSValueConst *argv, double *outNumber, int *outConsumed)
BOOL OOJSArgumentListGetNumberNoError (JSContext *context, int argc, JSValueConst *argv, double *outNumber, int *outConsumed)
NSString * OOStringFromJSValueEvenIfNull (JSContext *context, JSValueConst value)
NSString * OOStringFromJSValue (JSContext *context, JSValueConst value)
NSString * OOJSClassNameOfValue (JSContext *context, JSValueConst value)
static NSString * DescribeValue (JSContext *context, JSValueConst value, BOOL abbreviateObjects, BOOL recursing)
NSString * OOJSDescribeValue (JSContext *context, JSValueConst value, BOOL abbreviateObjects)

Variables

static OOJavaScriptEngine * sSharedEngine = nil
JSContext * gOOJSMainThreadContext = NULL
static const JSCFunctionListEntry sGlobalMethods []
static unsigned sLimiterStartDepth
static int sLimiterPauseDepth
static double sLimiterStart
static double sLimiterPauseStart
static double sLimiterTimeLimit

Macro Definition Documentation

◆ OOJS_RUNTIME_SIZE_MiB

#define OOJS_RUNTIME_SIZE_MiB   256

Definition at line 37 of file OOJavaScriptEnginePoc.m.

◆ OOJS_TIME_LIMIT

#define OOJS_TIME_LIMIT   (1)

Definition at line 38 of file OOJavaScriptEnginePoc.m.

Function Documentation

◆ CallerPrefix()

NSString * CallerPrefix ( NSString * scriptClass,
NSString * function )
static

Definition at line 420 of file OOJavaScriptEnginePoc.m.

421{
422 if (function == nil)
423 return @"";
424 if (scriptClass == nil)
425 return [function stringByAppendingString:@": "];
426 return [NSString stringWithFormat:@"%@.%@: ", scriptClass, function];
427}
return nil

References nil.

Referenced by OOJSReportErrorForCaller(), and OOJSReportWarningForCaller().

Here is the caller graph for this function:

◆ DescribeValue()

NSString * DescribeValue ( JSContext * context,
JSValueConst value,
BOOL abbreviateObjects,
BOOL recursing )
static

Definition at line 641 of file OOJavaScriptEnginePoc.m.

642{
643 if (JS_IsFunction(context, value)) {
644 JSValue nameVal = JS_GetPropertyStr(context, value, "name");
645 NSString* name = OOStringFromJSValue(context, nameVal);
646 JS_FreeValue(context, nameVal);
647 if ([name length] != 0)
648 return [NSString stringWithFormat:@"function %@", name];
649 else
650 return @"function";
651 }
652
653 NSString* result = nil;
654
655 if (JS_IsString(value)) {
656 enum { kMaxLength = 200 };
657
658 NSString* string = OOStringFromJSValueEvenIfNull(context, value);
659 NSUInteger length = [string length];
660 result = [string substringToIndex:MIN(length, (NSUInteger)kMaxLength)];
661 result = [NSString stringWithFormat:@"\"%@%@\"", [result escapedForJavaScriptLiteral], (length > kMaxLength) ? @"..." : @""];
662 } else if (JS_IsArray(value)) {
663 // Descibe up to four elements of an array.
664 int64_t count;
665 if (JS_GetLength(context, value, &count) == 0) {
666 if (!recursing) {
667 NSMutableString* arrayDesc = [NSMutableString stringWithString:@"["];
668 int64_t i, effectiveCount = MIN(count, (int64_t)4);
669 for (i = 0; i < effectiveCount; i++) {
670 JSValue item = JS_GetPropertyUint32(context, value, (uint32_t)i);
671 NSString* itemDesc = @"?";
672 if (!JS_IsException(item)) {
673 itemDesc = DescribeValue(context, item, YES /* always abbreviate objects in arrays */, YES);
674 }
675 JS_FreeValue(context, item);
676 if (i != 0)
677 [arrayDesc appendString:@", "];
678 [arrayDesc appendString:itemDesc];
679 }
680 if (effectiveCount != count) {
681 [arrayDesc appendFormat:@", ... <%lli items total>]", (long long)count];
682 } else {
683 [arrayDesc appendString:@"]"];
684 }
685
686 result = arrayDesc;
687 } else {
688 result = [NSString stringWithFormat:@"[<%lli items>]", (long long)count];
689 }
690 } else {
691 result = @"[...]";
692 }
693 }
694
695 if (result == nil) {
696 result = OOStringFromJSValueEvenIfNull(context, value);
697
698 if (abbreviateObjects && JS_IsObject(value) && [result isEqualToString:@"[object Object]"]) {
699 result = @"{...}";
700 }
701
702 if (result == nil)
703 result = @"?";
704 }
705
706 return result;
707}
static NSString * DescribeValue(JSContext *context, JSValueConst value, BOOL abbreviateObjects, BOOL recursing)
NSString * OOStringFromJSValueEvenIfNull(JSContext *context, JSValueConst value)
NSString * OOStringFromJSValue(JSContext *context, JSValueConst value)
#define MIN(A, B)
Definition OOMaths.h:91
unsigned count
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque

References count, DescribeValue(), MIN, nil, OOStringFromJSValue(), and OOStringFromJSValueEvenIfNull().

Referenced by DescribeValue(), and OOJSDescribeValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLocationFromStack()

BOOL GetLocationFromStack ( NSString * stack,
NSString ** outFile,
unsigned * outLine )
static

Definition at line 340 of file OOJavaScriptEnginePoc.m.

341{
342 for (NSString* frame in [stack componentsSeparatedByString:@"\n"]) {
343 frame = [frame stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
344 if (![frame hasPrefix:@"at "])
345 continue;
346 frame = [frame substringFromIndex:3];
347
348 NSRange open = [frame rangeOfString:@"(" options:NSBackwardsSearch];
349 if (open.location != NSNotFound && [frame hasSuffix:@")"]) {
350 frame = [frame substringWithRange:NSMakeRange(open.location + 1, [frame length] - open.location - 2)];
351 }
352
353 NSArray* parts = [frame componentsSeparatedByString:@":"];
354 if ([parts count] < 3)
355 continue; // Native frame, e.g. "at log (native)".
356
357 NSUInteger count = [parts count];
358 *outFile = [[parts subarrayWithRange:NSMakeRange(0, count - 2)] componentsJoinedByString:@":"];
359 *outLine = (unsigned)[[parts objectAtIndex:count - 2] intValue];
360 return YES;
361 }
362 return NO;
363}

References count.

Referenced by LogProblem().

Here is the caller graph for this function:

◆ GlobalLog()

JSValue GlobalLog ( OOJS_NATIVE_ARGS )
static

Definition at line 219 of file OOJavaScriptEnginePoc.m.

220{
221 OOJS_NATIVE_ENTER(context)
222
223 NSString* message = nil;
224 NSString* messageClass = nil;
225
226 if (EXPECT_NOT(argc < 1)) {
228 }
229 if (argc < 2) {
230 messageClass = @"script.debug.message";
231 message = OOStringFromJSValue(context, OOJS_ARGV[0]);
232 } else {
233 messageClass = OOStringFromJSValueEvenIfNull(context, OOJS_ARGV[0]);
234 if (!OOLogWillDisplayMessagesInClass(messageClass)) {
235 // Do nothing (and short-circuit) if message class is filtered out.
237 }
238
239 NSMutableArray* parts = [NSMutableArray arrayWithCapacity:argc - 1];
240 for (int i = 1; i < argc; i++) {
241 [parts addObject:OOStringFromJSValueEvenIfNull(context, OOJS_ARGV[i])];
242 }
243 message = [parts componentsJoinedByString:@", "];
244 }
245
247 OOLog(messageClass, @"%@", message);
249
251
253}
#define EXPECT_NOT(x)
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_ARGV
#define OOJS_RETURN_VOID
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
Definition OOLogging.m:136
#define OOLog(class, format,...)
Definition OOLogging.h:84

References OOJavaScriptEngine::context, EXPECT_NOT, nil, OOJS_ARGV, OOJS_BEGIN_FULL_NATIVE, OOJS_END_FULL_NATIVE, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_VOID, OOLog, OOLogWillDisplayMessagesInClass(), OOStringFromJSValue(), and OOStringFromJSValueEvenIfNull().

Here is the call graph for this function:

◆ InterruptHandler()

int InterruptHandler ( OOJSInterruptHandlerArg * arg,
void * opaque )
static

Definition at line 319 of file OOJavaScriptEnginePoc.m.

320{
322 return 0;
323
324 double elapsed = MonotonicTime() - sLimiterStart;
325 if (elapsed <= sLimiterTimeLimit)
326 return 0;
327
328 OOLogERR(@"script.javaScript.timeLimit", @"Script \"%@\" ran for %g seconds and has been terminated.", [[OOJSScript currentlyRunningScript] name], elapsed);
329 return 1;
330}
#define EXPECT(x)
static double sLimiterTimeLimit
static int sLimiterPauseDepth
static unsigned sLimiterStartDepth
static OOHighResTimeValue sLimiterStart
static double MonotonicTime(void)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125

References EXPECT, MonotonicTime(), OOLogERR, sLimiterPauseDepth, sLimiterStart, sLimiterStartDepth, and sLimiterTimeLimit.

Here is the call graph for this function:

◆ LogProblem()

void LogProblem ( NSString * severity,
NSString * errorName,
NSString * message,
NSString * stack )
static

Definition at line 375 of file OOJavaScriptEnginePoc.m.

376{
377 NSString* messageClass = [NSString stringWithFormat:@"script.javaScript.%@.%@", severity, errorName];
378 NSString* highlight = [severity isEqualToString:@"warning"] ? @"-----" : @"*****";
379
380 if (!OOLogWillDisplayMessagesInClass(messageClass))
381 return;
382
383 NSString* activeScript = [[OOJSScript currentlyRunningScript] displayName];
384 if (activeScript == nil)
385 activeScript = @"<unidentified script>";
386 OOLog(messageClass, @"%@ JavaScript %@ (%@): %@", highlight, severity, activeScript, message);
387
388 NSString* file = nil;
389 unsigned line = 0;
390 if ([[OOJavaScriptEngine sharedEngine] showErrorLocations] && GetLocationFromStack(stack, &file, &line)) {
391 OOLog(messageClass, @" %@, line %u.", file, line);
392 }
393}
static BOOL GetLocationFromStack(NSString *stack, NSString **outFile, unsigned *outLine)

References OOJSScript::currentlyRunningScript, OOJSScript::displayName, GetLocationFromStack(), nil, OOLog, and OOLogWillDisplayMessagesInClass().

Referenced by OOJSReportPendingException(), and OOJSReportWarningWithArguments().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MonotonicTime()

double MonotonicTime ( void )
static

Definition at line 265 of file OOJavaScriptEnginePoc.m.

266{
267#if OOLITE_WINDOWS
268 static LARGE_INTEGER frequency;
269 LARGE_INTEGER counter;
270 if (frequency.QuadPart == 0)
271 QueryPerformanceFrequency(&frequency);
272 QueryPerformanceCounter(&counter);
273 return (double)counter.QuadPart / (double)frequency.QuadPart;
274#else
275 struct timespec now;
276 clock_gettime(CLOCK_MONOTONIC, &now);
277 return now.tv_sec + now.tv_nsec * 1e-9;
278#endif
279}

Referenced by InterruptHandler(), OOJSPauseTimeLimiter(), OOJSResumeTimeLimiter(), and OOJSStartTimeLimiterWithTimeLimit().

Here is the caller graph for this function:

◆ OOJSArgumentListGetNumber()

BOOL OOJSArgumentListGetNumber ( JSContext * context,
NSString * scriptClass,
NSString * function,
int argc,
JSValueConst * argv,
double * outNumber,
int * outConsumed )

Definition at line 568 of file OOJavaScriptEnginePoc.m.

569{
570 if (OOJSArgumentListGetNumberNoError(context, argc, argv, outNumber, outConsumed)) {
571 return YES;
572 } else {
573 OOJSReportBadArguments(context, scriptClass, function, argc, argv,
574 @"Expected number, got", NULL);
575 return NO;
576 }
577}
BOOL OOJSArgumentListGetNumberNoError(JSContext *context, int argc, JSValueConst *argv, double *outNumber, int *outConsumed)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, int argc, JSValueConst *argv, NSString *message, NSString *expectedArgsDescription)

References OOJSArgumentListGetNumberNoError(), OOJSArgumentListGetNumberNoError(), OOJSReportBadArguments(), and OOJSReportBadArguments().

Here is the call graph for this function:

◆ OOJSArgumentListGetNumberNoError()

BOOL OOJSArgumentListGetNumberNoError ( JSContext * context,
int argc,
JSValueConst * argv,
double * outNumber,
int * outConsumed )

Definition at line 579 of file OOJavaScriptEnginePoc.m.

580{
581 double value;
582
583 NSCParameterAssert(context != NULL && (argv != NULL || argc == 0) && outNumber != NULL);
584
585 // Get value, if possible.
586 if (EXPECT_NOT(argc < 1 || JS_ToFloat64(context, &value, argv[0]) < 0 || isnan(value))) {
587 // ToNumber can throw (e.g. for a Symbol); this variant must not leave an exception pending.
588 if (JS_HasException(context))
589 JS_FreeValue(context, JS_GetException(context));
590 if (outConsumed != NULL)
591 *outConsumed = 0;
592 return NO;
593 }
594
595 // Success.
596 *outNumber = value;
597 if (outConsumed != NULL)
598 *outConsumed = 1;
599 return YES;
600}

References EXPECT_NOT, OOJS_PROFILE_ENTER, and OOJS_PROFILE_EXIT.

Referenced by OOJSArgumentListGetNumber().

Here is the caller graph for this function:

◆ OOJSClassNameOfValue()

NSString * OOJSClassNameOfValue ( JSContext * context,
JSValueConst value )

Definition at line 628 of file OOJavaScriptEnginePoc.m.

629{
630 if (!JS_IsObject(value))
631 return @"primitive";
632
633 JSValue ctor = JS_GetPropertyStr(context, value, "constructor");
634 JSValue name = JS_IsObject(ctor) ? JS_GetPropertyStr(context, ctor, "name") : JS_UNDEFINED;
635 NSString* result = OOStringFromJSValue(context, name);
636 JS_FreeValue(context, name);
637 JS_FreeValue(context, ctor);
638 return result ?: @"Object";
639}

References OOJSClassName(), OOStringFromJSValue(), and OOStringFromJSValue().

Referenced by ManifestSetProperty(), ManifestSetPropertyExotic(), OOJSReportBadPropertySelector(), and OOJSReportBadPropertyValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSDescribeValue()

NSString * OOJSDescribeValue ( JSContext * context,
JSValueConst value,
BOOL abbreviateObjects )

Definition at line 709 of file OOJavaScriptEnginePoc.m.

710{
711 return DescribeValue(context, value, abbreviateObjects, NO);
712}

References DescribeValue(), and DescribeValue().

Referenced by OOJSReportBadPropertyValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSIDFromString()

JSAtom OOJSIDFromString ( NSString * string)

Definition at line 191 of file OOJavaScriptEnginePoc.m.

192{
193 // Interned for the life of the process, as in the full engine.
194 return JS_NewAtom(OOJSAcquireContext(), [string UTF8String]);
195}
OOINLINE JSContext * OOJSAcquireContext(void)

References EXPECT, EXPECT_NOT, nil, OOJSAcquireContext(), and OOJSRelinquishContext().

Referenced by JSObjectFromNSDictionary(), and RecordSharedProperties().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSNativeFailure()

JSValue OOJSNativeFailure ( JSContext * context)

Definition at line 197 of file OOJavaScriptEnginePoc.m.

198{
199 if (JS_HasException(context))
200 return JS_EXCEPTION;
201
202 JSValue error = JS_NewError(context);
203 JS_SetUncatchableError(context, error);
204 return JS_Throw(context, error);
205}

References OOJavaScriptEngine::context.

Referenced by DockGetProperty(), DockIsQueued(), DockSetProperty(), EquipmentInfoGetProperty(), EquipmentInfoSetProperty(), EquipmentInfoStaticInfoForKey(), ExhaustPlumeGetProperty(), ExhaustPlumeSetProperty(), FlasherGetProperty(), FlasherSetProperty(), FontMeasureString(), PlanetGetProperty(), PlanetSetProperty(), PlayerAddMessageToArrivalReport(), PlayerAudioMessage(), PlayerCommsMessage(), PlayerConsoleMessage(), PlayerEndScenario(), PlayerGetProperty(), PlayerReplaceShip(), PlayerSetEscapePodDestination(), PlayerSetPlayerRole(), PlayerSetProperty(), PlayerShipAddParcel(), PlayerShipAddPassenger(), PlayerShipAwardContract(), PlayerShipAwardEquipmentToCurrentPylon(), PlayerShipBeginGalacticHyperspaceCountdown(), PlayerShipBeginHyperspaceCountdown(), PlayerShipCancelDockingRequest(), PlayerShipEngageAutopilotToStation(), PlayerShipGetProperty(), PlayerShipHideHUDSelector(), PlayerShipRemoveAllCargo(), PlayerShipRemoveContract(), PlayerShipRemoveParcel(), PlayerShipRemovePassenger(), PlayerShipRequestDockingClearance(), PlayerShipResetCustomView(), PlayerShipSetCustomHUDDial(), PlayerShipSetCustomView(), PlayerShipSetMultiFunctionDisplay(), PlayerShipSetMultiFunctionText(), PlayerShipSetPrimedEquipment(), PlayerShipSetProperty(), PlayerShipShowHUDSelector(), PlayerShipUseSpecialCargo(), RemoveOrExplodeShip(), ShipAddCargoEntity(), ShipAddCollisionException(), ShipAddDefenseTarget(), ShipAdjustCargo(), ShipAwardEquipment(), ShipCanAwardEquipment(), ShipCheckScanner(), ShipCommsMessage(), ShipDealEnergyDamage(), ShipDumpCargo(), ShipEjectItem(), ShipEjectSpecificItem(), ShipEnterWormhole(), ShipEquipmentStatus(), ShipExitAI(), ShipExitSystem(), ShipGetProperty(), ShipGroupAddShip(), ShipGroupConstruct(), ShipGroupContainsShip(), ShipGroupGetProperty(), ShipGroupRemoveShip(), ShipGroupSetProperty(), ShipHasEquipmentProviding(), ShipHasRole(), ShipOfferToEscort(), ShipPatrolReportIn(), ShipReactToAIMessage(), ShipRemove(), ShipRemoveCollisionException(), ShipRemoveDefenseTarget(), ShipRemoveEquipment(), ShipRunLegacyScriptActions(), ShipSendAIMessage(), ShipSetAI(), ShipSetBounty(), ShipSetCargo(), ShipSetCargoType(), ShipSetCrew(), ShipSetEquipmentStatus(), ShipSetMaterials(), ShipSetProperty(), ShipSetScript(), ShipSetShaders(), ShipSpawn(), ShipStaticKeysForRole(), ShipStaticRoleIsInCategory(), ShipStaticSetShipDataForKey(), ShipStaticShipDataForKey(), ShipSwitchAI(), ShipThreatAssessment(), SoundGetProperty(), SoundSourceGetProperty(), SoundSourcePlay(), SoundSourcePlayOrRepeat(), SoundSourceSetProperty(), SoundSourceStop(), SoundStaticLoad(), SoundStaticPlayMusic(), SoundStaticStopMusic(), StationAbortDockingForShip(), StationAddShipToShipyard(), StationCanDockShip(), StationGetProperty(), StationLaunchShipWithRole(), StationRemoveShipFromShipyard(), StationSetInterface(), StationSetMarketPrice(), StationSetMarketQuantity(), StationSetProperty(), SunCancelNova(), SunGetProperty(), SunGoNova(), SystemAddMoon(), SystemAddPlanet(), SystemAddVisualEffect(), SystemCountEntitiesWithScanClass(), SystemCountShipsWithPrimaryRole(), SystemCountShipsWithRole(), SystemEntitiesWithScanClass(), SystemFilteredEntities(), SystemGetProperty(), SystemInfoDistanceToSystem(), SystemInfoGetProperty(), SystemInfoRouteToSystem(), SystemInfoSamplePrice(), SystemInfoSetPropertyMethod(), SystemInfoStaticFilteredSystems(), SystemInfoStaticSetInterstellarProperty(), SystemLegacyAddShips(), SystemLegacyAddShipsAt(), SystemLegacyAddShipsAtPrecisely(), SystemLegacyAddShipsWithinRadius(), SystemLegacyAddSystemShips(), SystemLegacySpawnShip(), SystemLocationFromCode(), SystemSetPopulator(), SystemSetProperty(), SystemSetWaypoint(), SystemShipsWithPrimaryRole(), SystemShipsWithRole(), SystemStaticInfoForSystem(), SystemStaticSystemIDForName(), SystemStaticSystemNameForID(), TimerConstruct(), TimerGetProperty(), TimerSetProperty(), TimerStart(), TimerStop(), VectorFromCoordinateSystem(), VectorToCoordinateSystem(), VisualEffectGetProperty(), VisualEffectScale(), VisualEffectSetMaterials(), VisualEffectSetProperty(), VisualEffectSetShaders(), WaypointGetProperty(), WaypointSetProperty(), and WormholeGetProperty().

Here is the call graph for this function:

◆ OOJSPauseTimeLimiter()

void OOJSPauseTimeLimiter ( void )

Definition at line 303 of file OOJavaScriptEnginePoc.m.

304{
305 if (sLimiterPauseDepth++ == 0)
307}
static OOHighResTimeValue sLimiterPauseStart

References MonotonicTime(), OODisposeHighResTime, OOGetHighResTime, sLimiterPauseDepth, and sLimiterPauseStart.

Referenced by Universe::findEntitiesMatchingPredicate:parameter:inRange:ofEntity:, Universe::findOneEntityMatchingPredicate:parameter:, JSFunctionPredicate(), MissionRunScreen(), MissionRunScreen(), OOJSReportPendingException(), OOJSReportWarningWithArguments(), PlanetSetProperty(), PlanetSetProperty(), ReportJSError(), SoundFromJSValue(), SoundFromJSValue(), SystemFilteredEntities(), SystemFilteredEntities(), SystemInfoStaticFilteredSystems(), and SystemInfoStaticFilteredSystems().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportBadArguments()

void OOJSReportBadArguments ( JSContext * context,
NSString * scriptClass,
NSString * function,
int argc,
JSValueConst * argv,
NSString * message,
NSString * expectedArgsDescription )

Definition at line 552 of file OOJavaScriptEnginePoc.m.

553{
554 @try {
555 if (message == nil)
556 message = @"Invalid arguments";
557 message = [NSString stringWithFormat:@"%@ %@", message, [NSString stringWithJavaScriptParameters:argv count:argc inContext:context]];
558 if (expectedArgsDescription != nil)
559 message = [NSString stringWithFormat:@"%@ -- expected %@", message, expectedArgsDescription];
560
561 OOJSReportErrorForCaller(context, scriptClass, function, @"%@.", message);
562 }
563 @catch (id exception) {
564 // Squash any secondary errors during error handling.
565 }
566}
void OOJSReportErrorForCaller(JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)

References nil, and OOJSReportErrorForCaller().

Referenced by OOJSArgumentListGetNumber().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportBadPropertySelector()

void OOJSReportBadPropertySelector ( JSContext * context,
JSValueConst thisObj,
int magic,
const JSCFunctionListEntry * list,
size_t count )

Definition at line 535 of file OOJavaScriptEnginePoc.m.

536{
537 NSString* propName = PropertyNameForMagic(magic, list, count);
538 NSString* className = OOJSClassNameOfValue(context, thisObj);
539
540 OOJSReportError(context, @"Invalid property identifier %@ for instance of %@.", propName, className);
541}
NSString * OOJSClassNameOfValue(JSContext *context, JSValueConst value)
void OOJSReportError(JSContext *context, NSString *format,...)
static NSString * PropertyNameForMagic(int magic, const JSCFunctionListEntry *list, size_t count)

References count, OOJSClassNameOfValue(), OOJSReportError(), OOStringFromJSPropertyMagic(), and PropertyNameForMagic().

Here is the call graph for this function:

◆ OOJSReportBadPropertyValue()

void OOJSReportBadPropertyValue ( JSContext * context,
JSValueConst thisObj,
int magic,
const JSCFunctionListEntry * list,
size_t count,
JSValueConst value )

Definition at line 543 of file OOJavaScriptEnginePoc.m.

544{
545 NSString* propName = PropertyNameForMagic(magic, list, count);
546 NSString* className = OOJSClassNameOfValue(context, thisObj);
547 NSString* valueDesc = OOJSDescribeValue(context, value, YES);
548
549 OOJSReportError(context, @"Cannot set property %@ of instance of %@ to invalid value %@.", propName, className, valueDesc);
550}
NSString * OOJSDescribeValue(JSContext *context, JSValueConst value, BOOL abbreviateObjects)

References count, OOJSClassNameOfValue(), OOJSDescribeValue(), OOJSDescribeValue(), OOJSReportError(), OOStringFromJSPropertyMagic(), and PropertyNameForMagic().

Here is the call graph for this function:

◆ OOJSReportError()

void OOJSReportError ( JSContext * context,
NSString * format,
... )

Definition at line 429 of file OOJavaScriptEnginePoc.m.

430{
431 va_list args;
432
433 va_start(args, format);
434 OOJSReportErrorWithArguments(context, format, args);
435 va_end(args);
436}
void OOJSReportErrorWithArguments(JSContext *context, NSString *format, va_list args)

References OOJSReportErrorWithArguments().

Referenced by EntitySetProperty(), EntitySetProperty(), GlobalAddFrameCallback(), GlobalAddFrameCallback(), ManifestSetProperty(), ManifestSetPropertyExotic(), MissionVariablesDefineOwnProperty(), MissionVariablesSetProperty(), OOJSObjectGetterImplPRIVATE(), OOJSObjectGetterImplPRIVATE(), OOJSReportBadPropertySelector(), OOJSReportBadPropertySelector(), OOJSReportBadPropertyValue(), OOJSReportBadPropertyValue(), OOJSReportErrorForCaller(), OOJSReportWrappedException(), OOJSUnconstructableConstruct(), OOJSUnconstructableConstruct(), OOJSValueToInt32(), PlayerReplaceShip(), PlayerReplaceShip(), PlayerSetEscapePodDestination(), PlayerSetEscapePodDestination(), PlayerShipRemoveAllCargo(), PlayerShipRemoveAllCargo(), PlayerShipResetCustomView(), PlayerShipResetCustomView(), PlayerShipSetCustomView(), PlayerShipSetCustomView(), PlayerShipSetProperty(), PlayerShipSetProperty(), RemoveOrExplodeShip(), RemoveOrExplodeShip(), SetMissionVariable(), ShipAdjustCargo(), ShipAdjustCargo(), ShipEnterWormhole(), ShipEnterWormhole(), ShipGroupConstruct(), ShipGroupConstruct(), ShipRemove(), ShipRemove(), ShipSetProperty(), ShipSetProperty(), SoundSourceConstruct(), SoundSourceConstruct(), SystemInfoGetProperty(), SystemInfoGetProperty(), TimerConstruct(), and TimerConstruct().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportErrorForCaller()

void OOJSReportErrorForCaller ( JSContext * context,
NSString * scriptClass,
NSString * function,
NSString * format,
... )

Definition at line 438 of file OOJavaScriptEnginePoc.m.

439{
440 va_list args;
441 NSString* msg = nil;
442
443 @try {
444 va_start(args, format);
445 msg = [[NSString alloc] initWithFormat:format arguments:args];
446 va_end(args);
447
448 OOJSReportError(context, @"%@%@", CallerPrefix(scriptClass, function), msg);
449 }
450 @catch (id exception) {
451 // Squash any secondary errors during error handling.
452 }
453 [msg release];
454}
static NSString * CallerPrefix(NSString *scriptClass, NSString *function)

References CallerPrefix(), CallerPrefix(), nil, and OOJSReportError().

Referenced by OOJSReportBadArguments(), OOJSReportBadArguments(), ShipExitAI(), ShipExitAI(), ShipExitSystem(), ShipExitSystem(), ShipReactToAIMessage(), ShipReactToAIMessage(), ShipSendAIMessage(), ShipSendAIMessage(), ShipSetAI(), ShipSetAI(), ShipSetEquipmentStatus(), ShipSetEquipmentStatus(), ShipSetScript(), ShipSetScript(), ShipSwitchAI(), ShipSwitchAI(), SystemInfoDistanceToSystem(), SystemInfoDistanceToSystem(), SystemInfoRouteToSystem(), SystemInfoRouteToSystem(), SystemInfoSamplePrice(), and SystemInfoSamplePrice().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportErrorWithArguments()

void OOJSReportErrorWithArguments ( JSContext * context,
NSString * format,
va_list args )

Definition at line 456 of file OOJavaScriptEnginePoc.m.

457{
458 NSString* msg = nil;
459
460 @try {
461 msg = [[NSString alloc] initWithFormat:format arguments:args];
462 JS_ThrowPlainError(context, "%s", [msg UTF8String]);
463 }
464 @catch (id exception) {
465 // Squash any secondary errors during error handling.
466 }
467 [msg release];
468}

References JS_IsInRequest, and nil.

Referenced by OOJSReportError().

Here is the caller graph for this function:

◆ OOJSReportPendingException()

void OOJSReportPendingException ( JSContext * context)

Definition at line 395 of file OOJavaScriptEnginePoc.m.

396{
397 JSValue exception = JS_GetException(context);
398
399 // The time limiter has already logged a termination.
400 if (JS_IsUncatchableError(exception)) {
401 JS_FreeValue(context, exception);
402 return;
403 }
404
406
407 NSString* errorName = @"exception";
408 if (JS_IsError(exception)) {
409 JSValue name = JS_GetPropertyStr(context, exception, "name");
410 errorName = OOStringFromJSValue(context, name) ?: errorName;
411 JS_FreeValue(context, name);
412 }
413
414 LogProblem(@"exception", errorName, OOStringFromJSValueEvenIfNull(context, exception), StackOfValue(context, exception));
415
416 JS_FreeValue(context, exception);
418}
void OOJSPauseTimeLimiter(void)
static NSString * StackOfValue(JSContext *context, JSValueConst value)
static void LogProblem(NSString *severity, NSString *errorName, NSString *message, NSString *stack)
void OOJSResumeTimeLimiter(void)

References LogProblem(), OOJSPauseTimeLimiter(), OOJSResumeTimeLimiter(), OOStringFromJSValue(), OOStringFromJSValueEvenIfNull(), and StackOfValue().

Referenced by OOJavaScriptEngine::callJSFunction:forObject:argc:argv:result:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportWarning()

void OOJSReportWarning ( JSContext * context,
NSString * format,
... )

Definition at line 481 of file OOJavaScriptEnginePoc.m.

482{
483 va_list args;
484
485 va_start(args, format);
486 OOJSReportWarningWithArguments(context, format, args);
487 va_end(args);
488}
void OOJSReportWarningWithArguments(JSContext *context, NSString *format, va_list args)

References OOJSReportWarningWithArguments().

Referenced by OOJSScript(OOPrivate)::checkForTickle:, EntityFromArgumentList(), EntityFromArgumentList(), EquipmentInfoSetProperty(), EquipmentInfoSetProperty(), GetJSSystemInfoForSystem(), GlobalRemoveFrameCallback(), GlobalRemoveFrameCallback(), GlobalSetScreenBackground(), GlobalSetScreenBackground(), GlobalSetScreenOverlay(), GlobalSetScreenOverlay(), ManifestSetProperty(), ManifestSetProperty(), MissionRunScreen(), MissionRunScreen(), MissionRunShipLibrary(), MissionRunShipLibrary(), MissionSetInstructionsInternal(), MissionSetInstructionsInternal(), OOJSReportWarningForCaller(), OoliteSetProperty(), OoliteSetProperty(), PlanetSetProperty(), PlanetSetProperty(), PlayerShipSetProperty(), PlayerShipSetProperty(), ScriptAddProperty(), ShipFireECM(), ShipFireECM(), ShipSetMaterialsInternal(), ShipSetMaterialsInternal(), ShipSetShaders(), ShipSetShaders(), SpecialJSWarning(), SpecialJSWarning(), GuiDisplayGen::textureDescriptorFromJSValue:inContext:callerDescription:, TimerSetProperty(), TimerSetProperty(), VisualEffectSetMaterialsInternal(), VisualEffectSetMaterialsInternal(), VisualEffectSetShaders(), and VisualEffectSetShaders().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportWarningForCaller()

void OOJSReportWarningForCaller ( JSContext * context,
NSString * scriptClass,
NSString * function,
NSString * format,
... )

Definition at line 490 of file OOJavaScriptEnginePoc.m.

491{
492 va_list args;
493 NSString* msg = nil;
494
495 @try {
496 va_start(args, format);
497 msg = [[NSString alloc] initWithFormat:format arguments:args];
498 va_end(args);
499
500 OOJSReportWarning(context, @"%@%@", CallerPrefix(scriptClass, function), msg);
501 }
502 @catch (id exception) {
503 // Squash any secondary errors during error handling.
504 }
505 [msg release];
506}
void OOJSReportWarning(JSContext *context, NSString *format,...)

References CallerPrefix(), CallerPrefix(), nil, and OOJSReportWarning().

Referenced by ShipAddCargoEntity(), ShipAddCargoEntity(), ShipDumpCargo(), ShipDumpCargo(), ShipExitAI(), ShipExitAI(), ShipGroupAddShip(), ShipGroupAddShip(), ShipSetEquipmentStatus(), ShipSetEquipmentStatus(), StationAddShipToShipyard(), StationAddShipToShipyard(), StationRemoveShipFromShipyard(), and StationRemoveShipFromShipyard().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportWarningWithArguments()

void OOJSReportWarningWithArguments ( JSContext * context,
NSString * format,
va_list args )

Definition at line 508 of file OOJavaScriptEnginePoc.m.

509{
510 NSString* msg = nil;
511
512 @try {
513 msg = [[NSString alloc] initWithFormat:format arguments:args];
514
515 // A fresh Error object captures the current JS stack, giving the location.
516 JSValue error = JS_NewError(context);
517 LogProblem(@"warning", @"warning", msg, StackOfValue(context, error));
518 JS_FreeValue(context, error);
519 }
520 @catch (id exception) {
521 // Squash any secondary errors during error handling.
522 }
523 [msg release];
524}

References LogProblem(), nil, and StackOfValue().

Referenced by OOJSReportWarning(), and SyntaxIssue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOJSReportWrappedException()

void OOJSReportWrappedException ( JSContext * context,
id exception )

Definition at line 470 of file OOJavaScriptEnginePoc.m.

471{
472 if (!JS_HasException(context)) {
473 if ([exception isKindOfClass:[NSException class]])
474 OOJSReportError(context, @"Native exception: %@", [exception reason]);
475 else
476 OOJSReportError(context, @"Unidentified native exception");
477 }
478 // Else, let the pending exception propagate.
479}

References OOJSReportError().

Here is the call graph for this function:

◆ OOJSResumeTimeLimiter()

void OOJSResumeTimeLimiter ( void )

◆ OOJSStartTimeLimiterWithTimeLimit()

void OOJSStartTimeLimiterWithTimeLimit ( OOTimeDelta limit)

Definition at line 281 of file OOJavaScriptEnginePoc.m.

282{
283 if (sLimiterStartDepth++ == 0) {
284 if (limit <= 0.0)
285 limit = OOJS_TIME_LIMIT;
286 sLimiterTimeLimit = limit;
289 }
290}
#define OOJS_TIME_LIMIT

References MonotonicTime(), MonotonicTime(), OOJS_TIME_LIMIT, sLimiterPauseDepth, sLimiterStart, sLimiterStartDepth, and sLimiterTimeLimit.

Here is the call graph for this function:

◆ OOJSStopTimeLimiter()

void OOJSStopTimeLimiter ( void )

Definition at line 292 of file OOJavaScriptEnginePoc.m.

293{
294 if (sLimiterStartDepth == 0) {
295 OOLog(@"bug.javaScript.limiterDepth", @"%@", @"Attempt to stop JavaScript time limiter while it is already fully stopped. This is an internal bug, please report it.");
296 return;
297 }
298
299 if (--sLimiterStartDepth == 0)
300 sLimiterTimeLimit = 0.0;
301}

References OOLog, sLimiterStartDepth, and sLimiterTimeLimit.

Referenced by OOJavaScriptEngine::callJSFunction:forObject:argc:argv:result:.

Here is the caller graph for this function:

◆ OOJSWrapScriptSource()

NSString * OOJSWrapScriptSource ( NSString * source)

Definition at line 207 of file OOJavaScriptEnginePoc.m.

208{
209 // A newline before the closing part is needed if the last line is a // comment.
210 NSString* lineEnd = [source hasSuffix:@"\n"] ? @"" : @"\n";
211 return [NSString stringWithFormat:@"with(this){(function(){%@%@}).call(this)}", source, lineEnd];
212}

◆ OOStringFromJSValue()

NSString * OOStringFromJSValue ( JSContext * context,
JSValueConst value )

Definition at line 620 of file OOJavaScriptEnginePoc.m.

621{
622 if (EXPECT(!JS_IsNull(value) && !JS_IsUndefined(value))) {
623 return OOStringFromJSValueEvenIfNull(context, value);
624 }
625 return nil;
626}

References EXPECT, nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOStringFromJSValueEvenIfNull(), and OOStringFromJSValueEvenIfNull().

Referenced by DescribeValue(), GlobalLog(), OOJSClassNameOfValue(), OOJSReportPendingException(), and StackOfValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOStringFromJSValueEvenIfNull()

NSString * OOStringFromJSValueEvenIfNull ( JSContext * context,
JSValueConst value )

Definition at line 606 of file OOJavaScriptEnginePoc.m.

607{
608 size_t length;
609 const char* chars = JS_ToCStringLen(context, &length, value); // Calls the value's toString method if needed.
610 if (chars == NULL) {
611 JS_FreeValue(context, JS_GetException(context));
612 return nil;
613 }
614
615 NSString* result = [[[NSString alloc] initWithBytes:chars length:length encoding:NSUTF8StringEncoding] autorelease];
616 JS_FreeCString(context, chars);
617 return result;
618}

References nil, OOJS_PROFILE_ENTER, and OOJS_PROFILE_EXIT.

Referenced by DescribeValue(), GlobalLog(), OOJSReportPendingException(), and OOStringFromJSValue().

Here is the caller graph for this function:

◆ PropertyNameForMagic()

NSString * PropertyNameForMagic ( int magic,
const JSCFunctionListEntry * list,
size_t count )
static

Definition at line 526 of file OOJavaScriptEnginePoc.m.

527{
528 for (size_t i = 0; i < count; i++) {
529 if (list[i].def_type == JS_DEF_CGETSET_MAGIC && list[i].magic == magic)
530 return [NSString stringWithUTF8String:list[i].name];
531 }
532 return [NSString stringWithFormat:@"%i", magic];
533}

References count.

Referenced by OOJSReportBadPropertySelector(), and OOJSReportBadPropertyValue().

Here is the caller graph for this function:

◆ StackOfValue()

NSString * StackOfValue ( JSContext * context,
JSValueConst value )
static

Definition at line 365 of file OOJavaScriptEnginePoc.m.

366{
367 if (!JS_IsObject(value))
368 return nil;
369 JSValue stack = JS_GetPropertyStr(context, value, "stack");
370 NSString* result = OOStringFromJSValue(context, stack);
371 JS_FreeValue(context, stack);
372 return result;
373}

References nil, and OOStringFromJSValue().

Referenced by OOJSReportPendingException(), and OOJSReportWarningWithArguments().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ gOOJSMainThreadContext

JSContext* gOOJSMainThreadContext = NULL

Definition at line 41 of file OOJavaScriptEnginePoc.m.

◆ sGlobalMethods

const JSCFunctionListEntry sGlobalMethods[]
static
Initial value:
= {
JS_CFUNC_DEF("log", 1, GlobalLog),
}
static JSBool GlobalLog(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:230

Definition at line 46 of file OOJavaScriptEnginePoc.m.

46 {
47 JS_CFUNC_DEF("log", 1, GlobalLog),
48};

◆ sLimiterPauseDepth

int sLimiterPauseDepth
static

Definition at line 260 of file OOJavaScriptEnginePoc.m.

◆ sLimiterPauseStart

double sLimiterPauseStart
static

Definition at line 262 of file OOJavaScriptEnginePoc.m.

◆ sLimiterStart

double sLimiterStart
static

Definition at line 261 of file OOJavaScriptEnginePoc.m.

◆ sLimiterStartDepth

unsigned sLimiterStartDepth
static

Definition at line 259 of file OOJavaScriptEnginePoc.m.

◆ sLimiterTimeLimit

double sLimiterTimeLimit
static

Definition at line 263 of file OOJavaScriptEnginePoc.m.

◆ sSharedEngine

OOJavaScriptEngine* sSharedEngine = nil
static

Definition at line 40 of file OOJavaScriptEnginePoc.m.