Line data Source code
1 0 : /* 2 : 3 : OOJSScript.h 4 : 5 : JavaScript support for Oolite 6 : Copyright (C) 2007-2013 David Taylor and Jens Ayton. 7 : 8 : This program is free software; you can redistribute it and/or 9 : modify it under the terms of the GNU General Public License 10 : as published by the Free Software Foundation; either version 2 11 : of the License, or (at your option) any later version. 12 : 13 : This program is distributed in the hope that it will be useful, 14 : but WITHOUT ANY WARRANTY; without even the implied warranty of 15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 : GNU General Public License for more details. 17 : 18 : You should have received a copy of the GNU General Public License 19 : along with this program; if not, write to the Free Software 20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 21 : MA 02110-1301, USA. 22 : 23 : */ 24 : 25 : 26 : #import "OOScript.h" 27 : #import "OOJavaScriptEngine.h" 28 : 29 0 : static NSString * const kLocalManifestProperty = @"oolite_manifest_identifier"; 30 : 31 0 : @interface OOJSScript: OOScript <OOWeakReferenceSupport> 32 : { 33 : @private 34 : JSObject *_jsSelf; 35 0 : 36 : NSString *name; 37 0 : NSString *description; 38 0 : NSString *version; 39 0 : NSString *filePath; 40 0 : 41 : OOWeakReference *weakSelf; 42 0 : } 43 : 44 : + (id) scriptWithPath:(NSString *)path properties:(NSDictionary *)properties; 45 0 : 46 : - (id) initWithPath:(NSString *)path properties:(NSDictionary *)properties; 47 0 : 48 : + (OOJSScript *) currentlyRunningScript; 49 0 : + (NSArray *) scriptStack; 50 0 : 51 : /* External manipulation of acrtive script stack. Used, for instance, by 52 : timers. Failing to balance these will crash! 53 : Passing a nil script is valid for cases where JS is used which is not 54 : attached to a specific script. 55 : */ 56 : + (void) pushScript:(OOJSScript *)script; 57 0 : + (void) popScript:(OOJSScript *)script; 58 0 : 59 : /* Call a method. 60 : Requires a request on context. 61 : outResult may be NULL. 62 : */ 63 : - (BOOL) callMethod:(jsid)methodID 64 0 : inContext:(JSContext *)context 65 : withArguments:(jsval *)argv count:(intN)argc 66 : result:(jsval *)outResult; 67 : 68 : - (id) propertyWithID:(jsid)propID inContext:(JSContext *)context; 69 0 : // Set a property which can be modified or deleted by the script. 70 : - (BOOL) setProperty:(id)value withID:(jsid)propID inContext:(JSContext *)context; 71 0 : // Set a special property which cannot be modified or deleted by the script. 72 : - (BOOL) defineProperty:(id)value withID:(jsid)propID inContext:(JSContext *)context; 73 0 : 74 : - (id) propertyNamed:(NSString *)name; 75 0 : - (BOOL) setProperty:(id)value named:(NSString *)name; 76 0 : - (BOOL) defineProperty:(id)value named:(NSString *)name; 77 0 : 78 : @end 79 : 80 : 81 : @interface OOScript (JavaScriptEvents) 82 : 83 : // For simplicity, calling methods on non-JS scripts works but does nothing. 84 : - (BOOL) callMethod:(jsid)methodID 85 0 : inContext:(JSContext *)context 86 : withArguments:(jsval *)argv count:(intN)argc 87 : result:(jsval *)outResult; 88 : 89 : @end 90 : 91 : 92 : void InitOOJSScript(JSContext *context, JSObject *global); 93 0 :