-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle-request-function.html
More file actions
executable file
·410 lines (380 loc) · 22.3 KB
/
article-request-function.html
File metadata and controls
executable file
·410 lines (380 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE html>
<html lang="en">
<head>
<!--__RIM_REDIRECT__-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300..700&display=swap" rel="stylesheet">
<title>Functions and parameters in RimStone</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="HandheldFriendly" content="True"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="canonical" href="https://rimstone-lang.com/article-request-function.html" />
<style>
body, html {
font-family: Quicksand,monospace,Helvetica, Arial, sans-serif;
line-height: 150%;
font-size:16px;
}
body {
padding-left:1vw;
padding-right:1vw;
}
.ncode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.shcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.sqlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.htmlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.code {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
/*Just like h1 but for pdf conversion it would be indented this way it's not*/
.vhub {
display: block;
font-size: 1.6em;
margin-top: 0.63em;
margin-bottom: 0.63em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
/*Just like h2 but for pdf conversion it would be indented this way it's not*/
.vsub {
display: block;
font-size: 1.25em;
margin-top: 0.53em;
margin-bottom: 0.53em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
ul {
margin-left: 0.75vw;
padding-left: 0;
}
li {
margin-left: 0.75vw;
padding-left: 0;
}
/* this must be last, as it overrides previous settings, for mobile */
@media (hover: none) {
a {
display: inline-block;
padding-top: 3px;
padding-bottom: 2px;
}
body {
padding-left:2vw;
padding-right:2vw;
letter-spacing: 1px;
}
}
/*The following is for code snippets that are highlighted by 2html vim*/
pre { overflow-x: scroll; margin:0; padding:0; font-family:monospace; }
.Identifier { color: #008b8b; }
.Statement { color: #af5f00; }
.PreProc { color: #5fd7ff; }
.Type { color: #005f00; }
.Comment { color: blue ; }
.Constant { color: #ff00ff; }
/*end of highlighted snippets*/
a {
text-decoration:none;
padding-bottom: 0px;
color:inherit;
border-bottom: 2px solid #6cb8f0;
}
a:hover {
text-decoration: none;
color:black;
border-bottom: 1px solid red;
}
/*do not underline links nor should they be active*/
pre a {
text-decoration:none;
color:black;
border-bottom: none;
pointer-events: none;
cursor: default;
}
.golfSnippet {display:none;}
ul {
list-style-type:square;
list-style-position: outside;
}
</style>
</head>
<body>
<div id="google_translate_element" style='float:right'></div>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script>
function rim_copy(gt, eid, gc) {
gt.textContent = eid.textContent;
gt.select();
document.execCommand("copy");
gc.style.visibility="visible"
setTimeout(()=>{ gc.style.visibility="hidden"; }, 1000);
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
<!--RIMMENU13-->
<!--RIMENDMENU13-->
<!--BEGVDOC90-->
<div class='vhub' style='margin-top:10px;margin-right:20px;text-align:left;background-color:white;'><a href='https://rimstone-lang.com' style='border-bottom:0px'><img src='https://rimstone-lang.com/rimstone.png' style='width:180px;height:auto'/></a></div><div class='vhub' style='margin-top:10px;'>Functions and parameters in RimStone</div><hr/><br/>
In RimStone, there are no "functions" or "methods" as you may be used to in other languages. Any encapsulation is a request handler - you can think of it as a simple function that executes to handle a request - it can an external request (i.e. from an outside caller such as web browser, web API, or from a command-line), or internal requests from another handler in your application. <br/>
<br/>
By the same token, there are no formal parameters in a way that you may be used to. Instead, there are named parameters, basically name/value pairs, which you can set or get anywhere during the request execution. In addition, your request handler can handle the request body, environment variables, the specific request method etc. (see <a href='https://rimstone-lang.com/request.html'>request</a>). Here though, we'll focus on parameters only.<br/>
<br/>
You'll use <a href='https://rimstone-lang.com/set-param.html'>set-param</a> to set a parameter, which can then be obtained anywhere in the current request, including in the current handler's caller or callee. Use <a href='https://rimstone-lang.com/get-param.html'>get-param</a> to obtain a parameter that's set with set-param. <br/>
<br/>
Parameters are very fast - they are static creatures implemented at compile time, meaning only fixed memory locations are used to store them (making for great CPU caching), and any name-based resolution is used only when necessary, and always with fast hash tables and static caching. <br/>
<div class="vsub"><a id="Calling one handler from another"></a>Calling one handler from another</div>
In this article, we'll talk about <a href='https://rimstone-lang.com/call-handler.html'>call-handler</a> which is used to call a handler from within another handler.<br/>
<br/>
Here you'll see a few examples of passing input and output parameters between requests handlers. These handlers are both running in the same process of an application (note that application can run as many processes working in parallel). To begin, create an application:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_318' class=notranslate>
<span class="Statement">mkdir</span> param
<span class="Statement">cd</span> param
rim <span class="Special">-k</span> param</pre>
<span id=rimstone_copied_318 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_318' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_318, code_318, rimstone_copied_318)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
You'll also create two source files ("local.rim" and "some.rim") a bit later with the code below.<br/>
<div class="vsub"><a id="Simple service"></a>Simple service</div>
Let's start with a simple service that provides current time based on a timezone as an input parameter (in file "local.rim"):<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_317' class=notranslate>
cat << '<span class="Constant">EOF</span>' > local.rim
<span class="Statement">begin-handler</span> /local/time
<span class="Statement"> get-param</span> tzone<span class="Identifier"> default-value</span> <span class="Constant">"MST"</span> <span class="Comment">// get time zone as input parameter (i.e. </span><span class="Constant">"EST"</span><span class="Comment">, </span><span class="Constant">"MST"</span><span class="Comment">, </span><span class="Constant">"PST"</span><span class="Comment"> etc.)</span>
<span class="Statement"> get-time</span><span class="Type"> to</span> curr_time<span class="Identifier"> timezone</span> tzone
<span class="Statement"> @</span><div>Current time is <span class="Statement"><<print-out</span> curr_time<span class="Statement">>></span></div>
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_317 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_317' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_317, code_317, rimstone_copied_317)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
In this case, HTML code is output. Make the application:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_321' class=notranslate>
rim <span class="Special">-q</span> <span class="Special">--public</span></pre>
<span id=rimstone_copied_321 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_321' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_321, code_321, rimstone_copied_321)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
and run it from command line (just as if it were called from a web browser):<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_323' class=notranslate>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/local/time</span><span class="Statement">"</span> <span class="Special">--silent-header</span> <span class="Special">--exec</span></pre>
<span id=rimstone_copied_323 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_323' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_323, code_323, rimstone_copied_323)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
with the result something like:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_316' class=notranslate>
<div>Current time is Mon, 02 Dec 2024 16:27:03 EST</div></pre>
<span id=rimstone_copied_316 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_316' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_316, code_316, rimstone_copied_316)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
<div class="vsub"><a id="Calling request from another, example #1"></a>Calling request from another, example #1</div>
In this example, one request handler calls another from within the same process of an application using <a href='https://rimstone-lang.com/call-handler.html'>call-handler</a>.<br/>
<br/>
For instance, here's the caller code which calls the above "/local/time" service (in file "some.rim"):<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_320' class=notranslate>
cat << '<span class="Constant">EOF</span>' > some.rim
<span class="Statement">begin-handler</span> /some/service
<span class="Statement"> call-handler</span> <span class="Constant">"/local/time"</span><span class="Identifier"> set-param</span> tzone=<span class="Constant">"EST"</span>
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_320 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_320' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_320, code_320, rimstone_copied_320)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
and in this case the output of the "/local/time" would simply become output of "/some/service" (and be presumably sent to a client like web browser). <br/>
<br/>
You can also, however, return the string to the caller using <a href='https://rimstone-lang.com/set-param.html'>set-param</a> - here's the reworked "/local/time" service (in file "local.rim"):<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_322' class=notranslate>
cat << '<span class="Constant">EOF</span>' > local.rim
<span class="Statement">begin-handler</span> /local/time
<span class="Statement"> get-param</span> tzone<span class="Identifier"> default-value</span> <span class="Constant">"MST"</span>
<span class="Statement"> get-time</span><span class="Type"> to</span> curr_time<span class="Identifier"> timezone</span> tzone
<span class="Statement"> set-param</span> curr_time <span class="Comment">// set the return parameter to be obtained by the caller</span>
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_322 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_322' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_322, code_322, rimstone_copied_322)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
The result parameter will be obtained in the caller, and then output as HTML from there (we could save it to a file instead, or whatever), so here's what it'd be now in file "some.rim":<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_324' class=notranslate>
cat << '<span class="Constant">EOF</span>' > some.rim
<span class="Statement">begin-handler</span> /some/service
<span class="Statement"> call-handler</span> <span class="Constant">"/local/time"</span><span class="Identifier"> set-param</span> tzone=<span class="Constant">"EST"</span><span class="Identifier"> get-param</span> curr_time
<span class="Statement"> @</span><div>Current time is <span class="Statement"><<print-out</span> curr_time<span class="Statement">>></span></div>
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_324 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_324' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_324, code_324, rimstone_copied_324)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Make the project and run it:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_325' class=notranslate>
rim <span class="Special">-q</span> <span class="Special">--public</span>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/some/service</span><span class="Statement">"</span> <span class="Special">--silent-header</span> <span class="Special">--exec</span></pre>
<span id=rimstone_copied_325 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_325' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_325, code_325, rimstone_copied_325)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
with the similar end result.<br/>
<div class="vsub"><a id="Calling request from another, example #2"></a>Calling request from another, example #2</div>
Consider this handler, which checks if number is even, and returns true or false (file "check.rim"):<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_326' class=notranslate>
cat << '<span class="Constant">EOF</span>' > check.rim
<span class="Statement">begin-handler</span> /check/even
<span class="Statement"> get-param</span> n<span class="Identifier"> type number</span> <span class="Comment">// get input parameter</span>
<span class="Statement"> if-true</span> n<span class="Identifier"> every</span> <span class="Constant">2</span>
<span class="Statement"> set-param</span> is_even = <span class="Constant">true</span> <span class="Comment">// set output parameter</span>
<span class="Statement"> else-if</span>
<span class="Statement"> set-param</span> is_even = <span class="Constant">false</span> <span class="Comment">// set output parameter</span>
<span class="Statement"> end-if</span>
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_326 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_326' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_326, code_326, rimstone_copied_326)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Here's calling it from another handler (add this to file "some.rim"): <br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_328' class=notranslate>
cat << '<span class="Constant">EOF</span>' > some.rim
<span class="Statement">begin-handler</span> /some/task
<span class="Statement"> call-handler</span> <span class="Constant">"/check/even"</span><span class="Identifier"> set-param</span> n = <span class="Constant">23</span><span class="Identifier"> get-param</span> is_even<span class="Identifier"> type bool</span>
<span class="Statement"> if-true</span> is_even<span class="Identifier"> equal</span> <span class="Constant">true</span>
<span class="Statement"> @</span>EVEN
<span class="Statement"> else-if</span>
<span class="Statement"> @</span>ODD
<span class="Statement"> end-if</span>
<span class="Statement">end-handler</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_328 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_328' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_328, code_328, rimstone_copied_328)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Make the project and run it:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_327' class=notranslate>
rim <span class="Special">-q</span> <span class="Special">--public</span>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/some/task</span><span class="Statement">"</span> <span class="Special">--silent-header</span> <span class="Special">--exec</span></pre>
<span id=rimstone_copied_327 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_327' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_327, code_327, rimstone_copied_327)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
with the result (since 23 is an odd number):<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_319' class=notranslate>
ODD</pre>
<span id=rimstone_copied_319 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_319' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_319, code_319, rimstone_copied_319)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
<div class="vsub"><a id="Conclusion"></a>Conclusion</div>
RimStone is a language and a platform built to provide native web services, and everything in it is centered around the notion of handling service calls. Simple name/value constructs rule the communication between handlers on the network as well as locally. Suffice it to say, RimStone's not a classic C-like programming language (though ironically is built in C and compiles to C).<br/>
<!--ENDVDOC90-->
<br/><div style='width:100%;clear:both;'>
<hr/>
<!--RIMFOOT77--><span style='font-size:80%'><a href="https://rimstone-lang.com/copyright.html">Copyright</a> (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.</span>
</div><br/></body></html>