diff --git a/CHANGELOG.md b/CHANGELOG.md index 03ed0ca5..003f9669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- C formatting via `clang-format` to aid future development + ## [v0.3.0] ### Added diff --git a/README.md b/README.md index 320ee40e..5951f96f 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ Go Reference: https://pkg.go.dev/rogchap.com/v8go V8 version: 8.7.220.31 In order to make `v8go` usable as a standard Go package, prebuilt static libraries of V8 -are included for Linux and OSX ie. you *should not* require to build V8 yourself. +are included for Linux and macOS ie. you *should not* require to build V8 yourself. V8 requires 64-bit, therefore will not work on 32-bit systems. @@ -103,6 +103,15 @@ To set this up: you will need to copy the `snapshot_blob.bin` file from the Mingw-w64 bin folder to your program's working directory (which is typically wherever `main.go` is) +## Development + +### Formatting + +Go has `go fmt`, C has `clang-format`. Any changes to the `v8go.h|cc` should be formated with `clang-format` with the +"Chromium" Coding style. This can be done easily by running the `go generate` command. + +`brew install clang-format` to install on macOS. + --- V8 Gopher image based on original artwork from the amazing [Renee French](http://reneefrench.blogspot.com). diff --git a/cgo.go b/cgo.go index 199181f4..d1e7b4b6 100644 --- a/cgo.go +++ b/cgo.go @@ -1,5 +1,7 @@ package v8go +//go:generate clang-format -i --verbose -style=Chromium v8go.h v8go.cc + // #cgo CXXFLAGS: -fno-rtti -fpic -std=c++14 -DV8_COMPRESS_POINTERS -DV8_31BIT_SMIS_ON_64BIT_ARCH // #cgo darwin linux CXXFLAGS: -I${SRCDIR}/deps/include // #cgo LDFLAGS: -pthread -lv8 diff --git a/v8go.cc b/v8go.cc index b393dfb1..d97c626a 100644 --- a/v8go.cc +++ b/v8go.cc @@ -1,13 +1,14 @@ #include "v8go.h" -#include "v8.h" -#include "libplatform/libplatform.h" +#include #include #include -#include #include -#include +#include + +#include "libplatform/libplatform.h" +#include "v8.h" using namespace v8; @@ -26,7 +27,7 @@ typedef struct { const char* CopyString(std::string str) { int len = str.length(); - char *mem = (char*)malloc(len+1); + char* mem = (char*)malloc(len + 1); memcpy(mem, str.data(), len); mem[len] = 0; return mem; @@ -47,7 +48,8 @@ RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local ctx) { RtnError rtn = {nullptr, nullptr, nullptr}; if (try_catch.HasTerminated()) { - rtn.msg = CopyString("ExecutionTerminated: script execution has been terminated"); + rtn.msg = + CopyString("ExecutionTerminated: script execution has been terminated"); return rtn; } @@ -65,51 +67,51 @@ RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local ctx) { } Maybe start = try_catch.Message()->GetStartColumn(ctx); if (start.IsJust()) { - sb << ":" << start.ToChecked() + 1; // + 1 to match output from stack trace + sb << ":" + << start.ToChecked() + 1; // + 1 to match output from stack trace } rtn.location = CopyString(sb.str()); } - + MaybeLocal mstack = try_catch.StackTrace(ctx); if (!mstack.IsEmpty()) { String::Utf8Value stack(iso, mstack.ToLocalChecked()); rtn.stack = CopyString(stack); } - + return rtn; } -extern "C" -{ +extern "C" { /********** Isolate **********/ void Init() { #ifdef _WIN32 - V8::InitializeExternalStartupData("."); + V8::InitializeExternalStartupData("."); #endif - V8::InitializePlatform(default_platform.get()); - V8::Initialize(); - return; + V8::InitializePlatform(default_platform.get()); + V8::Initialize(); + return; } IsolatePtr NewIsolate() { - Isolate::CreateParams params; - params.array_buffer_allocator = default_allocator; - return static_cast(Isolate::New(params)); + Isolate::CreateParams params; + params.array_buffer_allocator = default_allocator; + return static_cast(Isolate::New(params)); } void IsolateDispose(IsolatePtr ptr) { - if (ptr == nullptr) { - return; - } - Isolate* iso = static_cast(ptr); - iso->Dispose(); + if (ptr == nullptr) { + return; + } + Isolate* iso = static_cast(ptr); + iso->Dispose(); } void IsolateTerminateExecution(IsolatePtr ptr) { - Isolate* iso = static_cast(ptr); - iso->TerminateExecution(); + Isolate* iso = static_cast(ptr); + iso->TerminateExecution(); } IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr) { @@ -119,84 +121,84 @@ IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr) { Isolate* iso = static_cast(ptr); v8::HeapStatistics hs; iso->GetHeapStatistics(&hs); - - return IsolateHStatistics{ - hs.total_heap_size(), - hs.total_heap_size_executable(), - hs.total_physical_size(), - hs.total_available_size(), - hs.used_heap_size(), - hs.heap_size_limit(), - hs.malloced_memory(), - hs.external_memory(), - hs.peak_malloced_memory(), - hs.number_of_native_contexts(), - hs.number_of_detached_contexts() - }; + + return IsolateHStatistics{hs.total_heap_size(), + hs.total_heap_size_executable(), + hs.total_physical_size(), + hs.total_available_size(), + hs.used_heap_size(), + hs.heap_size_limit(), + hs.malloced_memory(), + hs.external_memory(), + hs.peak_malloced_memory(), + hs.number_of_native_contexts(), + hs.number_of_detached_contexts()}; } /********** Context **********/ ContextPtr NewContext(IsolatePtr ptr) { - Isolate* iso = static_cast(ptr); - Locker locker(iso); - Isolate::Scope isolate_scope(iso); - HandleScope handle_scope(iso); - - iso->SetCaptureStackTraceForUncaughtExceptions(true); - - m_ctx* ctx = new m_ctx; - ctx->ptr.Reset(iso, Context::New(iso)); - ctx->iso = iso; - return static_cast(ctx); + Isolate* iso = static_cast(ptr); + Locker locker(iso); + Isolate::Scope isolate_scope(iso); + HandleScope handle_scope(iso); + + iso->SetCaptureStackTraceForUncaughtExceptions(true); + + m_ctx* ctx = new m_ctx; + ctx->ptr.Reset(iso, Context::New(iso)); + ctx->iso = iso; + return static_cast(ctx); } RtnValue RunScript(ContextPtr ctx_ptr, const char* source, const char* origin) { - m_ctx* ctx = static_cast(ctx_ptr); - Isolate* iso = ctx->iso; - Locker locker(iso); - Isolate::Scope isolate_scope(iso); - HandleScope handle_scope(iso); - TryCatch try_catch(iso); - - Local local_ctx = ctx->ptr.Get(iso); - Context::Scope context_scope(local_ctx); - - Local src = String::NewFromUtf8(iso, source, NewStringType::kNormal).ToLocalChecked(); - Local ogn = String::NewFromUtf8(iso, origin, NewStringType::kNormal).ToLocalChecked(); - - RtnValue rtn = { nullptr, nullptr }; - - ScriptOrigin script_origin(ogn); - MaybeLocal