From be4941d50ee97620cad3531a9d4ae4227b6fc2bb Mon Sep 17 00:00:00 2001 From: sharmaashish13 Date: Wed, 5 Jan 2022 11:31:38 +0530 Subject: [PATCH 1/6] byte array to string. with issues --- cgo.go | 10 +++++----- go.mod | 4 +++- go.sum | 2 ++ v8go.cc | 19 +++++++++++++++++++ v8go.h | 1 + value.go | 9 +++++++++ value_test.go | 20 ++++++++++++++++++++ 7 files changed, 59 insertions(+), 6 deletions(-) diff --git a/cgo.go b/cgo.go index a8d2a8783..03a64a5c5 100644 --- a/cgo.go +++ b/cgo.go @@ -18,9 +18,9 @@ import "C" // contain V8 libraries and headers which otherwise would be ignored. // DO NOT REMOVE import ( - _ "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" + _ "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" ) diff --git a/go.mod b/go.mod index 0bacfa294..6a2a37881 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ -module rogchap.com/v8go +module github.com/esoptra/v8go go 1.16 + +require rogchap.com/v8go v0.7.0 diff --git a/go.sum b/go.sum index e69de29bb..a95b64830 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +rogchap.com/v8go v0.7.0 h1:kgjbiO4zE5itA962ze6Hqmbs4HgZbGzmueCXsZtremg= +rogchap.com/v8go v0.7.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs= diff --git a/v8go.cc b/v8go.cc index 97390883e..d4d94146f 100644 --- a/v8go.cc +++ b/v8go.cc @@ -808,6 +808,25 @@ ValuePtr NewValueIntegerFromUnsigned(IsolatePtr iso, uint32_t v) { return tracked_value(ctx, val); } +RtnValue NewValueStringFromByteArray(IsolatePtr iso, const uint8_t* v, int len){ + ISOLATE_SCOPE_INTERNAL_CONTEXT(iso); + TryCatch try_catch(iso); + RtnValue rtn = {}; + Local str; + if (!String::NewFromOneByte(iso, v).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>(iso, str); + rtn.value = tracked_value(ctx, val); + return rtn; +} + RtnValue NewValueString(IsolatePtr iso, const char* v, int v_length) { ISOLATE_SCOPE_INTERNAL_CONTEXT(iso); TryCatch try_catch(iso); diff --git a/v8go.h b/v8go.h index 7acaf0425..68c2aa28e 100644 --- a/v8go.h +++ b/v8go.h @@ -195,6 +195,7 @@ extern ValuePtr NewValueUndefined(IsolatePtr iso_ptr); extern ValuePtr NewValueInteger(IsolatePtr iso_ptr, int32_t v); extern ValuePtr NewValueIntegerFromUnsigned(IsolatePtr iso_ptr, uint32_t v); extern RtnValue NewValueString(IsolatePtr iso_ptr, const char* v, int v_length); +extern RtnValue NewValueStringFromByteArray(IsolatePtr iso, const uint8_t* v, int len); extern ValuePtr NewValueBoolean(IsolatePtr iso_ptr, int v); extern ValuePtr NewValueNumber(IsolatePtr iso_ptr, double v); extern ValuePtr NewValueBigInt(IsolatePtr iso_ptr, int64_t v); diff --git a/value.go b/value.go index c4ba9acf0..dc65daa0f 100644 --- a/value.go +++ b/value.go @@ -53,6 +53,15 @@ func Null(iso *Isolate) *Value { return iso.null } +func NewStringFromByteArray(iso *Isolate, val []byte) (*Value, error) { + if iso == nil { + return nil, errors.New("v8go: failed to create new Value: Isolate cannot be ") + } + cUint := (*C.uchar)(unsafe.Pointer(&val[0])) + rtnVal := C.NewValueStringFromByteArray(iso.ptr, cUint, C.int(len(val))) + return valueResult(nil, rtnVal) +} + // NewValue will create a primitive value. Supported values types to create are: // string -> V8::String // int32 -> V8::Integer diff --git a/value_test.go b/value_test.go index d386a5893..e948c39db 100644 --- a/value_test.go +++ b/value_test.go @@ -519,6 +519,26 @@ func TestValueFunction(t *testing.T) { } +func TestNewStringFromByteArray(t *testing.T) { + t.Parallel() + 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) { t.Parallel() iso := v8.NewIsolate() From 47c6c3d538dbd87dc548a8d295f848dbef467a03 Mon Sep 17 00:00:00 2001 From: sharmaashish13 Date: Thu, 6 Jan 2022 12:39:14 +0530 Subject: [PATCH 2/6] convert byte array to string as NewValueString is unable to handle binary strings --- cgo.go | 10 +++++----- go.mod | 6 ++---- go.sum | 2 -- v8go.cc | 6 ++---- value_test.go | 6 ------ 5 files changed, 9 insertions(+), 21 deletions(-) diff --git a/cgo.go b/cgo.go index 03a64a5c5..a8d2a8783 100644 --- a/cgo.go +++ b/cgo.go @@ -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" ) diff --git a/go.mod b/go.mod index 6a2a37881..4f34d027e 100644 --- a/go.mod +++ b/go.mod @@ -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 \ No newline at end of file diff --git a/go.sum b/go.sum index a95b64830..e69de29bb 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +0,0 @@ -rogchap.com/v8go v0.7.0 h1:kgjbiO4zE5itA962ze6Hqmbs4HgZbGzmueCXsZtremg= -rogchap.com/v8go v0.7.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs= diff --git a/v8go.cc b/v8go.cc index d4d94146f..5f2e58a93 100644 --- a/v8go.cc +++ b/v8go.cc @@ -813,18 +813,16 @@ RtnValue NewValueStringFromByteArray(IsolatePtr iso, const uint8_t* v, int len){ TryCatch try_catch(iso); RtnValue rtn = {}; Local 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>(iso, str); rtn.value = tracked_value(ctx, val); - return rtn; + return rtn; } RtnValue NewValueString(IsolatePtr iso, const char* v, int v_length) { diff --git a/value_test.go b/value_test.go index e948c39db..e0bcc9493 100644 --- a/value_test.go +++ b/value_test.go @@ -524,8 +524,6 @@ 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) @@ -533,10 +531,6 @@ func TestNewStringFromByteArray(t *testing.T) { 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) { From 3951f1e6b22cdb9993d4662f151f16e5127f3cb3 Mon Sep 17 00:00:00 2001 From: sharmaashish13 Date: Mon, 10 Jan 2022 10:54:44 +0530 Subject: [PATCH 3/6] test fix - compare output string --- value_test.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/value_test.go b/value_test.go index e0bcc9493..14b75fe5b 100644 --- a/value_test.go +++ b/value_test.go @@ -6,6 +6,7 @@ package v8go_test import ( "bytes" + "encoding/base64" "fmt" "math" "math/big" @@ -523,13 +524,20 @@ func TestNewStringFromByteArray(t *testing.T) { t.Parallel() 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} - val, err := v8.NewStringFromByteArray(iso, input) + + inputString := "CN7ySxWjjWNpbYPB1n/TBR8avujZS2cHdXRR5ZM7Fi6QBjlVzBqPu0CI9pQXcW9fvbMXdqU+57XY/QdGozJT19+gbQDM0ZQVzjwqtpyLorcPHjMqum+7dHD6XF5cXo3NZlKGsxcnvSVyClBDU5M1dUCe8bB9yV0wVM6ge+0WAmTX2GYbncilTjDw0bSJI1Z+71NT8UQCfmimKhVxJiKrnkaTrTw2Ma/1I2w4Dny3cRlFtCtob9cvNOeeIm8HtQoi/7HXoE0uFr1C39OL2hCC1TJsxX94djtNFqd9aUOPYrwT+zErSokSvbNYS5WpEjEpRJze9+TCV9NLmqCnARK4Bw" + byts, _ := base64.RawStdEncoding.DecodeString(inputString) + + expected := "ÞòK£cimƒÁÖÓ¾èÙKgutQå“;.9Ȕ»@ˆö”qo_½³v¥>çµØýF£2S×ß m" + val, err := v8.NewStringFromByteArray(iso, byts) if err != nil { - t.Errorf("expeced nil but got error %#v", err) + t.Errorf("expected nil but got error %#v", err) } if !val.IsString() { - t.Errorf("expeced string but got %s", reflect.TypeOf(val)) + t.Errorf("expected string but got %s", reflect.TypeOf(val)) + } + if val.String() != expected { + t.Errorf("expected not same as actual") } } From 41fcfd313611aaa0bcf969da89a5df81aba1baee Mon Sep 17 00:00:00 2001 From: sharmaashish13 Date: Wed, 12 Jan 2022 21:35:29 +0530 Subject: [PATCH 4/6] added changelog, new method documentation and updated test --- CHANGELOG.md | 3 +++ value.go | 3 ++- value_test.go | 61 ++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab1b80bd4..3dd73c5c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Support for [binary strings](https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary). `NewStringFromByteArray` takes bytes array as input and uses `String::NewFromOneByte` to convert each byte to an equivalent character. + ### Fixed - Use string length to ensure null character-containing strings in Go/JS are not terminated early. - Object.Set with an empty key string is now supported diff --git a/value.go b/value.go index dc65daa0f..fd61d9e2f 100644 --- a/value.go +++ b/value.go @@ -53,9 +53,10 @@ func Null(iso *Isolate) *Value { return iso.null } +// NewStringFromByteArray returns V8::string from byte array. Converts each byte into equivalent character. func NewStringFromByteArray(iso *Isolate, val []byte) (*Value, error) { if iso == nil { - return nil, errors.New("v8go: failed to create new Value: Isolate cannot be ") + panic(errors.New("v8go: failed to create new Value: Isolate cannot be ")) } cUint := (*C.uchar)(unsafe.Pointer(&val[0])) rtnVal := C.NewValueStringFromByteArray(iso.ptr, cUint, C.int(len(val))) diff --git a/value_test.go b/value_test.go index 14b75fe5b..bb599660b 100644 --- a/value_test.go +++ b/value_test.go @@ -6,7 +6,6 @@ package v8go_test import ( "bytes" - "encoding/base64" "fmt" "math" "math/big" @@ -522,23 +521,55 @@ func TestValueFunction(t *testing.T) { func TestNewStringFromByteArray(t *testing.T) { t.Parallel() - ctx := v8.NewContext() - iso := ctx.Isolate() - - inputString := "CN7ySxWjjWNpbYPB1n/TBR8avujZS2cHdXRR5ZM7Fi6QBjlVzBqPu0CI9pQXcW9fvbMXdqU+57XY/QdGozJT19+gbQDM0ZQVzjwqtpyLorcPHjMqum+7dHD6XF5cXo3NZlKGsxcnvSVyClBDU5M1dUCe8bB9yV0wVM6ge+0WAmTX2GYbncilTjDw0bSJI1Z+71NT8UQCfmimKhVxJiKrnkaTrTw2Ma/1I2w4Dny3cRlFtCtob9cvNOeeIm8HtQoi/7HXoE0uFr1C39OL2hCC1TJsxX94djtNFqd9aUOPYrwT+zErSokSvbNYS5WpEjEpRJze9+TCV9NLmqCnARK4Bw" - byts, _ := base64.RawStdEncoding.DecodeString(inputString) + iso := v8.NewIsolate() + defer iso.Dispose() + global := v8.NewObjectTemplate(iso) + + testFn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value { + args := info.Args() + input := args[0].String() + if len(input) == 0 { + //String() terminates input at null character. hence hardcoding for 3rd input + input = "a\x00\xffe" + } + val, _ := v8.NewStringFromByteArray(iso, []byte(input)) + return val + }) + global.Set("foo", testFn, v8.ReadOnly) - expected := "ÞòK£cimƒÁÖÓ¾èÙKgutQå“;.9Ȕ»@ˆö”qo_½³v¥>çµØýF£2S×ß m" - val, err := v8.NewStringFromByteArray(iso, byts) - if err != nil { - t.Errorf("expected nil but got error %#v", err) - } - if !val.IsString() { - t.Errorf("expected string but got %s", reflect.TypeOf(val)) + ctx := v8.NewContext(iso, global) + defer ctx.Close() + tests := [...]struct { + name string + script string + }{ + {"normal string", + ` let res = foo("str"); + if(res.length != 3){ + throw Error("expected length 3") + } + `}, + {"multi-byte sequence", + ` res = foo("Ò"); + if(res.length != 2 ){ + throw Error("expected length 2") + } + `}, + {"null terminated sequence", + ` res = foo(""); + if(res.length != 4){ + throw Error("expected length 4") + } + `}, } - if val.String() != expected { - t.Errorf("expected not same as actual") + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if _, err := ctx.RunScript(tt.script, ""); err != nil { + t.Errorf("expected error, but got error: %v", err) + } + }) } + } func TestValueSameValue(t *testing.T) { From 4dee01c44b1632a9a5fdf8cb24e10b543cf9794e Mon Sep 17 00:00:00 2001 From: sharmaashish13 Date: Mon, 17 Jan 2022 10:52:41 +0530 Subject: [PATCH 5/6] renaming functions --- CHANGELOG.md | 2 +- v8go.cc | 3 ++- v8go.h | 2 +- value.go | 8 ++++---- value_test.go | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dd73c5c5..2bc26fcb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- Support for [binary strings](https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary). `NewStringFromByteArray` takes bytes array as input and uses `String::NewFromOneByte` to convert each byte to an equivalent character. +- NewStringFromBytes to support the creation of [binary strings](https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary) from Go byte array. ### Fixed - Use string length to ensure null character-containing strings in Go/JS are not terminated early. diff --git a/v8go.cc b/v8go.cc index 5f2e58a93..4b24f0a64 100644 --- a/v8go.cc +++ b/v8go.cc @@ -808,7 +808,7 @@ ValuePtr NewValueIntegerFromUnsigned(IsolatePtr iso, uint32_t v) { return tracked_value(ctx, val); } -RtnValue NewValueStringFromByteArray(IsolatePtr iso, const uint8_t* v, int len){ +RtnValue NewStringFromBytes(IsolatePtr iso, const uint8_t* v, int len){ ISOLATE_SCOPE_INTERNAL_CONTEXT(iso); TryCatch try_catch(iso); RtnValue rtn = {}; @@ -979,6 +979,7 @@ RtnString ValueToString(ValuePtr ptr) { // TODO: Consider propagating the JS error. A fallback value could be returned // in Value.String() String::Utf8Value src(iso, value); + printf("___ valuetostring %d \n", src.length()); char* data = static_cast(malloc(src.length())); memcpy(data, *src, src.length()); rtn.data = data; diff --git a/v8go.h b/v8go.h index 68c2aa28e..fb500f7fb 100644 --- a/v8go.h +++ b/v8go.h @@ -195,7 +195,7 @@ extern ValuePtr NewValueUndefined(IsolatePtr iso_ptr); extern ValuePtr NewValueInteger(IsolatePtr iso_ptr, int32_t v); extern ValuePtr NewValueIntegerFromUnsigned(IsolatePtr iso_ptr, uint32_t v); extern RtnValue NewValueString(IsolatePtr iso_ptr, const char* v, int v_length); -extern RtnValue NewValueStringFromByteArray(IsolatePtr iso, const uint8_t* v, int len); +extern RtnValue NewStringFromBytes(IsolatePtr iso, const uint8_t* v, int len); extern ValuePtr NewValueBoolean(IsolatePtr iso_ptr, int v); extern ValuePtr NewValueNumber(IsolatePtr iso_ptr, double v); extern ValuePtr NewValueBigInt(IsolatePtr iso_ptr, int64_t v); diff --git a/value.go b/value.go index fd61d9e2f..bcd07595b 100644 --- a/value.go +++ b/value.go @@ -53,13 +53,13 @@ func Null(iso *Isolate) *Value { return iso.null } -// NewStringFromByteArray returns V8::string from byte array. Converts each byte into equivalent character. -func NewStringFromByteArray(iso *Isolate, val []byte) (*Value, error) { +// NewStringFromBytes returns V8::string from byte array. Creates binary string from bytes. +func NewStringFromBytes(iso *Isolate, bytes []byte) (*Value, error) { if iso == nil { panic(errors.New("v8go: failed to create new Value: Isolate cannot be ")) } - cUint := (*C.uchar)(unsafe.Pointer(&val[0])) - rtnVal := C.NewValueStringFromByteArray(iso.ptr, cUint, C.int(len(val))) + cUint := (*C.uchar)(unsafe.Pointer(&bytes[0])) + rtnVal := C.NewStringFromBytes(iso.ptr, cUint, C.int(len(bytes))) return valueResult(nil, rtnVal) } diff --git a/value_test.go b/value_test.go index bb599660b..ac3d47c3d 100644 --- a/value_test.go +++ b/value_test.go @@ -519,7 +519,7 @@ func TestValueFunction(t *testing.T) { } -func TestNewStringFromByteArray(t *testing.T) { +func TestNewStringFromBytes(t *testing.T) { t.Parallel() iso := v8.NewIsolate() defer iso.Dispose() @@ -532,7 +532,7 @@ func TestNewStringFromByteArray(t *testing.T) { //String() terminates input at null character. hence hardcoding for 3rd input input = "a\x00\xffe" } - val, _ := v8.NewStringFromByteArray(iso, []byte(input)) + val, _ := v8.NewStringFromBytes(iso, []byte(input)) return val }) global.Set("foo", testFn, v8.ReadOnly) From 184e50880a330cd671864d1fec730427920f56e3 Mon Sep 17 00:00:00 2001 From: sharmaashish13 Date: Mon, 17 Jan 2022 10:56:58 +0530 Subject: [PATCH 6/6] test case for null isolate panic --- value_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/value_test.go b/value_test.go index ac3d47c3d..9ed222aab 100644 --- a/value_test.go +++ b/value_test.go @@ -525,6 +525,10 @@ func TestNewStringFromBytes(t *testing.T) { defer iso.Dispose() global := v8.NewObjectTemplate(iso) + if recoverPanic(func() { v8.NewStringFromBytes(nil, []byte{}) }) == nil { + t.Error("expected panic") + } + testFn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value { args := info.Args() input := args[0].String()