Oolite
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 687 of file OOJSScript.m.

688{
689 if (stack != NULL)
690 {
691 AddStackToArrayReversed(array, stack->back);
692 [array addObject:stack->current];
693 }
694}
static void AddStackToArrayReversed(NSMutableArray *array, RunningStack *stack)
Definition OOJSScript.m:687
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 766 of file OOJSScript.m.

767{
768 JSXDRState *xdr = NULL;
769 NSData *result = nil;
770 uint32 length;
771 void *bytes = NULL;
772
773 xdr = JS_XDRNewMem(context, JSXDR_ENCODE);
774 if (xdr != NULL)
775 {
776 if (JS_XDRScript(xdr, &script))
777 {
778 bytes = JS_XDRMemGetData(xdr, &length);
779 if (bytes != NULL)
780 {
781 result = [NSData dataWithBytes:bytes length:length];
782 }
783 }
784 JS_XDRDestroy(xdr);
785 }
786
787 return result;
788}
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 662 of file OOJSScript.m.

663{
664 sScriptPrototype = JS_InitClass(context, global, NULL, &sScriptClass, OOJSUnconstructableConstruct, 0, NULL, sScriptMethods, NULL, NULL);
666}
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 697 of file OOJSScript.m.

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

670{
671 // Complain about attempts to set the property tickle.
672 if (JSID_IS_STRING(propID))
673 {
674 JSString *propName = JSID_TO_STRING(propID);
675 JSBool match;
676 if (JS_StringEqualsAscii(context, propName, "tickle", &match) && match)
677 {
678 OOJSScript *thisScript = OOJSNativeObjectOfClassFromJSObject(context, this, [OOJSScript class]);
679 OOJSReportWarning(context, @"Script %@ appears to use the tickle() event handler, which is no longer supported.", [thisScript name]);
680 }
681 }
682
683 return YES;
684}
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 791 of file OOJSScript.m.

792{
793 JSXDRState *xdr = NULL;
794 JSScript *result = NULL;
795
796 if (data == nil) return NULL;
797
798 xdr = JS_XDRNewMem(context, JSXDR_DECODE);
799 if (xdr != NULL)
800 {
801 NSUInteger length = [data length];
802 if (EXPECT_NOT(length > UINT32_MAX)) return NULL;
803
804 JS_XDRMemSetData(xdr, (void *)[data bytes], (uint32_t)length);
805 if (!JS_XDRScript(xdr, &result)) result = NULL;
806
807 JS_XDRMemSetData(xdr, NULL, 0); // Don't let it be freed by XDRDestroy
808 JS_XDRDestroy(xdr);
809 }
810
811 return result;
812}
#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 816 of file OOJSScript.m.

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

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:669
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().