Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | * Infrastructure for sharing DOMString data with JSStrings. |
michael@0 | 9 | * |
michael@0 | 10 | * Importing an nsAString into JS: |
michael@0 | 11 | * If possible (GetSharedBufferHandle works) use the external string support in |
michael@0 | 12 | * JS to create a JSString that points to the readable's buffer. We keep a |
michael@0 | 13 | * reference to the buffer handle until the JSString is finalized. |
michael@0 | 14 | * |
michael@0 | 15 | * Exporting a JSString as an nsAReadable: |
michael@0 | 16 | * Wrap the JSString with a root-holding XPCJSReadableStringWrapper, which roots |
michael@0 | 17 | * the string and exposes its buffer via the nsAString interface, as |
michael@0 | 18 | * well as providing refcounting support. |
michael@0 | 19 | */ |
michael@0 | 20 | |
michael@0 | 21 | #include "nscore.h" |
michael@0 | 22 | #include "nsString.h" |
michael@0 | 23 | #include "nsStringBuffer.h" |
michael@0 | 24 | #include "jsapi.h" |
michael@0 | 25 | #include "xpcpublic.h" |
michael@0 | 26 | |
michael@0 | 27 | using namespace JS; |
michael@0 | 28 | |
michael@0 | 29 | // static |
michael@0 | 30 | void |
michael@0 | 31 | XPCStringConvert::FreeZoneCache(JS::Zone *zone) |
michael@0 | 32 | { |
michael@0 | 33 | // Put the zone user data into an AutoPtr (which will do the cleanup for us), |
michael@0 | 34 | // and null out the user data (which may already be null). |
michael@0 | 35 | nsAutoPtr<ZoneStringCache> cache(static_cast<ZoneStringCache*>(JS_GetZoneUserData(zone))); |
michael@0 | 36 | JS_SetZoneUserData(zone, nullptr); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | // static |
michael@0 | 40 | void |
michael@0 | 41 | XPCStringConvert::ClearZoneCache(JS::Zone *zone) |
michael@0 | 42 | { |
michael@0 | 43 | ZoneStringCache *cache = static_cast<ZoneStringCache*>(JS_GetZoneUserData(zone)); |
michael@0 | 44 | if (cache) { |
michael@0 | 45 | cache->mBuffer = nullptr; |
michael@0 | 46 | cache->mString = nullptr; |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | // static |
michael@0 | 51 | void |
michael@0 | 52 | XPCStringConvert::FinalizeLiteral(const JSStringFinalizer *fin, jschar *chars) |
michael@0 | 53 | { |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | const JSStringFinalizer XPCStringConvert::sLiteralFinalizer = |
michael@0 | 57 | { XPCStringConvert::FinalizeLiteral }; |
michael@0 | 58 | |
michael@0 | 59 | // static |
michael@0 | 60 | void |
michael@0 | 61 | XPCStringConvert::FinalizeDOMString(const JSStringFinalizer *fin, jschar *chars) |
michael@0 | 62 | { |
michael@0 | 63 | nsStringBuffer* buf = nsStringBuffer::FromData(chars); |
michael@0 | 64 | buf->Release(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | const JSStringFinalizer XPCStringConvert::sDOMStringFinalizer = |
michael@0 | 68 | { XPCStringConvert::FinalizeDOMString }; |
michael@0 | 69 | |
michael@0 | 70 | // convert a readable to a JSString, copying string data |
michael@0 | 71 | // static |
michael@0 | 72 | bool |
michael@0 | 73 | XPCStringConvert::ReadableToJSVal(JSContext *cx, |
michael@0 | 74 | const nsAString &readable, |
michael@0 | 75 | nsStringBuffer** sharedBuffer, |
michael@0 | 76 | MutableHandleValue vp) |
michael@0 | 77 | { |
michael@0 | 78 | *sharedBuffer = nullptr; |
michael@0 | 79 | |
michael@0 | 80 | uint32_t length = readable.Length(); |
michael@0 | 81 | |
michael@0 | 82 | if (readable.IsLiteral()) { |
michael@0 | 83 | JSString *str = JS_NewExternalString(cx, |
michael@0 | 84 | static_cast<const jschar*>(readable.BeginReading()), |
michael@0 | 85 | length, &sLiteralFinalizer); |
michael@0 | 86 | if (!str) |
michael@0 | 87 | return false; |
michael@0 | 88 | vp.setString(str); |
michael@0 | 89 | return true; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | nsStringBuffer *buf = nsStringBuffer::FromString(readable); |
michael@0 | 93 | if (buf) { |
michael@0 | 94 | bool shared; |
michael@0 | 95 | if (!StringBufferToJSVal(cx, buf, length, vp, &shared)) |
michael@0 | 96 | return false; |
michael@0 | 97 | if (shared) |
michael@0 | 98 | *sharedBuffer = buf; |
michael@0 | 99 | return true; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | // blech, have to copy. |
michael@0 | 103 | JSString *str = JS_NewUCStringCopyN(cx, readable.BeginReading(), length); |
michael@0 | 104 | if (!str) |
michael@0 | 105 | return false; |
michael@0 | 106 | vp.setString(str); |
michael@0 | 107 | return true; |
michael@0 | 108 | } |