Parameter Passing in JavaScript
On this page:
Server-Side Parameters in JavaScript
For each parameter of a Slice operation, the JavaScript mapping generates a corresponding parameter for the method in the servant. In addition, every operation has an additional, trailing parameter of type Ice.Current
. For example, the name
operation of the Node
interface has no parameters, but the name
method of the servant has a single parameter of type Ice.Current
. We will ignore this parameter for now.
For a Slice operation that returns multiple values, the implementation returns them in an array consisting of a non-void
return value, if any, followed by the out
parameters in the order of declaration. An operation returning only one value simply returns the value itself.
An operation returns multiple values when it declares multiple out parameters, or when it declares a non-void
return type and at least one out
parameter.
To illustrate the rules, consider the following interface that passes string parameters in all possible directions:
module M { interface Example { string op1(string sin); void op2(string sin, out string sout); string op3(string sin, out string sout); } }
The servant methods would look as follows:
class ExampleI extends M.Example { op1(sin, current){ ... } op2(sin, current){ ... } op3(sin, current){ ... } }
class ExampleI extends M.Example { op1(sin:string, current:Ice.Current):string; op2(sin:string, current:Ice.Current):string; op3(sin:string, current:Ice.Current):[string, string]; }
The signatures of the JavaScript methods are identical because they all accept a single in
parameter, but their implementations differ in the way they return values. For example, we could implement the operations as follows:
class ExampleI extends M.Example { op1(sin, current) { console.log(sin); // In params are initialized return "Done"; // Return value } op2(sin, current):string { console.log(sin); // In params are initialized return "Hello World!"; // Out parameter } op3(sin, current) { console.log(sin); // In params are initialized return ["Done", "Hello World!"]; } }
class ExampleI extends M.Example { op1(sin:string, current:Ice.Current):string { console.log(sin); // In params are initialized return "Done"; // Return value } op2(sin:string, current:Ice.Current):string { console.log(sin); // In params are initialized return "Hello World!"; // Out parameter } op3(sin:string, current:Ice.Current):[string, string] { console.log(sin); // In params are initialized return ["Done", "Hello World!"]; } }
Notice that op1
and op2
return their string values directly, whereas op3
returns an array consisting of the return value followed by the out
parameter.
This code is in no way different from what you would normally write if you were to pass strings to and from a function; the fact that remote procedure calls are involved does not impact on your code in any way. The same is true for parameters of other types, such as proxies, classes, or dictionaries: the parameter passing conventions follow normal JavaScript rules and do not require special-purpose API calls.
With other programming languages, you need to be mindful about marshaling objects returned by your operation implementation that may be modified concurrently by other threads. There is no such concern in JavaScript since there is only one execution thread.