Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/js/xpconnect/src/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 3 kB image not shown  

Quelle  XPCString.cpp   Sprache: C

 
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


/*
 * Infrastructure for sharing DOMString data with JSStrings.
 *
 * Importing an nsAString into JS:
 * If possible (GetSharedBufferHandle works) use the external string support in
 * JS to create a JSString that points to the readable's buffer.  We keep a
 * reference to the buffer handle until the JSString is finalized.
 *
 * Exporting a JSString as an nsAReadable:
 * Wrap the JSString with a root-holding XPCJSReadableStringWrapper, which roots
 * the string and exposes its buffer via the nsAString interface, as
 * well as providing refcounting support.
 */


#include "nscore.h"
#include "nsString.h"
#include "mozilla/StringBuffer.h"
#include "jsapi.h"
#include "xpcpublic.h"

using namespace JS;
using mozilla::StringBuffer;

const XPCStringConvert::LiteralExternalString
    XPCStringConvert::sLiteralExternalString;

void XPCStringConvert::LiteralExternalString::finalize(
    JS::Latin1Char* aChars) const {
  // Nothing to do.
}

void XPCStringConvert::LiteralExternalString::finalize(char16_t* aChars) const {
  // Nothing to do.
}

size_t XPCStringConvert::LiteralExternalString::sizeOfBuffer(
    const JS::Latin1Char* aChars, mozilla::MallocSizeOf aMallocSizeOf) const {
  // This string's buffer is not heap-allocated, so its malloc size is 0.
  return 0;
}

size_t XPCStringConvert::LiteralExternalString::sizeOfBuffer(
    const char16_t* aChars, mozilla::MallocSizeOf aMallocSizeOf) const {
  // This string's buffer is not heap-allocated, so its malloc size is 0.
  return 0;
}

// convert a string to a JSString, either copying or sharing string data
// static
bool xpc::NonVoidStringToJsval(JSContext* cx, const nsAString& readable,
                               MutableHandleValue vp) {
  uint32_t length = readable.Length();

  if (readable.IsLiteral()) {
    return XPCStringConvert::StringLiteralToJSVal(cx, readable.BeginReading(),
                                                  length, vp);
  }

  if (StringBuffer* buf = readable.GetStringBuffer()) {
    return XPCStringConvert::UCStringBufferToJSVal(cx, buf, length, vp);
  }

  // blech, have to copy.
  JSString* str = JS_NewUCStringCopyN(cx, readable.BeginReading(), length);
  if (!str) {
    return false;
  }
  vp.setString(str);
  return true;
}

bool xpc::NonVoidLatin1StringToJsval(JSContext* cx, const nsACString& latin1,
                                     MutableHandleValue vp) {
  uint32_t length = latin1.Length();

  if (latin1.IsLiteral()) {
    return XPCStringConvert::StringLiteralToJSVal(
        cx, reinterpret_cast<const JS::Latin1Char*>(latin1.BeginReading()),
        length, vp);
  }

  if (StringBuffer* buf = latin1.GetStringBuffer()) {
    return XPCStringConvert::Latin1StringBufferToJSVal(cx, buf, length, vp);
  }

  JSString* str = JS_NewStringCopyN(cx, latin1.BeginReading(), length);
  if (!str) {
    return false;
  }
  vp.setString(str);
  return true;
}

bool xpc::NonVoidUTF8StringToJsval(JSContext* cx, const nsACString& utf8,
                                   MutableHandleValue vp) {
  uint32_t length = utf8.Length();

  if (utf8.IsLiteral()) {
    return XPCStringConvert::UTF8StringLiteralToJSVal(
        cx, JS::UTF8Chars(utf8.BeginReading(), length), vp);
  }

  if (StringBuffer* buf = utf8.GetStringBuffer()) {
    return XPCStringConvert::UTF8StringBufferToJSVal(cx, buf, length, vp);
  }

  JSString* str =
      JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(utf8.BeginReading(), length));
  if (!str) {
    return false;
  }
  vp.setString(str);
  return true;
}

Messung V0.5
C=90 H=93 G=91

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.