Reviewed by Darin Adler.
authorzimmermann@webkit.org <zimmermann@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 16 Dec 2008 21:15:22 +0000 (21:15 +0000)
committerzimmermann@webkit.org <zimmermann@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 16 Dec 2008 21:15:22 +0000 (21:15 +0000)
Fixes: https://bugs.webkit.org/show_bug.cgi?id=22876

Unify random number generation in JavaScriptCore & WebCore, by introducing
wtf/RandomNumber.h and moving wtf_random/wtf_random_init out of MathExtras.h.

wtf_random_init() has been renamed to initializeRandomNumberGenerator() and
lives in it's own private header: wtf/RandomNumberSeed.h, only intended to
be used from within JavaScriptCore.

wtf_random() has been renamed to randomNumber() and lives in a public header
wtf/RandomNumber.h, usable from within JavaScriptCore & WebCore. It encapsulates
the code taking care of initializing the random number generator (only when
building without ENABLE(JSC_MULTIPLE_THREADS), otherwhise initializeThreading()
already took care of that).

Functional change on darwin: Use random() instead of rand(), as it got a larger
period (more randomness). HTMLFormElement already contains this implementation
and I just moved it in randomNumber(), as  special case for PLATFORM(DARWIN).

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@39337 268f45cc-cd09-0410-ab3c-d52691b4dbfc

20 files changed:
JavaScriptCore/ChangeLog
JavaScriptCore/GNUmakefile.am
JavaScriptCore/JavaScriptCore.exp
JavaScriptCore/JavaScriptCore.pri
JavaScriptCore/JavaScriptCore.scons
JavaScriptCore/JavaScriptCore.vcproj/WTF/WTF.vcproj
JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
JavaScriptCore/JavaScriptCoreSources.bkl
JavaScriptCore/runtime/MathObject.cpp
JavaScriptCore/wtf/MathExtras.h
JavaScriptCore/wtf/RandomNumber.cpp [new file with mode: 0644]
JavaScriptCore/wtf/RandomNumber.h [new file with mode: 0644]
JavaScriptCore/wtf/RandomNumberSeed.h [new file with mode: 0644]
JavaScriptCore/wtf/ThreadingGtk.cpp
JavaScriptCore/wtf/ThreadingPthreads.cpp
JavaScriptCore/wtf/ThreadingQt.cpp
JavaScriptCore/wtf/ThreadingWin.cpp
WebCore/ChangeLog
WebCore/ForwardingHeaders/wtf/RandomNumber.h [new file with mode: 0644]
WebCore/html/HTMLFormElement.cpp

index 8da85d8..612b17f 100644 (file)
@@ -1,3 +1,50 @@
+2008-12-16  Nikolas Zimmermann  <nikolas.zimmermann@torchmobile.com>
+
+        Reviewed by Darin Adler.
+
+        Fixes: https://bugs.webkit.org/show_bug.cgi?id=22876
+
+        Unify random number generation in JavaScriptCore & WebCore, by introducing
+        wtf/RandomNumber.h and moving wtf_random/wtf_random_init out of MathExtras.h.
+
+        wtf_random_init() has been renamed to initializeRandomNumberGenerator() and
+        lives in it's own private header: wtf/RandomNumberSeed.h, only intended to
+        be used from within JavaScriptCore.
+
+        wtf_random() has been renamed to randomNumber() and lives in a public header
+        wtf/RandomNumber.h, usable from within JavaScriptCore & WebCore. It encapsulates
+        the code taking care of initializing the random number generator (only when
+        building without ENABLE(JSC_MULTIPLE_THREADS), otherwhise initializeThreading()
+        already took care of that).
+
+        Functional change on darwin: Use random() instead of rand(), as it got a larger
+        period (more randomness). HTMLFormElement already contains this implementation
+        and I just moved it in randomNumber(), as  special case for PLATFORM(DARWIN).
+
+        * GNUmakefile.am: Add RandomNumber.(cpp/h) / RandomNumberSeed.h.
+        * JavaScriptCore.exp: Ditto.
+        * JavaScriptCore.pri: Ditto.
+        * JavaScriptCore.scons: Ditto.
+        * JavaScriptCore.vcproj/WTF/WTF.vcproj: Ditto.
+        * JavaScriptCore.xcodeproj/project.pbxproj: Ditto.
+        * JavaScriptCoreSources.bkl: Ditto.
+        * runtime/MathObject.cpp: Use new WTF::randomNumber() functionality.
+        (JSC::mathProtoFuncRandom):
+        * wtf/MathExtras.h: Move wtf_random / wtf_random_init to new files.
+        * wtf/RandomNumber.cpp: Added.
+        (WTF::randomNumber):
+        * wtf/RandomNumber.h: Added.
+        * wtf/RandomNumberSeed.h: Added. Internal usage within JSC only.
+        (WTF::initializeRandomNumberGenerator):
+        * wtf/ThreadingGtk.cpp: Rename wtf_random_init() to initializeRandomNumberGenerator().
+        (WTF::initializeThreading):
+        * wtf/ThreadingPthreads.cpp: Ditto.
+        (WTF::initializeThreading):
+        * wtf/ThreadingQt.cpp: Ditto.
+        (WTF::initializeThreading):
+        * wtf/ThreadingWin.cpp: Ditto.
+        (WTF::initializeThreading):
+
 2008-12-16 Yael Aharon <yael.aharon@nokia.com>
 
         Reviewed by Tor Arne Vestbø.
index 5b7ba5b..9f7ca76 100644 (file)
@@ -218,6 +218,9 @@ javascriptcore_sources += \
        JavaScriptCore/wtf/OwnPtr.h \
        JavaScriptCore/wtf/PassRefPtr.h \
        JavaScriptCore/wtf/Platform.h \
+       JavaScriptCore/wtf/RandomNumber.cpp \
+       JavaScriptCore/wtf/RandomNumber.h \
+       JavaScriptCore/wtf/RandomNumberSeed.h \
        JavaScriptCore/wtf/RefCounted.h \
        JavaScriptCore/wtf/RefCountedLeakCounter.cpp \
        JavaScriptCore/wtf/RefCountedLeakCounter.h \
index 7129f83..da427fc 100644 (file)
@@ -270,6 +270,7 @@ __ZN3WTF12createThreadEPFPvS0_ES0_
 __ZN3WTF12createThreadEPFPvS0_ES0_PKc
 __ZN3WTF12detachThreadEj
 __ZN3WTF12isMainThreadEv
+__ZN3WTF12randomNumberEv
 __ZN3WTF13currentThreadEv
 __ZN3WTF13tryFastCallocEmm
 __ZN3WTF15ThreadCondition4waitERNS_5MutexE
index e7bb045..98dff80 100644 (file)
@@ -48,6 +48,7 @@ SOURCES += \
     wtf/Assertions.cpp \
     wtf/HashTable.cpp \
     wtf/MainThread.cpp \
+    wtf/RandomNumber.cpp \
     wtf/RefCountedLeakCounter.cpp \
     wtf/unicode/CollatorDefault.cpp \
     wtf/unicode/icu/CollatorICU.cpp \
index 3ad2a99..1053fe2 100644 (file)
@@ -142,6 +142,7 @@ sources['wtf'] = [
     'wtf/Assertions.cpp',
     'wtf/FastMalloc.cpp',
     'wtf/HashTable.cpp',
+    'wtf/RandomNumber.cpp',
     'wtf/RefCountedLeakCounter.cpp',
     'wtf/dtoa.cpp',
 ]
index 5c4b595..fa269ce 100644 (file)
                        >\r
                </File>\r
                <File\r
+                       RelativePath="..\..\wtf\RandomNumber.cpp"\r
+                       >\r
+               </File>\r
+               <File\r
+                       RelativePath="..\..\wtf\RandomNumber.h"\r
+                       >\r
+               </File>\r
+               <File\r
+                       RelativePath="..\..\wtf\RandomNumberSeed.h"\r
+                       >\r
+               </File>\r
+               <File\r
                        RelativePath="..\..\wtf\RefCounted.h"\r
                        >\r
                </File>\r
index 48c225e..47e2297 100644 (file)
@@ -36,6 +36,9 @@
 /* Begin PBXBuildFile section */
                06D358B30DAADAA4003B174E /* MainThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 06D358A20DAAD9C4003B174E /* MainThread.cpp */; };
                06D358B40DAADAAA003B174E /* MainThreadMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 06D358A10DAAD9C4003B174E /* MainThreadMac.mm */; };
+               088FA5BB0EF76D4300578E6F /* RandomNumber.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 088FA5B90EF76D4300578E6F /* RandomNumber.cpp */; };
+               088FA5BC0EF76D4300578E6F /* RandomNumber.h in Headers */ = {isa = PBXBuildFile; fileRef = 088FA5BA0EF76D4300578E6F /* RandomNumber.h */; settings = {ATTRIBUTES = (Private, ); }; };
+               08E279E90EF83B10007DB523 /* RandomNumberSeed.h in Headers */ = {isa = PBXBuildFile; fileRef = 08E279E80EF83B10007DB523 /* RandomNumberSeed.h */; };
                140B7D1D0DC69AF7009C42B8 /* JSActivation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 14DA818F0D99FD2000B0A4FB /* JSActivation.cpp */; };
                140D17D70E8AD4A9000CD17D /* JSBasePrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 140D17D60E8AD4A9000CD17D /* JSBasePrivate.h */; settings = {ATTRIBUTES = (Private, ); }; };
                141211310A48794D00480255 /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 932F5BD90822A1C700736975 /* JavaScriptCore.framework */; };
                06D358A10DAAD9C4003B174E /* MainThreadMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MainThreadMac.mm; sourceTree = "<group>"; };
                06D358A20DAAD9C4003B174E /* MainThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MainThread.cpp; sourceTree = "<group>"; };
                06D358A30DAAD9C4003B174E /* MainThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MainThread.h; sourceTree = "<group>"; };
+               088FA5B90EF76D4300578E6F /* RandomNumber.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RandomNumber.cpp; sourceTree = "<group>"; };
+               088FA5BA0EF76D4300578E6F /* RandomNumber.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RandomNumber.h; sourceTree = "<group>"; };
+               08E279E80EF83B10007DB523 /* RandomNumberSeed.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RandomNumberSeed.h; sourceTree = "<group>"; };
                140D17D60E8AD4A9000CD17D /* JSBasePrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSBasePrivate.h; sourceTree = "<group>"; };
                141211020A48780900480255 /* minidom.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = minidom.c; path = tests/minidom.c; sourceTree = "<group>"; };
                1412110D0A48788700480255 /* minidom.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = minidom.js; path = tests/minidom.js; sourceTree = "<group>"; };
                                9303F567099118FA00AD71B8 /* OwnPtr.h */,
                                6580F795094070560082C219 /* PassRefPtr.h */,
                                65D6D87E09B5A32E0002E4D7 /* Platform.h */,
+                               088FA5B90EF76D4300578E6F /* RandomNumber.cpp */,
+                               088FA5BA0EF76D4300578E6F /* RandomNumber.h */,
+                               08E279E80EF83B10007DB523 /* RandomNumberSeed.h */,
                                1419D32C0CEA7CDE00FF507A /* RefCounted.h */,
                                905B02AD0E28640F006DF882 /* RefCountedLeakCounter.cpp */,
                                90D3469B0E285280009492EE /* RefCountedLeakCounter.h */,
                                BC257DF40E1F53740016B6C9 /* PrototypeFunction.h in Headers */,
                                147B84630E6DE6B1004775A4 /* PutPropertySlot.h in Headers */,
                                1429DA4A0ED245EC00B89619 /* Quantifier.h in Headers */,
+                               088FA5BC0EF76D4300578E6F /* RandomNumber.h in Headers */,
+                               08E279E90EF83B10007DB523 /* RandomNumberSeed.h in Headers */,
                                BC18C4570E16F5CD00B34460 /* RefCounted.h in Headers */,
                                90D3469C0E285280009492EE /* RefCountedLeakCounter.h in Headers */,
                                BC18C4580E16F5CD00B34460 /* RefPtr.h in Headers */,
                                BC18C5260E16FCA700B34460 /* StringPrototype.lut.h in Headers */,
                                BCDE3AB80E6C82F5001453A7 /* Structure.h in Headers */,
                                7E4EE7090EBB7963005934AA /* StructureChain.h in Headers */,
+                               BCCF0D080EF0AAB900413C8F /* StructureStubInfo.h in Headers */,
                                BC9041480EB9250900FE26FA /* StructureTransitionTable.h in Headers */,
                                BC18C46B0E16F5CD00B34460 /* SymbolTable.h in Headers */,
                                BC18C46C0E16F5CD00B34460 /* TCPackedCache.h in Headers */,
                                BC18C44D0E16F5CD00B34460 /* pcre.h in Headers */,
                                BC18C44E0E16F5CD00B34460 /* pcre_internal.h in Headers */,
                                BC18C4720E16F5CD00B34460 /* ucpinternal.h in Headers */,
-                               BCCF0D080EF0AAB900413C8F /* StructureStubInfo.h in Headers */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                                95AB83560DA43C3000BC83F3 /* ProfileNode.cpp in Sources */,
                                95AB83420DA4322500BC83F3 /* Profiler.cpp in Sources */,
                                1C61516C0EBAC7A00031376F /* ProfilerServer.mm in Sources */,
+                               088FA5BB0EF76D4300578E6F /* RandomNumber.cpp in Sources */,
                                905B02AE0E28640F006DF882 /* RefCountedLeakCounter.cpp in Sources */,
                                1429D8850ED21C3D00B89619 /* SamplingTool.cpp in Sources */,
                                9330402C0E6A764000786E6A /* SmallStrings.cpp in Sources */,
                                BCDE3B430E6C832D001453A7 /* Structure.cpp in Sources */,
                                7E4EE70F0EBB7A5B005934AA /* StructureChain.cpp in Sources */,
+                               BCCF0D0C0EF0B8A500413C8F /* StructureStubInfo.cpp in Sources */,
                                E1EE793D0D6C9B9200FEA3BA /* ThreadingPthreads.cpp in Sources */,
                                95CD41B30E1BF6560085358E /* TreeProfile.cpp in Sources */,
                                E1EF79AA0CE97BA60088D500 /* UTF8.cpp in Sources */,
                                930754D008B0F74600AB3056 /* pcre_tables.cpp in Sources */,
                                937013480CA97E0E00FA14D3 /* pcre_ucp_searchfuncs.cpp in Sources */,
                                93E26BD408B1514100F85226 /* pcre_xclass.cpp in Sources */,
-                               BCCF0D0C0EF0B8A500413C8F /* StructureStubInfo.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
index a966a19..96450f7 100644 (file)
@@ -160,6 +160,7 @@ Source files for JSCore.
         wtf/FastMalloc.cpp
         wtf/HashTable.cpp
         wtf/MainThread.cpp
+        wtf/RandomNumber.cpp
         wtf/RefCountedLeakCounter.cpp
         wtf/TCSystemAlloc.cpp
         wtf/ThreadingNone.cpp
index 916f69b..5c530d1 100644 (file)
@@ -26,6 +26,7 @@
 #include <time.h>
 #include <wtf/Assertions.h>
 #include <wtf/MathExtras.h>
+#include <wtf/RandomNumber.h>
 
 namespace JSC {
 
@@ -210,15 +211,7 @@ JSValue* mathProtoFuncPow(ExecState* exec, JSObject*, JSValue*, const ArgList& a
 
 JSValue* mathProtoFuncRandom(ExecState* exec, JSObject*, JSValue*, const ArgList&)
 {
-#if !ENABLE(JSC_MULTIPLE_THREADS)
-    static bool didInitRandom;
-    if (!didInitRandom) {
-        wtf_random_init();
-        didInitRandom = true;
-    }
-#endif
-
-    return jsNumber(exec, wtf_random());
+    return jsNumber(exec, randomNumber());
 }
 
 JSValue* mathProtoFuncRound(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
index 6897c24..76488b4 100644 (file)
@@ -28,7 +28,6 @@
 
 #include <math.h>
 #include <stdlib.h>
-#include <time.h>
 
 #if PLATFORM(SOLARIS)
 #include <ieeefp.h>
@@ -158,41 +157,8 @@ inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
 #define fmod(x, y) wtf_fmod(x, y)
 #define pow(x, y) wtf_pow(x, y)
 
-#if defined(_CRT_RAND_S)
-// Initializes the random number generator.
-inline void wtf_random_init()
-{
-    // No need to initialize for rand_s.
-}
-
-// Returns a pseudo-random number in the range [0, 1).
-inline double wtf_random()
-{
-    unsigned u;
-    rand_s(&u);
-
-    return static_cast<double>(u) / (static_cast<double>(UINT_MAX) + 1.0);
-}
-#endif // _CRT_RAND_S
-
 #endif // COMPILER(MSVC)
 
-#if !COMPILER(MSVC) || !defined(_CRT_RAND_S)
-
-// Initializes the random number generator.
-inline void wtf_random_init()
-{
-    srand(static_cast<unsigned>(time(0)));
-}
-
-// Returns a pseudo-random number in the range [0, 1).
-inline double wtf_random()
-{
-    return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
-}
-
-#endif // #if COMPILER(MSVC)
-
 inline double deg2rad(double d)  { return d * piDouble / 180.0; }
 inline double rad2deg(double r)  { return r * 180.0 / piDouble; }
 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
diff --git a/JavaScriptCore/wtf/RandomNumber.cpp b/JavaScriptCore/wtf/RandomNumber.cpp
new file mode 100644 (file)
index 0000000..9b473e3
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
+ *           (C) 2008 Nikolas Zimmermann <nikolas.zimmermann@torchmobile.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#include "config.h"
+#include "RandomNumber.h"
+
+#include "RandomNumberSeed.h"
+
+#include <limits.h>
+#include <stdlib.h>
+
+namespace WTF {
+
+double randomNumber()
+{
+#if !ENABLE(JSC_MULTIPLE_THREADS)
+    static bool s_initialized = false;
+    if (!s_initialized) {
+        initializeRandomNumberGenerator();
+        s_initialized = true;
+    }
+#endif
+
+#if COMPILER(MSVC) && defined(_CRT_RAND_S)
+    unsigned u;
+    rand_s(&u);
+
+    return static_cast<double>(u) / (static_cast<double>(UINT_MAX) + 1.0);
+#else
+    return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
+#endif
+}
+
+}
diff --git a/JavaScriptCore/wtf/RandomNumber.h b/JavaScriptCore/wtf/RandomNumber.h
new file mode 100644 (file)
index 0000000..2544b16
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2008 Nikolas Zimmermann <nikolas.zimmermann@torchmobile.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#ifndef WTF_RandomNumber_h
+#define WTF_RandomNumber_h
+
+namespace WTF {
+
+    // Returns a pseudo-random number in the range [0, 1)
+    double randomNumber();
+
+}
+
+#endif
diff --git a/JavaScriptCore/wtf/RandomNumberSeed.h b/JavaScriptCore/wtf/RandomNumberSeed.h
new file mode 100644 (file)
index 0000000..62170b5
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2008 Nikolas Zimmermann <nikolas.zimmermann@torchmobile.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#ifndef WTF_RandomNumberSeed_h
+#define WTF_RandomNumberSeed_h
+
+#include <stdlib.h>
+#include <time.h>
+
+// Internal JavaScriptCore usage only
+namespace WTF {
+
+inline void initializeRandomNumberGenerator()
+{
+#if PLATFORM(DARWIN)
+    srandomdev();
+#elif !COMPILER(MSVC) || !defined(_CRT_RAND_S)
+    srand(static_cast<unsigned>(time(0)));
+#endif
+}
+
+}
+
+#endif
index 2451048..b5cf3d3 100644 (file)
@@ -34,7 +34,7 @@
 
 #include "HashMap.h"
 #include "MainThread.h"
-#include "MathExtras.h"
+#include "RandomNumberSeed.h"
 
 #include <glib.h>
 
@@ -59,7 +59,7 @@ void initializeThreading()
     if (!atomicallyInitializedStaticMutex) {
         atomicallyInitializedStaticMutex = new Mutex;
         threadMapMutex();
-        wtf_random_init();
+        initializeRandomNumberGenerator();
         mainThreadIdentifier = currentThread();
         initializeMainThread();
     }
index de1bb55..4ea9999 100644 (file)
@@ -35,7 +35,7 @@
 
 #include "HashMap.h"
 #include "MainThread.h"
-#include "MathExtras.h"
+#include "RandomNumberSeed.h"
 
 #include <errno.h>
 #include <sys/time.h>
@@ -61,7 +61,7 @@ void initializeThreading()
     if (!atomicallyInitializedStaticMutex) {
         atomicallyInitializedStaticMutex = new Mutex;
         threadMapMutex();
-        wtf_random_init();
+        initializeRandomNumberGenerator();
 #if !PLATFORM(DARWIN)
         mainThreadIdentifier = currentThread();
 #endif
index 2acbc05..68d6a27 100644 (file)
@@ -31,7 +31,7 @@
 
 #include "HashMap.h"
 #include "MainThread.h"
-#include "MathExtras.h"
+#include "RandomNumberSeed.h"
 
 #include <QCoreApplication>
 #include <QMutex>
@@ -125,7 +125,7 @@ void initializeThreading()
     if (!atomicallyInitializedStaticMutex) {
         atomicallyInitializedStaticMutex = new Mutex;
         threadMapMutex();
-        wtf_random_init();
+        initializeRandomNumberGenerator();
         QThread* mainThread = QCoreApplication::instance()->thread();
         mainThreadIdentifier = identifierByQthreadHandle(mainThread);
         if (!mainThreadIdentifier)
index bdb3e64..b1310dd 100644 (file)
@@ -68,6 +68,7 @@
 #include <windows.h>
 #include <wtf/HashMap.h>
 #include <wtf/MathExtras.h>
+#include <wtf/RandomNumberSeed.h>
 
 #if PLATFORM(WIN) && USE(PTHREADS)
 // Currently, Apple's Windows port uses a mixture of native and pthreads functions in FastMalloc.
@@ -133,7 +134,7 @@ void initializeThreading()
     if (!atomicallyInitializedStaticMutex) {
         atomicallyInitializedStaticMutex = new Mutex;
         threadMapMutex();
-        wtf_random_init();
+        initializeRandomNumberGenerator();
         initializeMainThread();
         mainThreadIdentifier = currentThread();
         setThreadName(mainThreadIdentifier, "Main Thread");
index 7d78754..45a810e 100644 (file)
@@ -1,3 +1,16 @@
+2008-12-16  Nikolas Zimmermann  <nikolas.zimmermann@torchmobile.com>
+
+        Reviewed by Darin Adler.
+
+        Fixes: https://bugs.webkit.org/show_bug.cgi?id=22876
+
+        Remove legacy randomNumber() functionality from HTMLFormElement, and use the new wtf/RandomNumber.h.
+        Also remove an uneeded QFileInfo include while I'm at it - there is no Qt usage in this file.
+
+        * ForwardingHeaders/wtf/RandomNumber.h: Added.
+        * html/HTMLFormElement.cpp:
+        (WebCore::getUniqueBoundaryString):
+
 2008-12-16 Yael Aharon <yael.aharon@nokia.com>
 
         Reviewed by Tor Arne Vestbø.
diff --git a/WebCore/ForwardingHeaders/wtf/RandomNumber.h b/WebCore/ForwardingHeaders/wtf/RandomNumber.h
new file mode 100644 (file)
index 0000000..42e148a
--- /dev/null
@@ -0,0 +1 @@
+#include <JavaScriptCore/RandomNumber.h>
index 35a29d5..33aa70a 100644 (file)
 #include "MIMETypeRegistry.h"
 #include "Page.h"
 #include "RenderTextControl.h"
+#include <wtf/RandomNumber.h>
 
-#if PLATFORM(QT)
-#include <QtCore/QFileInfo>
-#endif
+#include <limits>
 
 #if PLATFORM(WX)
 #include <wx/defs.h>
@@ -195,26 +194,6 @@ static void appendEncodedString(Vector<char>& buffer, const CString& string)
     }
 }
 
-// FIXME: Move to platform directory?
-static int randomNumber()
-{
-    static bool randomSeeded = false;
-
-#if PLATFORM(DARWIN)
-    if (!randomSeeded) {
-        srandomdev();
-        randomSeeded = true;
-    }
-    return random();
-#else
-    if (!randomSeeded) {
-        srand(static_cast<unsigned>(time(0)));
-        randomSeeded = true;
-    }
-    return rand();
-#endif
-}
-
 TextEncoding HTMLFormElement::dataEncoding() const
 {
     if (isMailtoForm())
@@ -418,7 +397,7 @@ static void getUniqueBoundaryString(Vector<char>& boundary)
     Vector<char> randomBytes;
 
     for (int i = 0; i < 4; ++i) {
-        int randomness = randomNumber();
+        unsigned randomness = static_cast<unsigned>(WTF::randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0));
         randomBytes.append(AlphaNumericEncMap[(randomness >> 24) & 0x3F]);
         randomBytes.append(AlphaNumericEncMap[(randomness >> 16) & 0x3F]);
         randomBytes.append(AlphaNumericEncMap[(randomness >> 8) & 0x3F]);