From 40a3423cd78a2179ac70d1f1b3012bd6aca60b3a Mon Sep 17 00:00:00 2001 From: sharmaashish13 Date: Tue, 28 Dec 2021 18:45:37 +0530 Subject: [PATCH] build fix after latest rebase --- array_test.go | 51 ++++++++++++++++--------------------- arraybuffer_test.go | 61 +++++++++++++++++++-------------------------- isolate.go | 8 ------ object_test.go | 32 ++++++++---------------- v8go.cc | 29 +++++++++++---------- value_test.go | 12 +++------ 6 files changed, 75 insertions(+), 118 deletions(-) diff --git a/array_test.go b/array_test.go index b097f5f1b..2644bcc46 100644 --- a/array_test.go +++ b/array_test.go @@ -1,23 +1,24 @@ -package v8go +package v8go_test import ( "errors" "fmt" - "log" "testing" + + v8 "rogchap.com/v8go" ) -func reverseUint8ArrayFunctionCallback(info *FunctionCallbackInfo) *Value { - iso, err := info.Context().Isolate() - if err != nil { - log.Fatalf("Could not get isolate from context: %v\n", err) - } +func reverseUint8ArrayFunctionCallback(info *v8.FunctionCallbackInfo) *v8.Value { + iso := info.Context().Isolate() args := info.Args() + if len(args) != 1 { - return iso.ThrowException("Function ReverseUint8Array expects 1 parameter") + e, _ := v8.NewValue(iso, "Function ReverseUint8Array expects 1 parameter") + return iso.ThrowException(e) } if !args[0].IsUint8Array() { - return iso.ThrowException("Function ReverseUint8Array expects Uint8Array parameter") + e, _ := v8.NewValue(iso, "Function ReverseUint8Array expects Uint8Array parameter") + return iso.ThrowException(e) } array := args[0].Uint8Array() length := len(array) @@ -25,34 +26,26 @@ func reverseUint8ArrayFunctionCallback(info *FunctionCallbackInfo) *Value { for i := 0; i < length; i++ { reversed[i] = array[length-i-1] } - val, err := NewValue(iso, reversed) + val, err := v8.NewValue(iso, reversed) if err != nil { - return iso.ThrowException(fmt.Sprintf("Could not get value for array: %v\n", err)) + e, _ := v8.NewValue(iso, fmt.Sprintf("Could not get value for array: %v\n", err)) + return iso.ThrowException(e) } return val } -func injectUint8ArrayTester(ctx *Context) error { +func injectUint8ArrayTester(ctx *v8.Context) error { if ctx == nil { return errors.New("injectUint8ArrayTester: ctx is required") } - iso, err := ctx.Isolate() - if err != nil { - return fmt.Errorf("injectUint8ArrayTester: %v", err) - } + iso := ctx.Isolate() - con, err := NewObjectTemplate(iso) - if err != nil { - return fmt.Errorf("injectUint8ArrayTester: %v", err) - } + con := v8.NewObjectTemplate(iso) - reverseFn, err := NewFunctionTemplate(iso, reverseUint8ArrayFunctionCallback) - if err != nil { - return fmt.Errorf("injectUint8ArrayTester: %v", err) - } + reverseFn := v8.NewFunctionTemplate(iso, reverseUint8ArrayFunctionCallback) - if err := con.Set("reverseUint8Array", reverseFn, ReadOnly); err != nil { + if err := con.Set("reverseUint8Array", reverseFn, v8.ReadOnly); err != nil { return fmt.Errorf("injectUint8ArrayTester: %v", err) } @@ -74,8 +67,8 @@ func injectUint8ArrayTester(ctx *Context) error { func TestUint8Array(t *testing.T) { t.Parallel() - iso, _ := NewIsolate() - ctx, _ := NewContext(iso) + iso := v8.NewIsolate() + ctx := v8.NewContext(iso) if err := injectUint8ArrayTester(ctx); err != nil { t.Error(err) @@ -104,8 +97,8 @@ func TestUint8Array(t *testing.T) { func TestUint8ArrayException(t *testing.T) { t.Parallel() - iso, _ := NewIsolate() - ctx, _ := NewContext(iso) + iso := v8.NewIsolate() + ctx := v8.NewContext(iso) if err := injectUint8ArrayTester(ctx); err != nil { t.Error(err) diff --git a/arraybuffer_test.go b/arraybuffer_test.go index 83d3dbfc2..3a04a0936 100644 --- a/arraybuffer_test.go +++ b/arraybuffer_test.go @@ -1,23 +1,23 @@ -package v8go +package v8go_test import ( "errors" "fmt" - "log" "testing" + + v8 "rogchap.com/v8go" ) -func reverseArrayBufferFunctionCallback(info *FunctionCallbackInfo) *Value { - iso, err := info.Context().Isolate() - if err != nil { - log.Fatalf("Could not get isolate from context: %v\n", err) - } +func reverseArrayBufferFunctionCallback(info *v8.FunctionCallbackInfo) *v8.Value { + iso := info.Context().Isolate() args := info.Args() if len(args) != 1 { - return iso.ThrowException("Function ReverseArrayBuffer expects 1 parameter") + e, _ := v8.NewValue(iso, "Function ReverseArrayBuffer expects 1 parameter") + return iso.ThrowException(e) } if !args[0].IsArrayBuffer() { - return iso.ThrowException("Function ReverseArrayBuffer expects ArrayBuffer parameter") + e, _ := v8.NewValue(iso, "Function ReverseArrayBuffer expects ArrayBuffer parameter") + return iso.ThrowException(e) } ab := args[0].ArrayBuffer() // "cast" to ArrayBuffer length := int(ab.ByteLength()) @@ -30,20 +30,19 @@ func reverseArrayBufferFunctionCallback(info *FunctionCallbackInfo) *Value { return nil } -func createArrayBufferFunctionCallback(info *FunctionCallbackInfo) *Value { - iso, err := info.Context().Isolate() - if err != nil { - log.Fatalf("Could not get isolate from context: %v\n", err) - } +func createArrayBufferFunctionCallback(info *v8.FunctionCallbackInfo) *v8.Value { + iso := info.Context().Isolate() args := info.Args() if len(args) != 1 { - return iso.ThrowException("Function CreateArrayBuffer expects 1 parameter") + e, _ := v8.NewValue(iso, "Function CreateArrayBuffer expects 1 parameter") + return iso.ThrowException(e) } if !args[0].IsInt32() { - return iso.ThrowException("Function CreateArrayBuffer expects Int32 parameter") + e, _ := v8.NewValue(iso, "Function CreateArrayBuffer expects Int32 parameter") + return iso.ThrowException(e) } length := args[0].Int32() - ab := NewArrayBuffer(info.Context(), int64(length)) // create ArrayBuffer object of given length + ab := v8.NewArrayBuffer(info.Context(), int64(length)) // create ArrayBuffer object of given length bytes := make([]uint8, length) for i := uint8(0); i < uint8(length); i++ { bytes[i] = i @@ -52,27 +51,17 @@ func createArrayBufferFunctionCallback(info *FunctionCallbackInfo) *Value { return ab.Value // return the ArrayBuffer to javascript } -func injectArrayBufferTester(ctx *Context, funcName string, funcCb FunctionCallback) error { +func injectArrayBufferTester(ctx *v8.Context, funcName string, funcCb v8.FunctionCallback) error { if ctx == nil { return errors.New("injectArrayBufferTester: ctx is required") } - iso, err := ctx.Isolate() - if err != nil { - return fmt.Errorf("injectArrayBufferTester: %v", err) - } + iso := ctx.Isolate() - con, err := NewObjectTemplate(iso) - if err != nil { - return fmt.Errorf("injectArrayBufferTester: %v", err) - } - - funcTempl, err := NewFunctionTemplate(iso, funcCb) - if err != nil { - return fmt.Errorf("injectArrayBufferTester: %v", err) - } + con := v8.NewObjectTemplate(iso) + funcTempl := v8.NewFunctionTemplate(iso, funcCb) - if err := con.Set(funcName, funcTempl, ReadOnly); err != nil { + if err := con.Set(funcName, funcTempl, v8.ReadOnly); err != nil { return fmt.Errorf("injectArrayBufferTester: %v", err) } @@ -95,8 +84,8 @@ func injectArrayBufferTester(ctx *Context, funcName string, funcCb FunctionCallb func TestModifyArrayBuffer(t *testing.T) { t.Parallel() - iso, _ := NewIsolate() - ctx, _ := NewContext(iso) + iso := v8.NewIsolate() + ctx := v8.NewContext(iso) if err := injectArrayBufferTester(ctx, "reverseArrayBuffer", reverseArrayBufferFunctionCallback); err != nil { t.Error(err) } @@ -132,8 +121,8 @@ func TestModifyArrayBuffer(t *testing.T) { func TestCreateArrayBuffer(t *testing.T) { t.Parallel() - iso, _ := NewIsolate() - ctx, _ := NewContext(iso) + iso := v8.NewIsolate() + ctx := v8.NewContext(iso) if err := injectArrayBufferTester(ctx, "createArrayBuffer", createArrayBufferFunctionCallback); err != nil { t.Error(err) } diff --git a/isolate.go b/isolate.go index 5f4df5e4f..661fbec05 100644 --- a/isolate.go +++ b/isolate.go @@ -185,11 +185,3 @@ func (i *Isolate) getCallback(ref int) FunctionCallback { defer i.cbMutex.RUnlock() return i.cbs[ref] } - -// Throw an exception into javascript land from within a go function callback -func (i *Isolate) ThrowException(msg string) *Value { - cmsg := C.CString(msg) - defer C.free(unsafe.Pointer(cmsg)) - C.ThrowException(i.ptr, cmsg) - return nil -} diff --git a/object_test.go b/object_test.go index aa73a49c1..4fe408462 100644 --- a/object_test.go +++ b/object_test.go @@ -7,9 +7,9 @@ package v8go_test import ( "errors" "fmt" - "log" "testing" + "rogchap.com/v8go" v8 "rogchap.com/v8go" ) @@ -214,16 +214,15 @@ func ExampleObject_global() { } func createObjectFunctionCallback(info *v8go.FunctionCallbackInfo) *v8go.Value { - iso, err := info.Context().Isolate() - if err != nil { - log.Fatalf("Could not get isolate from context: %v\n", err) - } + iso := info.Context().Isolate() args := info.Args() if len(args) != 2 { - return iso.ThrowException("Function createObject expects 2 parameters") + e, _ := v8.NewValue(iso, "Function createObject expects 2 parameters") + return iso.ThrowException(e) } if !args[0].IsInt32() || !args[1].IsInt32() { - return iso.ThrowException("Function createObject expects 2 Int32 parameters") + e, _ := v8.NewValue(iso, "Function createObject expects 2 Int32 parameters") + return iso.ThrowException(e) } read := args[0].Int32() written := args[1].Int32() @@ -238,20 +237,11 @@ func injectObjectTester(ctx *v8go.Context, funcName string, funcCb v8go.Function return errors.New("ctx is required") } - iso, err := ctx.Isolate() - if err != nil { - return fmt.Errorf("ctx.Isolate: %v", err) - } + iso := ctx.Isolate() - con, err := v8go.NewObjectTemplate(iso) - if err != nil { - return fmt.Errorf("NewObjectTemplate: %v", err) - } + con := v8go.NewObjectTemplate(iso) - funcTempl, err := v8go.NewFunctionTemplate(iso, funcCb) - if err != nil { - return fmt.Errorf("NewFunctionTemplate: %v", err) - } + funcTempl := v8go.NewFunctionTemplate(iso, funcCb) if err := con.Set(funcName, funcTempl, v8go.ReadOnly); err != nil { return fmt.Errorf("ObjectTemplate.Set: %v", err) @@ -274,8 +264,8 @@ func injectObjectTester(ctx *v8go.Context, funcName string, funcCb v8go.Function // Test that golang can create an object with "read", "written" int32 properties and pass that back to JS. func TestObjectCreate(t *testing.T) { t.Parallel() - iso, _ := v8go.NewIsolate() - ctx, _ := v8go.NewContext(iso) + iso := v8go.NewIsolate() + ctx := v8go.NewContext(iso) if err := injectObjectTester(ctx, "createObject", createObjectFunctionCallback); err != nil { t.Error(err) diff --git a/v8go.cc b/v8go.cc index 1340083ed..a42bda3b2 100644 --- a/v8go.cc +++ b/v8go.cc @@ -848,7 +848,7 @@ ValuePtr NewValueUndefined(IsolatePtr iso) { // The function takes ownership over the incoming array's memory. ValuePtr NewValueUint8Array(IsolatePtr iso_ptr, const uint8_t *v, int len) { ISOLATE_SCOPE_INTERNAL_CONTEXT(iso_ptr); - Local c = ctx->ptr.Get(iso); + Local c = ctx->ptr.Get(iso_ptr); // The Context::Enter/Exit is only needed when calling this code from low-level unit tests, // otherwise ArrayBuffer::New() trips over missing context. @@ -861,13 +861,13 @@ ValuePtr NewValueUint8Array(IsolatePtr iso_ptr, const uint8_t *v, int len) { free(data); }, nullptr); - Local arbuf = ArrayBuffer::New(iso, std::move(bs)); + Local arbuf = ArrayBuffer::New(iso_ptr, std::move(bs)); m_value* val = new m_value; - val->iso = iso; + val->iso = iso_ptr; val->ctx = ctx; val->ptr = Persistent>( - iso, Uint8Array::New(arbuf, 0, len)); + iso_ptr, Uint8Array::New(arbuf, 0, len)); c->Exit(); // see comment above @@ -1333,13 +1333,12 @@ int ValueIsModuleNamespaceObject(ValuePtr ptr) { ValuePtr NewObject(IsolatePtr iso_ptr) { ISOLATE_SCOPE_INTERNAL_CONTEXT(iso_ptr); - Local c = ctx->ptr.Get(iso); - Local obj = Object::New(iso); + Local obj = Object::New(iso_ptr); m_value* val = new m_value; - val->iso = iso; + val->iso = iso_ptr; val->ctx = ctx; - val->ptr = Persistent>(iso, obj); + val->ptr = Persistent>(iso_ptr, obj); return tracked_value(ctx, val); } @@ -1670,8 +1669,8 @@ ValuePtr FunctionSourceMapUrl(ValuePtr ptr) { void ThrowException(IsolatePtr iso_ptr, const char* message) { ISOLATE_SCOPE(iso_ptr); - Local msg = String::NewFromUtf8(iso, message).ToLocalChecked(); - iso->ThrowException(msg); + Local msg = String::NewFromUtf8(iso_ptr, message).ToLocalChecked(); + iso_ptr->ThrowException(msg); } /********** v8::V8 **********/ @@ -1688,20 +1687,20 @@ void SetFlags(const char* flags) { // Create a new ArrayBuffer value of the requested size ValuePtr NewArrayBuffer(IsolatePtr iso_ptr, size_t byte_length) { ISOLATE_SCOPE_INTERNAL_CONTEXT(iso_ptr); - Local c = ctx->ptr.Get(iso); + Local c = ctx->ptr.Get(iso_ptr); // The Context::Enter/Exit is only needed when calling this code from low-level unit tests, // otherwise ArrayBuffer::New() trips over missing context. // They are not needed when this code gets called through an executing script. c->Enter(); - std::unique_ptr bs = ArrayBuffer::NewBackingStore(iso, byte_length); - Local arbuf = ArrayBuffer::New(iso, std::move(bs)); + std::unique_ptr bs = ArrayBuffer::NewBackingStore(iso_ptr, byte_length); + Local arbuf = ArrayBuffer::New(iso_ptr, std::move(bs)); m_value* val = new m_value; - val->iso = iso; + val->iso = iso_ptr; val->ctx = ctx; - val->ptr = Persistent>(iso, arbuf); + val->ptr = Persistent>(iso_ptr, arbuf); c->Exit(); // see comment above diff --git a/value_test.go b/value_test.go index 993284690..364721446 100644 --- a/value_test.go +++ b/value_test.go @@ -34,16 +34,10 @@ func TestValueNewBaseCases(t *testing.T) { func TestValueNewUint8Array(t *testing.T) { t.Parallel() - ctx, err := v8go.NewContext() - if err != nil { - t.Fatalf("NewContext() error: %v", err) - } - iso, err := ctx.Isolate() - if err != nil { - t.Fatalf("ctx.Isolate() error: %v", err) - } + ctx := v8.NewContext() + iso := ctx.Isolate() in := []uint8{1, 2, 3, 4, 5} - if val, err := v8go.NewValue(iso, in); err != nil { + if val, err := v8.NewValue(iso, in); err != nil { t.Fatalf("Error %v", err) } else if !val.IsUint8Array() { t.Errorf("Val is not []uint")