Bug 1486577 - Move JS::SourceBufferHolder into a new public js/SourceBufferHolder.h header. r=jandem
authorJeff Walden <jwalden@mit.edu>
Fri, 24 Aug 2018 14:00:35 -0500
changeset 491468 ea8351c63f54a43a568804e81b8a5a237d1f8084
parent 491467 31b77a399fcf7cd458ee255053dfc4e98392d1f0
child 491469 daa263f9eda35ced064b6c12626249c12427311d
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjandem
bugs1486577
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1486577 - Move JS::SourceBufferHolder into a new public js/SourceBufferHolder.h header. r=jandem
js/public/SourceBufferHolder.h
js/rust/etc/wrapper.hpp
js/src/jsapi.h
js/src/moz.build
new file mode 100644
--- /dev/null
+++ b/js/public/SourceBufferHolder.h
@@ -0,0 +1,140 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* 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/. */
+
+/*
+ * SourceBufferHolder groups buffer and length values and provides a way to
+ * optionally pass ownership of the buffer to the JS engine without copying.
+ *
+ * Rules for use:
+ *
+ *  1) The data array must be allocated with js_malloc() or js_realloc() if
+ *     ownership is being granted to the SourceBufferHolder.
+ *  2) If ownership is not given to the SourceBufferHolder, then the memory
+ *     must be kept alive until the JS compilation is complete.
+ *  3) Any code calling SourceBufferHolder::take() must guarantee to keep the
+ *     memory alive until JS compilation completes.  Normally only the JS
+ *     engine should be calling take().
+ *
+ * Example use:
+ *
+ *    size_t length = 512;
+ *    char16_t* chars = js_pod_malloc<char16_t>(length);
+ *    JS::SourceBufferHolder srcBuf(chars, length, JS::SourceBufferHolder::GiveOwnership);
+ *    JS::Compile(cx, options, srcBuf);
+ */
+
+#ifndef js_SourceBufferHolder_h
+#define js_SourceBufferHolder_h
+
+#include "mozilla/Assertions.h" // MOZ_ASSERT
+
+#include <stddef.h> // size_t
+
+#include "js/Utility.h" // JS::UniqueTwoByteChars
+
+namespace JS {
+
+class SourceBufferHolder final
+{
+  private:
+    const char16_t* data_;
+    size_t length_;
+    bool ownsChars_;
+
+  private:
+    void fixEmptyBuffer() {
+        // Ensure that null buffers properly return an unowned, empty,
+        // null-terminated string.
+        static const char16_t NullChar_ = 0;
+        if (!data_) {
+            data_ = &NullChar_;
+            length_ = 0;
+            ownsChars_ = false;
+        }
+    }
+
+  public:
+    enum Ownership {
+      NoOwnership,
+      GiveOwnership
+    };
+
+    SourceBufferHolder(const char16_t* data, size_t dataLength, Ownership ownership)
+      : data_(data),
+        length_(dataLength),
+        ownsChars_(ownership == GiveOwnership)
+    {
+        fixEmptyBuffer();
+    }
+
+    SourceBufferHolder(UniqueTwoByteChars&& data, size_t dataLength)
+      : data_(data.release()),
+        length_(dataLength),
+        ownsChars_(true)
+    {
+        fixEmptyBuffer();
+    }
+
+    SourceBufferHolder(SourceBufferHolder&& other)
+      : data_(other.data_),
+        length_(other.length_),
+        ownsChars_(other.ownsChars_)
+    {
+        other.data_ = nullptr;
+        other.length_ = 0;
+        other.ownsChars_ = false;
+    }
+
+    ~SourceBufferHolder() {
+        if (ownsChars_)
+            js_free(const_cast<char16_t*>(data_));
+    }
+
+    /** Access the underlying source buffer without affecting ownership. */
+    const char16_t* get() const {
+        return data_;
+    }
+
+    /** Length of the source buffer in char16_t code units (not bytes). */
+    size_t length() const {
+        return length_;
+    }
+
+    /**
+     * Returns true if the SourceBufferHolder owns the buffer and will free it
+     * upon destruction.  If true, it is legal to call take().
+     */
+    bool ownsChars() const {
+        return ownsChars_;
+    }
+
+    /**
+     * Retrieve and take ownership of the underlying data buffer.  The caller
+     * is now responsible for calling js_free() on the returned value, *but
+     * only after JS script compilation has completed*.
+     *
+     * After the buffer has been taken the SourceBufferHolder functions as if
+     * it had been constructed on an unowned buffer;  get() and length() still
+     * work.  In order for this to be safe the taken buffer must be kept alive
+     * until after JS script compilation completes as noted above.
+     *
+     * It's the caller's responsibility to check ownsChars() before taking the
+     * buffer.  Taking and then free'ing an unowned buffer will have dire
+     * consequences.
+     */
+    char16_t* take() {
+        MOZ_ASSERT(ownsChars_);
+        ownsChars_ = false;
+        return const_cast<char16_t*>(data_);
+    }
+
+  private:
+    SourceBufferHolder(SourceBufferHolder&) = delete;
+    SourceBufferHolder& operator=(SourceBufferHolder&) = delete;
+};
+
+} // namespace JS
+
+#endif /* js_SourceBufferHolder_h */
--- a/js/rust/etc/wrapper.hpp
+++ b/js/rust/etc/wrapper.hpp
@@ -12,15 +12,16 @@
 typedef uint32_t HashNumber;
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "js/CompileOptions.h"
 #include "js/Conversions.h"
 #include "js/Initialization.h"
 #include "js/MemoryMetrics.h"
+#include "js/SourceBufferHolder.h"
 #include "js/StructuredClone.h"
 
 // Replacements for types that are too difficult for rust-bindgen.
 
 /// <div rustbindgen replaces="JS::detail::MaybeWrapped" />
 template <typename T>
 using replaces_MaybeWrapped = T;
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -33,16 +33,17 @@
 #include "js/GCVector.h"
 #include "js/HashTable.h"
 #include "js/Id.h"
 #include "js/MemoryFunctions.h"
 #include "js/Principals.h"
 #include "js/Realm.h"
 #include "js/RefCounted.h"
 #include "js/RootingAPI.h"
+#include "js/SourceBufferHolder.h"
 #include "js/Stream.h"
 #include "js/TracingAPI.h"
 #include "js/UniquePtr.h"
 #include "js/Utility.h"
 #include "js/Value.h"
 #include "js/Vector.h"
 
 /************************************************************************/
@@ -296,124 +297,16 @@ JS_NumberValue(double d)
 
 /************************************************************************/
 
 JS_PUBLIC_API(bool)
 JS_StringHasBeenPinned(JSContext* cx, JSString* str);
 
 namespace JS {
 
-/**
- * Container class for passing in script source buffers to the JS engine.  This
- * not only groups the buffer and length values, it also provides a way to
- * optionally pass ownership of the buffer to the JS engine without copying.
- * Rules for use:
- *
- *  1) The data array must be allocated with js_malloc() or js_realloc() if
- *     ownership is being granted to the SourceBufferHolder.
- *  2) If ownership is not given to the SourceBufferHolder, then the memory
- *     must be kept alive until the JS compilation is complete.
- *  3) Any code calling SourceBufferHolder::take() must guarantee to keep the
- *     memory alive until JS compilation completes.  Normally only the JS
- *     engine should be calling take().
- *
- * Example use:
- *
- *    size_t length = 512;
- *    char16_t* chars = js_pod_malloc<char16_t>(length);
- *    JS::SourceBufferHolder srcBuf(chars, length, JS::SourceBufferHolder::GiveOwnership);
- *    JS::Compile(cx, options, srcBuf);
- */
-class SourceBufferHolder final
-{
-  public:
-    enum Ownership {
-      NoOwnership,
-      GiveOwnership
-    };
-
-    SourceBufferHolder(const char16_t* data, size_t dataLength, Ownership ownership)
-      : data_(data),
-        length_(dataLength),
-        ownsChars_(ownership == GiveOwnership)
-    {
-        fixEmptyBuffer();
-    }
-
-    SourceBufferHolder(UniqueTwoByteChars&& data, size_t dataLength)
-      : data_(data.release()),
-        length_(dataLength),
-        ownsChars_(true)
-    {
-        fixEmptyBuffer();
-    }
-
-    SourceBufferHolder(SourceBufferHolder&& other)
-      : data_(other.data_),
-        length_(other.length_),
-        ownsChars_(other.ownsChars_)
-    {
-        other.data_ = nullptr;
-        other.length_ = 0;
-        other.ownsChars_ = false;
-    }
-
-    ~SourceBufferHolder() {
-        if (ownsChars_)
-            js_free(const_cast<char16_t*>(data_));
-    }
-
-    // Access the underlying source buffer without affecting ownership.
-    const char16_t* get() const { return data_; }
-
-    // Length of the source buffer in char16_t code units (not bytes)
-    size_t length() const { return length_; }
-
-    // Returns true if the SourceBufferHolder owns the buffer and will free
-    // it upon destruction.  If true, it is legal to call take().
-    bool ownsChars() const { return ownsChars_; }
-
-    // Retrieve and take ownership of the underlying data buffer.  The caller
-    // is now responsible for calling js_free() on the returned value, *but only
-    // after JS script compilation has completed*.
-    //
-    // After the buffer has been taken the SourceBufferHolder functions as if
-    // it had been constructed on an unowned buffer;  get() and length() still
-    // work.  In order for this to be safe the taken buffer must be kept alive
-    // until after JS script compilation completes as noted above.
-    //
-    // Note, it's the caller's responsibility to check ownsChars() before taking
-    // the buffer.  Taking and then free'ing an unowned buffer will have dire
-    // consequences.
-    char16_t* take() {
-        MOZ_ASSERT(ownsChars_);
-        ownsChars_ = false;
-        return const_cast<char16_t*>(data_);
-    }
-
-  private:
-    SourceBufferHolder(SourceBufferHolder&) = delete;
-    SourceBufferHolder& operator=(SourceBufferHolder&) = delete;
-
-    void fixEmptyBuffer() {
-        // Ensure that null buffers properly return an unowned, empty,
-        // null-terminated string.
-        static const char16_t NullChar_ = 0;
-        if (!get()) {
-            data_ = &NullChar_;
-            length_ = 0;
-            ownsChars_ = false;
-        }
-    }
-
-    const char16_t* data_;
-    size_t length_;
-    bool ownsChars_;
-};
-
 struct TranscodeSource;
 
 } /* namespace JS */
 
 /************************************************************************/
 
 /* Property attributes, set in JSPropertySpec and passed to API functions.
  *
--- a/js/src/moz.build
+++ b/js/src/moz.build
@@ -156,16 +156,17 @@ EXPORTS.js += [
     '../public/Proxy.h',
     '../public/Realm.h',
     '../public/RefCounted.h',
     '../public/RequiredDefines.h',
     '../public/Result.h',
     '../public/RootingAPI.h',
     '../public/SavedFrameAPI.h',
     '../public/SliceBudget.h',
+    '../public/SourceBufferHolder.h',
     '../public/StableStringChars.h',
     '../public/Stream.h',
     '../public/StructuredClone.h',
     '../public/SweepingAPI.h',
     '../public/TraceKind.h',
     '../public/TracingAPI.h',
     '../public/TrackedOptimizationInfo.h',
     '../public/TypeDecls.h',