michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=8 sts=4 et sw=4 tw=99: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAutoPtr.h" michael@0: michael@0: #include "jsapi.h" michael@0: #include "js/OldDebugAPI.h" michael@0: michael@0: #include "nsJSPrincipals.h" michael@0: michael@0: #include "mozilla/scache/StartupCache.h" michael@0: michael@0: using namespace JS; michael@0: using namespace mozilla::scache; michael@0: michael@0: // We only serialize scripts with system principals. So we don't serialize the michael@0: // principals when writing a script. Instead, when reading it back, we set the michael@0: // principals to the system principals. michael@0: nsresult michael@0: ReadCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx, michael@0: nsIPrincipal *systemPrincipal, MutableHandleScript scriptp) michael@0: { michael@0: nsAutoArrayPtr buf; michael@0: uint32_t len; michael@0: nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), michael@0: getter_Transfers(buf), &len); michael@0: if (NS_FAILED(rv)) michael@0: return rv; // don't warn since NOT_AVAILABLE is an ok error michael@0: michael@0: scriptp.set(JS_DecodeScript(cx, buf, len, nullptr)); michael@0: if (!scriptp) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: ReadCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx, michael@0: nsIPrincipal *systemPrincipal, JSFunction **functionp) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: /* This doesn't actually work ... michael@0: nsAutoArrayPtr buf; michael@0: uint32_t len; michael@0: nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), michael@0: getter_Transfers(buf), &len); michael@0: if (NS_FAILED(rv)) michael@0: return rv; // don't warn since NOT_AVAILABLE is an ok error michael@0: michael@0: JSObject *obj = JS_DecodeInterpretedFunction(cx, buf, len, nsJSPrincipals::get(systemPrincipal), nullptr); michael@0: if (!obj) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: JSFunction* function = JS_ValueToFunction(cx, OBJECT_TO_JSVAL(obj)); michael@0: *functionp = function; michael@0: return NS_OK;*/ michael@0: } michael@0: michael@0: nsresult michael@0: WriteCachedScript(StartupCache* cache, nsACString &uri, JSContext *cx, michael@0: nsIPrincipal *systemPrincipal, HandleScript script) michael@0: { michael@0: MOZ_ASSERT(JS_GetScriptPrincipals(script) == nsJSPrincipals::get(systemPrincipal)); michael@0: MOZ_ASSERT(JS_GetScriptOriginPrincipals(script) == nsJSPrincipals::get(systemPrincipal)); michael@0: michael@0: uint32_t size; michael@0: void *data = JS_EncodeScript(cx, script, &size); michael@0: if (!data) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: MOZ_ASSERT(size); michael@0: nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast(data), size); michael@0: js_free(data); michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: WriteCachedFunction(StartupCache* cache, nsACString &uri, JSContext *cx, michael@0: nsIPrincipal *systemPrincipal, JSFunction *function) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: /* This doesn't actually work ... michael@0: uint32_t size; michael@0: void *data = michael@0: JS_EncodeInterpretedFunction(cx, JS_GetFunctionObject(function), &size); michael@0: if (!data) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: MOZ_ASSERT(size); michael@0: nsresult rv = cache->PutBuffer(PromiseFlatCString(uri).get(), static_cast(data), size); michael@0: js_free(data); michael@0: return rv;*/ michael@0: }