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

Support for invoking a function value #100

Merged
merged 4 commits into from
Apr 9, 2021
Merged

Conversation

robfig
Copy link
Contributor

@robfig robfig commented Apr 3, 2021

Fixes #95

@robfig
Copy link
Contributor Author

robfig commented Apr 3, 2021

This is a work in progress -- still need to add more tests -- but let me know if I'm not on the right track.

Thanks!

@codecov
Copy link

codecov bot commented Apr 3, 2021

Codecov Report

Merging #100 (1afdf87) into master (6341a3b) will increase coverage by 0.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #100      +/-   ##
==========================================
+ Coverage   96.53%   96.61%   +0.08%     
==========================================
  Files          11       12       +1     
  Lines         404      414      +10     
==========================================
+ Hits          390      400      +10     
  Misses          9        9              
  Partials        5        5              
Impacted Files Coverage Δ
function.go 100.00% <100.00%> (ø)
value.go 97.53% <100.00%> (+0.04%) ⬆️

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 6341a3b...1afdf87. Read the comment docs.

@robfig
Copy link
Contributor Author

robfig commented Apr 3, 2021

OK, this should have 100% test coverage. The current failure is about a missing FOSSA key(?)

Copy link
Collaborator

@tmc tmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks reasonable to me. @rogchap ?

Copy link
Owner

@rogchap rogchap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thank you for putting this PR together, just a few minor comments/changes

function.go Outdated
}

// Call this JavaScript function with the given arguments.
func (fn *Function) Call(args []*Value) (*Value, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the args to be the Valuer interface? Then you can use any Value type. ie Object etc

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

function.go Outdated
var rtn C.RtnValue
if len(args) == 0 {
rtn = C.FunctionCall(fn.ptr, C.int(0), (*C.ValuePtr)(nil))
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this else statement needed? The range would still work with a empty array?
If we need to pass the nil pointer can we return in the above if statement to avoid the extra branching caused by the else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was something I found by writing the test -- to get the *C.ValuePtr, I have to take the value of the first element, and that is an illegal index on an empty slice.

Updated to avoid calling FunctionCall in two different branches.

@robfig
Copy link
Contributor Author

robfig commented Apr 5, 2021

Thanks for the review. Comments addressed, and tests pass locally.

Copy link
Owner

@rogchap rogchap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have mentioned this in the first review, sorry for the back and forth. Please could you also update the CHANGELOG.md? Don't forget to add your attribution 💪

function_test.go Outdated
failIf(t, err)

fn, _ := addValue.AsFunction()
resultValue, err := fn.Call([]v8go.Valuer{arg1, arg1})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great @robfig.
Sorry to be a pain, but looking at this I'm wondering if a variadic function would be more idiomatic? Then you could do:

fn.Call(arg1, arg2, etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem at all -- agreed that it looks cleaner. Updated

@robfig
Copy link
Contributor Author

robfig commented Apr 5, 2021

Added & updated!

@rogchap
Copy link
Owner

rogchap commented Apr 5, 2021

Perfect @robfig
Thanks again. I'll fix up the FOSSA error (GitHub secrets don't work in forks)

@robfig
Copy link
Contributor Author

robfig commented Apr 6, 2021

Sounds good -- not sure if you expect me to merge, but I don't have access to do that despite your approval. I'll leave it to you.

@rogchap
Copy link
Owner

rogchap commented Apr 6, 2021

I'll merge shortly.

@rogchap rogchap merged commit 25d7afa into rogchap:master Apr 9, 2021
@robfig robfig deleted the invoke branch April 13, 2021 01:13
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.

Invoke a v8go.Value ?
3 participants