-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support variadic javascript function parameters #726
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
Merged
alexcrichton
merged 15 commits into
rustwasm:master
from
richard-uk1:variadic_js_functions
Sep 3, 2018
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d9fd214
[wip] support variadic javascript function parameters
richard-uk1 a483530
Add codegen to make test work.
richard-uk1 7c83c73
Comment typo
richard-uk1 003cf10
Fix webidl
richard-uk1 385e805
Work on review comments
richard-uk1 5342a26
Clean up checking code a bit
richard-uk1 496d8ca
Actually span makes more sense on whole function
richard-uk1 e92536f
Allow Vec as well as slice
richard-uk1 b8c1f72
Comment out failing code
richard-uk1 8ff0da6
Add more tests then comment them out
richard-uk1 958e557
try removing typecheck
richard-uk1 ce1cb84
Merge branch 'master' into variadic_js_functions
richard-uk1 7d5d845
Add docs and remove typecheck from variadic attribute
richard-uk1 e279987
Merge remote-tracking branch 'upstream/master' into variadic_js_funct…
richard-uk1 5c7e638
Handle variadic no args more gracefully.
richard-uk1 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
# Variadic Parameters | ||
|
||
In javascript, both the types of function arguments, and the number of function arguments are | ||
dynamic. For example | ||
|
||
```js | ||
function sum(...rest) { | ||
let i; | ||
// the old way | ||
let old_way = 0; | ||
for (i=0; i<arguments.length; i++) { | ||
old_way += arguments[i]; | ||
} | ||
// the new way | ||
let new_way = 0; | ||
for (i=0; i<rest.length; i++) { | ||
new_way += rest[i]; | ||
} | ||
// both give the same answer | ||
assert(old_way === new_way); | ||
return new_way; | ||
} | ||
``` | ||
|
||
This function doesn't translate directly into rust, since we don't currently support variadic | ||
arguments on the wasm target. To bind to it, we use a slice as the last argument, and annotate the | ||
function as variadic: | ||
|
||
```rust | ||
#[wasm_bindgen] | ||
extern { | ||
#[wasm_bindgen(variadic)] | ||
fn sum(args: &[i32]) -> i32; | ||
} | ||
``` | ||
|
||
when we call this function, the last argument will be expanded as the javascript expects. | ||
|
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -31,3 +31,4 @@ pub mod slice; | |
pub mod structural; | ||
pub mod u64; | ||
pub mod validate_prt; | ||
pub mod variadic; |
This file contains hidden or 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,59 @@ | ||
const assert = require('assert'); | ||
|
||
// a function for testing numbers | ||
function variadic_sum(...args) { | ||
let answer = 0; | ||
for(var i=0; i<args.length; i++) { | ||
answer += args[i]; | ||
} | ||
return answer; | ||
} | ||
|
||
exports.variadic_sum_u8 = variadic_sum; | ||
exports.variadic_sum_u16 = variadic_sum; | ||
exports.variadic_sum_u32 = variadic_sum; | ||
exports.variadic_sum_u64 = variadic_sum; | ||
exports.variadic_sum_i8 = variadic_sum; | ||
exports.variadic_sum_i16 = variadic_sum; | ||
exports.variadic_sum_i32 = variadic_sum; | ||
exports.variadic_sum_i64 = variadic_sum; | ||
exports.variadic_sum_f32 = variadic_sum; | ||
exports.variadic_sum_f64 = variadic_sum; | ||
exports.variadic_sum_rest_vec = variadic_sum; | ||
|
||
// a function for testing nullable numbers | ||
function variadic_sum_opt(...args) { | ||
let answer = 0; | ||
for(var i=0; i<args.length; i++) { | ||
if(args[i] != null) { | ||
answer += args[i]; | ||
} | ||
} | ||
return answer; | ||
} | ||
|
||
exports.variadic_sum_opt = variadic_sum_opt; | ||
|
||
// a function for testing strings | ||
function variadic_concat(...args) { | ||
let answer = ""; | ||
for(var i=0; i<args.length; i++) { | ||
answer = `${answer}${args[i]}`; | ||
} | ||
return answer; | ||
} | ||
|
||
exports.variadic_concat_str = variadic_concat; | ||
exports.variadic_concat_string = variadic_concat; | ||
|
||
// a test that takes any type of arguments (for testing JsValue). | ||
function variadic_compare_pairs(...args) { | ||
assert(args.length % 2 == 0, "args must be sequence of pairs"); | ||
for(var i=0; i<args.length; i++) { | ||
const first = args[i++]; | ||
const second = args[i]; | ||
assert.equal(first, second); | ||
} | ||
} | ||
|
||
exports.variadic_compare_pairs = variadic_compare_pairs; |
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.
Stylistically I think this could just be
bail!("...")