From 1993fd6d702187bc7eb851c3b040bcfa7ea16f48 Mon Sep 17 00:00:00 2001
From: Igor Randjelovic <rigor789@gmail.com>
Date: Fri, 22 Oct 2021 15:57:48 +0200
Subject: [PATCH] chore: wip

---
 .gitignore                                    |   4 +
 CMakeLists.txt                                |   2 +-
 build/scripts/build-nativescript-framework.sh |  67 +++++++----
 build/scripts/build-step-libffi.sh            |  13 +-
 cmake-gen.sh                                  |   2 +
 patches/PageAllocation.h                      | 112 ++++++++++++++++++
 patches/PageBlock.h                           |  69 +++++++++++
 patches/fix-webkit.sh                         |   3 +
 patches/webkit-PageBlock.patch                |  88 ++++++++++++++
 plugins/TKLiveSync/Podfile.lock               |   2 +-
 10 files changed, 334 insertions(+), 28 deletions(-)
 create mode 100644 patches/PageAllocation.h
 create mode 100644 patches/PageBlock.h
 create mode 100644 patches/fix-webkit.sh
 create mode 100644 patches/webkit-PageBlock.patch

diff --git a/.gitignore b/.gitignore
index affb62697..32172e779 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,6 +25,7 @@ Thumbs.db
 /.metadata/
 
 # Build
+out/
 /cmake-build*
 /build.log
 /dist/
@@ -38,3 +39,6 @@ node_modules/
 package-lock.json
 *.pyc
 .vscode/
+
+
+WebKit-master/
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 51410bfc5..7feada3a4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -42,7 +42,7 @@ set(CMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS "iphoneos iphonesimulator")
 set(CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphoneos;-iphonesimulator")
 set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO")
 
-set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "8.0")
+set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "9.0")
 
 # TODO: Versions, license, etc
 
diff --git a/build/scripts/build-nativescript-framework.sh b/build/scripts/build-nativescript-framework.sh
index 3a56d5f56..09846dea9 100755
--- a/build/scripts/build-nativescript-framework.sh
+++ b/build/scripts/build-nativescript-framework.sh
@@ -18,36 +18,57 @@ rm -f "$WORKSPACE/cmake-build/CMakeCache.txt"
 # the performance of Jenkins builds. After building the {N} framework, we
 # build TestRunner for device and thus, it is best to build NativeScript.framework
 # for device last.
-checkpoint "Building NativeScript.framework - iphonesimulator SDK"
-xcodebuild -configuration $CONFIGURATION -sdk "iphonesimulator" -target "NativeScript" -project $NATIVESCRIPT_XCODEPROJ -quiet
+# checkpoint "Building NativeScript.framework - iphonesimulator SDK"
+# xcodebuild -configuration $CONFIGURATION \
+#            -sdk "iphonesimulator" \
+#            -target "NativeScript" \
+#            -project $NATIVESCRIPT_XCODEPROJ \
+#            BUILD_DIR=$(PWD)/out \
+#            SKIP_INSTALL=NO \
+#            -quiet
+
 checkpoint "Building NativeScript.framework - iphoneos SDK"
-xcodebuild -configuration $CONFIGURATION -sdk "iphoneos"        -target "NativeScript" -project $NATIVESCRIPT_XCODEPROJ -quiet
+xcodebuild -configuration $CONFIGURATION \
+           -sdk "iphoneos" \
+           -target "NativeScript" \
+           -project $NATIVESCRIPT_XCODEPROJ \
+           BUILD_DIR=$(PWD)/out \
+           SKIP_INSTALL=NO \
+           -quiet
+
+checkpoint "Creating NativeScript.xcframework"
+xcodebuild \
+    -create-xcframework \
+    -framework $(PWD)/out/Release-iphoneos/NativeScript.framework \
+    -debug-symbols $(PWD)/out/Release-iphoneos/NativeScript.framework.dSYM \
+    -framework $(PWD)/out/Release-iphonesimulator/NativeScript.framework \
+    -debug-symbols $(PWD)/out/Release-iphonesimulator/NativeScript.framework.dSYM \
+    -output $DIST_DIR/NativeScript.xcframework
 
-checkpoint "Creating fat NativeScript.framework"
-mkdir -p "$DIST_DIR" && pushd "$_"
-cp -r "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework" "."
-rm "NativeScript.framework/NativeScript"
+# mkdir -p "$DIST_DIR" && pushd "$_"
+# cp -r "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework" "."
+# rm "NativeScript.framework/NativeScript"
 
 # Strip debug information, dSYM package must be used for debugging and symbolicating
-strip -S "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript" \
-    "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework/NativeScript"
-echo "CodeSign $WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript"
+# strip -S "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript" \
+    # "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework/NativeScript"
+# echo "CodeSign $WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript"
 
-/usr/bin/codesign --force --sign - --timestamp=none "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript"
+# /usr/bin/codesign --force --sign - --timestamp=none "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript"
 
-lipo -create -output "NativeScript.framework/NativeScript" \
-    "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript" \
-    "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework/NativeScript"
+# lipo -create -output "NativeScript.framework/NativeScript" \
+    # "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework/NativeScript" \
+    # "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework/NativeScript"
 
-cp -r "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework.dSYM" "."
-rm "NativeScript.framework.dSYM/Contents/Resources/DWARF/NativeScript"
-lipo -create -output "NativeScript.framework.dSYM/Contents/Resources/DWARF/NativeScript" \
-    "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework.dSYM/Contents/Resources/DWARF/NativeScript" \
-    "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework.dSYM/Contents/Resources/DWARF/NativeScript"
+# cp -r "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework.dSYM" "."
+# rm "NativeScript.framework.dSYM/Cont/ents/Resources/DWARF/NativeScript"
+# lipo -create -output "NativeScript.framework.dSYM/Contents/Resources/DWARF/NativeScript" \
+    # "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphonesimulator/NativeScript.framework.dSYM/Contents/Resources/DWARF/NativeScript" \
+    # "$WORKSPACE/cmake-build/src/NativeScript/$CONFIGURATION-iphoneos/NativeScript.framework.dSYM/Contents/Resources/DWARF/NativeScript"
 
-zip -qr NativeScript.framework.dSYM.zip NativeScript.framework.dSYM
-rm -rf NativeScript.framework.dSYM
+# zip -qr NativeScript.framework.dSYM.zip NativeScript.framework.dSYM
+# rm -rf NativeScript.framework.dSYM
 
-popd
+# popd
 
-checkpoint "Finished building NativeScript.framework - $DIST_DIR/NativeScript.framework"
+checkpoint "Finished building NativeScript.xcframework - $DIST_DIR/NativeScript.xcbframework"
diff --git a/build/scripts/build-step-libffi.sh b/build/scripts/build-step-libffi.sh
index 9f2966ebe..82552a19d 100755
--- a/build/scripts/build-step-libffi.sh
+++ b/build/scripts/build-step-libffi.sh
@@ -53,6 +53,13 @@ function build {
     popd
 }
 
-for CURRENT_ARCH in $ARCHS; do
-    build "$CURRENT_ARCH"
-done
+export
+
+# ARCHS_TO_BUILD = ["i386", "x86_64", "armv7", "arm64"]
+build "i386"
+build "x86_64"
+build "armv7"
+build "arm64"
+# for CURRENT_ARCH in ${ARCHS_TO_BUILD[@]}; do
+   
+# done
diff --git a/cmake-gen.sh b/cmake-gen.sh
index af27e0814..1b72f073f 100755
--- a/cmake-gen.sh
+++ b/cmake-gen.sh
@@ -3,3 +3,5 @@ cmake -DBUILD_SHARED_LIBS=1 -GXcode . -Bcmake-build
 
 # Workaround for https://cmake.org/pipermail/cmake/2015-April/060484.html
 sed -i bak -e 's/Versions\/A\///g' cmake-build/NativeScript.xcodeproj/project.pbxproj
+
+sh ./patches/fix-webkit.sh
diff --git a/patches/PageAllocation.h b/patches/PageAllocation.h
new file mode 100644
index 000000000..f95e00d76
--- /dev/null
+++ b/patches/PageAllocation.h
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2010 Apple Inc. All rights reserved.
+ *
+ * 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 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 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. 
+ */
+
+#pragma once
+
+#include <wtf/Assertions.h>
+#include <wtf/OSAllocator.h>
+#include <wtf/PageBlock.h>
+#include <algorithm>
+
+#if OS(DARWIN)
+#include <mach/mach_init.h>
+#include <mach/vm_map.h>
+#endif
+
+#if OS(WINDOWS)
+#include <malloc.h>
+#include <windows.h>
+#endif
+
+#if HAVE(ERRNO_H)
+#include <errno.h>
+#endif
+
+#if HAVE(MMAP)
+#include <sys/mman.h>
+#include <unistd.h>
+#endif
+
+namespace WTF {
+
+/*
+    PageAllocation
+
+    The PageAllocation class provides a cross-platform memory allocation interface
+    with similar capabilities to posix mmap/munmap.  Memory is allocated by calling
+    PageAllocation::allocate, and deallocated by calling deallocate on the
+    PageAllocation object.  The PageAllocation holds the allocation's base pointer
+    and size.
+
+    The allocate method is passed the size required (which must be a multiple of
+    the system page size, which can be accessed using PageAllocation::pageSize).
+    Callers may also optinally provide a flag indicating the usage (for use by
+    system memory usage tracking tools, where implemented), and boolean values
+    specifying the required protection (defaulting to writable, non-executable).
+*/
+
+class PageAllocation : private PageBlock {
+public:
+    using PageBlock::PageBlock;
+    using PageBlock::size;
+    using PageBlock::base;
+
+#ifndef __clang__
+    using PageBlock::operator bool;
+#else
+    // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access
+    // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool".
+    operator bool() const { return PageBlock::operator bool(); }
+#endif
+
+    static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false)
+    {
+        ASSERT(isPageAligned(size));
+        return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size);
+    }
+
+    void deallocate()
+    {
+        // Clear base & size before calling release; if this is *inside* allocation
+        // then we won't be able to clear then after deallocating the memory.
+        PageAllocation tmp;
+        std::swap(tmp, *this);
+
+        ASSERT(tmp);
+        ASSERT(!*this);
+
+        OSAllocator::decommitAndRelease(tmp.base(), tmp.size());
+    }
+
+private:
+    PageAllocation(void* base, size_t size)
+        : PageBlock(base, size, false)
+    {
+    }
+};
+
+} // namespace WTF
+
+using WTF::PageAllocation;
diff --git a/patches/PageBlock.h b/patches/PageBlock.h
new file mode 100644
index 000000000..8b7c46f9a
--- /dev/null
+++ b/patches/PageBlock.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2010 Apple Inc. All rights reserved.
+ *
+ * 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 INC. AND ITS CONTRIBUTORS ``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 INC. OR ITS 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.
+ */
+
+#pragma once
+
+namespace WTF {
+
+WTF_EXPORT_PRIVATE size_t pageSize();
+WTF_EXPORT_PRIVATE size_t pageMask();
+inline bool isPageAligned(void* address) { return !(reinterpret_cast<intptr_t>(address) & (pageSize() - 1)); }
+inline bool isPageAligned(size_t size) { return !(size & (pageSize() - 1)); }
+inline bool isPowerOfTwo(size_t size) { return !(size & (size - 1)); }
+
+class PageBlock {
+public:
+    PageBlock() = default;
+    PageBlock(void*, size_t, bool hasGuardPages);
+    
+    void* base() const { return m_base; }
+    size_t size() const { return m_size; }
+
+    operator bool() const { return !!m_realBase; }
+
+    bool contains(void* containedBase, size_t containedSize)
+    {
+        return containedBase >= m_base
+            && (static_cast<char*>(containedBase) + containedSize) <= (static_cast<char*>(m_base) + m_size);
+    }
+
+private:
+    void* m_realBase { nullptr };
+    void* m_base { nullptr };
+    size_t m_size { 0 };
+};
+
+inline PageBlock::PageBlock(void* base, size_t size, bool hasGuardPages)
+    : m_realBase(base)
+    , m_base(static_cast<char*>(base) + ((base && hasGuardPages) ? pageSize() : 0))
+    , m_size(size)
+{
+}
+
+} // namespace WTF
+
+using WTF::pageSize;
+using WTF::isPageAligned;
+using WTF::isPowerOfTwo;
diff --git a/patches/fix-webkit.sh b/patches/fix-webkit.sh
new file mode 100644
index 000000000..d306633bd
--- /dev/null
+++ b/patches/fix-webkit.sh
@@ -0,0 +1,3 @@
+
+cp patches/PageAllocation.h src/webkit/Source/WTF/wtf
+cp patches/PageBlock.h src/webkit/Source/WTF/wtf
\ No newline at end of file
diff --git a/patches/webkit-PageBlock.patch b/patches/webkit-PageBlock.patch
new file mode 100644
index 000000000..98ee51821
--- /dev/null
+++ b/patches/webkit-PageBlock.patch
@@ -0,0 +1,88 @@
+Subversion Revision: 260343
+diff --git a/Source/WTF/ChangeLog b/Source/WTF/ChangeLog
+index b0da441d416e92c64d86967c8fd20d939081e8f6..6fb2cd6613047ae75d13b70706a6049618c663ee 100644
+--- a/Source/WTF/ChangeLog
++++ b/Source/WTF/ChangeLog
+@@ -1,3 +1,22 @@
++2020-04-20  Charlie Turner  <cturner@igalia.com>
++
++        [Clang 10] Fix warning: definition of implicit copy assignment operator for 'PageBlock' is deprecated because it has a user-declared copy constructor
++        https://bugs.webkit.org/show_bug.cgi?id=210748
++
++        Reviewed by NOBODY (OOPS!).
++
++        Recent Clang's will issue a warning if you declare an explicit
++        copy construction, but leave the compiler to fill in an implicit
++        assignment operator. I think this is to catch cases where you do
++        something exciting in an assignment operator/copy constructor and
++        forget to do the same exciting thing in the other.
++
++        * wtf/PageAllocation.h: Import the base's constructor to avoid
++        defining our own identical one.
++        * wtf/PageBlock.h: Remove trivial constructor and replace with
++        member initializers. Remove copy constructor. This looks identical
++        to what the compiler would generate anyway.
++
+ 2020-04-18  Robin Morisset  <rmorisset@apple.com>
+ 
+         Support an inlined representation in JSValue of small BigInts ("BigInt32")
+diff --git a/Source/WTF/wtf/PageAllocation.h b/Source/WTF/wtf/PageAllocation.h
+index b296203a3859afa077696f1f98a7a8a790acbd30..f95e00d767edd097ce01ebdf61cf89721672b6c5 100644
+--- a/Source/WTF/wtf/PageAllocation.h
++++ b/Source/WTF/wtf/PageAllocation.h
+@@ -69,10 +69,7 @@ namespace WTF {
+ 
+ class PageAllocation : private PageBlock {
+ public:
+-    PageAllocation()
+-    {
+-    }
+-
++    using PageBlock::PageBlock;
+     using PageBlock::size;
+     using PageBlock::base;
+ 
+diff --git a/Source/WTF/wtf/PageBlock.h b/Source/WTF/wtf/PageBlock.h
+index e670e43713303cb07ca236471af1bbce9cab1d93..cf5cf31b2c93e0219d3723146b7fb95be840b3a5 100644
+--- a/Source/WTF/wtf/PageBlock.h
++++ b/Source/WTF/wtf/PageBlock.h
+@@ -66,8 +66,7 @@ inline bool isPowerOfTwo(size_t size) { return !(size & (size - 1)); }
+ class PageBlock {
+     WTF_MAKE_FAST_ALLOCATED;
+ public:
+-    PageBlock();
+-    PageBlock(const PageBlock&);
++    PageBlock() = default;
+     PageBlock(void*, size_t, bool hasGuardPages);
+     
+     void* base() const { return m_base; }
+@@ -82,25 +81,11 @@ public:
+     }
+ 
+ private:
+-    void* m_realBase;
+-    void* m_base;
+-    size_t m_size;
++    void* m_realBase { nullptr };
++    void* m_base { nullptr };
++    size_t m_size { 0 };
+ };
+ 
+-inline PageBlock::PageBlock()
+-    : m_realBase(0)
+-    , m_base(0)
+-    , m_size(0)
+-{
+-}
+-
+-inline PageBlock::PageBlock(const PageBlock& other)
+-    : m_realBase(other.m_realBase)
+-    , m_base(other.m_base)
+-    , m_size(other.m_size)
+-{
+-}
+-
+ inline PageBlock::PageBlock(void* base, size_t size, bool hasGuardPages)
+     : m_realBase(base)
+     , m_base(static_cast<char*>(base) + ((base && hasGuardPages) ? pageSize() : 0))
+
diff --git a/plugins/TKLiveSync/Podfile.lock b/plugins/TKLiveSync/Podfile.lock
index 6a496bdb7..962acb27f 100644
--- a/plugins/TKLiveSync/Podfile.lock
+++ b/plugins/TKLiveSync/Podfile.lock
@@ -15,4 +15,4 @@ SPEC CHECKSUMS:
 
 PODFILE CHECKSUM: f5ca86f956288d6d48fd6aa384d742ee2b5999b3
 
-COCOAPODS: 1.9.3
+COCOAPODS: 1.11.2
