Old 04-15-2018, 02:55 PM   #1
preferred.nomenclature
Human being with feelings
 
Join Date: Dec 2014
Posts: 371
Default mutating arguments a la midirecv

After learning how to program in that other JS language (JavaScript), I'm finally getting the hang of Reaper's JSFX. I am trying to implement my own pseudo-array methods using pseudo-object notation, and I've got
Code:
someArray.push(foo)
working but just realized there is no function pop() I could write to satisfy the use case
Code:
foo = someArray.pop()
since the return value from pop() doesn't get assigned to anything (...do I have that right?).

Then I thought maybe I could write it something like
Code:
someArray.pop(foo)
and mutate foo within the function, since that seems to be how midirecv works (you call midirecv(foo, bar, baz) and on the next line foo bar and baz have values they didn't have before) - but I'm guessing that's an implementation specific to midirecv.

I suppose I'm just looking for confirmation that there's no way in JS to pass in a variable by name, mutate it within a function and access that mutated value by name again outside of the function. (and/or to write a function that assigns a value to the left of = regardless of of the right side value it was called on)

Thanks! I look forward to sharing some fun MIDI FX as soon as I've tinkered with them to my satisfaction.
preferred.nomenclature is offline   Reply With Quote
Old 04-15-2018, 04:10 PM   #2
geraintluff
Human being with feelings
 
geraintluff's Avatar
 
Join Date: Nov 2009
Location: mostly inside my own head
Posts: 346
Default

You're correct, modifying arguments is a privilege only built-in functions get.

I'm not sure what you mean when you say "the return value from pop() doesn't get assigned to anything" - someArray.pop() will return a value like any other function. I don't see why you couldn't implement that function, unless I've misunderstood what you want.
geraintluff is offline   Reply With Quote
Old 04-15-2018, 04:26 PM   #3
preferred.nomenclature
Human being with feelings
 
Join Date: Dec 2014
Posts: 371
Default

Quote:
Originally Posted by geraintluff View Post
someArray.pop() will return a value like any other function. I don't see why you couldn't implement that function, unless I've misunderstood what you want.
You're right, I checked again and it was my implementation that was buggy. Thanks!
preferred.nomenclature is offline   Reply With Quote
Old 04-15-2018, 10:06 PM   #4
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,773
Default

The "JS" in "JSFX" has absolutely nothing to do with "Java (Script)". so don't do any comparing between those languages (Java family and eel).

-Michael

Last edited by mschnell; 12-28-2018 at 02:36 PM.
mschnell is offline   Reply With Quote
Old 04-16-2018, 12:09 AM   #5
geraintluff
Human being with feelings
 
geraintluff's Avatar
 
Join Date: Nov 2009
Location: mostly inside my own head
Posts: 346
Default

Quote:
Originally Posted by mschnell View Post
The "JS" an "JSFX" hat absolutely nothing to do with "Java (Script)". so don't do any comparing between those languages (Java family and eel).
Given your comment was about separating similarly-named programming languages, I feel honour-bound to point out that JavaScript is not in the "Java family", just has a similar name.
geraintluff is offline   Reply With Quote
Old 04-17-2018, 02:41 AM   #6
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,891
Default

Quote:
Originally Posted by preferred.nomenclature View Post
I suppose I'm just looking for confirmation that there's no way in JS to pass in a variable by name, mutate it within a function and access that mutated value by name again outside of the function
No, but there is this little trick which can sometimes be useful...
Code:
function func(n*)
(
  n.x += 1;
  n.y += 1;
);

foo.x = 0;
foo.y = 1;

func(foo);
IXix is offline   Reply With Quote
Old 04-17-2018, 03:06 AM   #7
geraintluff
Human being with feelings
 
geraintluff's Avatar
 
Join Date: Nov 2009
Location: mostly inside my own head
Posts: 346
Default

Quote:
Originally Posted by IXix View Post
No, but there is this little trick which can sometimes be useful...
OK, am I looking in the wrong place for documentation? Which versions of REAPER support this syntax? Where's the specification for this?

Returning/ multiple values using the pseudo-object macros is nice, but I've gently wished for this *particular* bit of syntax for a while now.

Is there a wiki version of the spec which has these things added? Or somewhere like GitHub where people can make pull requests to update the official docs?
geraintluff is offline   Reply With Quote
Old 04-17-2018, 03:17 AM   #8
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,891
Default

Quote:
Originally Posted by geraintluff View Post
OK, am I looking in the wrong place for documentation? Which versions of REAPER support this syntax? Where's the specification for this?
That doesn't seem to have made it into the docs yet. I only know about it from hanging around the pre-release forum. Give it time, it's only been four and a half years.
Quote:
v4.53 - October 4 2013
+ JS: added !== and === operators (exact comparisons)
+ JS: avoid excess undo state save on finishing a slider drag
+ JS: user functions can now have namespaces as parameters -- function t(a*) ( a.foo = 1; );
+ JS: user functions can now have their own default namespaces -- function foo.bar() ( this.a=1; );
+ JS: user functions can now override builtin functions -- function sin(x) ( x - x^3/6 + x^5/120 );
Quote:
Originally Posted by geraintluff View Post
Is there a wiki version of the spec which has these things added? Or somewhere like GitHub where people can make pull requests to update the official docs?
Not that I'm aware of. The docs do tend to lag a bit.
IXix is offline   Reply With Quote
Old 04-17-2018, 04:15 AM   #9
geraintluff
Human being with feelings
 
geraintluff's Avatar
 
Join Date: Nov 2009
Location: mostly inside my own head
Posts: 346
Default

Quote:
Originally Posted by IXix View Post
I only know about it from hanging around the pre-release forum.
Ah, great! Thanks.

geraintluff is offline   Reply With Quote
Old 04-17-2018, 04:28 AM   #10
preferred.nomenclature
Human being with feelings
 
Join Date: Dec 2014
Posts: 371
Default

Very cool, glad I asked!
preferred.nomenclature is offline   Reply With Quote
Old 04-17-2018, 06:28 PM   #11
preferred.nomenclature
Human being with feelings
 
Join Date: Dec 2014
Posts: 371
Default

Finally got to play around with this, it's a game changer. You can mutate the value of n itself, not just its n.whatevers. You can't even do that in JavaScript! Any other secrets we should know about?
preferred.nomenclature is offline   Reply With Quote
Old 04-18-2018, 02:04 AM   #12
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,891
Default

Quote:
Originally Posted by preferred.nomenclature View Post
Finally got to play around with this, it's a game changer. You can mutate the value of n itself, not just its n.whatevers. You can't even do that in JavaScript!
Oh yeah, I forgot that you could do that. I suppose that means that you could actually do what you wanted to in terms of passing variables into a function and altering their values. You just have to use the namespace passing syntax...
Code:
function test(a*, b*)
(
  a *= 2;
  b *= 3;
);

x = 2;
y = 3;

test(x, y);
Quote:
Originally Posted by preferred.nomenclature View Post
Any other secrets we should know about?
The functions mem_get_values(offset, ...) and mem_set_values(offset, ...) , added in 5.28, don't seem to be in the docs either...
Code:
mem_set_values(buf, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
mem_get_values(buf, a, b, c, d, e, f, g, h, i, j);
There's also the entirely mysterious export_buffer_to_project() (added in 5.1) which you'll find lurking on line 1283 of Super8. I know nothing about how that works except that it will dump a buffer into a media item in the project. I've been meaning to figure out how it works for ages but haven't gotten around to it yet.
IXix 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 02:25 AM.


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