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

Split c files #75

Merged
merged 40 commits into from
Dec 4, 2024
Merged

Split c files #75

merged 40 commits into from
Dec 4, 2024

Conversation

stroiman
Copy link

@stroiman stroiman commented Dec 2, 2024

This is not a complete job yet, but paves a road forward.

There is some naming inconsistencies, that I've not done anything about. Example

Here the IsolatePtr references a v8 class

typedef v8::Isolate Isolate;
typedef Isolate IsolatePtr

Here the ContextPtr references a wrapper type in the v8go C++ code.

typedef struct m_ctx m_ctx;
typedef m_ctx ContextPtr

And then there, a v8go C++ struct ScriptCompilerCachedData but a ScriptCompilerCachedDataPtr pointing to a v8 type.

typedef v8::ScriptCompiler::CachedData* ScriptCompilerCachedDataPtr;

typedef struct {
  ScriptCompilerCachedDataPtr ptr;
  const uint8_t* data;
  int length;
  int rejected;
} ScriptCompilerCachedData;

I don't think the Ptr is really useful here though. It seems that the XyzPtr types are needed for Go code; it wouldn't compile when I just created a *v8Isolate in Go alone - but I could have made a mistake.

At any rate I would consider adopting a style like this:

#idfef __cplusplus
typedef v8::Isolate v8Isolate;
#else
typedef struct v8Isolate v8Isolate
#endif
typedef Isolate v8IsolatePtr

As now it's clear what is a v8 type, and what is v8go type.

Avoid using namespace

The header file has a using namespace v8. That is generally a not-so-good practice. Including a header file shouldn't cause namespaces to be included.

Only include what's needed

The v8go.cc and .h file included the entire v8.h header file. This just includes what's necessary for the particular files; speeding up compilation

Use forward declarations

A type definition of structs/classes doesn't need to know the full definition of used types, just the size in memory. For classes and structs, I've created forward declarations to avoid having to include unnecessary header files.

Some non-trivial have been placed in forward-declarations, mostly as the C++ classes are non-trivial in their forward declarations.

I think there might be tools that can automate header file dependency code, i.e., remove unnecessary includes, add necessary ones, create forward decl. But I have no experience using any of those, so I don't know how they would be set up, and what the are capable and incapable of.

Use consistent style in editor
These functions are used through all types
Using the macro pulls in a lot of dependencies that would slow down
compile time for the importer that only needs the type declarations
@stroiman
Copy link
Author

stroiman commented Dec 2, 2024

And added a clang config file so the editor should pick the current style on foatting

Copy link
Owner

@tommie tommie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to contribute! Some small thoughts on code structure.

context.cc Show resolved Hide resolved
context.cc Show resolved Hide resolved
unbound_script.cc Outdated Show resolved Hide resolved
value-macros.h Show resolved Hide resolved
forward-declarations.h Outdated Show resolved Hide resolved
interop.h Outdated
typedef struct {
ValuePtr value;
RtnError error;
} RtnValue;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving this to errors.h. It only exists to add an error to a value, so I think it belongs in the realm of error handling (and this file is small, with a vague scope.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had trouble placing this. I didn't think it so much of as an error, but more like converting the result to idiomatic go.

But I wouldn't mind putting it in errors. I didn't look too much at what the "errors.h" really contains, as extracting the errors file was mostly the result of, if I move A, I also need to move B.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My problem is this file is too small and unfocused: no one will think of looking at this file to find RtnValue. A file that is about mapping error handling domains makes more sense to me. That said, it's not a strong opinion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved it to errors.h for now. Maybe a sensible place will reveal itself later :)

I don't have a strong opinion either.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, on "small files", there is also utils.h/utils.cc containing string copying functionality ...

Opinion on those?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they're fine. Having one slush bucket is better than two, and probably unavoidable. :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the almost empty file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, I thought I did that.

@stroiman
Copy link
Author

stroiman commented Dec 3, 2024

Ahh, just realised I've made quite the mistake.

I have this checked out under go/src/github/stroiman/v8go, but when I run the test suite, the tests are exercising code under go/src/github/tommie/... 🙈

Code probably works as intended, as I've used it from my own project; which uses the "rewrite" feature of go mod to point to my own version.

@stroiman
Copy link
Author

stroiman commented Dec 4, 2024

I made the original comment:

I don't think the Ptr is really useful here though. It seems that the XyzPtr types are needed for Go code; it wouldn't compile when I just created a *v8Isolate in Go alone - but I could have made a mistake.

But it appears that I did make a mistake, if I forward declare a v8Isolate for instance

#ifdef __cplusplus
namespace v8 { class Isolate } // ...
typedef v8::Isolate v8Isolate
#else
typedef struct v8Isolate v8Isolate
#endif

I can declare/define the functions as

extern v8Isolate* NewIsolate();
// ...
v8Isolate* NewIsolate() { };

Then the Go code is happy with

type Isolate struct {
	ptr *C.v8Isolate
}
func NewIsolate() *Isolate {
	initializeIfNecessary()
	iso := &Isolate{
		ptr: C.NewIsolate(),
		cbs: make(map[int]FunctionCallbackWithError),
	}
}

So I would be in favour of just removing the XyzPtr typedefs.

Wouldn't do it in this PR, just cleanup when working on new functionality.

@tommie
Copy link
Owner

tommie commented Dec 4, 2024

go vet complains of stdint.h (and stddef.h for size_t) missing in value.h.

@stroiman
Copy link
Author

stroiman commented Dec 4, 2024

Ah thanks, just added them, was heading out the door, so didn't see what errors, were produced. Maybe I'll check up on it later, or tomorrow.

json.h Outdated Show resolved Hide resolved
@tommie
Copy link
Owner

tommie commented Dec 4, 2024

Tests are happy now. This looks great!

Just the question about removing interop.h and the reference to it.

@stroiman stroiman force-pushed the split-c-files branch 2 times, most recently from b7d6fa8 to 69bb5b1 Compare December 4, 2024 20:23
@tommie
Copy link
Owner

tommie commented Dec 4, 2024

@stroiman pushed 0 commits.

Thank you, GitHub email notification, for letting me know nothing happened. :)

@tommie tommie merged commit 9b006d0 into tommie:master Dec 4, 2024
16 checks passed
@stroiman stroiman deleted the split-c-files branch December 4, 2024 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants