Skip to content

Commit

Permalink
byte array to string. with issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmaashish13 committed Jan 5, 2022
1 parent a0417ad commit d4e9353
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 6 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 (
_ "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"
)
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module rogchap.com/v8go
module github.com/esoptra/v8go

go 1.16

require rogchap.com/v8go v0.7.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rogchap.com/v8go v0.7.0 h1:kgjbiO4zE5itA962ze6Hqmbs4HgZbGzmueCXsZtremg=
rogchap.com/v8go v0.7.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs=
19 changes: 19 additions & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<Value, CopyablePersistentTraits<Value>>(iso, str);
rtn.value = tracked_value(ctx, val);
return rtn;
}

RtnValue NewValueString(IsolatePtr iso, const char* v) {
ISOLATE_SCOPE_INTERNAL_CONTEXT(iso);
TryCatch try_catch(iso);
Expand Down
1 change: 1 addition & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ extern ValuePtr NewValueNull(IsolatePtr iso_ptr);
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 NewValueStringFromByteArray(IsolatePtr iso, const uint8_t* v, int len);
extern RtnValue NewValueString(IsolatePtr iso_ptr, const char* v);
extern ValuePtr NewValueBoolean(IsolatePtr iso_ptr, int v);
extern ValuePtr NewValueNumber(IsolatePtr iso_ptr, double v);
Expand Down
9 changes: 9 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nil>")
}
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
Expand Down
21 changes: 21 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ 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 @@ -473,6 +474,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()
Expand Down

0 comments on commit d4e9353

Please sign in to comment.