Skip to content
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

Add the ThrowException fn to the isolate #221

Merged
merged 10 commits into from
Nov 10, 2021

Conversation

wisepythagoras
Copy link
Contributor

@wisepythagoras wisepythagoras commented Oct 31, 2021

I've re-implemented the ThrowException function from #143, but added support for passing in a Value instead of just a string, as pointed out by @dylanahsmith. This allows us to pass a string, or forward in a TypeError.

A note: The return value is always null or undefined.

@codecov
Copy link

codecov bot commented Oct 31, 2021

Codecov Report

Merging #221 (8252b3e) into master (4a6bcfb) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #221      +/-   ##
==========================================
+ Coverage   95.83%   95.86%   +0.02%     
==========================================
  Files          17       17              
  Lines         576      580       +4     
==========================================
+ Hits          552      556       +4     
  Misses         15       15              
  Partials        9        9              
Impacted Files Coverage Δ
isolate.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4a6bcfb...8252b3e. Read the comment docs.

@genevieve
Copy link
Collaborator

Might be able to test it by calling the function from within another function like:

func TestIsolateThrowException(t *testing.T) {
	t.Parallel()
	iso := v8.NewIsolate()
	defer iso.Dispose()

	typeErr, err := v8.NewValue(iso, "some type error")
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	fn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
		v := iso.ThrowException(typeErr)
		if !v.IsNull() && !v.IsUndefined() {
			t.Error("expected result to be <nil> or undefined")
		}
		return nil
	})

	global := v8.NewObjectTemplate(iso)
	global.Set("foo", fn)

	ctx := v8.NewContext(iso, global)
	defer ctx.Close()

	_, e := ctx.RunScript("foo()", "forever.js")
	if e.Error() != "some type error" {
		t.Errorf("expected error but got: %v", e)
	}
}

@wisepythagoras
Copy link
Contributor Author

Thanks, @genevieve. I had forgotten about the error returned by RunScript. I modified it a little to include a test case for a TypeError.

isolate.go Outdated Show resolved Hide resolved
isolate_test.go Outdated Show resolved Hide resolved
isolate_test.go Outdated Show resolved Hide resolved
isolate_test.go Outdated Show resolved Hide resolved
v8go.cc Outdated Show resolved Hide resolved
@genevieve genevieve linked an issue Nov 9, 2021 that may be closed by this pull request
v8go.cc Outdated Show resolved Hide resolved
@dylanahsmith dylanahsmith merged commit d8d94c2 into rogchap:master Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Isolate.ThrowException(exception) support
3 participants