-
-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support FunctionCallback returning an error, and native Exceptions. #195
Draft
tommie
wants to merge
4
commits into
rogchap:master
Choose a base branch
from
tommie:exception
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go | ||
|
||
import ( | ||
// #include <stdlib.h> | ||
// #include "v8go.h" | ||
"C" | ||
|
||
"fmt" | ||
"unsafe" | ||
) | ||
|
||
// NewRangeError creates a RangeError. | ||
func NewRangeError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_RANGE, msg) | ||
} | ||
|
||
// NewReferenceError creates a ReferenceError. | ||
func NewReferenceError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_REFERENCE, msg) | ||
} | ||
|
||
// NewSyntaxError creates a SyntaxError. | ||
func NewSyntaxError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_SYNTAX, msg) | ||
} | ||
|
||
// NewTypeError creates a TypeError. | ||
func NewTypeError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_TYPE, msg) | ||
} | ||
|
||
// NewWasmCompileError creates a WasmCompileError. | ||
func NewWasmCompileError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_WASM_COMPILE, msg) | ||
} | ||
|
||
// NewWasmLinkError creates a WasmLinkError. | ||
func NewWasmLinkError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_WASM_LINK, msg) | ||
} | ||
|
||
// NewWasmRuntimeError creates a WasmRuntimeError. | ||
func NewWasmRuntimeError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_WASM_RUNTIME, msg) | ||
} | ||
|
||
// NewError creates an Error, which is the common thing to throw from | ||
// user code. | ||
func NewError(iso *Isolate, msg string) *Exception { | ||
return newExceptionError(iso, C.ERROR_GENERIC, msg) | ||
} | ||
|
||
func newExceptionError(iso *Isolate, typ C.ErrorTypeIndex, msg string) *Exception { | ||
cmsg := C.CString(msg) | ||
defer C.free(unsafe.Pointer(cmsg)) | ||
eptr := C.NewValueError(iso.ptr, typ, cmsg) | ||
if eptr == nil { | ||
panic(fmt.Errorf("invalid error type index: %d", typ)) | ||
} | ||
return &Exception{&Value{ptr: eptr}} | ||
} | ||
|
||
// An Exception is a JavaScript exception. | ||
type Exception struct { | ||
*Value | ||
} | ||
|
||
// value implements Valuer. | ||
func (e *Exception) value() *Value { | ||
return e.Value | ||
} | ||
|
||
// Error implements error. | ||
func (e *Exception) Error() string { | ||
return e.String() | ||
} | ||
|
||
// As provides support for errors.As. | ||
func (e *Exception) As(target interface{}) bool { | ||
ep, ok := target.(**Exception) | ||
if !ok { | ||
return false | ||
} | ||
*ep = e | ||
return true | ||
} | ||
|
||
// Is provides support for errors.Is. | ||
func (e *Exception) Is(err error) bool { | ||
eerr, ok := err.(*Exception) | ||
if !ok { | ||
return false | ||
} | ||
return eerr.String() == e.String() | ||
} | ||
|
||
// String implements fmt.Stringer. | ||
func (e *Exception) String() string { | ||
if e.Value == nil { | ||
return "<nil>" | ||
} | ||
s := C.ExceptionGetMessageString(e.ptr) | ||
defer C.free(unsafe.Pointer(s)) | ||
return C.GoString(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go_test | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
v8 "rogchap.com/v8go" | ||
) | ||
|
||
func TestNewError(t *testing.T) { | ||
t.Parallel() | ||
|
||
tsts := []struct { | ||
New func(*v8.Isolate, string) *v8.Exception | ||
WantType string | ||
}{ | ||
{v8.NewRangeError, "RangeError"}, | ||
{v8.NewReferenceError, "ReferenceError"}, | ||
{v8.NewSyntaxError, "SyntaxError"}, | ||
{v8.NewTypeError, "TypeError"}, | ||
{v8.NewWasmCompileError, "CompileError"}, | ||
{v8.NewWasmLinkError, "LinkError"}, | ||
{v8.NewWasmRuntimeError, "RuntimeError"}, | ||
{v8.NewError, "Error"}, | ||
} | ||
for _, tst := range tsts { | ||
t.Run(tst.WantType, func(t *testing.T) { | ||
iso := v8.NewIsolate() | ||
defer iso.Dispose() | ||
|
||
got := tst.New(iso, "amessage") | ||
if !got.IsNativeError() { | ||
t.Error("IsNativeError returned false, want true") | ||
} | ||
if got := got.Error(); !strings.Contains(got, " "+tst.WantType+":") { | ||
t.Errorf("Error(): got %q, want containing %q", got, tst.WantType) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestExceptionAs(t *testing.T) { | ||
iso := v8.NewIsolate() | ||
defer iso.Dispose() | ||
|
||
want := v8.NewRangeError(iso, "faked error") | ||
|
||
var got *v8.Exception | ||
if !want.As(&got) { | ||
t.Fatalf("As failed") | ||
} | ||
|
||
if got != want { | ||
t.Errorf("As: got %#v, want %#v", got, want) | ||
} | ||
} | ||
|
||
func TestExceptionIs(t *testing.T) { | ||
iso := v8.NewIsolate() | ||
defer iso.Dispose() | ||
|
||
t.Run("ok", func(t *testing.T) { | ||
ex := v8.NewRangeError(iso, "faked error") | ||
if !ex.Is(v8.NewRangeError(iso, "faked error")) { | ||
t.Fatalf("Is: got false, want true") | ||
} | ||
}) | ||
|
||
t.Run("notok", func(t *testing.T) { | ||
if (&v8.Exception{}).Is(errors.New("other error")) { | ||
t.Fatalf("Is: got true, want false") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already the JSError type which is used to return exceptions from v8go functions. It seems like there should be consistency between the exception value returned by other v8go functions and these functions to construct errors. That should make it easier to propagate exceptions, even without adding a new feature to return errors from function callbacks.
Currently, JSError is converting the underlying JS error object to a string and storing it in the Message field. Instead, it should store the underlying JS error object, so that the error can be propagated. That Message field could be deprecated in favour of a method so that the conversion can be done on-demand in the future. It looks like the stack trace can similarly be obtained on-demand using v8::Exception::GetStackTrace.
The JSError.Location field is derived from the a v8::Message on v8::TryCatch, which may be present without a stack trace for compilation errors. v8::Exception::CreateMessage can be used to obtained a v8::Message from an exception value, but we don't want to automatically create it in a getter method. Since it is a public field, it will need to continue populating on creation for the current code paths, but it could similarly be deprecated in favour of a method to obtain this on-demand for new code paths. This on-demand code path could at least obtain the when the error has a stack trace, even if we don't get it to initially work on this new code path errors with a location but no stack trace.
It seems like this refactoring and the addition of these functions to create exceptions could be split out into a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened this as an issue (#274) for greater visibility on this issue with JSError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember exactly why I created a new type for it, but it was probably to avoid having to refactor
JSError
(avoiding merge conflicts). If there's consensus that's better, that sounds good to me. Thanks for reviewing.