Go Back   Cockos Incorporated Forums > REAPER Forums > REAPER Feature Requests

Reply
 
Thread Tools Display Modes
Old 04-07-2019, 11:03 AM   #1
juliansader
Human being with feelings
 
Join Date: Jul 2009
Posts: 2,584
Default API functions: Parameters with "Optional" tag should pass nullptr (DONE)

Optional parameters can be added to extension-provided API functions by adding the "Optional" suffix to parameter names:

Quote:
// When defining/documenting API function parameters:
// - if a (char*,int) pair is encountered, name them buf, buf_sz
// - if a (const char*,int) pair is encountered, buf, buf_sz as well
// - if a lone basicType *, use varNameOut or varNameIn or
// varNameInOptional (if last parameter(s))
However, there are two problems with optional parameters, both of which (as far as I can see) make it impossible for extensions to know which arguments were passed by the script and which were not:

* Even though extensions should register a "vararg" wrapper function for each API function, and this "vararg" function takes two arguments, (void* arglist[], int numparms), this isn't truly a variadic function since numparms is always the same, namely the total number of possible parameters, including all optional ones.

* As implied by the previous point, optional parameters that are omitted by the scripts are still all included in the arglist[]. This would have been acceptable, if the extension could somehow distinguish omitted parameters, for example if omitted parameters were NULL pointers (NB: optional parameters are always pointers). However, omitted parameters are not NULL pointers; instead they look like normal parameters and they simply point to zero values. From the extension's perspective, there is no difference between an omitted parameter and a parameter with argument 0.


Optional parameters are very useful, particularly to expand the features of an existing API function without breaking existing scripts.

There are two possible solutions:
* numparms should reflect the actual number of arguments passed, or
* omitted parameters should be nullptr.
juliansader is offline   Reply With Quote
Old 05-18-2019, 02:41 PM   #2
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 8,795
Default

I ran into this too:
https://forum.cockos.com/showthread.php?t=221102

Last edited by nofish; 05-18-2019 at 03:45 PM.
nofish is offline   Reply With Quote
Old 05-18-2019, 03:56 PM   #3
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,090
Default

Looking at the code on our side, optional arguments from Lua to extension-provided functions should work for REAPER-specific datatypes (like MediaTrack*, etc), but not int, double, etc. Does that match what you're seeing?
schwa is offline   Reply With Quote
Old 05-18-2019, 04:15 PM   #4
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 8,795
Default

Thanks schwa.
I can define an extension-provided function which takes e.g. an int* as optional argument and that function can be called fine (i.e. without error) from ReaScript also if the caller doesn't provide the optional argument. (In case the caller does provide the argument, it gets assigned to the pointer correctly.)
edit:
I haven't tried this with REAPER-specific datatypes yet though.

The issue is that the extension can't differentiate if the caller provided the additional argument or not because Reaper always assigns 0 (zero value) to it (see OP).

Therefore we're suggesting that a nullptr is assigned / passed in case the caller doesn't provide an optional argument instead of assigning zero value.
Does this address your question?

Last edited by nofish; 05-18-2019 at 04:29 PM.
nofish is offline   Reply With Quote
Old 05-18-2019, 05:46 PM   #5
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 10,090
Default

I'm saying that for REAPER-specific datatypes like MediaTrack*, NULL is passed if the script doesn't supply the parameter. The missing piece is that this doesn't also happen for pointers to plain datatypes.
schwa is offline   Reply With Quote
Old 05-18-2019, 06:04 PM   #6
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 8,795
Default

Sorry, wasn't sure what you mean by 'should work' exactly but yep, that seems it.
nofish is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:55 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.