-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer-API.html
executable file
·385 lines (350 loc) · 17.2 KB
/
Server-API.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<!--__GOLF_REDIRECT__-->
<title>Server API</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://golf-lang.com/Server-API.html" />
<style>
body, html {
color:black;
text-rendering: optimizelegibility;
background-color: white;
min-height: 150%;
font-family: "Times New Roman";
font-weight:400;
font-size:18px;
line-height:27px;
letter-spacing: 2px;
z-index: 1;
height: 100%;
text-align:left;
width:80%;
margin-left:auto;
margin-right:auto;
padding:0;
/*font-family: Helvetica, Arial, sans-serif;*/
}
body {
padding-left:1vw;
padding-right:1vw;
}
.ncode {
line-height:20px;
letter-spacing: 0px;
font-size:14px;
font-family: monospace;
display:inline-block;
max-width:100%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:15px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.shcode {
line-height:23px;
letter-spacing: 0px;
font-size:14px;
font-family: monospace;
display:inline-block;
max-width:100%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:15px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.sqlcode {
line-height:23px;
letter-spacing: 0px;
font-size:14px;
font-family: monospace;
display:inline-block;
max-width:100%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:15px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.htmlcode {
line-height:23px;
letter-spacing: 0px;
font-size:14px;
font-family: monospace;
display:inline-block;
max-width:100%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:15px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.code {
line-height:23px;
letter-spacing: 0px;
font-size:14px;
font-family: monospace;
display:inline-block;
max-width:100%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:15px;
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;
font-size:14px;
line-height:22px;
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>
<script>
function gg_copy(gt, eid, gc) {
gt.textContent = eid.textContent;
gt.select();
document.execCommand("copy");
gc.style.visibility="visible"
setTimeout(()=>{ gc.style.visibility="hidden"; }, 1000);
}
</script>
<!--GOLFMENU13-->
<!--GOLFENDMENU13-->
<!--BEGVDOC90-->
<div class='vhub' style='margin-top:10px;margin-right:20px;text-align:right;background-color:white;'><a href='https://golf-lang.com' style='border-bottom:0px'><img src='https://golf-lang.com/golf.png'/></a></div><div class='vhub' style='margin-top:10px;'> Server API</div><hr/>Golf can be used in <a href='https://golf-lang.com/extended-mode.html'>extended-mode</a>, where non-Golf code or libraries can be linked with your application.<br/>
<br/>
Such code can be from a library (see --llflag and --cflag options in <a href='https://golf-lang.com/gg.html'>gg</a>), or can be written directly as C code, i.e. files with .c and .h extension together with your Golf application. To do this, use <a href='https://golf-lang.com/call-extended.html'>call-extended</a> statement.<br/>
<br/>
Any function with C linkage can be used provided:<br/>
<ul><li>its parameters are (by value or reference) only of type: "int64_t" (number type in Golf), "bool" (bool type in Golf) or "char *" (string type in Golf).<br/>
</li><li>it must not return any value (i.e. it must have a "void" return type).</li></ul>
When allocating strings in extended code, you must use Golf memory management functions. These functions are based on standard C library (such as malloc or free), but are not compatible with them because Golf manages such memory on top of the standard C library.<br/>
<br/>
The functions you can use are:<br/>
<ul><li><span style="font-weight:bold;">char *gg_strdup (char *s)</span> which creates a copy of a null-terminated string "s". A pointer to memory data is returned.<br/>
<br/>
</li><li><span style="font-weight:bold;">char *gg_strdupl (char *s, gg_num from, gg_num l)</span> which creates a copy of memory data pointed to by "s", starting from byte "from" of length "l". Note that "from" is indexed from 0. A pointer to memory data is returned.<br/>
<br/>
</li><li><span style="font-weight:bold;">void *gg_malloc(size_t size)</span> which allocates memory of size "s" and returns a pointer to it.<br/>
<br/>
</li><li><span style="font-weight:bold;">void *gg_calloc(size_t nmemb, size_t size)</span> allocates "nmemb" blocks of memory (each of size "size") and returns a pointer to it. Memory is initialized to all zero bytes.<br/>
<br/>
</li><li><span style="font-weight:bold;">num gg_mem_get_id (void *ptr)</span> returns Golf memory handle for memory "ptr".<br/>
<br/>
</li><li><span style="font-weight:bold;">void *gg_realloc(gg_num r, size_t size)</span> reallocates memory identified with Golf memory handle "r" (see gg_mem_get_id()) to a new size of "size" and returns a pointer to it. Note that you can only reallocate the memory you created with gg_malloc() and gg_calloc() - do not attempt to reallocate Golf memory passed to your function. Golf in general never reallocates any existing memory in any statement.<br/>
<br/>
</li><li><span style="font-weight:bold;">void gg_mem_set_len (gg_num r, gg_num len)</span> sets the length of memory identified with Golf memory handle "r" to "len" bytes. Note that all Golf memory must have a null-byte at the end for consistency, regardless of whether such memory holds pure binary data or an actual null-delimited string. So for example, a string "abc" would have "len" set to 4 to include a null byte, and binary data "\xFF\x00\x01" (which consists of 3 bytes, the middle of which is a null byte) would have "len" also set to 4 and you would place an extra zero byte at the end of it even if it's not part of the actual useful data. Note that whatever memory length you set, it must be lesser or equal to the length of memory you have actually allocated.<br/>
<br/>
</li><li><span style="font-weight:bold;">num gg_mem_get_len (gg_num r)</span> returns the length of memory identified with Golf memory handle "r". The length returned is 1 bytes less than the memory set by gg_mem_set_len(), so for example for string "abc" the return value would be 3, as it would be for "\xFF\x00\x01".</li></ul>
<br/>
You can use gg_malloc(), gg_calloc() and gg_realloc() to create new Golf-compatible memory - and assuming you have set the last byte of any such memory to a null byte, the resulting memory will be properly sized for Golf usage. <br/>
<br/>
If you have memory that's already provided from elsewhere, you can use gg_strdup() or gg_strdupl() to create a copy of it that's compatible with Golf. <br/>
<br/>
If Golf memory you created with these functions has extra unused bytes, you can use either gg_realloc() to reduce its footprint, or you can use gg_mem_set_len() to set its length.<br/>
<br/>
Note that if you use C code included with a Golf project, you must include "golf.h" file in each of them. You do not need to manually include any other ".h" files (header files), as they will be automatically picked up.<br/>
<div class="vsub"><a id="Examples"></a>Examples</div>
Place the following files in a separate directory for demonstration purposes.<br/>
<br/>
In this example, "example.golf" will use C functions from "example.c", and "example.h" will have declarations of those functions. File "example.c" implements a factorial function, as well as a function that will store the factorial result in an output message that's allocated and passed back to your Golf code:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_753'>
<span class="PreProc">#include </span><span class="Constant">"golf.h"</span>
<span class="Type">void</span> get_factorial(gg_num f, gg_num *res)
<span class="Statement"> {</span>
*res = <span class="Constant">1</span>;
gg_num i;
<span class="Statement">for</span> (i = <span class="Constant">2</span>; i <= f; i++) {
*res *= i;
<span class="Statement"> }</span>
<span class="Statement"> }</span>
<span class="PreProc">#</span>define<span class="PreProc"> </span>MEMSIZE<span class="PreProc"> </span><span class="Constant">200</span>
<span class="Type">void</span> fact_msg (gg_num i, <span class="Type">char</span> **res)
<span class="Statement"> {</span>
<span class="Comment">// golf rule: outgoing string must NOT free or realloc its incoming value</span>
<span class="Comment">// all else is allowed</span>
<span class="Type">char</span> *r = gg_malloc (MEMSIZE);
gg_num f;
get_factorial (i, &f);
gg_num bw = snprintf(r, MEMSIZE, <span class="Constant">"Factorial value (message from C function) is </span><span class="Special">%ld</span><span class="Constant">"</span>, f) + <span class="Constant">1</span>;
<span class="Comment">// reduce memory footprint to match the memory used (including the null byte)</span>
<span class="Comment">// you can also use gg_mem_set_len() with bw as length for higher performance </span>
<span class="Comment">// (but also higher memory usage)</span>
*res = gg_realloc (gg_mem_get_id(r), bw);
<span class="Statement"> }</span></pre>
<span id=golf_copied_753 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='golft_753' style='position: absolute;left: -500%;'></textarea>
<img src='https://golf-lang.com/golf-copy-small-1.png' id='golfb' onclick='gg_copy(golft_753, code_753, golf_copied_753)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
File "example.h" declares the above functions:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_756'>
<span class="Type">void</span> get_factorial(gg_num f, gg_num *res);
<span class="Type">void</span> fact_msg (gg_num i, <span class="Type">char</span> **res);</pre>
<span id=golf_copied_756 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='golft_756' style='position: absolute;left: -500%;'></textarea>
<img src='https://golf-lang.com/golf-copy-small-1.png' id='golfb' onclick='gg_copy(golft_756, code_756, golf_copied_756)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
File "example.golf" will call the above functions and display the results:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_758'>
<span class="Statement"> extended-mode</span>
<span class="Statement"> begin-handler</span> /example<span class="Identifier"> public</span>
<span class="Statement"> set-number</span> fact
<span class="Statement"> call-extended</span> get_factorial (<span class="Constant">10</span>, &fact)
<span class="Statement"> @</span>Factorial is <span class="Statement"><<p-num</span> fact<span class="Statement">>></span>
<span class="Statement"> set-string</span> res
<span class="Statement"> call-extended</span> fact_msg (<span class="Constant">10</span>, &res)
<span class="Statement"> p-out</span> res
<span class="Statement"> @</span>
<span class="Statement"> end-handler</span></pre>
<span id=golf_copied_758 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='golft_758' style='position: absolute;left: -500%;'></textarea>
<img src='https://golf-lang.com/golf-copy-small-1.png' id='golfb' onclick='gg_copy(golft_758, code_758, golf_copied_758)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Create application "example":<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_755'>
sudo mgrg <span class="Special">-i</span> <span class="Special">-u</span> <span class="PreProc">$(</span><span class="Special">whoami</span><span class="PreProc">)</span> example</pre>
<span id=golf_copied_755 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='golft_755' style='position: absolute;left: -500%;'></textarea>
<img src='https://golf-lang.com/golf-copy-small-1.png' id='golfb' onclick='gg_copy(golft_755, code_755, golf_copied_755)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Make the application:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_757'>
gg <span class="Special">-q</span></pre>
<span id=golf_copied_757 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='golft_757' style='position: absolute;left: -500%;'></textarea>
<img src='https://golf-lang.com/golf-copy-small-1.png' id='golfb' onclick='gg_copy(golft_757, code_757, golf_copied_757)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Run it:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_759'>
gg <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/example</span><span class="Statement">"</span> <span class="Special">--exec</span> <span class="Special">--silent-header</span></pre>
<span id=golf_copied_759 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='golft_759' style='position: absolute;left: -500%;'></textarea>
<img src='https://golf-lang.com/golf-copy-small-1.png' id='golfb' onclick='gg_copy(golft_759, code_759, golf_copied_759)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
The output is, as expected:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_754'>
Factorial is 3628800
Factorial value (message from C function) is 362880</pre>
<span id=golf_copied_754 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='golft_754' style='position: absolute;left: -500%;'></textarea>
<img src='https://golf-lang.com/golf-copy-small-1.png' id='golfb' onclick='gg_copy(golft_754, code_754, golf_copied_754)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
<!--BEFSAL13--><div class="vsub"><a id="See also"></a>See also</div>
<a name='API'></a><span style="font-weight:bold;">API</span><br/>
<a href='https://golf-lang.com/Client-API.html'>Client-API</a> <br/>
<a href='https://golf-lang.com/Server-API.html'>Server-API</a> <br/>
<span style="font-weight:bold;">See all</span> <br/>
<a href='https://golf-lang.com/documentation.html'>documentation</a><br/>
<!--ENDVDOC90-->
<br/><div style='width:100%;clear:both;'>
<hr/>
<!--GOLFFOOT77--><span style='font-size:80%'><a href="https://golf-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>