From a32e8016cd69332a4a443aa0aa63179cee3c6a91 Mon Sep 17 00:00:00 2001
From: Calvin Hill <calvin@hakobaito.co.uk>
Date: Tue, 12 Feb 2019 12:51:52 +0000
Subject: Haiku platform support.


diff --git a/BUILD.gn b/BUILD.gn
index fddd525..2614bcd 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -607,7 +607,7 @@ config("toolchain") {
     }
   }
 
-  if (is_clang) {
+  if (is_clang && !is_haiku) {
     cflags += [
       "-Wmissing-field-initializers",
 
@@ -3350,6 +3350,11 @@ v8_component("v8_libbase") {
       "src/base/debug/stack_trace_fuchsia.cc",
       "src/base/platform/platform-fuchsia.cc",
     ]
+  } else if (is_haiku) {
+    sources += [
+      "src/base/debug/stack_trace_posix.cc",
+      "src/base/platform/platform-haiku.cc",
+    ]
   } else if (is_mac) {
     sources += [
       "src/base/debug/stack_trace_posix.cc",
@@ -3666,6 +3671,13 @@ if (is_fuchsia && !build_with_chromium) {
 
 group("v8_fuzzers") {
   testonly = true
+
+  if(host_os == "haiku"){
+    data_deps = []
+  }
+
+  if (host_os != "haiku") {
+
   data_deps = [
     ":v8_simple_json_fuzzer",
     ":v8_simple_multi_return_fuzzer",
@@ -3677,6 +3689,8 @@ group("v8_fuzzers") {
     ":v8_simple_wasm_compile_fuzzer",
     ":v8_simple_wasm_fuzzer",
   ]
+  }
+
 }
 
 if (is_component_build) {
diff --git a/include/v8config.h b/include/v8config.h
index e30a582..9e65520 100644
--- a/include/v8config.h
+++ b/include/v8config.h
@@ -61,6 +61,7 @@
 //  V8_OS_CYGWIN        - Cygwin
 //  V8_OS_DRAGONFLYBSD  - DragonFlyBSD
 //  V8_OS_FREEBSD       - FreeBSD
+//  V8_OS_HAIKU         - Haiku
 //  V8_OS_FUCHSIA       - Fuchsia
 //  V8_OS_LINUX         - Linux
 //  V8_OS_MACOSX        - Mac OS X
@@ -96,6 +97,9 @@
 # define V8_OS_BSD 1
 # define V8_OS_FREEBSD 1
 # define V8_OS_POSIX 1
+#elif defined(__HAIKU__)
+# define V8_OS_HAIKU 1
+# define V8_OS_POSIX 1
 #elif defined(__Fuchsia__)
 # define V8_OS_FUCHSIA 1
 # define V8_OS_POSIX 1
diff --git a/src/base/debug/stack_trace_posix.cc b/src/base/debug/stack_trace_posix.cc
index ed602af..58b3b5d 100644
--- a/src/base/debug/stack_trace_posix.cc
+++ b/src/base/debug/stack_trace_posix.cc
@@ -25,14 +25,15 @@
 #include <string>
 #include <vector>
 
-#if V8_LIBC_GLIBC || V8_LIBC_BSD || V8_LIBC_UCLIBC || V8_OS_SOLARIS
+#if V8_LIBC_GLIBC || V8_LIBC_BSD || V8_LIBC_UCLIBC || V8_OS_SOLARIS || V8_OS_HAIKU
 #define HAVE_EXECINFO_H 1
 #endif
 
-#if HAVE_EXECINFO_H
+#if defined(HAVE_EXECINFO_H)
 #include <cxxabi.h>
 #include <execinfo.h>
 #endif
+
 #if V8_OS_MACOSX
 #include <AvailabilityMacros.h>
 #endif
diff --git a/src/base/platform/platform-haiku.cc b/src/base/platform/platform-haiku.cc
new file mode 100644
index 0000000..86bf945
--- /dev/null
+++ b/src/base/platform/platform-haiku.cc
@@ -0,0 +1,48 @@
+// Copyright 2012 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Platform-specific code for Haiku goes here. For the POSIX-compatible
+// parts, the implementation is in platform-posix.cc.
+
+#include <pthread.h>
+#include <semaphore.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+
+#include <errno.h>
+#include <fcntl.h>  // open
+#include <stdarg.h>
+#include <strings.h>    // index
+#include <sys/mman.h>   // mmap & munmap
+#include <sys/stat.h>   // open
+#include <sys/types.h>  // mmap & munmap
+#include <unistd.h>     // sysconf
+
+#include <cmath>
+
+#undef MAP_TYPE
+
+#include "src/base/macros.h"
+#include "src/base/platform/platform-posix-time.h"
+#include "src/base/platform/platform-posix.h"
+#include "src/base/platform/platform.h"
+
+namespace v8 {
+namespace base {
+
+TimezoneCache* OS::CreateTimezoneCache() {
+  return new PosixDefaultTimezoneCache();
+}
+
+std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
+  return std::vector<SharedLibraryAddress>();
+}
+
+void OS::SignalCodeMovingGC() {}
+
+}  // namespace base
+}  // namespace v8
diff --git a/src/base/platform/platform-posix.cc b/src/base/platform/platform-posix.cc
index 33a9371..17d7a41 100644
--- a/src/base/platform/platform-posix.cc
+++ b/src/base/platform/platform-posix.cc
@@ -60,7 +60,7 @@
 #include <sys/resource.h>
 #endif
 
-#if !defined(_AIX) && !defined(V8_OS_FUCHSIA)
+#if !defined(_AIX) && !defined(V8_OS_FUCHSIA) && !defined(__HAIKU__)
 #include <sys/syscall.h>
 #endif
 
@@ -76,6 +76,14 @@ extern int madvise(caddr_t, size_t, int);
 #endif
 #endif
 
+#if defined(__HAIKU__)
+extern "C" int posix_madvise(void*, size_t, int);
+#endif
+
+#if defined(__HAIKU__)
+#define MADV_FREE POSIX_MADV_DONTNEED
+#endif
+
 #ifndef MADV_FREE
 #define MADV_FREE MADV_DONTNEED
 #endif
@@ -127,7 +135,7 @@ int GetProtectionFromMemoryPermission(OS::MemoryPermission access) {
 int GetFlagsForMemoryPermission(OS::MemoryPermission access) {
   int flags = MAP_PRIVATE | MAP_ANONYMOUS;
   if (access == OS::MemoryPermission::kNoAccess) {
-#if !V8_OS_AIX && !V8_OS_FREEBSD && !V8_OS_QNX
+#if !V8_OS_AIX && !V8_OS_FREEBSD && !V8_OS_QNX && !V8_OS_HAIKU
     flags |= MAP_NORESERVE;
 #endif  // !V8_OS_AIX && !V8_OS_FREEBSD && !V8_OS_QNX
 #if V8_OS_QNX
@@ -356,6 +364,8 @@ bool OS::DiscardSystemPages(void* address, size_t size) {
   int ret = madvise(address, size, MADV_FREE_REUSABLE);
 #elif defined(_AIX) || defined(V8_OS_SOLARIS)
   int ret = madvise(reinterpret_cast<caddr_t>(address), size, MADV_FREE);
+#elif defined(__HAIKU__)
+  int ret = posix_madvise(address, size, MADV_FREE);
 #else
   int ret = madvise(address, size, MADV_FREE);
 #endif
@@ -367,6 +377,8 @@ bool OS::DiscardSystemPages(void* address, size_t size) {
 // imply runtime support.
 #if defined(_AIX) || defined(V8_OS_SOLARIS)
     ret = madvise(reinterpret_cast<caddr_t>(address), size, MADV_DONTNEED);
+#elif defined(__HAIKU__)
+    ret = posix_madvise(address, size, MADV_FREE);
 #else
     ret = madvise(address, size, MADV_DONTNEED);
 #endif
diff --git a/src/libsampler/sampler.cc b/src/libsampler/sampler.cc
index 94ad3fd..eec9861 100644
--- a/src/libsampler/sampler.cc
+++ b/src/libsampler/sampler.cc
@@ -12,7 +12,7 @@
 #include <sys/time.h>
 #include <atomic>
 
-#if !V8_OS_QNX && !V8_OS_AIX
+#if !V8_OS_QNX && !V8_OS_AIX && !V8_OS_HAIKU
 #include <sys/syscall.h>  // NOLINT
 #endif
 
@@ -20,7 +20,7 @@
 #include <mach/mach.h>
 // OpenBSD doesn't have <ucontext.h>. ucontext_t lives in <signal.h>
 // and is a typedef for struct sigcontext. There is no uc_mcontext.
-#elif !V8_OS_OPENBSD
+#elif !V8_OS_OPENBSD && !V8_OS_HAIKU
 #include <ucontext.h>
 #endif
 
@@ -470,6 +470,16 @@ void SignalHandler::FillRegisterState(void* context, RegisterState* state) {
   state->sp = reinterpret_cast<void*>(mcontext.mc_r13);
   state->fp = reinterpret_cast<void*>(mcontext.mc_r11);
 #endif  // V8_HOST_ARCH_*
+#elif V8_OS_HAIKU
+#if V8_HOST_ARCH_IA32
+  state->pc = reinterpret_cast<void*>(mcontext.eip);
+  state->sp = reinterpret_cast<void*>(mcontext.esp);
+  state->fp = reinterpret_cast<void*>(mcontext.ebp);
+#elif V8_HOST_ARCH_X64
+  state->pc = reinterpret_cast<void*>(mcontext.rip);
+  state->sp = reinterpret_cast<void*>(mcontext.rsp);
+  state->fp = reinterpret_cast<void*>(mcontext.rbp);
+#endif  // V8_HOST_ARCH_*
 #elif V8_OS_NETBSD
 #if V8_HOST_ARCH_IA32
   state->pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_EIP]);
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 6891634..0e32744 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -7,6 +7,16 @@ import("../gni/v8.gni")
 group("gn_all") {
   testonly = true
 
+ # Don't build tests for Haiku yet
+
+  if(host_os != "haiku"){
+    data_deps = []
+  }
+
+  deps = []
+
+  if (host_os != "haiku") {
+
   data_deps = [
     "benchmarks:v8_benchmarks",
     "intl:v8_intl",
@@ -34,6 +44,8 @@ group("gn_all") {
       "unittests:unittests",
     ]
   }
+ }
+
 }
 
 ###############################################################################
diff --git a/tools/mb/mb.py b/tools/mb/mb.py
index 1466079..a4fb2e9 100755
--- a/tools/mb/mb.py
+++ b/tools/mb/mb.py
@@ -839,12 +839,15 @@ class MetaBuildWrapper(object):
       subdir, exe = 'linux64', 'gn'
     elif self.platform == 'darwin':
       subdir, exe = 'mac', 'gn'
+    elif self.platform == 'haiku1':
+      subdir, exe = 'haiku','gn'
+      gn_path = "/bin/gn"
     else:
       subdir, exe = 'win', 'gn.exe'
 
     arch = platform.machine()
     if (arch.startswith('s390') or arch.startswith('ppc') or
-        self.platform.startswith('aix')):
+	  self.platform.startswith('aix') or self.platform.startswith('haiku')):
       # use gn in PATH
       gn_path = 'gn'
     else:
-- 
2.21.0


From 685d3e51b0112c665fc7723a205127d1ec76f9bc Mon Sep 17 00:00:00 2001
From: Calvin Hill <calvin@hakobaito.co.uk>
Date: Mon, 27 May 2019 11:57:08 +0000
Subject: Fix template macro definition for GCC.


diff --git a/src/base/export-template.h b/src/base/export-template.h
index 861cfe4..8c6d885 100644
--- a/src/base/export-template.h
+++ b/src/base/export-template.h
@@ -83,6 +83,7 @@
 // definition sites instead.
 #define EXPORT_TEMPLATE_DECLARE_MSVC_HACK(export, _)
 #define EXPORT_TEMPLATE_DEFINE_MSVC_HACK(export, _) export
+#define EXPORT_TEMPLATE_TEST_MSVC_HACK_DEFAULT(...) true
 
 // EXPORT_TEMPLATE_STYLE is an internal helper macro that identifies which
 // export style needs to be used for the provided FOO_EXPORT macro definition.
@@ -159,5 +160,5 @@ EXPORT_TEMPLATE_TEST(DEFAULT, __declspec(dllimport));
 #undef EXPORT_TEMPLATE_TEST
 #undef EXPORT_TEMPLATE_TEST_DEFAULT_DEFAULT
 #undef EXPORT_TEMPLATE_TEST_MSVC_HACK_MSVC_HACK
-
+#undef EXPORT_TEMPLATE_TEST_MSVC_HACK_DEFAULT
 #endif  // V8_BASE_EXPORT_TEMPLATE_H_
-- 
2.21.0

