-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
|
@@ -565,3 +585,9 @@ int ValueIsModuleNamespaceObject(ValuePtr ptr) { | |
LOCAL_VALUE(ptr); | ||
return value->IsModuleNamespaceObject(); | ||
} | ||
|
||
RtnString ValueTypeOf(ValuePtr ptr) { | ||
LOCAL_VALUE(ptr); | ||
Local<String> result = value->TypeOf(iso); | ||
return StringToRtnString(iso, result); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,8 +240,8 @@ func (v *Value) Object() *Object { | |
// are returned as-is, objects will return `[object Object]` and functions will | ||
// print their definition. | ||
func (v *Value) String() string { | ||
s := C.ValueToString(v.ptr) | ||
defer C.free(unsafe.Pointer(s.data)) | ||
var s C.RtnString = C.ValueToString(v.ptr) | ||
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. Normally |
||
defer C.RtnStringRelease(s) | ||
return C.GoStringN(s.data, C.int(s.length)) | ||
} | ||
|
||
|
@@ -614,3 +614,9 @@ func (v *Value) SharedArrayBufferGetContents() ([]byte, func(), error) { | |
|
||
return byte_slice, release, nil | ||
} | ||
|
||
func (v *Value) TypeOf() string { | ||
var s C.RtnString = C.ValueTypeOf(v.ptr) | ||
defer C.RtnStringRelease(s) | ||
return C.GoStringN(s.data, C.int(s.length)) | ||
} |
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