Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOJSScript.m File Reference
+ Include dependency graph for OOJSScript.m:

Go to the source code of this file.

Classes

struct  RunningStack
 
category  OOJSScript(OOPrivate)
 

Macros

#define OO_CACHE_JS_SCRIPTS   1
 

Typedefs

typedef struct RunningStack RunningStack
 

Functions

static void AddStackToArrayReversed (NSMutableArray *array, RunningStack *stack)
 
static JSScript * LoadScriptWithName (JSContext *context, NSString *path, JSObject *object, JSObject **outScriptObject, NSString **outErrorMessage)
 
static NSData * CompiledScriptData (JSContext *context, JSScript *script)
 
static JSScript * ScriptWithCompiledData (JSContext *context, NSData *data)
 
static NSString * StrippedName (NSString *string)
 
static JSBool ScriptAddProperty (JSContext *context, JSObject *this, jsid propID, jsval *value)
 
void InitOOJSScript (JSContext *context, JSObject *global)
 

Variables

static JSObject * sScriptPrototype
 
static RunningStacksRunningStack = NULL
 
static JSClass sScriptClass
 
static JSFunctionSpec sScriptMethods []
 

Macro Definition Documentation

◆ OO_CACHE_JS_SCRIPTS

#define OO_CACHE_JS_SCRIPTS   1

Definition at line 26 of file OOJSScript.m.

Typedef Documentation

◆ RunningStack

typedef struct RunningStack RunningStack

Definition at line 51 of file OOJSScript.m.

Function Documentation

◆ AddStackToArrayReversed()

static void AddStackToArrayReversed ( NSMutableArray * array,
RunningStack * stack )
static

Definition at line 688 of file OOJSScript.m.

689{
690 if (stack != NULL)
691 {
692 AddStackToArrayReversed(array, stack->back);
693 [array addObject:stack->current];
694 }
695}
static void AddStackToArrayReversed(NSMutableArray *array, RunningStack *stack)
Definition OOJSScript.m:688
RunningStack * back
Definition OOJSScript.m:54

References AddStackToArrayReversed(), and RunningStack::back.

Referenced by AddStackToArrayReversed().

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

◆ CompiledScriptData()

static NSData * CompiledScriptData ( JSContext * context,
JSScript * script )
static

Definition at line 767 of file OOJSScript.m.

768{
769 JSXDRState *xdr = NULL;
770 NSData *result = nil;
771 uint32 length;
772 void *bytes = NULL;
773
774 xdr = JS_XDRNewMem(context, JSXDR_ENCODE);
775 if (xdr != NULL)
776 {
777 if (JS_XDRScript(xdr, &script))
778 {
779 bytes = JS_XDRMemGetData(xdr, &length);
780 if (bytes != NULL)
781 {
782 result = [NSData dataWithBytes:bytes length:length];
783 }
784 }
785 JS_XDRDestroy(xdr);
786 }
787
788 return result;
789}
return nil

References nil.

Referenced by LoadScriptWithName().

+ Here is the caller graph for this function:

◆ InitOOJSScript()

void InitOOJSScript ( JSContext * context,
JSObject * global )

Definition at line 663 of file OOJSScript.m.

664{
665 sScriptPrototype = JS_InitClass(context, global, NULL, &sScriptClass, OOJSUnconstructableConstruct, 0, NULL, sScriptMethods, NULL, NULL);
667}
static JSObject * sScriptPrototype
Definition OOJSScript.m:59
static JSFunctionSpec sScriptMethods[]
Definition OOJSScript.m:94
static JSClass sScriptClass
Definition OOJSScript.m:78
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)

References OOJSBasicPrivateObjectConverter(), OOJSRegisterObjectConverter(), OOJSUnconstructableConstruct(), sScriptClass, sScriptMethods, and sScriptPrototype.

+ Here is the call graph for this function:

◆ LoadScriptWithName()

static JSScript * LoadScriptWithName ( JSContext * context,
NSString * path,
JSObject * object,
JSObject ** outScriptObject,
NSString ** outErrorMessage )
static

Definition at line 698 of file OOJSScript.m.

699{
700#if OO_CACHE_JS_SCRIPTS
701 OOCacheManager *cache = nil;
702#endif
703 NSString *fileContents = nil;
704 NSData *data = nil;
705 JSScript *script = NULL;
706
707 NSCParameterAssert(outScriptObject != NULL && outErrorMessage != NULL);
708 *outErrorMessage = nil;
709
710#if OO_CACHE_JS_SCRIPTS
711 // Look for cached compiled script
712 cache = [OOCacheManager sharedCache];
713 data = [cache objectForKey:path inCache:@"compiled JavaScript scripts"];
714 if (data != nil)
715 {
716 script = ScriptWithCompiledData(context, data);
717 }
718#endif
719
720 if (script == NULL)
721 {
722 fileContents = [NSString stringWithContentsOfUnicodeFile:path];
723
724 if (fileContents != nil)
725 {
726#ifndef NDEBUG
727 /* FIXME: this isn't strictly the right test, since strict
728 * mode can be enabled with this string within a function
729 * definition, but it seems unlikely anyone is actually doing
730 * that here. */
731 if ([fileContents rangeOfString:@"\"use strict\";"].location == NSNotFound && [fileContents rangeOfString:@"'use strict';"].location == NSNotFound)
732 {
733 OOStandardsDeprecated([NSString stringWithFormat:@"Script %@ does not \"use strict\";",path]);
734 if (OOEnforceStandards())
735 {
736 // prepend it anyway
737 // TODO: some time after 1.82, make this required
738 fileContents = [@"\"use strict\";\n" stringByAppendingString:fileContents];
739 }
740 }
741#endif
742 data = [fileContents utf16DataWithBOM:NO];
743 }
744 if (data == nil) *outErrorMessage = @"could not load file";
745 else
746 {
747 script = JS_CompileUCScript(context, object, [data bytes], [data length] / sizeof(unichar), [path UTF8String], 1);
748 if (script != NULL) *outScriptObject = JS_NewScriptObject(context, script);
749 else *outErrorMessage = @"compilation failed";
750 }
751
752#if OO_CACHE_JS_SCRIPTS
753 if (script != NULL)
754 {
755 // Write compiled script to cache
756 data = CompiledScriptData(context, script);
757 [cache setObject:data forKey:path inCache:@"compiled JavaScript scripts"];
758 }
759#endif
760 }
761
762 return script;
763}
void OOStandardsDeprecated(NSString *message)
BOOL OOEnforceStandards(void)
static NSData * CompiledScriptData(JSContext *context, JSScript *script)
Definition OOJSScript.m:767
static JSScript * ScriptWithCompiledData(JSContext *context, NSData *data)
Definition OOJSScript.m:792
void setObject:forKey:inCache:(id inElement,[forKey] NSString *inKey,[inCache] NSString *inCacheKey)
id objectForKey:inCache:(NSString *inKey,[inCache] NSString *inCacheKey)
OOCacheManager * sharedCache()

References CompiledScriptData(), nil, OOCacheManager::objectForKey:inCache:, OOEnforceStandards(), OOStandardsDeprecated(), ScriptWithCompiledData(), OOCacheManager::setObject:forKey:inCache:, and OOCacheManager::sharedCache.

+ Here is the call graph for this function:

◆ ScriptAddProperty()

static JSBool ScriptAddProperty ( JSContext * context,
JSObject * this,
jsid propID,
jsval * value )
static

Definition at line 670 of file OOJSScript.m.

671{
672 // Complain about attempts to set the property tickle.
673 if (JSID_IS_STRING(propID))
674 {
675 JSString *propName = JSID_TO_STRING(propID);
676 JSBool match;
677 if (JS_StringEqualsAscii(context, propName, "tickle", &match) && match)
678 {
679 OOJSScript *thisScript = OOJSNativeObjectOfClassFromJSObject(context, this, [OOJSScript class]);
680 OOJSReportWarning(context, @"Script %@ appears to use the tickle() event handler, which is no longer supported.", [thisScript name]);
681 }
682 }
683
684 return YES;
685}
void OOJSReportWarning(JSContext *context, NSString *format,...)
id OOJSNativeObjectOfClassFromJSObject(JSContext *context, JSObject *object, Class requiredClass)

References OOJSNativeObjectOfClassFromJSObject(), and OOJSReportWarning().

+ Here is the call graph for this function:

◆ ScriptWithCompiledData()

static JSScript * ScriptWithCompiledData ( JSContext * context,
NSData * data )
static

Definition at line 792 of file OOJSScript.m.

793{
794 JSXDRState *xdr = NULL;
795 JSScript *result = NULL;
796
797 if (data == nil) return NULL;
798
799 xdr = JS_XDRNewMem(context, JSXDR_DECODE);
800 if (xdr != NULL)
801 {
802 NSUInteger length = [data length];
803 if (EXPECT_NOT(length > UINT32_MAX)) return NULL;
804
805 JS_XDRMemSetData(xdr, (void *)[data bytes], (uint32_t)length);
806 if (!JS_XDRScript(xdr, &result)) result = NULL;
807
808 JS_XDRMemSetData(xdr, NULL, 0); // Don't let it be freed by XDRDestroy
809 JS_XDRDestroy(xdr);
810 }
811
812 return result;
813}
#define EXPECT_NOT(x)

References EXPECT_NOT, and nil.

Referenced by LoadScriptWithName().

+ Here is the caller graph for this function:

◆ StrippedName()

static NSString * StrippedName ( NSString * string)
static

Definition at line 817 of file OOJSScript.m.

818{
819 static NSCharacterSet *invalidSet = nil;
820 if (invalidSet == nil) invalidSet = [[NSCharacterSet characterSetWithCharactersInString:@"_ \t\n\r\v"] retain];
821
822 return [string stringByTrimmingCharactersInSet:invalidSet];
823}

References nil.

Variable Documentation

◆ sRunningStack

RunningStack* sRunningStack = NULL
static

Definition at line 60 of file OOJSScript.m.

◆ sScriptClass

JSClass sScriptClass
static
Initial value:
=
{
"Script",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
}
static JSBool ScriptAddProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSScript.m:670
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)

Definition at line 78 of file OOJSScript.m.

79{
80 "Script",
81 JSCLASS_HAS_PRIVATE,
82
84 JS_PropertyStub,
85 JS_PropertyStub,
86 JS_StrictPropertyStub,
87 JS_EnumerateStub,
88 JS_ResolveStub,
89 JS_ConvertStub,
91};

Referenced by InitOOJSScript().

◆ sScriptMethods

JSFunctionSpec sScriptMethods[]
static
Initial value:
=
{
{ "toString", OOJSObjectWrapperToString, 0, },
{ 0 }
}
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)

Definition at line 94 of file OOJSScript.m.

95{
96 // JS name Function min args
97 { "toString", OOJSObjectWrapperToString, 0, },
98 { 0 }
99};

Referenced by InitOOJSScript().

◆ sScriptPrototype

JSObject* sScriptPrototype
static

Definition at line 59 of file OOJSScript.m.

Referenced by InitOOJSScript().