diff --git a/src/jsvm_dfx.h b/src/jsvm_dfx.h new file mode 100644 index 0000000000000000000000000000000000000000..9cbe2af97481cd0049027ebd01a8ac5809db43b6 --- /dev/null +++ b/src/jsvm_dfx.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JSVM_DFX_H +#define JSVM_DFX_H + +#include + +#include "jsvm_log.h" +#include "jsvm_version.h" +#include "platform/platform.h" + +// v8 header +#include "v8.h" + +namespace jsvm { +[[noreturn]] inline void OnFatalError(const char* location, const char* message) +{ + LOG(Fatal) << "JSVM Fatal Error Position : " << (location ? location : "Unkown"); + LOG(Fatal) << "JSVM Fatal Error Message : " << (message ? message : "Unkown"); + platform::OS::Abort(); +} + +class DebugSealHandleScope { +public: + explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr) +#ifdef DEBUG + : sealHandleScope(isolate != nullptr ? isolate : v8::Isolate::GetCurrent()) +#endif + {} + +private: +#ifdef DEBUG + v8::SealHandleScope sealHandleScope; +#endif +}; +} // namespace jsvm + +#define JSVM_FATAL(message) \ + do { \ + /* Make sure that this struct does not end up in inline code, but */ \ + /* rather in a read-only data section when modifying this code. */ \ + jsvm::OnFatalError(STRINGIFY(__FILE__) ":" STRINGIFY(__LINE__) " ", STRINGIFY(message)); \ + } while (0) + +#define UNREACHABLE(...) JSVM_FATAL("Unreachable code reached" __VA_OPT__(": ") __VA_ARGS__) + +#ifdef __GNUC__ +#define LIKELY(expr) __builtin_expect(!!(expr), 1) +#define UNLIKELY(expr) __builtin_expect(!!(expr), 0) +#else +#define LIKELY(expr) expr +#define UNLIKELY(expr) expr +#endif + +#define CHECK(expr) \ + do { \ + if (UNLIKELY(!(expr))) { \ + JSVM_FATAL(expr); \ + } \ + } while (0) + +#define CHECK_EQ(a, b) CHECK((a) == (b)) +#define CHECK_NE(a, b) CHECK((a) != (b)) +#define CHECK_LE(a, b) CHECK((a) <= (b)) +#define CHECK_GE(a, b) CHECK((a) >= (b)) +#define CHECK_LT(a, b) CHECK((a) < (b)) +#define CHECK_GT(a, b) CHECK((a) > (b)) +#define CHECK_NULL(val) CHECK((val) == nullptr) +#define CHECK_NOT_NULL(val) CHECK((val) != nullptr) +#define CHECK_IMPLIES(a, b) CHECK(!(a) || (b)) + +#ifdef DEBUG +#define DCHECK(expr) CHECK(expr) +#define DCHECK_EQ(a, b) CHECK_EQ(a, b) +#define DCHECK_NE(a, b) CHECK_NE(a, b) +#define DCHECK_LE(a, b) CHECK_LE(a, b) +#define DCHECK_GE(a, b) CHECK_GE(a, b) +#define DCHECK_LT(a, b) CHECK_LT(a, b) +#define DCHECK_GT(a, b) CHECK_GT(a, b) +#define DCHECK_NULL(val) CHECK_NULL(val) +#define DCHECK_NOT_NULL(val) CHECK_NOT_NULL(val) +#define DCHECK_IMPLIES(a, b) CHECK_IMPLIES(a, b) +#else +#define DCHECK(expr) +#define DCHECK_EQ(a, b) +#define DCHECK_NE(a, b) +#define DCHECK_LE(a, b) +#define DCHECK_GE(a, b) +#define DCHECK_LT(a, b) +#define DCHECK_GT(a, b) +#define DCHECK_NULL(val) +#define DCHECK_NOT_NULL(val) +#define DCHECK_IMPLIES(a, b) +#endif + +#endif \ No newline at end of file diff --git a/src/jsvm_log.h b/src/jsvm_log.h new file mode 100644 index 0000000000000000000000000000000000000000..22c51af670e626fe883ad9ae23fcb0ae4e7eac12 --- /dev/null +++ b/src/jsvm_log.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JSVM_LOG_H +#define JSVM_LOG_H +#include +#include +#include + +#include "platform/platform.h" + +namespace jsvm { +class LogStream : public std::stringstream { +public: + LogStream() : std::stringstream() {} + + virtual ~LogStream() = default; +}; + +class LogConsole : public LogStream { +public: + explicit LogConsole(platform::OS::LogLevel level) : LogStream(), level(level) {} + + ~LogConsole() override + { + platform::OS::PrintString(level, str().c_str()); + } + +private: + platform::OS::LogLevel level; +}; + +class LogFile : public LogStream { +public: + explicit LogFile(const char* filename) : file(filename, std::ios::app) {}; + + ~LogFile() override + { + file << str() << std::endl; + } + +private: + std::ofstream file; +}; + +class LogInfo : public LogConsole { +public: + LogInfo() : LogConsole(platform::OS::LogLevel::LOG_INFO) {} +}; + +class LogError : public LogConsole { +public: + LogError() : LogConsole(platform::OS::LogLevel::LOG_ERROR) {} +}; + +class LogFatal : public LogConsole { +public: + LogFatal() : LogConsole(platform::OS::LogLevel::LOG_FATAL) {} +}; +} // namespace jsvm + +// Support LOG(Info), LOG(Error), LOG(Fatal) +#define LOG(level) jsvm::Log##level() + +// Print Log to file +#define LOG_FILE(filename) jsvm::LogFile(filename) +#endif \ No newline at end of file diff --git a/src/jsvm_util.h b/src/jsvm_util.h new file mode 100644 index 0000000000000000000000000000000000000000..cc71e8270019cce849cdea038261af487574d600 --- /dev/null +++ b/src/jsvm_util.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JSVM_UTIL_H +#define JSVM_UTIL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// jsvm header +#include "jsvm_dfx.h" +#include "jsvm_log.h" +#include "jsvm_version.h" +#include "platform/platform.h" + +// v8 header +#include "v8-inspector.h" +#include "v8-platform.h" +#include "v8-profiler.h" +#include "v8.h" + +// Use FORCE_INLINE on functions that have a debug-category-enabled check first +// and then ideally only a single function call following it, to maintain +// performance for the common case (no debugging used). +#ifdef __GNUC__ +#define FORCE_INLINE __attribute__((always_inline)) +#define COLD_NOINLINE __attribute__((cold, noinline)) +#else +#define FORCE_INLINE +#define COLD_NOINLINE +#endif + +namespace jsvm { +template +constexpr size_t ArraySize(const T (&)[N]) +{ + return N; +} +} // namespace jsvm + +namespace v8impl { +template +using Persistent = v8::Global; +} // namespace v8impl +#endif \ No newline at end of file diff --git a/src/jsvm_version.h b/src/jsvm_version.h new file mode 100644 index 0000000000000000000000000000000000000000..c0acd21e6fda077f41b61c70a28b7041c2644be4 --- /dev/null +++ b/src/jsvm_version.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JSVM_VERSION_H +#define JSVM_VERSION_H + +// JSVM version +#define JSVM_MAJOR_VERSION 1 +#define JSVM_MINOR_VERSION 0 +#define JSVM_PATCH_VERSION 0 + +#ifndef DEBUG +#define JSVM_COMPILE_STATUS "Debug" +#else +#define JSVM_COMPILE_STATUS "Release" +#endif + +#define STRINGIFY_HELPER(x) #x +#define STRINGIFY(x) STRINGIFY_HELPER(x) + +#define JSVM_VERSION_STRING \ + "v" STRINGIFY(JSVM_MAJOR_VERSION) "." STRINGIFY(JSVM_MINOR_VERSION) "." STRINGIFY( \ + JSVM_PATCH_VERSION) "(" JSVM_COMPILE_STATUS ")" + +// JSVM_API_VERSION +#define JSVM_API_VERSION 9 + +#endif