25#ifndef OO_CACHE_JS_SCRIPTS
26#define OO_CACHE_JS_SCRIPTS 1
44#if OO_CACHE_JS_SCRIPTS
60static JSScript*
LoadScriptWithName(JSContext* context, NSString* path, JSObject*
object, JSObject** outScriptObject, NSString** outErrorMessage);
62#if OO_CACHE_JS_SCRIPTS
69static JSBool
ScriptAddProperty(JSContext* context, JSObject*
this, jsid propID, jsval* value);
78 JS_StrictPropertyStub,
95@interface OOJSScript (OOPrivate)
97- (NSString*)scriptNameFromPath:(NSString*)path;
98- (NSDictionary*)defaultPropertiesFromPath:(NSString*)path;
104+ (id)scriptWithPath:(NSString*)path properties:(NSDictionary*)properties
106 return [[[
self alloc] initWithPath:path properties:properties] autorelease];
109- (id)initWithPath:(NSString*)path properties:(NSDictionary*)properties
111 JSContext* context = NULL;
112 NSString* problem =
nil;
113 JSScript* script = NULL;
114 JSObject* scriptObject = NULL;
115 jsval returnValue = JSVAL_VOID;
121 problem =
@"allocation failure";
125 if (JS_IsExceptionPending(context)) {
126 JS_ClearPendingException(context);
127 OOLogERR(
@"script.javaScript.load.waitingException",
@"Prior to loading script %@, there was a pending JavaScript exception, which has been cleared. This is an internal error, please report it.", path);
134 problem =
@"allocation failure";
138 problem =
@"could not add JavaScript root object";
142 problem =
@"could not add JavaScript root object";
147 problem =
@"could not set private backreference";
158 filePath = [path retain];
161 OOLog(
@"script.javaScript.willLoad",
@"About to load JavaScript %@", path);
167 NSDictionary* defaultProperties = [
self defaultPropertiesFromPath:path];
170 if ([key isKindOfClass:[NSString
class]]) {
171 property = [defaultProperties objectForKey:key];
174 [
self defineProperty:property named:key];
177 [
self setProperty:property named:key];
183 if (!problem && properties !=
nil) {
186 if ([key isKindOfClass:[NSString
class]]) {
187 property = [properties objectForKey:key];
188 [
self defineProperty:property named:key];
199 jsid nameID =
OOJSID(
"name");
200 [
self setProperty:[
self scriptNameFromPath:path] withID:nameID inContext:context];
205 if (!JS_ExecuteScript(context, _jsSelf, script, &returnValue)) {
206 problem =
@"could not run script";
211 JS_DestroyScript(context, script);
214 JS_RemoveObjectRoot(context, &scriptObject);
221 name = [StrippedName([[
self propertyWithID:nameID inContext:context] description]) copy];
223 name = [[
self scriptNameFromPath:path] retain];
224 [
self setProperty:name withID:nameID inContext:context];
227 version = [[[
self propertyWithID:OOJSID("version") inContext:context] description] copy];
228 description = [[[
self propertyWithID:OOJSID("description") inContext:context] description] copy];
230 OOLog(
@"script.javaScript.load.success",
@"Loaded JavaScript: %@ -- %@", [
self displayName], description ? description : (NSString*)
@"(no description)");
239 OOLog(
@"script.javaScript.load.failed",
@"***** Error loading JavaScript script %@ -- %@", path, problem);
240 JS_ReportPendingException(context);
247 [[NSNotificationCenter defaultCenter] addObserver:
self
248 selector:@selector(javaScriptEngineWillReset:)
249 name:kOOJavaScriptEngineWillResetNotification
259 name:kOOJavaScriptEngineWillResetNotification
271 JS_RemoveObjectRoot(context, &
_jsSelf);
289 return [
super descriptionComponents];
291 return @"invalid script";
294- (void)javaScriptEngineWillReset:(NSNotification*)notification
300 JS_RemoveObjectRoot(context, &
_jsSelf);
312+ (NSArray*)scriptStack
314 NSMutableArray* result =
nil;
316 result = [NSMutableArray array];
337 name = [[
self propertyNamed:@"name"] copy];
339 return [
self scriptNameFromPath:filePath];
357- (BOOL)callMethod:(jsid)methodID
358 inContext:(JSContext*)context
359 withArguments:(jsval*)argv
361 result:(jsval*)outResult
363 NSParameterAssert(name != NULL && (argv != NULL || argc == 0) && context != NULL &&
JS_IsInRequest(context));
367 JSObject* root = NULL;
370 jsval ignoredResult = JSVAL_VOID;
372 if (outResult == NULL)
373 outResult = &ignoredResult;
376 if (
EXPECT(JS_GetMethodById(context, _jsSelf, methodID, &root, &method) && !JSVAL_IS_VOID(method))) {
378 if (JS_IsExceptionPending(context)) {
379 OOLog(
@"script.internalBug",
@"Exception pending on context before calling method in %s, clearing. This is an internal error, please report it.", __PRETTY_FUNCTION__);
380 JS_ClearPendingException(context);
396 OK = JS_CallFunctionValue(context, _jsSelf, method, argc, argv, outResult);
399 if (JS_IsExceptionPending(context)) {
400 JS_ReportPendingException(context);
412 JS_RemoveObjectRoot(context, &root);
417- (id)propertyWithID:(jsid)propID inContext:(JSContext*)context
423 jsval jsValue = JSVAL_VOID;
424 if (JS_GetPropertyById(context, _jsSelf, propID, &jsValue)) {
430- (BOOL)setProperty:(
id)value withID:(jsid)propID inContext:(JSContext*)context
437 return JS_SetPropertyById(context, _jsSelf, propID, &jsValue);
440- (BOOL)defineProperty:(
id)value withID:(jsid)propID inContext:(JSContext*)context
447 return JS_DefinePropertyById(context, _jsSelf, propID, jsValue, NULL, NULL,
OOJS_PROP_READONLY);
450- (id)propertyNamed:(NSString*)propName
458 id result = [
self propertyWithID:OOJSIDFromString(propName) inContext:context];
464- (BOOL)setProperty:(
id)value named:(NSString*)propName
466 if (value ==
nil || propName ==
nil)
472 BOOL result = [
self setProperty:value withID:OOJSIDFromString(propName) inContext:context];
478- (BOOL)defineProperty:(
id)value named:(NSString*)propName
480 if (value ==
nil || propName ==
nil)
486 BOOL result = [
self defineProperty:value withID:OOJSIDFromString(propName) inContext:context];
492- (jsval)oo_jsValueInContext:(JSContext*)context
496 return OBJECT_TO_JSVAL(
_jsSelf);
503 element = malloc(
sizeof *element);
512+ (void)popScript:(OOJSScript*)script
525@implementation OOJSScript (OOPrivate)
541- (NSString*)scriptNameFromPath:(NSString*)path
543 NSString* lastComponent =
nil;
544 NSString* truncatedPath =
nil;
545 NSString* theName =
nil;
548 theName = [NSString stringWithFormat:@"%p",
self];
550 lastComponent = [path lastPathComponent];
551 if (![lastComponent hasPrefix:
@"script."])
552 theName = lastComponent;
554 truncatedPath = [path stringByDeletingLastPathComponent];
555 if (NSOrderedSame == [[truncatedPath lastPathComponent] caseInsensitiveCompare:
@"Config"]) {
556 truncatedPath = [truncatedPath stringByDeletingLastPathComponent];
558 if (NSOrderedSame == [[truncatedPath pathExtension] caseInsensitiveCompare:
@"oxp"]) {
559 truncatedPath = [truncatedPath stringByDeletingPathExtension];
562 lastComponent = [truncatedPath lastPathComponent];
563 theName = lastComponent;
567 if (0 == [theName length])
570 return StrippedName([theName stringByAppendingString:
@".anon-script"]);
573- (NSDictionary*)defaultPropertiesFromPath:(NSString*)path
576 NSString* manifestPath = [[[path stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"manifest.plist"];
578 NSMutableDictionary* properties = [NSMutableDictionary dictionaryWithCapacity:3];
583 [properties setObject:[manifest oo_stringForKey:kOOManifestVersion] forKey:@"version"];
587 [properties setObject:[manifest oo_stringForKey:kOOManifestIdentifier] forKey:kLocalManifestProperty];
590 [properties setObject:[manifest oo_stringForKey:kOOManifestAuthor] forKey:@"author"];
593 [properties setObject:[manifest oo_stringForKey:kOOManifestLicense] forKey:@"license"];
601@implementation OOScript (JavaScriptEvents)
603- (BOOL)callMethod:(jsid)methodID
604 inContext:(JSContext*)context
605 withArguments:(jsval*)argv
607 result:(jsval*)outResult
623 if (JSID_IS_STRING(propID)) {
624 JSString* propName = JSID_TO_STRING(propID);
626 if (JS_StringEqualsAscii(context, propName,
"tickle", &match) && match) {
628 OOJSReportWarning(context,
@"Script %@ appears to use the tickle() event handler, which is no longer supported.", [thisScript name]);
639 [array addObject:stack->current];
643static JSScript*
LoadScriptWithName(JSContext* context, NSString* path, JSObject*
object, JSObject** outScriptObject, NSString** outErrorMessage)
645#if OO_CACHE_JS_SCRIPTS
648 NSString* fileContents =
nil;
650 JSScript* script = NULL;
652 NSCParameterAssert(outScriptObject != NULL && outErrorMessage != NULL);
653 *outErrorMessage =
nil;
655#if OO_CACHE_JS_SCRIPTS
664 if (script == NULL) {
665 fileContents = [NSString stringWithContentsOfUnicodeFile:path];
667 if (fileContents !=
nil) {
668#ifdef OO_DEBUG_MONITOR_ENABLED
673 if ([fileContents rangeOfString:
@"\"use strict\
";"].location == NSNotFound && [fileContents rangeOfString:
@"'use strict';"].location == NSNotFound) {
678 fileContents = [@"\"use strict\";\n" stringByAppendingString:fileContents];
682 data = [fileContents utf16DataWithBOM:NO];
685 *outErrorMessage =
@"could not load file";
687 script = JS_CompileUCScript(context,
object, [data bytes], [data length] /
sizeof(unichar), [path UTF8String], 1);
689 *outScriptObject = JS_NewScriptObject(context, script);
691 *outErrorMessage =
@"compilation failed";
694#if OO_CACHE_JS_SCRIPTS
695 if (script != NULL) {
706#if OO_CACHE_JS_SCRIPTS
709 JSXDRState* xdr = NULL;
710 NSData* result =
nil;
714 xdr = JS_XDRNewMem(context, JSXDR_ENCODE);
716 if (JS_XDRScript(xdr, &script)) {
717 bytes = JS_XDRMemGetData(xdr, &length);
719 result = [NSData dataWithBytes:bytes length:length];
730 JSXDRState* xdr = NULL;
731 JSScript* result = NULL;
736 xdr = JS_XDRNewMem(context, JSXDR_DECODE);
738 NSUInteger length = [data length];
742 JS_XDRMemSetData(xdr, (
void*)[data bytes], (uint32_t)length);
743 if (!JS_XDRScript(xdr, &result))
746 JS_XDRMemSetData(xdr, NULL, 0);
756 static NSCharacterSet* invalidSet =
nil;
757 if (invalidSet ==
nil)
758 invalidSet = [[NSCharacterSet characterSetWithCharactersInString:@"_ \t\n\r\v"] retain];
760 return [string stringByTrimmingCharactersInSet:invalidSet];
void OOStandardsDeprecated(NSString *message)
BOOL OOEnforceStandards(void)
#define foreachkey(VAR, DICT)
id OOConsumeReference(id OO_NS_CONSUMED value)
#define OOJSStopTimeLimiter()
#define OOJSStartTimeLimiter()
#define kOOJSLongTimeLimit
#define OOJSStartTimeLimiterWithTimeLimit(limit)
void InitOOJSScript(JSContext *context, JSObject *global)
static NSString *const kLocalManifestProperty
static NSString * StrippedName(NSString *string)
static JSBool ScriptAddProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static NSData * CompiledScriptData(JSContext *context, JSScript *script)
static JSObject * sScriptPrototype
static RunningStack * sRunningStack
static JSFunctionSpec sScriptMethods[]
struct RunningStack RunningStack
static JSClass sScriptClass
static JSScript * LoadScriptWithName(JSContext *context, NSString *path, JSObject *object, JSObject **outScriptObject, NSString **outErrorMessage)
static void AddStackToArrayReversed(NSMutableArray *array, RunningStack *stack)
static JSScript * ScriptWithCompiledData(JSContext *context, NSData *data)
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
void OOJSReportWarning(JSContext *context, NSString *format,...)
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
#define JS_IsInRequest(context)
NSString * OOStringFromJSID(jsid propID)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
OOINLINE JSContext * OOJSAcquireContext(void)
#define OOJSAddGCObjectRoot(context, root, name)
OOINLINE void OOJSRelinquishContext(JSContext *context)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
id OOJSNativeObjectOfClassFromJSObject(JSContext *context, JSObject *object, Class requiredClass)
#define OOLogERR(class, format,...)
#define OOLogOutdentIf(class)
#define OOLog(class, format,...)
#define OOLogIndentIf(class)
static NSString *const kOOManifestLicense
static NSString *const kOOManifestIdentifier
static NSString *const kOOManifestVersion
static NSString *const kOOManifestAuthor
NSDictionary * OODictionaryFromFile(NSString *path)
static NSString * StrippedName(NSString *string)
void setObject:forKey:inCache:(id inElement,[forKey] NSString *inKey,[inCache] NSString *inCacheKey)
id objectForKey:inCache:(NSString *inKey,[inCache] NSString *inCacheKey)
OOCacheManager * sharedCache()
OOWeakReference * weakSelf
NSString * oo_jsClassName()
NSString * descriptionComponents()
NSString * scriptDescription()
OOJavaScriptEngine * sharedEngine()
id weakRefWithObject:(id< OOWeakReferenceSupport > object)