Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clang-format for C files #51

Merged
merged 2 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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).
2 changes: 2 additions & 0 deletions cgo.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
193 changes: 96 additions & 97 deletions v8go.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "v8go.h"

#include "v8.h"
#include "libplatform/libplatform.h"
#include <stdio.h>

#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <stdio.h>
#include <string>

#include "libplatform/libplatform.h"
#include "v8.h"

using namespace v8;

Expand All @@ -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;
Expand All @@ -47,7 +48,8 @@ RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local<Context> 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;
}

Expand All @@ -65,51 +67,51 @@ RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local<Context> ctx) {
}
Maybe<int> 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<Value> 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<IsolatePtr>(Isolate::New(params));
Isolate::CreateParams params;
params.array_buffer_allocator = default_allocator;
return static_cast<IsolatePtr>(Isolate::New(params));
}

void IsolateDispose(IsolatePtr ptr) {
if (ptr == nullptr) {
return;
}
Isolate* iso = static_cast<Isolate*>(ptr);
iso->Dispose();
if (ptr == nullptr) {
return;
}
Isolate* iso = static_cast<Isolate*>(ptr);
iso->Dispose();
}

void IsolateTerminateExecution(IsolatePtr ptr) {
Isolate* iso = static_cast<Isolate*>(ptr);
iso->TerminateExecution();
Isolate* iso = static_cast<Isolate*>(ptr);
iso->TerminateExecution();
}

IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr) {
Expand All @@ -119,84 +121,84 @@ IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr) {
Isolate* iso = static_cast<Isolate*>(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<Isolate*>(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<ContextPtr>(ctx);
Isolate* iso = static_cast<Isolate*>(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<ContextPtr>(ctx);
}

RtnValue RunScript(ContextPtr ctx_ptr, const char* source, const char* origin) {
m_ctx* ctx = static_cast<m_ctx*>(ctx_ptr);
Isolate* iso = ctx->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
TryCatch try_catch(iso);

Local<Context> local_ctx = ctx->ptr.Get(iso);
Context::Scope context_scope(local_ctx);

Local<String> src = String::NewFromUtf8(iso, source, NewStringType::kNormal).ToLocalChecked();
Local<String> ogn = String::NewFromUtf8(iso, origin, NewStringType::kNormal).ToLocalChecked();

RtnValue rtn = { nullptr, nullptr };

ScriptOrigin script_origin(ogn);
MaybeLocal<Script> script = Script::Compile(local_ctx, src, &script_origin);
if (script.IsEmpty()) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
return rtn;
}
MaybeLocal<v8::Value> result = script.ToLocalChecked()->Run(local_ctx);
if (result.IsEmpty()) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
return rtn;
}
m_value* val = new m_value;
val->ctx_ptr = ctx;
val->ptr.Reset(iso, Persistent<Value>(iso, result.ToLocalChecked()));
m_ctx* ctx = static_cast<m_ctx*>(ctx_ptr);
Isolate* iso = ctx->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
TryCatch try_catch(iso);

Local<Context> local_ctx = ctx->ptr.Get(iso);
Context::Scope context_scope(local_ctx);

Local<String> src =
String::NewFromUtf8(iso, source, NewStringType::kNormal).ToLocalChecked();
Local<String> ogn =
String::NewFromUtf8(iso, origin, NewStringType::kNormal).ToLocalChecked();

RtnValue rtn = {nullptr, nullptr};

rtn.value = static_cast<ValuePtr>(val);
ScriptOrigin script_origin(ogn);
MaybeLocal<Script> script = Script::Compile(local_ctx, src, &script_origin);
if (script.IsEmpty()) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
return rtn;
}
MaybeLocal<v8::Value> result = script.ToLocalChecked()->Run(local_ctx);
if (result.IsEmpty()) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
return rtn;
}
m_value* val = new m_value;
val->ctx_ptr = ctx;
val->ptr.Reset(iso, Persistent<Value>(iso, result.ToLocalChecked()));

rtn.value = static_cast<ValuePtr>(val);
return rtn;
}

void ContextDispose(ContextPtr ptr) {
if (ptr == nullptr) {
return;
}
m_ctx* ctx = static_cast<m_ctx*>(ptr);
if (ctx == nullptr) {
return;
}
ctx->ptr.Reset();
delete ctx;
}
if (ptr == nullptr) {
return;
}
m_ctx* ctx = static_cast<m_ctx*>(ptr);
if (ctx == nullptr) {
return;
}
ctx->ptr.Reset();
delete ctx;
}

/********** Value **********/

Expand All @@ -216,16 +218,13 @@ const char* ValueToString(ValuePtr ptr) {

Local<Value> value = val->ptr.Get(iso);
String::Utf8Value utf8(iso, value);

return CopyString(utf8);
}

return CopyString(utf8);
}

/********** Version **********/

const char* Version() {
return V8::GetVersion();
return V8::GetVersion();
}

}

26 changes: 14 additions & 12 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ typedef struct {
} RtnValue;

typedef struct {
size_t total_heap_size;
size_t total_heap_size_executable;
size_t total_physical_size;
size_t total_available_size;
size_t used_heap_size;
size_t heap_size_limit;
size_t malloced_memory;
size_t external_memory;
size_t peak_malloced_memory;
size_t number_of_native_contexts;
size_t number_of_detached_contexts;
size_t total_heap_size;
size_t total_heap_size_executable;
size_t total_physical_size;
size_t total_available_size;
size_t used_heap_size;
size_t heap_size_limit;
size_t malloced_memory;
size_t external_memory;
size_t peak_malloced_memory;
size_t number_of_native_contexts;
size_t number_of_detached_contexts;
} IsolateHStatistics;

extern void Init();
Expand All @@ -43,7 +43,9 @@ extern IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr);

extern ContextPtr NewContext(IsolatePtr prt);
extern void ContextDispose(ContextPtr ptr);
extern RtnValue RunScript(ContextPtr ctx_ptr, const char* source, const char* origin);
extern RtnValue RunScript(ContextPtr ctx_ptr,
const char* source,
const char* origin);

extern void ValueDispose(ValuePtr ptr);
const char* ValueToString(ValuePtr ptr);
Expand Down