JSScript should have an accessor saying if it's cached or not
authorsbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Mar 2019 01:28:49 +0000 (01:28 +0000)
committersbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Mar 2019 01:28:49 +0000 (01:28 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195783

Reviewed by Michael Saboff.

* API/JSScript.h:
* API/JSScript.mm:
(-[JSScript isUsingBytecodeCache]):
* API/tests/testapi.mm:
(testIsUsingBytecodeCacheAccessor):
(testObjectiveCAPI):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242982 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/API/JSScript.h
Source/JavaScriptCore/API/JSScript.mm
Source/JavaScriptCore/API/tests/testapi.mm
Source/JavaScriptCore/ChangeLog

index adc5646..6a07110 100644 (file)
@@ -100,6 +100,12 @@ JSC_CLASS_AVAILABLE(macosx(JSC_MAC_TBA), ios(JSC_IOS_TBA))
 
 /*!
  @method
+ @abstract Returns true when evaluating this JSScript will use the bytecode cache. Returns false otherwise.
+ */
+- (BOOL)isUsingBytecodeCache;
+
+/*!
+ @method
  @abstract Returns the JSScriptType of this JSScript.
  */
 - (JSScriptType)type;
index e5039ad..6471277 100644 (file)
@@ -220,6 +220,11 @@ static JSScript *createError(NSString *message, NSError** error)
     return YES;
 }
 
+- (BOOL)isUsingBytecodeCache
+{
+    return !!m_cachedBytecode.size();
+}
+
 - (NSURL *)sourceURL
 {
     return m_sourceURL.get();
index 4eb6ec4..5ad486f 100644 (file)
@@ -2237,6 +2237,41 @@ static void testCanCacheManyFilesWithTheSameVM()
     checkResult(@"Removed all cache files", removedAll);
 }
 
+static void testIsUsingBytecodeCacheAccessor()
+{
+    NSURL* cachePath = tempFile(@"foo.program.cache");
+    NSURL* sourceURL = [NSURL URLWithString:@"my-path"];
+    NSString *source = @"function foo() { return 1337; } foo();";
+
+    @autoreleasepool {
+        JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
+        JSContext* context = [[JSContext alloc] initWithVirtualMachine:vm];
+        JSScript *script = [JSScript scriptOfType:kJSScriptTypeProgram withSource:source andSourceURL:sourceURL andBytecodeCache:cachePath inVirtualMachine:vm error:nil];
+        RELEASE_ASSERT(script);
+        checkResult(@"Should not yet be using the bytecode cache", ![script isUsingBytecodeCache]);
+        checkResult(@"Should be able to cache the script", [script cacheBytecodeWithError:nil]);
+        checkResult(@"Should now using the bytecode cache", [script isUsingBytecodeCache]);
+        JSC::Options::forceDiskCache() = true;
+        JSValue *result = [context evaluateJSScript:script];
+        JSC::Options::forceDiskCache() = false;
+        checkResult(@"Result should be 1337", [result isNumber] && [result toInt32] == 1337);
+    }
+
+    @autoreleasepool {
+        JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
+        JSContext* context = [[JSContext alloc] initWithVirtualMachine:vm];
+        JSScript *script = [JSScript scriptOfType:kJSScriptTypeProgram withSource:source andSourceURL:sourceURL andBytecodeCache:cachePath inVirtualMachine:vm error:nil];
+        RELEASE_ASSERT(script);
+        checkResult(@"Should be using the bytecode cache", [script isUsingBytecodeCache]);
+        JSValue *result = [context evaluateJSScript:script];
+        checkResult(@"Result should be 1337", [result isNumber] && [result toInt32] == 1337);
+    }
+
+    NSFileManager* fileManager = [NSFileManager defaultManager];
+    BOOL removedAll = [fileManager removeItemAtURL:cachePath error:nil];
+    checkResult(@"Successfully removed cache file", removedAll);
+}
+
 @interface JSContextFileLoaderDelegate : JSContext <JSModuleLoaderDelegate>
 
 + (instancetype)newContext;
@@ -2446,6 +2481,7 @@ void testObjectiveCAPI(const char* filter)
     RUN(testProgramJSScriptException());
     RUN(testCacheFileFailsWhenItsAlreadyCached());
     RUN(testCanCacheManyFilesWithTheSameVM());
+    RUN(testIsUsingBytecodeCacheAccessor());
 
     RUN(testLoaderRejectsNilScriptURL());
     RUN(testLoaderRejectsFailedFetch());
index 4f15643..e42ac9c 100644 (file)
@@ -1,5 +1,19 @@
 2019-03-14  Saam barati  <sbarati@apple.com>
 
+        JSScript should have an accessor saying if it's cached or not
+        https://bugs.webkit.org/show_bug.cgi?id=195783
+
+        Reviewed by Michael Saboff.
+
+        * API/JSScript.h:
+        * API/JSScript.mm:
+        (-[JSScript isUsingBytecodeCache]):
+        * API/tests/testapi.mm:
+        (testIsUsingBytecodeCacheAccessor):
+        (testObjectiveCAPI):
+
+2019-03-14  Saam barati  <sbarati@apple.com>
+
         Remove retain cycle from JSScript and also don't keep the cache file descriptor open so many JSScripts can be cached in a loop
         https://bugs.webkit.org/show_bug.cgi?id=195782
         <rdar://problem/48880625>