Skip to content

Commit

Permalink
Reuse the existing RtnString type
Browse files Browse the repository at this point in the history
  • Loading branch information
Genevieve L'Esperance committed Jan 7, 2022
1 parent 3348d80 commit 8eb8550
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
9 changes: 6 additions & 3 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -948,20 +948,23 @@ RtnString ValueToDetailString(ValuePtr ptr) {
return rtn;
}
String::Utf8Value ds(iso, str);
rtn.string = CopyString(ds);
rtn.data = CopyString(ds);
return rtn;
}

FullString ValueToString(ValuePtr ptr) {
RtnString ValueToString(ValuePtr ptr) {
LOCAL_VALUE(ptr);
RtnString rtn = {0};
// String::Utf8Value will result in an empty string if conversion to a string
// fails
// TODO: Consider propagating the JS error. A fallback value could be returned
// in Value.String()
String::Utf8Value src(iso, value);
char* data = static_cast<char*>(malloc(src.length()));
memcpy(data, *src, src.length());
return (FullString){data, src.length()};
rtn.data = data;
rtn.length = src.length();
return rtn;
}

uint32_t ValueToUint32(ValuePtr ptr) {
Expand Down
10 changes: 3 additions & 7 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,11 @@ typedef struct {
RtnError error;
} RtnValue;

typedef struct {
const char* string;
RtnError error;
} RtnString;

typedef struct {
const char* data;
int length;
} FullString;
RtnError error;
} RtnString;

typedef struct {
size_t total_heap_size;
Expand Down Expand Up @@ -207,7 +203,7 @@ extern RtnValue NewValueBigIntFromWords(IsolatePtr iso_ptr,
int sign_bit,
int word_count,
const uint64_t* words);
extern FullString ValueToString(ValuePtr ptr);
extern RtnString ValueToString(ValuePtr ptr);
const uint32_t* ValueToArrayIndex(ValuePtr ptr);
int ValueToBoolean(ValuePtr ptr);
int32_t ValueToInt32(ValuePtr ptr);
Expand Down
4 changes: 2 additions & 2 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ func (v *Value) Boolean() bool {
// DetailString provide a string representation of this value usable for debugging.
func (v *Value) DetailString() string {
rtn := C.ValueToDetailString(v.ptr)
if rtn.string == nil {
if rtn.data == nil {
err := newJSError(rtn.error)
panic(err) // TODO: Return a fallback value
}
s := rtn.string
s := rtn.data
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}
Expand Down

0 comments on commit 8eb8550

Please sign in to comment.