JSScript.h
1 /* 2 * Copyright (C) 2019 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #import <JavaScriptCore/JSValue.h> 27 28 #if JSC_OBJC_API_ENABLED 29 30 #if defined(DARLING) && __i386__ 31 #import <wtf/WeakObjCPtr.h> 32 #import <wtf/FileSystem.h> 33 #import "CachedBytecode.h" 34 #endif 35 36 NS_ASSUME_NONNULL_BEGIN 37 38 @class JSVirtualMachine; 39 40 /*! 41 @enum JSScriptType 42 @abstract A constant identifying the execution type of a JSScript. 43 @constant kJSScriptTypeProgram The type of a normal JavaScript program. 44 @constant kJSScriptTypeModule The type of a module JavaScript program. 45 */ 46 typedef NS_ENUM(NSInteger, JSScriptType) { 47 kJSScriptTypeProgram, 48 kJSScriptTypeModule, 49 }; 50 51 52 JSC_CLASS_AVAILABLE(macos(10.15), ios(13.0)) 53 #if defined(DARLING) && __i386__ 54 @interface JSScript : NSObject { 55 WeakObjCPtr<JSVirtualMachine> m_virtualMachine; 56 JSScriptType m_type; 57 FileSystem::MappedFileData m_mappedSource; 58 String m_source; 59 RetainPtr<NSURL> m_sourceURL; 60 RetainPtr<NSURL> m_cachePath; 61 RefPtr<JSC::CachedBytecode> m_cachedBytecode; 62 } 63 #else 64 @interface JSScript : NSObject 65 #endif 66 67 /*! 68 @method 69 @abstract Create a JSScript for the specified virtual machine. 70 @param type The type of JavaScript source. 71 @param source The source code to use when the script is evaluated by the JS vm. 72 @param sourceURL The source URL to associate with this script. For modules, this is the module identifier. 73 @param cachePath A URL containing the path where the VM should cache for future execution. On creation, we use this path to load the cached bytecode off disk. If the cached bytecode at this location is stale, you should delete that file before calling this constructor. 74 @param vm The JSVirtualMachine the script can be evaluated in. 75 @param error A description of why the script could not be created if the result is nil. 76 @result The new script. 77 @discussion The file at cachePath should not be externally modified for the lifecycle of vm. 78 */ 79 + (nullable instancetype)scriptOfType:(JSScriptType)type withSource:(NSString *)source andSourceURL:(NSURL *)sourceURL andBytecodeCache:(nullable NSURL *)cachePath inVirtualMachine:(JSVirtualMachine *)vm error:(out NSError * _Nullable * _Nullable)error; 80 81 /*! 82 @method 83 @abstract Create a JSScript for the specified virtual machine with a path to a codesigning and bytecode caching. 84 @param type The type of JavaScript source. 85 @param filePath A URL containing the path to a JS source code file on disk. 86 @param sourceURL The source URL to associate with this script. For modules, this is the module identifier. 87 @param cachePath A URL containing the path where the VM should cache for future execution. On creation, we use this path to load the cached bytecode off disk. If the cached bytecode at this location is stale, you should delete that file before calling this constructor. 88 @param vm The JSVirtualMachine the script can be evaluated in. 89 @param error A description of why the script could not be created if the result is nil. 90 @result The new script. 91 @discussion The files at filePath and cachePath should not be externally modified for the lifecycle of vm. This method will file back the memory for the source. 92 93 If the file at filePath is not ascii this method will return nil. 94 */ 95 + (nullable instancetype)scriptOfType:(JSScriptType)type memoryMappedFromASCIIFile:(NSURL *)filePath withSourceURL:(NSURL *)sourceURL andBytecodeCache:(nullable NSURL *)cachePath inVirtualMachine:(JSVirtualMachine *)vm error:(out NSError * _Nullable * _Nullable)error; 96 97 /*! 98 @method 99 @abstract Cache the bytecode for this JSScript to disk at the path passed in during creation. 100 @param error A description of why the script could not be cached if the result is FALSE. 101 */ 102 - (BOOL)cacheBytecodeWithError:(out NSError * _Nullable * _Nullable)error; 103 104 /*! 105 @method 106 @abstract Returns true when evaluating this JSScript will use the bytecode cache. Returns false otherwise. 107 */ 108 - (BOOL)isUsingBytecodeCache; 109 110 /*! 111 @method 112 @abstract Returns the JSScriptType of this JSScript. 113 */ 114 - (JSScriptType)type; 115 116 /*! 117 @method 118 @abstract Returns the sourceURL of this JSScript. 119 */ 120 - (NSURL *)sourceURL; 121 122 @end 123 124 NS_ASSUME_NONNULL_END 125 126 #endif // JSC_OBJC_API_ENABLED