forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Implement Value.TypeOf() #104
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
Open
stroiman
wants to merge
4
commits into
tommie:master
Choose a base branch
from
stroiman:value-typeof
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
#include "value.h" | ||
#include <stdlib.h> | ||
#include "context.h" | ||
#include "deps/include/v8-context.h" | ||
#include "deps/include/v8-exception.h" | ||
#include "errors.h" | ||
#include "isolate-macros.h" | ||
#include "utils.h" | ||
#include "value-macros.h" | ||
|
||
#define ISOLATE_SCOPE_INTERNAL_CONTEXT(iso) \ | ||
|
@@ -11,6 +13,29 @@ | |
|
||
using namespace v8; | ||
|
||
RtnString StringToRtnString(v8::Isolate* iso, Local<String> val) { | ||
RtnString res = {}; | ||
res.length = val->Utf8LengthV2(iso); | ||
res.data = (char*)(malloc(res.length)); | ||
val->WriteUtf8V2(iso, (char*)res.data, res.length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest sticking to either C- or C++-style casts in one function. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree :) |
||
return res; | ||
} | ||
|
||
RtnString ExceptionToRtnString(TryCatch& try_catch, | ||
v8::Isolate* iso, | ||
Local<Context> ctx) { | ||
RtnString res = {}; | ||
res.error = ExceptionError(try_catch, iso, ctx); | ||
return res; | ||
} | ||
|
||
void RtnStringRelease(RtnString rtnString) { | ||
if (rtnString.data) { | ||
free((void*)rtnString.data); | ||
} | ||
ErrorRelease(rtnString.error); | ||
} | ||
|
||
void ValueRelease(ValuePtr ptr) { | ||
if (ptr == nullptr) { | ||
return; | ||
|
@@ -223,16 +248,11 @@ double ValueToNumber(ValuePtr ptr) { | |
|
||
RtnString ValueToDetailString(ValuePtr ptr) { | ||
LOCAL_VALUE(ptr); | ||
RtnString rtn = {0}; | ||
Local<String> str; | ||
if (!value->ToDetailString(local_ctx).ToLocal(&str)) { | ||
rtn.error = ExceptionError(try_catch, iso, local_ctx); | ||
return rtn; | ||
return ExceptionToRtnString(try_catch, iso, local_ctx); | ||
} | ||
String::Utf8Value ds(iso, str); | ||
rtn.data = CopyString(ds); | ||
rtn.length = ds.length(); | ||
return rtn; | ||
return StringToRtnString(iso, str); | ||
} | ||
|
||
RtnString ValueToString(ValuePtr ptr) { | ||
|
@@ -571,3 +591,9 @@ int ValueStrictEquals(ValuePtr ptr, ValuePtr otherPtr) { | |
Local<Value> other = otherPtr->ptr.Get(iso); | ||
return value->StrictEquals(other); | ||
} | ||
|
||
RtnString ValueTypeOf(ValuePtr ptr) { | ||
LOCAL_VALUE(ptr); | ||
Local<String> result = value->TypeOf(iso); | ||
return StringToRtnString(iso, result); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the {} initialization does anything (the original code had
{0}
with no purpose there).RtnString and RtnError should have constructors (or default initializers) to ensure values are cleared.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{}
zeros the fields. But I don't know the difference between that and{0}
.However, these two types
RtnString
andRtnError
are exposed to Go code, i.e. Go code actually looks up the field - so I don't think you can add constructors? Because then it would be a C++ type, not a C type?You could make them "anonymous" (don't know the correct term), like
m_ctx
where Go code just has pointer values it can pass back to C functions - and then add functions to look up values, but that also seems somewhat cumbersome.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as you don't add a vtable, it's just compile-time syntactic sugar for running a function every time a value is created.
Relying on a destructor might be dangerous, since the cgo code wouldn't run it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I what you mean. But this works - but it generates warnings.
To get "methods" on the type, I changed from
typedef struct { ... } RtnError
tostruct RtnError { }
.This caused compilation errors, both in
.h
and.go
files..h
files,extern RtnValue SomeFunction(...);
had to be changed toextern struct RtnValue SomeFunction(...)
.go
files,C.RtnValue
had to be changed toC.struct_RtnValue
.I could avoid those changes by adding
typedef struct RtnError RtnError;
though I probably shouldn't use the same identifier?The warnings are:
But if I remove the initializer from some function creating a
RtnError
, all tests are still passing, and if I then remove the constructor, it fails at runtime.But I had the impression from your comments that there is a simpler way?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see, this needs to compile in C mode. Of course it does. My bad.
Ok, helper function it is...
Edit The helper function was about another comment... (Does it show there's too much going on for me to keep track. ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But here: still need to add back the zero into the intializer to make it do something. https://stackoverflow.com/a/10828333
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I know I added a lot in one batch. It was just one day I was like "hey, let me just get all these things I created over time made into isolated PRs".
After this batch, I think I just have two large WIP: ESM and Handlers/Interceptors, but I want to see them work reliably in my own project before considering them merge for PR. But I might consider creating refactor PRs based on some of the discussions regarding helpers/resource management/RAII