Skip to content

Commit

Permalink
convert byte array to string as NewValueString is unable to handle bi…
Browse files Browse the repository at this point in the history
…nary strings
  • Loading branch information
sharmaashish13 committed Jan 6, 2022
1 parent d4e9353 commit 55ffec1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 22 deletions.
10 changes: 5 additions & 5 deletions cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import "C"
// contain V8 libraries and headers which otherwise would be ignored.
// DO NOT REMOVE
import (
_ "github.com/esoptra/v8go/deps/darwin_arm64"
_ "github.com/esoptra/v8go/deps/darwin_x86_64"
_ "github.com/esoptra/v8go/deps/include"
_ "github.com/esoptra/v8go/deps/linux_arm64"
_ "github.com/esoptra/v8go/deps/linux_x86_64"
_ "rogchap.com/v8go/deps/darwin_arm64"
_ "rogchap.com/v8go/deps/darwin_x86_64"
_ "rogchap.com/v8go/deps/include"
_ "rogchap.com/v8go/deps/linux_arm64"
_ "rogchap.com/v8go/deps/linux_x86_64"
)
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/esoptra/v8go
module rogchap.com/v8go

go 1.16

require rogchap.com/v8go v0.7.0
go 1.16
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
rogchap.com/v8go v0.7.0 h1:kgjbiO4zE5itA962ze6Hqmbs4HgZbGzmueCXsZtremg=
rogchap.com/v8go v0.7.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs=
6 changes: 2 additions & 4 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -813,18 +813,16 @@ RtnValue NewValueStringFromByteArray(IsolatePtr iso, const uint8_t* v, int len){
TryCatch try_catch(iso);
RtnValue rtn = {};
Local<String> str;
if (!String::NewFromOneByte(iso, v).ToLocal(&str)) {
if (!String::NewFromOneByte(iso, v, NewStringType::kNormal, len).ToLocal(&str)) {
rtn.error = ExceptionError(try_catch, iso, ctx->ptr.Get(iso));
return rtn;
}
// printf("\n");
// printf("str %d \n", str->Length());
m_value* val = new m_value;
val->iso = iso;
val->ctx = ctx;
val->ptr = Persistent<Value, CopyablePersistentTraits<Value>>(iso, str);
rtn.value = tracked_value(ctx, val);
return rtn;
return rtn;
}

RtnValue NewValueString(IsolatePtr iso, const char* v) {
Expand Down
7 changes: 0 additions & 7 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func TestValueString(t *testing.T) {
}{
{"Number", `13 * 2`, "26"},
{"String", `"string"`, "string"},
{"String with null char", `"before\x00after"`, "before\x00after"},
{"Object", `let obj = {}; obj`, "[object Object]"},
{"Function", `let fn = function(){}; fn`, "function(){}"},
}
Expand Down Expand Up @@ -479,19 +478,13 @@ func TestNewStringFromByteArray(t *testing.T) {
ctx := v8.NewContext()
iso := ctx.Isolate()
input := []byte{8, 222, 242, 75, 21, 163, 141, 99, 105, 109, 131, 193, 214, 127, 211, 5, 31, 26, 190, 232, 217, 75, 103, 7, 117, 116, 81, 229, 147, 59, 22, 46, 144, 6, 57, 85, 204, 26, 143, 187, 64, 136, 246, 148, 23, 113, 111, 95, 189, 179, 23, 118, 165, 62, 231, 181, 216, 253, 7, 70, 163, 50, 83, 215, 223, 160, 109, 0, 204, 209, 148, 21, 206, 60, 42, 182, 156, 139, 162, 183, 15, 30, 51, 42, 186, 111, 187, 116, 112, 250, 92, 94, 92, 94, 141, 205, 102, 82, 134, 179, 23, 39, 189, 37, 114, 10, 80, 67, 83, 147, 53, 117, 64, 158, 241, 176, 125, 201, 93, 48, 84, 206, 160, 123, 237, 22, 2, 100, 215, 216, 102, 27, 157, 200, 165, 78, 48, 240, 209, 180, 137, 35, 86, 126, 239, 83, 83, 241, 68, 2, 126, 104, 166, 42, 21, 113, 38, 34, 171, 158, 70, 147, 173, 60, 54, 49, 175, 245, 35, 108, 56, 14, 124, 183, 113, 25, 69, 180, 43, 104, 111, 215, 47, 52, 231, 158, 34, 111, 7, 181, 10, 34, 255, 177, 215, 160, 77, 46, 22, 189, 66, 223, 211, 139, 218, 16, 130, 213, 50, 108, 197, 127, 120, 118, 59, 77, 22, 167, 125, 105, 67, 143, 98, 188, 19, 251, 49, 43, 74, 137, 18, 189, 179, 88, 75, 149, 169, 18, 49, 41, 68, 156, 222, 247, 228, 194, 87, 211, 75, 154, 160, 167, 1, 18, 184, 7}
expectedLength := len(string(input))
_ = input
val, err := v8.NewStringFromByteArray(iso, input)
if err != nil {
t.Errorf("expeced nil but got error %#v", err)
}
if !val.IsString() {
t.Errorf("expeced string but got %s", reflect.TypeOf(val))
}
if len(val.String()) != expectedLength {
t.Error("expected vs actual length not same")

}
}

func TestValueSameValue(t *testing.T) {
Expand Down

0 comments on commit 55ffec1

Please sign in to comment.