|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "nsAutoPtr.h" |
|
8 |
|
9 #include "jsapi.h" |
|
10 #include "js/OldDebugAPI.h" |
|
11 |
|
12 #include "nsJSPrincipals.h" |
|
13 |
|
14 #include "mozilla/scache/StartupCache.h" |
|
15 |
|
16 using namespace JS; |
|
17 using namespace mozilla::scache; |
|
18 |
|
19 // We only serialize scripts with system principals. So we don't serialize the |
|
20 // principals when writing a script. Instead, when reading it back, we set the |
|
21 // principals to the system principals. |
|
22 nsresult |
|
23 ReadCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx, |
|
24 nsIPrincipal *systemPrincipal, MutableHandleScript scriptp) |
|
25 { |
|
26 nsAutoArrayPtr<char> buf; |
|
27 uint32_t len; |
|
28 nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), |
|
29 getter_Transfers(buf), &len); |
|
30 if (NS_FAILED(rv)) |
|
31 return rv; // don't warn since NOT_AVAILABLE is an ok error |
|
32 |
|
33 scriptp.set(JS_DecodeScript(cx, buf, len, nullptr)); |
|
34 if (!scriptp) |
|
35 return NS_ERROR_OUT_OF_MEMORY; |
|
36 return NS_OK; |
|
37 } |
|
38 |
|
39 nsresult |
|
40 ReadCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx, |
|
41 nsIPrincipal *systemPrincipal, JSFunction **functionp) |
|
42 { |
|
43 return NS_ERROR_FAILURE; |
|
44 /* This doesn't actually work ... |
|
45 nsAutoArrayPtr<char> buf; |
|
46 uint32_t len; |
|
47 nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), |
|
48 getter_Transfers(buf), &len); |
|
49 if (NS_FAILED(rv)) |
|
50 return rv; // don't warn since NOT_AVAILABLE is an ok error |
|
51 |
|
52 JSObject *obj = JS_DecodeInterpretedFunction(cx, buf, len, nsJSPrincipals::get(systemPrincipal), nullptr); |
|
53 if (!obj) |
|
54 return NS_ERROR_OUT_OF_MEMORY; |
|
55 JSFunction* function = JS_ValueToFunction(cx, OBJECT_TO_JSVAL(obj)); |
|
56 *functionp = function; |
|
57 return NS_OK;*/ |
|
58 } |
|
59 |
|
60 nsresult |
|
61 WriteCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx, |
|
62 nsIPrincipal *systemPrincipal, HandleScript script) |
|
63 { |
|
64 MOZ_ASSERT(JS_GetScriptPrincipals(script) == nsJSPrincipals::get(systemPrincipal)); |
|
65 MOZ_ASSERT(JS_GetScriptOriginPrincipals(script) == nsJSPrincipals::get(systemPrincipal)); |
|
66 |
|
67 uint32_t size; |
|
68 void *data = JS_EncodeScript(cx, script, &size); |
|
69 if (!data) |
|
70 return NS_ERROR_OUT_OF_MEMORY; |
|
71 |
|
72 MOZ_ASSERT(size); |
|
73 nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast<char *>(data), size); |
|
74 js_free(data); |
|
75 return rv; |
|
76 } |
|
77 |
|
78 nsresult |
|
79 WriteCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx, |
|
80 nsIPrincipal *systemPrincipal, JSFunction *function) |
|
81 { |
|
82 return NS_ERROR_FAILURE; |
|
83 /* This doesn't actually work ... |
|
84 uint32_t size; |
|
85 void *data = |
|
86 JS_EncodeInterpretedFunction(cx, JS_GetFunctionObject(function), &size); |
|
87 if (!data) |
|
88 return NS_ERROR_OUT_OF_MEMORY; |
|
89 |
|
90 MOZ_ASSERT(size); |
|
91 nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast<char *>(data), size); |
|
92 js_free(data); |
|
93 return rv;*/ |
|
94 } |